Skip to content

Commit b9a28c5

Browse files
authored
Merge pull request #322 from imagej/dataset-dim-renaming
Dataset dimension/axis label renaming
2 parents c518c33 + c7aa447 commit b9a28c5

File tree

4 files changed

+157
-66
lines changed

4 files changed

+157
-66
lines changed

doc/06-Working-with-Images.ipynb

Lines changed: 113 additions & 51 deletions
Large diffs are not rendered by default.

src/imagej/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,7 @@ def to_dataset(self, data, dim_order=None) -> "jc.Dataset":
437437
:return: A net.imagej.Dataset.
438438
"""
439439
if sj.isjava(data):
440-
if dim_order:
441-
_logger.warning(
442-
f"Dimension reordering is not supported for {type(data)}."
443-
)
444-
return convert.java_to_dataset(self._ij, data)
440+
return convert.java_to_dataset(self._ij, data, dim_order)
445441

446442
if images.is_xarraylike(data):
447443
return convert.xarray_to_dataset(

src/imagej/convert.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,35 @@
2727
###############
2828

2929

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":
3131
"""
3232
Convert the given Java image data into an ImageJ2 Dataset.
3333
3434
:param ij: The ImageJ2 gateway (see imagej.init)
3535
:param jobj: The Java image (e.g. RandomAccessibleInterval)
36+
:param dim_order: Sequence of desired dimensions for the Dataset.
3637
:return: The converted ImageJ2 Dataset
3738
"""
3839
assert sj.isjava(jobj)
3940
if isinstance(jobj, jc.Dataset):
40-
return jobj
41+
return _rename_dataset_dims(jobj, dim_order)
4142

4243
# NB: This try checking is necessary because the set of ImageJ2 converters is
4344
# not complete. E.g., there is no way to directly go from Img to Dataset,
4445
# instead you need to chain the Img->ImgPlus->Dataset converters.
4546
try:
4647
if ij.convert().supports(jobj, jc.Dataset):
47-
return ij.convert().convert(jobj, jc.Dataset)
48+
ds = ij.convert().convert(jobj, jc.Dataset)
4849
if ij.convert().supports(jobj, jc.ImgPlus):
4950
imgplus = ij.convert().convert(jobj, jc.ImgPlus)
50-
return ij.dataset().create(imgplus)
51+
ds = ij.dataset().create(imgplus)
5152
if ij.convert().supports(jobj, jc.Img):
5253
img = ij.convert().convert(jobj, jc.Img)
53-
return ij.dataset().create(jc.ImgPlus(img))
54+
ds = ij.dataset().create(jc.ImgPlus(img))
5455
if ij.convert().supports(jobj, jc.RandomAccessibleInterval):
5556
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)
5759
except Exception as exc:
5860
_log_exception(_logger, exc)
5961
raise exc
@@ -642,7 +644,38 @@ def _permute_rai_to_python(rich_rai: "jc.RandomAccessibleInterval"):
642644
return permuted_rai
643645

644646

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+
645671
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+
"""
646679
curr_dims = xarr.dims
647680
if not new_dims:
648681
return xarr

tests/test_image_conversion.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,15 +521,15 @@ def test_index_image_converts_to_imglib_roi(ij):
521521
(
522522
get_img,
523523
"java",
524-
["a", "b", "c", "d", "e"],
525-
("X", "Y", "Unknown", "Unknown", "Unknown"),
524+
["X", "Y", "monday", "green", "Time"],
525+
("X", "Y", "monday", "green", "Time"),
526526
(1, 2, 3, 4, 5),
527527
),
528528
(
529529
get_imgplus,
530530
"java",
531-
["a", "b", "c", "d", "e", "f", "g"],
532-
("X", "Y", "foo", "bar", "Channel", "Time", "Z"),
531+
["Y", "X", "Channel", "gray", "e", "foo", "gold"],
532+
("Y", "X", "Channel", "gray", "e", "foo", "gold"),
533533
(7, 8, 4, 2, 3, 5, 6),
534534
),
535535
(

0 commit comments

Comments
 (0)