|
27 | 27 | ############### |
28 | 28 |
|
29 | 29 |
|
30 | | -def java_to_dataset(ij: "jc.ImageJ", jobj) -> "jc.Dataset": |
| 30 | +def java_to_dataset(ij: "jc.ImageJ", jobj, dim_order=None) -> "jc.Dataset": |
31 | 31 | """ |
32 | 32 | Convert the given Java image data into an ImageJ2 Dataset. |
33 | 33 |
|
34 | 34 | :param ij: The ImageJ2 gateway (see imagej.init) |
35 | 35 | :param jobj: The Java image (e.g. RandomAccessibleInterval) |
| 36 | + :param dim_order: Sequence of desired dimensions for the Dataset. |
36 | 37 | :return: The converted ImageJ2 Dataset |
37 | 38 | """ |
38 | 39 | assert sj.isjava(jobj) |
39 | 40 | if isinstance(jobj, jc.Dataset): |
40 | | - return jobj |
| 41 | + return _rename_dataset_dims(jobj, dim_order) |
41 | 42 |
|
42 | 43 | # NB: This try checking is necessary because the set of ImageJ2 converters is |
43 | 44 | # not complete. E.g., there is no way to directly go from Img to Dataset, |
44 | 45 | # instead you need to chain the Img->ImgPlus->Dataset converters. |
45 | 46 | try: |
46 | 47 | if ij.convert().supports(jobj, jc.Dataset): |
47 | | - return ij.convert().convert(jobj, jc.Dataset) |
| 48 | + ds = ij.convert().convert(jobj, jc.Dataset) |
48 | 49 | if ij.convert().supports(jobj, jc.ImgPlus): |
49 | 50 | imgplus = ij.convert().convert(jobj, jc.ImgPlus) |
50 | | - return ij.dataset().create(imgplus) |
| 51 | + ds = ij.dataset().create(imgplus) |
51 | 52 | if ij.convert().supports(jobj, jc.Img): |
52 | 53 | img = ij.convert().convert(jobj, jc.Img) |
53 | | - return ij.dataset().create(jc.ImgPlus(img)) |
| 54 | + ds = ij.dataset().create(jc.ImgPlus(img)) |
54 | 55 | if ij.convert().supports(jobj, jc.RandomAccessibleInterval): |
55 | 56 | rai = ij.convert().convert(jobj, jc.RandomAccessibleInterval) |
56 | | - return ij.dataset().create(rai) |
| 57 | + ds = ij.dataset().create(rai) |
| 58 | + return _rename_dataset_dims(ds, dim_order) |
57 | 59 | except Exception as exc: |
58 | 60 | _log_exception(_logger, exc) |
59 | 61 | raise exc |
@@ -642,7 +644,38 @@ def _permute_rai_to_python(rich_rai: "jc.RandomAccessibleInterval"): |
642 | 644 | return permuted_rai |
643 | 645 |
|
644 | 646 |
|
| 647 | +def _rename_dataset_dims(ds, new_dims: Sequence[str]): |
| 648 | + """ |
| 649 | + Rename, without reshaping, a Dataset's dimension labels. |
| 650 | +
|
| 651 | + :param ds: Input Dataset. |
| 652 | + :param new_dims: Dimension labels to apply |
| 653 | + :return: Dataset with dimension labels renamed. |
| 654 | + """ |
| 655 | + # return the dataset if no new_dims |
| 656 | + if not new_dims: |
| 657 | + return ds |
| 658 | + |
| 659 | + # validate dim sequence |
| 660 | + new_dims = dims._validate_dim_order(new_dims, ds.shape) |
| 661 | + |
| 662 | + # set axis type for each axis |
| 663 | + for i in range(ds.ndim): |
| 664 | + new_axis_str = dims._convert_dim(new_dims[i], "java") |
| 665 | + new_axis_type = jc.Axes.get(new_axis_str) |
| 666 | + ds.dim_axes[i].setType(new_axis_type) |
| 667 | + |
| 668 | + return ds |
| 669 | + |
| 670 | + |
645 | 671 | def _rename_xarray_dims(xarr, new_dims: Sequence[str]): |
| 672 | + """ |
| 673 | + Rename, without reshaping, an xarray's dimension labels. |
| 674 | +
|
| 675 | + :param xarr: Input xarray. |
| 676 | + :param new_dims: Dimension labels to apply. |
| 677 | + :return: xarray with dimension labels renamed. |
| 678 | + """ |
646 | 679 | curr_dims = xarr.dims |
647 | 680 | if not new_dims: |
648 | 681 | return xarr |
|
0 commit comments