diff --git a/.gitignore b/.gitignore index 924894f..5b84625 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .pytest_cache coverage.xml +.coverage* htmlcov pytools/_version.py __pycache__ diff --git a/pytools/HedwigZarrImage.py b/pytools/HedwigZarrImage.py index 3bdf61d..4d188d9 100644 --- a/pytools/HedwigZarrImage.py +++ b/pytools/HedwigZarrImage.py @@ -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" @@ -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 {} diff --git a/pytools/HedwigZarrImages.py b/pytools/HedwigZarrImages.py index 87ff9a2..7ed57d5 100644 --- a/pytools/HedwigZarrImages.py +++ b/pytools/HedwigZarrImages.py @@ -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]: @@ -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.""" diff --git a/requirements-dev.txt b/requirements-dev.txt index e019291..cd22df1 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -6,4 +6,5 @@ flake8 black wheel packaging +pytest-cov -r requirements.txt diff --git a/test/test_HedwigZarrImage.py b/test/test_HedwigZarrImage.py new file mode 100644 index 0000000..9166902 --- /dev/null +++ b/test/test_HedwigZarrImage.py @@ -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() diff --git a/test/test_HedwigZarrImages.py b/test/test_HedwigZarrImages.py index 362666d..b63e0a9 100644 --- a/test/test_HedwigZarrImages.py +++ b/test/test_HedwigZarrImages.py @@ -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 @@ -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()