Skip to content

Commit e3b79e9

Browse files
authored
Merge branch 'main' into actions-updates
2 parents 9185f0c + 9c75ef5 commit e3b79e9

File tree

7 files changed

+168
-77
lines changed

7 files changed

+168
-77
lines changed

dev-environment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ name: pyimagej-dev
2020
channels:
2121
- conda-forge
2222
dependencies:
23-
- python >= 3.8
23+
- python >= 3.9
2424
# Project dependencies
2525
- imglyb >= 2.1.0
2626
- jgo >= 1.0.3

doc/06-Working-with-Images.ipynb

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

doc/Install.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,15 @@ If installing via pip, we recommend using a
4848
[virtualenv](https://virtualenv.pypa.io/) to avoid cluttering up or mangling
4949
your system-wide or user-wide Python environment. Alternately, you can use
5050
mamba just for its virtual environment feature (`mamba create -n pyimagej
51-
python=3.8; mamba activate pyimagej`) and then simply `pip install` everything
51+
python=3.9; mamba activate pyimagej`) and then simply `pip install` everything
5252
into that active environment.
5353

5454
There are several ways to install things via pip, but we will not enumerate
5555
them all here; these instructions will assume you know what you are doing if
5656
you chose this route over conda/mamba above.
5757

58-
1. Install [Python 3](https://python.org/). As of this writing, PyImageJ has
59-
been tested with Python 3.6, 3.7, 3.8, 3.9, and 3.10.
60-
You might have issues with Python 3.10 on Windows.
58+
1. Install [Python 3](https://python.org/). As of this writing,
59+
PyImageJ has been tested with Python up through 3.13.
6160

6261
2. Install OpenJDK 8 or OpenJDK 11. PyImageJ should work with whichever
6362
distribution of OpenJDK you prefer; we recommend
@@ -176,7 +175,7 @@ USER root
176175
RUN apt-get update
177176
RUN apt-get install -y wget unzip > /dev/null && rm -rf /var/lib/apt/lists/* > /dev/null
178177
RUN micromamba install -y -n base -c conda-forge \
179-
python=3.8\
178+
python=3.9\
180179
pyimagej \
181180
openjdk=11 && \
182181
micromamba clean --all --yes

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[build-system]
2-
requires = [ "setuptools>=61.2" ]
2+
requires = [ "setuptools>=77.0.0" ]
33
build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "pyimagej"
7-
version = "1.6.1.dev0"
7+
version = "1.7.0.dev0"
88
description = "Python wrapper for ImageJ"
9-
license = {text = "Apache-2.0"}
9+
license = "Apache-2.0"
1010
authors = [{name = "ImageJ2 developers", email = "ctrueden@wisc.edu"}]
1111
readme = "README.md"
1212
keywords = ["java", "imagej", "imagej2", "fiji"]

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: 40 additions & 8 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
@@ -237,8 +239,7 @@ def java_to_xarray(ij: "jc.ImageJ", jobj) -> xr.DataArray:
237239
xr_dims.reverse()
238240
xr_dims = dims._convert_dims(xr_dims, direction="python")
239241
xr_coords = dims._get_axes_coords(xr_axes, xr_dims, narr.shape)
240-
name = jobj.getName() if isinstance(jobj, jc.Named) else None
241-
name = ij.py.from_java(name)
242+
name = str(jobj.getName()) if isinstance(jobj, jc.Named) else None
242243
return xr.DataArray(narr, dims=xr_dims, coords=xr_coords, attrs=xr_attrs, name=name)
243244

244245

@@ -642,7 +643,38 @@ def _permute_rai_to_python(rich_rai: "jc.RandomAccessibleInterval"):
642643
return permuted_rai
643644

644645

646+
def _rename_dataset_dims(ds, new_dims: Sequence[str]):
647+
"""
648+
Rename, without reshaping, a Dataset's dimension labels.
649+
650+
:param ds: Input Dataset.
651+
:param new_dims: Dimension labels to apply
652+
:return: Dataset with dimension labels renamed.
653+
"""
654+
# return the dataset if no new_dims
655+
if not new_dims:
656+
return ds
657+
658+
# validate dim sequence
659+
new_dims = dims._validate_dim_order(new_dims, ds.shape)
660+
661+
# set axis type for each axis
662+
for i in range(ds.ndim):
663+
new_axis_str = dims._convert_dim(new_dims[i], "java")
664+
new_axis_type = jc.Axes.get(new_axis_str)
665+
ds.dim_axes[i].setType(new_axis_type)
666+
667+
return ds
668+
669+
645670
def _rename_xarray_dims(xarr, new_dims: Sequence[str]):
671+
"""
672+
Rename, without reshaping, an xarray's dimension labels.
673+
674+
:param xarr: Input xarray.
675+
:param new_dims: Dimension labels to apply.
676+
:return: xarray with dimension labels renamed.
677+
"""
646678
curr_dims = xarr.dims
647679
if not new_dims:
648680
return xarr

tests/test_image_conversion.py

Lines changed: 6 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
(
@@ -650,6 +650,8 @@ def test_direct_to_xarray_conversion(
650650
im_data = im_req()
651651
# convert the image data to xarray
652652
xarr_out = ij.py.to_xarray(im_data, dim_order=new_dims)
653+
name = xarr_out.name
654+
assert name is None or isinstance(name, str)
653655
assert xarr_out.dims == exp_dims
654656
assert xarr_out.shape == exp_shape
655657
if hasattr(im_data, "dim_axes") and obj_type == "java":

0 commit comments

Comments
 (0)