Skip to content

Commit

Permalink
Add test for .czi data file
Browse files Browse the repository at this point in the history
  • Loading branch information
annshress committed Sep 6, 2023
1 parent 874ac7c commit 285eb6e
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
75 changes: 74 additions & 1 deletion test/test_HedwigZarrImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
import pytest
import shutil
from pytools.HedwigZarrImages import HedwigZarrImages
from pytools.HedwigZarrImage import HedwigZarrImage


@pytest.mark.parametrize(
"zarr_name, image_ext, array_shape, dims, shader_type, ngff_dims, shader_params",
[("OM_P1_S1_ScanOnly_1k.zarr", "png", (1, 3, 1, 1024, 521), "XYC", "MultiChannel", "TCZYX", {})],
)
def test_HedwigZarrImage_info(
def test_HedwigZarrImage_info_bad_ome(
data_path, zarr_name, image_ext, array_shape, dims, shader_type, ngff_dims, shader_params, tmp_path
):
# Remove OME directory from the .zarr file
Expand All @@ -39,3 +40,75 @@ def test_HedwigZarrImage_info(
assert shader_type == z_img.shader_type
assert ngff_dims == z_img._ome_ngff_multiscale_dims()
assert shader_params == z_img.neuroglancer_shader_parameters()


@pytest.mark.parametrize(
"zarr_name, attrs",
[
(
"KC_M3_S2_ReducedImageSubset2.zarr",
[
("czi", (1, 2, 1, 3102, 206), "XYC", "MultiChannel", "TCZYX", {"channelArray": 2}),
("label", (1, 3, 1, 758, 1588), "XYC", "RGB", "TCZYX", {}),
("macro", (1, 3, 1, 685, 567), "XYC", "RGB", "TCZYX", {}),
],
)
],
)
def test_HedwigZarrImage_info_for_czi(data_path, zarr_name, attrs):
zi = HedwigZarrImages(data_path / zarr_name)
assert zi.ome_xml_path is not None
image_names = list(zi.get_series_keys())
assert len(image_names) == 3
assert all(image_names)

for (k, z_img), attr in zip(zi.series(), attrs):
image_ext, array_shape, dims, shader_type, ngff_dims, shader_params = attr
# assert image_ext in k
assert array_shape == z_img.shape
assert dims == z_img.dims
assert shader_type == z_img.shader_type
assert ngff_dims == z_img._ome_ngff_multiscale_dims()
for param_key in shader_params:
shader_params[param_key] == len(z_img.neuroglancer_shader_parameters()[param_key])


@pytest.mark.parametrize("targetx, targety", [(300, 300), (600, 600), (1024, 1024)])
def test_hedwigimage_extract_2d(data_path, targetx, targety):
zarr_name = "KC_M3_S2_ReducedImageSubset2.zarr"
zi = HedwigZarrImages(data_path / zarr_name)
for k, z_img in zi.series():
sitk_img = z_img.extract_2d(targetx, targety)
x, y = sitk_img.GetSize()
shape = dict(zip(z_img._ome_ngff_multiscale_dims(), z_img.shape))
actualx, actualy = shape["X"], shape["Y"]
assert x == targetx if actualx > actualy else y == targety


def test_hedwigimage_extract_2d_invalid_shapes(data_path, monkeypatch):
zarr_name = "KC_M3_S2_ReducedImageSubset2.zarr"
zi = HedwigZarrImages(data_path / zarr_name)

@property
def _mock_shape(obj):
return (2, 3, 1, 100, 100)

monkeypatch.setattr(HedwigZarrImage, "shape", _mock_shape)

z_img = zi[list(zi.get_series_keys())[0]]
assert z_img.shape[0] == 2
with pytest.raises(ValueError) as execinfo:
z_img.extract_2d(300, 300)
assert "Time dimension" in str(execinfo.value)

@property
def _mock_shape(obj):
return (1, 3, 3, 100, 100)

monkeypatch.setattr(HedwigZarrImage, "shape", _mock_shape)

z_img = zi[list(zi.get_series_keys())[0]]
assert z_img.shape[2] == 3
with pytest.raises(ValueError) as execinfo:
z_img.extract_2d(300, 300)
assert "Z dimension" in str(execinfo.value)
29 changes: 29 additions & 0 deletions test/test_omeinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from pytools.HedwigZarrImages import HedwigZarrImages


def test_ome_info(data_path):
zarr_name = "KC_M3_S2_ReducedImageSubset2.zarr"
zi = HedwigZarrImages(data_path / zarr_name)
assert zi.ome_xml_path is not None
assert zi.ome_info is not None
ome = zi.ome_info

assert ome.number_of_images() == 3
# assert all(list(ome.image_names()))
assert len(list(ome.channel_names(0))) == 2
assert len(list(ome.channel_names(1))) == 0
assert len(list(ome.channel_names(2))) == 0

0 comments on commit 285eb6e

Please sign in to comment.