Skip to content

Commit

Permalink
add pixel_size testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzocerrone committed Nov 7, 2024
1 parent ff60b9a commit 9e90980
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/ngio/ngff_meta/fractal_image_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions tests/ngff_meta/test_pixel_size.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9e90980

Please sign in to comment.