From b73ccc270f591f0d25f6ce1272a08362dc9a2752 Mon Sep 17 00:00:00 2001 From: lorenzo Date: Thu, 29 Aug 2024 23:33:25 +0200 Subject: [PATCH] expand testing --- src/ngio/core/image_like_handler.py | 2 +- src/ngio/ngff_meta/fractal_image_meta.py | 8 ++-- .../meta_v04/base_ome_zarr_label_meta.json | 44 +++++++++++++++++++ tests/ngff_meta/conftest.py | 14 ++++++ tests/ngff_meta/test_fractal_image_meta.py | 30 +++++++++++++ 5 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 tests/data/meta_v04/base_ome_zarr_label_meta.json diff --git a/src/ngio/core/image_like_handler.py b/src/ngio/core/image_like_handler.py index d70ff67..fa430dd 100644 --- a/src/ngio/core/image_like_handler.py +++ b/src/ngio/core/image_like_handler.py @@ -2,7 +2,7 @@ from zarr.store.common import StoreLike -from ngio.ngff_meta import FractalImageMeta, get_ngff_image_meta_handler, PixelSize +from ngio.ngff_meta import FractalImageMeta, PixelSize, get_ngff_image_meta_handler class ImageLikeHandler: diff --git a/src/ngio/ngff_meta/fractal_image_meta.py b/src/ngio/ngff_meta/fractal_image_meta.py index 2c96e60..67f751a 100644 --- a/src/ngio/ngff_meta/fractal_image_meta.py +++ b/src/ngio/ngff_meta/fractal_image_meta.py @@ -169,7 +169,7 @@ def to_ordered_list(self) -> list: """Return the pixel size according to the axis order.""" return [getattr(self, axis) for axis in self.axis_order] - def change_units(self, new_unit: SpaceUnits | str) -> "PixelSize": + def to_units(self, new_unit: SpaceUnits | str) -> "PixelSize": """Return a new PixelSize object with the pixel size in the new unit. Args: @@ -486,10 +486,10 @@ def axes_names(self) -> list[str]: """List of axes names in the Image.""" names = [] for ax in self.axes: - if isinstance(ax.name, str): - names.append(ax.name) - else: + if isinstance(ax.name, SpaceNames) or isinstance(ax.name, TimeNames): names.append(ax.name.value) + else: + names.append(ax.name) return names @property diff --git a/tests/data/meta_v04/base_ome_zarr_label_meta.json b/tests/data/meta_v04/base_ome_zarr_label_meta.json new file mode 100644 index 0000000..69c66d8 --- /dev/null +++ b/tests/data/meta_v04/base_ome_zarr_label_meta.json @@ -0,0 +1,44 @@ +{ + "multiscales": [ + { + "axes": [ + {"name": "z", "type": "space", "unit": "micrometer"}, + {"name": "y", "type": "space", "unit": "micrometer"}, + {"name": "x", "type": "space", "unit": "micrometer"} + ], + "datasets": [ + { + "coordinateTransformations": [ + {"scale": [1.0, 0.1625, 0.1625], "type": "scale"} + ], + "path": "0" + }, + { + "coordinateTransformations": [ + {"scale": [1.0, 0.325, 0.325], "type": "scale"} + ], + "path": "1" + }, + { + "coordinateTransformations": [ + {"scale": [1.0, 0.65, 0.65], "type": "scale"} + ], + "path": "2" + }, + { + "coordinateTransformations": [ + {"scale": [1.0, 1.3, 1.3], "type": "scale"} + ], + "path": "3" + }, + { + "coordinateTransformations": [ + {"scale": [1.0, 2.6, 2.6], "type": "scale"} + ], + "path": "4" + } + ], + "version": "0.4" + } + ] +} \ No newline at end of file diff --git a/tests/ngff_meta/conftest.py b/tests/ngff_meta/conftest.py index 0233e9c..f2ed3ba 100644 --- a/tests/ngff_meta/conftest.py +++ b/tests/ngff_meta/conftest.py @@ -18,3 +18,17 @@ def ome_zarr_image_v04_path(tmpdir): base_ome_zarr_meta = base_ome_zarr_meta group.attrs.update(base_ome_zarr_meta) return zarr_path + + +@fixture +def ome_zarr_label_v04_path(tmpdir): + zarr_path = Path(tmpdir) / "test_ome_ngff_image_v04.zarr" + + group = zarr.open_group(store=zarr_path, mode="w", zarr_format=2) + + with open("tests/data/meta_v04/base_ome_zarr_label_meta.json") as f: + base_ome_zarr_meta = json.load(f) + + base_ome_zarr_meta = base_ome_zarr_meta + group.attrs.update(base_ome_zarr_meta) + return zarr_path diff --git a/tests/ngff_meta/test_fractal_image_meta.py b/tests/ngff_meta/test_fractal_image_meta.py index 54fffd2..9060e03 100644 --- a/tests/ngff_meta/test_fractal_image_meta.py +++ b/tests/ngff_meta/test_fractal_image_meta.py @@ -45,3 +45,33 @@ def test_modify_axis_from_metadata(self, ome_zarr_image_v04_path): metadata=meta_no_channel, idx=0, axis_name="c", units=None ) assert meta_add_channel.axes_names == fractal_meta.axes_names + + def test_pixel_size(self, ome_zarr_image_v04_path): + from ngio.ngff_meta import get_ngff_image_meta_handler + + handler = get_ngff_image_meta_handler( + store=ome_zarr_image_v04_path, meta_mode="image" + ) + + pixel_size = handler.load_meta().pixel_size(level_path=0) + assert pixel_size.to_ordered_list() == [1.0, 0.1625, 0.1625] + pixel_size_nm = pixel_size.to_units("nm") + assert pixel_size_nm.to_ordered_list() == [1000.0, 162.5, 162.5] + + def test_modify_axis_from_label_metadata(self, ome_zarr_label_v04_path): + from ngio.ngff_meta import get_ngff_image_meta_handler + from ngio.ngff_meta.utils import add_axis_to_metadata, remove_axis_from_metadata + + handler = get_ngff_image_meta_handler( + store=ome_zarr_label_v04_path, meta_mode="label" + ) + + fractal_meta = handler.load_meta() + + meta_no_channel = fractal_meta.remove_axis(axis_name="z") + assert meta_no_channel.axes_names == ["y", "x"] + + meta_add_channel = meta_no_channel.add_axis( + idx=0, axis_name="z", units="micrometer", axis_type="space" + ) + assert meta_add_channel.axes_names == fractal_meta.axes_names