Skip to content

Commit

Permalink
expand testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzocerrone committed Nov 13, 2024
1 parent 853f40f commit 2479961
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 15 deletions.
7 changes: 7 additions & 0 deletions docs/notebooks/basic_usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@
"print(f\"{new_ngff_image.levels_paths=}\")\n",
"print(f\"{new_ngff_image.num_levels=}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
35 changes: 26 additions & 9 deletions src/ngio/core/ngff_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,8 @@ def _compute_percentiles(
starts, ends = [], []
for c in range(num_c):
data = lowest_res_image.get_array(c=c, mode="dask").ravel()
_start_percentile = da.percentile(
data, start_percentile, method="nearest"
).compute()
_end_percentile = da.percentile(
data, end_percentile, method="nearest"
_start_percentile, _end_percentile = da.percentile(
data, [start_percentile, end_percentile], method="nearest"
).compute()

starts.append(_start_percentile)
Expand Down Expand Up @@ -209,7 +206,11 @@ def lazy_init_omero(
self._image_meta.write_meta(self.image_meta)

def update_omero_window(
self, start_percentile: int = 1, end_percentile: int = 99
self,
start_percentile: int = 1,
end_percentile: int = 99,
min_value: int | float | None = None,
max_value: int | float | None = None,
) -> None:
"""Update the OMERO window.
Expand All @@ -218,6 +219,8 @@ def update_omero_window(
Args:
start_percentile (int): The start percentile.
end_percentile (int): The end percentile
min_value (int | float | None): The minimum value of the window.
max_value (int | float | None): The maximum value of the window.
"""
start, ends = self._compute_percentiles(
Expand All @@ -226,7 +229,21 @@ def update_omero_window(
meta = self.image_meta
ref_image = self.get_image()

max_dtype = np.iinfo(ref_image.on_disk_array.dtype).max
for func in [np.iinfo, np.finfo]:
try:
type_max = func(ref_image.on_disk_array.dtype).max
type_min = func(ref_image.on_disk_array.dtype).min
break
except ValueError:
continue
else:
raise ValueError("Data type not recognized.")

Check warning on line 240 in src/ngio/core/ngff_image.py

View check run for this annotation

Codecov / codecov/patch

src/ngio/core/ngff_image.py#L240

Added line #L240 was not covered by tests

if min_value is None:
min_value = type_min
if max_value is None:
max_value = type_max

num_c = ref_image.dimensions.get("c", 1)

if meta.omero is None:
Expand All @@ -246,8 +263,8 @@ def update_omero_window(
):
channel.channel_visualisation.start = s
channel.channel_visualisation.end = e
channel.channel_visualisation.min = 0
channel.channel_visualisation.max = max_dtype
channel.channel_visualisation.min = min_value
channel.channel_visualisation.max = max_value

ngio_logger.info(
f"Updated window for channel {channel.label}. "
Expand Down
22 changes: 17 additions & 5 deletions src/ngio/ngff_meta/fractal_image_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,22 @@ def lazy_init(
data_type(Any): The data type of the channel.
active(bool): Whether the channel should be shown by default.
"""
start = start if start is not None else np.iinfo(data_type).min
end = end if end is not None else np.iinfo(data_type).max
for func in [np.iinfo, np.finfo]:
try:
min_value = func(data_type).min
max_value = func(data_type).max
break
except ValueError:
continue

Check warning on line 184 in src/ngio/ngff_meta/fractal_image_meta.py

View check run for this annotation

Codecov / codecov/patch

src/ngio/ngff_meta/fractal_image_meta.py#L183-L184

Added lines #L183 - L184 were not covered by tests
else:
raise ValueError(f"Invalid data type {data_type}.")

Check warning on line 186 in src/ngio/ngff_meta/fractal_image_meta.py

View check run for this annotation

Codecov / codecov/patch

src/ngio/ngff_meta/fractal_image_meta.py#L186

Added line #L186 was not covered by tests

start = start if start is not None else min_value
end = end if end is not None else max_value
return ChannelVisualisation(
color=color,
min=np.iinfo(data_type).min,
max=np.iinfo(data_type).max,
min=min_value,
max=max_value,
start=start,
end=end,
active=active,
Expand Down Expand Up @@ -246,7 +256,9 @@ def _check_elements(elements: Collection[T], expected_type: Any) -> Collection[T

for element in elements:
if not isinstance(element, expected_type):
raise ValueError(f"All elements must be of the same type {expected_type}.")
raise ValueError(

Check warning on line 259 in src/ngio/ngff_meta/fractal_image_meta.py

View check run for this annotation

Codecov / codecov/patch

src/ngio/ngff_meta/fractal_image_meta.py#L259

Added line #L259 was not covered by tests
f"All elements must be of the same type {expected_type}. Got {element}."
)

return elements

Expand Down
13 changes: 12 additions & 1 deletion tests/core/test_ngff_image.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from pathlib import Path


class TestNgffImage:
def test_ngff_image(self, ome_zarr_image_v04_path):
def test_ngff_image(self, ome_zarr_image_v04_path: Path) -> None:
from ngio.core.ngff_image import NgffImage

ngff_image = NgffImage(ome_zarr_image_v04_path)
image_handler = ngff_image.get_image(path="0")
ngff_image.__repr__()

assert ngff_image.num_levels == 5
assert ngff_image.levels_paths == ["0", "1", "2", "3", "4"]
Expand All @@ -27,3 +31,10 @@ def test_ngff_image(self, ome_zarr_image_v04_path):
assert (
new_image_handler.on_disk_array.chunks == image_handler.on_disk_array.chunks
)
new_ngff_image.lazy_init_omero(
labels=3,
wavelength_ids=["A01_C01", "A02_C02", "A03_C03"],
consolidate=True,
)

new_ngff_image.update_omero_window(start_percentile=1.1, end_percentile=98.9)
20 changes: 20 additions & 0 deletions tests/core/test_roi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class TestRoi:
def test_roi_conversion(self) -> None:
"""Test the conversion between world and raster coordinates."""
from ngio.core.roi import Dimensions, PixelSize, RasterCooROI, WorldCooROI

w_roi = WorldCooROI(x=0, y=0, z=0, x_length=10, y_length=10, z_length=10)

dim = Dimensions(
on_disk_shape=(10, 10, 10), axes_names=["z", "y", "x"], axes_order=[0, 1, 2]
)

r_roi = w_roi.to_raster_coo(PixelSize(x=0.1, y=0.1, z=0.1), dim)

assert (r_roi.x, r_roi.y, r_roi.z) == (0, 0, 0)

r_roi_2 = RasterCooROI(
x=0, y=0, z=0, x_length=10, y_length=10, z_length=10, original_roi=w_roi
)

assert r_roi_2.model_dump() == r_roi.model_dump()

0 comments on commit 2479961

Please sign in to comment.