Skip to content

Commit 087217b

Browse files
authored
Merge pull request #130 from thewtex/to-itk-c-index
ENH: c_index argument to ngff_image_to_itk_image
2 parents 9139e68 + 294d1a2 commit 087217b

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

ngff_zarr/ngff_image_to_itk_image.py

+24
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def ngff_image_to_itk_image(
3838
ngff_image: NgffImage,
3939
wasm: bool = True,
4040
t_index: Optional[int] = None,
41+
c_index: Optional[int] = None,
4142
):
4243
"""Convert a NgffImage to an ITK image."""
4344
from itkwasm import IntTypes, PixelTypes
@@ -65,6 +66,29 @@ def ngff_image_to_itk_image(
6566
axes_units=new_axes_units,
6667
)
6768

69+
if c_index is not None and "c" in ngff_image.dims:
70+
c_dim_index = ngff_image.dims.index("c")
71+
new_dims = list(ngff_image.dims)
72+
new_dims.remove("c")
73+
new_dims = tuple(new_dims)
74+
new_scale = {dim: ngff_image.scale[dim] for dim in new_dims}
75+
new_translation = {dim: ngff_image.translation[dim] for dim in new_dims}
76+
new_axes_units = {dim: ngff_image.axes_units[dim] for dim in new_dims}
77+
if isinstance(ngff_image.data, DaskArray):
78+
from dask.array import take
79+
80+
new_data = take(ngff_image.data, c_index, axis=c_dim_index)
81+
else:
82+
new_data = ngff_image.data.take(c_index, axis=c_dim_index)
83+
ngff_image = NgffImage(
84+
data=new_data,
85+
dims=new_dims,
86+
name=ngff_image.name,
87+
scale=new_scale,
88+
translation=new_translation,
89+
axes_units=new_axes_units,
90+
)
91+
6892
ngff_image = _channel_dim_last(ngff_image)
6993

7094
dims = ngff_image.dims

pixi.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test_ngff_image_to_itk_image.py

+37
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,40 @@ def test_t_index(input_images): # noqa: ARG001
8989
assert len(itk_image.origin) == 3
9090
assert len(itk_image.direction) == 3
9191
assert itk_image.data.shape == (12, 223, 198, 6)
92+
93+
94+
def test_c_index(input_images): # noqa: ARG001
95+
dataset_name = "13457537"
96+
store_path = test_data_dir / "input" / f"{dataset_name}.zarr"
97+
multiscales = from_ngff_zarr(store_path)
98+
ngff_image = multiscales.images[0]
99+
100+
itk_image = ngff_image_to_itk_image(ngff_image)
101+
102+
assert itk_image.imageType.dimension == 4
103+
assert itk_image.imageType.components == 6
104+
assert len(itk_image.size) == 4
105+
assert len(itk_image.spacing) == 4
106+
assert len(itk_image.origin) == 4
107+
assert len(itk_image.direction) == 4
108+
assert itk_image.data.shape == (18, 12, 223, 198, 6)
109+
110+
itk_image = ngff_image_to_itk_image(ngff_image, c_index=0)
111+
112+
assert itk_image.imageType.dimension == 4
113+
assert itk_image.imageType.components == 1
114+
assert len(itk_image.size) == 4
115+
assert len(itk_image.spacing) == 4
116+
assert len(itk_image.origin) == 4
117+
assert len(itk_image.direction) == 4
118+
assert itk_image.data.shape == (18, 12, 223, 198)
119+
120+
itk_image = ngff_image_to_itk_image(ngff_image, t_index=0, c_index=0)
121+
122+
assert itk_image.imageType.dimension == 3
123+
assert itk_image.imageType.components == 1
124+
assert len(itk_image.size) == 3
125+
assert len(itk_image.spacing) == 3
126+
assert len(itk_image.origin) == 3
127+
assert len(itk_image.direction) == 3
128+
assert itk_image.data.shape == (12, 223, 198)

0 commit comments

Comments
 (0)