Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ngff_zarr/methods/_itk.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ def _downsample_itk_bin_shrink(
# For consistency for now, do not utilize direction until there is standardized support for
# direction cosines / orientation in OME-NGFF
# block_0.attrs.pop("direction", None)
if "c" in previous_image.dims:
raise ValueError(
"Downsampling with ITK BinShrinkImageFilter does not support channel dimension 'c'. "
"Use ITK Gaussian downsampling instead."
)
block_input = itk.image_from_array(np.ones_like(block_0))
spacing = [previous_image.scale[d] for d in spatial_dims]
block_input.SetSpacing(spacing)
Expand Down
6 changes: 3 additions & 3 deletions ngff_zarr/methods/_itkwasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def _downsample_itkwasm(
]
assert all(
block_output.size[dim] == computed_size[dim]
for dim in range(block_output.data.ndim)
for dim in range(len(block_output.size))
)
output_chunks = list(previous_image.data.chunks)
output_chunks_start = 0
Expand All @@ -172,7 +172,7 @@ def _downsample_itkwasm(
]
assert all(
block_output.size[dim] == computed_size[dim]
for dim in range(block_output.data.ndim)
for dim in range(len(block_output.size))
)
for i in range(len(output_chunks)):
output_chunks[i][-1] = block_output.data.shape[i]
Expand All @@ -181,7 +181,7 @@ def _downsample_itkwasm(

non_spatial_dims = [d for d in dims if d not in _spatial_dims]
if "c" in non_spatial_dims and previous_image.dims[-1] == "c":
non_spatial_dims.pop("c")
non_spatial_dims.remove("c")

if output_chunks_start > 0:
# We'll iterate over each index for the non-spatial dimensions, run the desired
Expand Down
14 changes: 12 additions & 2 deletions ngff_zarr/methods/_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,25 @@ def _dim_scale_factors(dims, scale_factor, previous_dim_factors):
result_scale_factors = {
d: int(scale_factor[d] / previous_dim_factors[d]) for d in scale_factor
}
# if a dim is not in the scale_factors, add it with a scale factor of 1
for d in dims:
if d not in result_scale_factors:
result_scale_factors[d] = 1

return result_scale_factors


def _update_previous_dim_factors(scale_factor, spatial_dims, previous_dim_factors):
previous_dim_factors = copy.copy(previous_dim_factors)
if isinstance(scale_factor, int):
previous_dim_factors = { d : scale_factor for d in spatial_dims }
for d in spatial_dims:
previous_dim_factors[d] = scale_factor
else:
previous_dim_factors = scale_factor
for d in scale_factor:
previous_dim_factors[d] = scale_factor[d]
return previous_dim_factors


def _align_chunks(previous_image, default_chunks, dim_factors):
block_0_shape = [c[0] for c in previous_image.data.chunks]

Expand Down
1,333 changes: 666 additions & 667 deletions pixi.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies = [
"dask[array]",
"importlib_resources",
"itkwasm >= 1.0b183",
"itkwasm-downsample >= 1.2.0",
"itkwasm-downsample >= 1.7.1",
"numpy",
"platformdirs",
"psutil; sys_platform != \"emscripten\"",
Expand Down
37 changes: 36 additions & 1 deletion test/test_to_ngff_zarr_itk.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from ngff_zarr import Methods, to_multiscales
from ngff_zarr import Methods, to_multiscales, to_ngff_image

from ._data import verify_against_baseline
import platform
Expand All @@ -20,6 +20,41 @@ def test_bin_shrink_isotropic_scale_factors(input_images):
verify_against_baseline(dataset_name, baseline_name, multiscales)


def test_bin_shrink_tzyxc():
import dask.array as da

test_array = da.ones((96, 64, 64, 64, 2), chunks=(1, 64, 64, 64, 1), dtype="uint8")
img = to_ngff_image(
test_array,
dims=list("tzyxc"),
scale={
"t": 100_000.0,
"z": 0.98,
"y": 0.98,
"x": 0.98,
"c": 1.0,
},
axes_units={
"t": "millisecond",
"z": "micrometer",
"y": "micrometer",
"x": "micrometer",
},
name="000x_000y_000z",
)

# expect a ValueError
try:
to_multiscales(
img,
scale_factors=[{"z": 2, "y": 2, "x": 2}, {"z": 4, "y": 4, "x": 4}],
method=Methods.ITK_BIN_SHRINK,
)
assert False, "Expected ValueError for non-spatial dimensions"
except ValueError:
pass


@pytest.mark.skipif(
platform.system() == "Linux" and platform.machine() == "aarch64",
reason="Skipping on Linux ARM systems",
Expand Down
33 changes: 33 additions & 0 deletions test/test_to_ngff_zarr_itkwasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,39 @@ def test_bin_shrink_tczyx():
assert multiscales.images[1].data.shape[2] == 16


def test_bin_shrink_tzyxc():
import dask.array as da

test_array = da.ones(
(96, 256, 256, 256, 2), chunks=(1, 128, 128, 128, 1), dtype="uint16"
)
img = to_ngff_image(
test_array,
dims=list("tzyxc"),
scale={
"t": 100_000.0,
"z": 0.98,
"y": 0.98,
"x": 0.98,
"c": 1.0,
},
axes_units={
"t": "millisecond",
"z": "micrometer",
"y": "micrometer",
"x": "micrometer",
},
name="000x_000y_000z",
)

multiscales = to_multiscales(
img,
scale_factors=[{"z": 2, "y": 2, "x": 2}, {"z": 4, "y": 4, "x": 4}],
method=Methods.ITKWASM_BIN_SHRINK,
)
assert len(multiscales.images) == 3


def test_bin_shrink_isotropic_scale_factors(input_images):
dataset_name = "cthead1"
image = input_images[dataset_name]
Expand Down
Loading