Skip to content

Commit

Permalink
Add test for bad .zarr file with missing OME info
Browse files Browse the repository at this point in the history
  • Loading branch information
annshress committed Sep 5, 2023
1 parent d5e74f6 commit 5f29601
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.pytest_cache
coverage.xml
.coverage*
htmlcov
pytools/_version.py
__pycache__
Expand Down
5 changes: 4 additions & 1 deletion pytools/HedwigZarrImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def shader_type(
"""
Produces the shader type one of: RGB, Grayscale, or MultiChannel.
"""
if self.ome_info.maybe_rgb(self.ome_idx):
if self.ome_info and self.ome_info.maybe_rgb(self.ome_idx):
return "RGB"
if self._ome_ngff_multiscale_dims()[1] == "C" and self.shape[1] == 1:
return "Grayscale"
Expand All @@ -220,6 +220,9 @@ def neuroglancer_shader_parameters(self, mad_scale=3) -> dict:
}

if _shader_type == "MultiChannel":
if self.ome_info is None:
return {}

assert self._ome_ngff_multiscale_dims()[1] == "C"
assert len(list(self.ome_info.channel_names(self.ome_idx))) == self.shape[1]
color_sequence = ["red", "green", "blue", "cyan", "yellow", "magenta"]
Expand Down
6 changes: 2 additions & 4 deletions pytools/HedwigZarrImages.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ def ome_xml_path(self) -> Optional[Path]:
_xml_path = Path(self.zarr_store.path) / self.zarr_root["OME"].path / "METADATA.ome.xml"
if _xml_path.exists():
return _xml_path
else:
return None

@property
def ome_info(self) -> Optional[AnyStr]:
Expand Down Expand Up @@ -79,12 +77,12 @@ def get_series_keys(self) -> Iterable[str]:

def __getitem__(self, item) -> HedwigZarrImage:
for k_idx, k in enumerate(self.get_series_keys()):
if item == k:
if item == k and "OME" in self.zarr_root.group_keys():
ome_index_to_zarr_group = self.zarr_root["OME"].attrs["series"]
zarr_idx = ome_index_to_zarr_group[k_idx]
return HedwigZarrImage(self.zarr_root[zarr_idx], self.ome_info, k_idx)

return HedwigZarrImage(self.zarr_root[item])
return HedwigZarrImage(self.zarr_root[item], None, 404)

def series(self) -> Iterable[Tuple[str, HedwigZarrImage]]:
"""An Iterable of key and HedwigZarrImages stored in the ZARR structure."""
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ flake8
black
wheel
packaging
pytest-cov
-r requirements.txt
45 changes: 45 additions & 0 deletions test/test_HedwigZarrImage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# 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.
#

import pytest
import shutil
from pytools.HedwigZarrImages import HedwigZarrImages


@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(
data_path, zarr_name, image_ext, array_shape, dims, shader_type, ngff_dims, shader_params, tmp_path
):
# Remove OME directory from the .zarr file
shutil.copytree(data_path / zarr_name, tmp_path / zarr_name)
shutil.rmtree(tmp_path / zarr_name / "OME")

zi = HedwigZarrImages(tmp_path / zarr_name)
assert zi.ome_xml_path is None
assert zi.ome_info is None
keys = list(zi.get_series_keys())
print(f"zarr groups: {keys}")
print(zi.zarr_root[keys[0]])

for k, z_img in zi.series():
print(k)
assert array_shape == z_img.shape
print(f"{z_img.path=}")
assert dims == z_img.dims
assert shader_type == z_img.shader_type
assert ngff_dims == z_img._ome_ngff_multiscale_dims()
assert shader_params == z_img.neuroglancer_shader_parameters()

0 comments on commit 5f29601

Please sign in to comment.