Skip to content

Commit

Permalink
expand testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzocerrone committed Aug 29, 2024
1 parent a11c1c5 commit b73ccc2
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/ngio/core/image_like_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions src/ngio/ngff_meta/fractal_image_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions tests/data/meta_v04/base_ome_zarr_label_meta.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
14 changes: 14 additions & 0 deletions tests/ngff_meta/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions tests/ngff_meta/test_fractal_image_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b73ccc2

Please sign in to comment.