From 06e56c62de43342a25b2add625fb39daa003ce87 Mon Sep 17 00:00:00 2001 From: Alan O'Callaghan Date: Thu, 31 Oct 2024 11:54:49 +0000 Subject: [PATCH] Add zarr test --- .gitignore | 1 + qubalab/images/bioio_server.py | 2 +- tests/images/test_bioio_server.py | 112 ++++++++++++++++++- tests/res/single_resolution_float_5d_zarr.py | 60 ++++++++++ 4 files changed, 173 insertions(+), 2 deletions(-) create mode 100644 tests/res/single_resolution_float_5d_zarr.py diff --git a/.gitignore b/.gitignore index bb687b0..d226ef3 100644 --- a/.gitignore +++ b/.gitignore @@ -137,3 +137,4 @@ dmypy.json # Generated test images *.tif +*.zarr diff --git a/qubalab/images/bioio_server.py b/qubalab/images/bioio_server.py index b19259c..6d485ad 100644 --- a/qubalab/images/bioio_server.py +++ b/qubalab/images/bioio_server.py @@ -35,7 +35,7 @@ def __init__(self, path: str, scene: int = 0, detect_resolutions=True, bioio_kwa """ super().__init__(**kwargs) self._path = path - self._reader = BioImage(path, dask_tiles=True, **bioio_kwargs) + self._reader = BioImage(path, **bioio_kwargs) self._scene = scene self._detect_resolutions = detect_resolutions diff --git a/tests/images/test_bioio_server.py b/tests/images/test_bioio_server.py index b6f6ab3..25a735e 100644 --- a/tests/images/test_bioio_server.py +++ b/tests/images/test_bioio_server.py @@ -2,7 +2,7 @@ from qubalab.images.bioio_server import BioIOServer from qubalab.images.region_2d import Region2D from qubalab.images.metadata.pixel_calibration import PixelCalibration, PixelLength -from ..res import multi_resolution_uint8_3channels, single_resolution_float_5d, single_resolution_rgb_image +from ..res import multi_resolution_uint8_3channels, single_resolution_float_5d, single_resolution_float_5d_zarr, single_resolution_rgb_image def test_uint8_3channels_image_name(): @@ -217,6 +217,116 @@ def test_read_float_5d_image_with_dask(): bioio_server.close() + +## start + +def test_float_5d_zarr_image_name(): + bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) + + name = bioio_server.metadata.name + + assert name == single_resolution_float_5d_zarr.get_name() + + bioio_server.close() + + +def test_float_5d_zarr_image_shapes(): + bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) + + shapes = bioio_server.metadata.shapes + + assert shapes == single_resolution_float_5d_zarr.get_shapes() + + bioio_server.close() + + +def test_float_5d_zarr_image_pixel_calibration(): + bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) + + pixel_calibration = bioio_server.metadata.pixel_calibration + + assert pixel_calibration == PixelCalibration( + PixelLength(single_resolution_float_5d_zarr.get_pixel_size_x_y_in_micrometers()), # The BioIO library does not currently handle unit attachment + PixelLength(single_resolution_float_5d_zarr.get_pixel_size_x_y_in_micrometers()), + PixelLength(single_resolution_float_5d_zarr.get_pixel_size_x_y_in_micrometers()) + ) + + bioio_server.close() + + +def test_float_5d_zarr_image_is_not_rgb(): + bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) + + is_rgb = bioio_server.metadata.is_rgb + + assert not(is_rgb) + + bioio_server.close() + + +def test_float_5d_zarr_image_dtype(): + bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) + + dtype = bioio_server.metadata.dtype + + assert dtype == single_resolution_float_5d_zarr.get_dtype() + + bioio_server.close() + + +def test_float_5d_zarr_image_downsamples(): + bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) + + downsamples = bioio_server.metadata.downsamples + + assert downsamples == single_resolution_float_5d_zarr.get_downsamples() + + bioio_server.close() + + +def test_read_float_5d_zarr_image(): + full_resolution = single_resolution_float_5d_zarr.get_shapes()[0] + z = int(full_resolution.z / 2) + t = int(full_resolution.t / 2) + bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) + expected_pixels = np.array( + [[[single_resolution_float_5d_zarr.get_pixel_value(x, y, c, z, t) + for x in range(full_resolution.x)] + for y in range(full_resolution.y)] + for c in range(full_resolution.c)], + single_resolution_float_5d_zarr.get_dtype() + ) + + image = bioio_server.read_region( + 1, + Region2D(width=bioio_server.metadata.width, height=bioio_server.metadata.height, z=z, t=t) + ) + + np.testing.assert_array_equal(image, expected_pixels) + + bioio_server.close() + + +def test_read_float_5d_zarr_image_with_dask(): + full_resolution = single_resolution_float_5d_zarr.get_shapes()[0] + bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) + expected_pixels = np.array( + [[[[[single_resolution_float_5d_zarr.get_pixel_value(x, y, c, z, t) + for x in range(full_resolution.x)] + for y in range(full_resolution.y)] + for z in range(full_resolution.z)] + for c in range(full_resolution.c)] + for t in range(full_resolution.t)], + single_resolution_float_5d_zarr.get_dtype() + ) + + image = bioio_server.level_to_dask(0).compute() + + np.testing.assert_array_equal(image, expected_pixels) + + bioio_server.close() + + def test_rgb_image_name(): bioio_server = BioIOServer(single_resolution_rgb_image.get_path()) diff --git a/tests/res/single_resolution_float_5d_zarr.py b/tests/res/single_resolution_float_5d_zarr.py new file mode 100644 index 0000000..1abea0b --- /dev/null +++ b/tests/res/single_resolution_float_5d_zarr.py @@ -0,0 +1,60 @@ +import bioio.writers +import os +import shutil +import numpy as np +from bioio_base.types import PhysicalPixelSizes +from qubalab.images.metadata.image_shape import ImageShape + + +def get_name() -> str: + return "single_resolution_float_5d.ome.zarr" + + +def get_path() -> str: + return os.path.realpath(os.path.join(os.path.realpath(__file__), os.pardir, get_name())) + + +def get_shapes() -> tuple[ImageShape, ...]: + return (ImageShape(32, 16, 10, 5, 15),) + + +def get_pixel_size_x_y_in_micrometers() -> float: + return 0.5 + + +def get_dtype(): + return np.float64 + + +def get_downsamples() -> tuple[float, ...]: + return tuple([get_shapes()[0].x / shape.x for shape in get_shapes()]) + + +def get_pixel_value(x: int, y: int, c: int, z: int, t: int) -> int: + return pixels[t, c, z, y, x] + + +def _get_pixels() -> np.array: + return np.random.rand(get_shapes()[0].t, get_shapes()[0].c, get_shapes()[0].z, get_shapes()[0].y, get_shapes()[0].x) + + +def _write_image(pixels: np.array): + ## zarr writer fails if dir exists + if os.path.exists(get_path()) and os.path.isdir(get_path()): + shutil.rmtree(get_path()) + + zarr = bioio.writers.OmeZarrWriter(get_path()) + zarr.write_image( + image_data = pixels, + image_name = "single_resolution_float_5d", + channel_names = ["Channel " + str(i) for i in range(get_shapes()[0].c)], + channel_colors = [i for i in range(get_shapes()[0].c)], + physical_pixel_sizes = PhysicalPixelSizes( + X = get_pixel_size_x_y_in_micrometers(), + Y = get_pixel_size_x_y_in_micrometers(), + Z = get_pixel_size_x_y_in_micrometers()) + ) + + +pixels = _get_pixels() +_write_image(pixels)