diff --git a/src/ngio/ngff_meta/fractal_image_meta.py b/src/ngio/ngff_meta/fractal_image_meta.py index fe43273..d090076 100644 --- a/src/ngio/ngff_meta/fractal_image_meta.py +++ b/src/ngio/ngff_meta/fractal_image_meta.py @@ -105,7 +105,9 @@ def __str__(self) -> str: return f"PixelSize(x={self.x}, y={self.y}, z={self.z}, unit={self.unit.value})" @classmethod - def from_list(cls, sizes: list[float], unit: SpaceUnits) -> "PixelSize": + def from_list( + cls, sizes: list[float], unit: SpaceUnits = SpaceUnits.micrometer + ) -> "PixelSize": """Build a PixelSize object from a list of sizes. Note: The order of the sizes must be z, y, x. @@ -115,7 +117,7 @@ def from_list(cls, sizes: list[float], unit: SpaceUnits) -> "PixelSize": unit(SpaceUnits): The unit of the sizes. """ if len(sizes) == 2: - return cls(y=sizes[0], x=sizes[1], unit=unit) + return cls(y=sizes[0], x=sizes[1], z=1, unit=unit) elif len(sizes) == 3: return cls(z=sizes[0], y=sizes[1], x=sizes[2], unit=unit) else: @@ -135,10 +137,12 @@ def yx(self) -> tuple: """Return the xy plane pixel size in y, x order.""" return self.y, self.x + @property def voxel_volume(self) -> float: """Return the volume of a voxel.""" - return self.y * self.x * (self.z or 1) + return self.y * self.x * self.z + @property def xy_plane_area(self) -> float: """Return the area of the xy plane.""" return self.y * self.x diff --git a/tests/ngff_meta/test_pixel_size.py b/tests/ngff_meta/test_pixel_size.py new file mode 100644 index 0000000..80db6b6 --- /dev/null +++ b/tests/ngff_meta/test_pixel_size.py @@ -0,0 +1,27 @@ +import pytest + + +class TestPixelSize: + def test_pixel_size_from_list(self) -> None: + from ngio.ngff_meta import PixelSize + + pix_size_2d = PixelSize.from_list([0.1625, 0.1625]) + assert pix_size_2d.zyx == (1.0, 0.1625, 0.1625) + + pix_size_3d = PixelSize.from_list([0.1625, 0.1625, 0.1625]) + assert pix_size_3d.zyx == (0.1625, 0.1625, 0.1625) + + with pytest.raises(ValueError): + PixelSize.from_list([0.1625, 0.1625, 0.1625, 0.1625]) + + def test_pixel_size(self) -> None: + from ngio.ngff_meta import PixelSize + + pixel_size = PixelSize(x=0.1625, y=0.1625, z=0.25) + assert pixel_size.zyx == (0.25, 0.1625, 0.1625) + assert pixel_size.yx == (0.1625, 0.1625) + assert pixel_size.voxel_volume == 0.1625 * 0.1625 * 0.25 + assert pixel_size.xy_plane_area == 0.1625 * 0.1625 + + plixel_size2 = PixelSize(x=0.1625, y=0.1625, z=0.5) + assert pixel_size.distance(plixel_size2) == 0.25