Skip to content

Commit

Permalink
Add zarr test
Browse files Browse the repository at this point in the history
  • Loading branch information
alanocallaghan committed Oct 31, 2024
1 parent 9ea463e commit 06e56c6
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,4 @@ dmypy.json

# Generated test images
*.tif
*.zarr
2 changes: 1 addition & 1 deletion qubalab/images/bioio_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
112 changes: 111 additions & 1 deletion tests/images/test_bioio_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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())

Expand Down
60 changes: 60 additions & 0 deletions tests/res/single_resolution_float_5d_zarr.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 06e56c6

Please sign in to comment.