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 6, 2023
1 parent dcd6de2 commit 043f7f6
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 11 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 @@ -207,6 +207,9 @@ def neuroglancer_shader_parameters(self, mad_scale=3) -> dict:
Produces the "shaderParameters" portion of the metadata for Neuroglancer
returns JSON serializable object
"""
if self.ome_info is None:
return {}

_shader_type = self.shader_type
if _shader_type == "RGB":
return {}
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 @@ -6,4 +6,5 @@ flake8
black
wheel
packaging
pytest-cov
-r requirements.txt
41 changes: 41 additions & 0 deletions test/test_HedwigZarrImage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# 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())

for k, z_img in zi.series():
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()
assert shader_params == z_img.neuroglancer_shader_parameters()
6 changes: 0 additions & 6 deletions test/test_HedwigZarrImages.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def temp_zarr_path(request, tmp_path_factory, data_path):
tmp_zarr = tmp_path_factory.mktemp("Z") / zarr_name
shutil.copytree(data_path / zarr_name, tmp_zarr)

print(f"temp_zarr_path: {tmp_zarr}")
return tmp_zarr


Expand All @@ -52,15 +51,10 @@ def test_HedwigZarrImage_info(
data_path, zarr_name, image_ext, array_shape, dims, shader_type, ngff_dims, shader_params
):
zi = HedwigZarrImages(data_path / zarr_name)
keys = list(zi.get_series_keys())
print(f"zarr groups: {keys}")

print(zi.ome_xml_path)

for k, z_img in zi.series():
assert image_ext in 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()
Expand Down

0 comments on commit 043f7f6

Please sign in to comment.