From 5688425bbd0237283c888abef87bf33c79040d94 Mon Sep 17 00:00:00 2001 From: William Moore Date: Mon, 20 Mar 2023 18:24:22 +0000 Subject: [PATCH 1/3] Don't handle exception in Multiscales init() --- ome_zarr/reader.py | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/ome_zarr/reader.py b/ome_zarr/reader.py index 25312051..edbcaa78 100644 --- a/ome_zarr/reader.py +++ b/ome_zarr/reader.py @@ -277,28 +277,24 @@ def matches(zarr: ZarrLocation) -> bool: def __init__(self, node: Node) -> None: super().__init__(node) - try: - multiscales = self.lookup("multiscales", []) - version = multiscales[0].get( - "version", "0.1" - ) # should this be matched with Format.version? - datasets = multiscales[0]["datasets"] - axes = multiscales[0].get("axes") - fmt = format_from_version(version) - # Raises ValueError if not valid - axes_obj = Axes(axes, fmt) - node.metadata["axes"] = axes_obj.to_list() - # This will get overwritten by 'omero' metadata if present - node.metadata["name"] = multiscales[0].get("name") - paths = [d["path"] for d in datasets] - self.datasets: List[str] = paths - transformations = [d.get("coordinateTransformations") for d in datasets] - if any(trans is not None for trans in transformations): - node.metadata["coordinateTransformations"] = transformations - LOGGER.info("datasets %s", datasets) - except Exception: - LOGGER.exception("Failed to parse multiscale metadata") - return # EARLY EXIT + multiscales = self.lookup("multiscales", []) + version = multiscales[0].get( + "version", "0.1" + ) # should this be matched with Format.version? + datasets = multiscales[0]["datasets"] + axes = multiscales[0].get("axes") + fmt = format_from_version(version) + # Raises ValueError if not valid + axes_obj = Axes(axes, fmt) + node.metadata["axes"] = axes_obj.to_list() + # This will get overwritten by 'omero' metadata if present + node.metadata["name"] = multiscales[0].get("name") + paths = [d["path"] for d in datasets] + self.datasets: List[str] = paths + transformations = [d.get("coordinateTransformations") for d in datasets] + if any(trans is not None for trans in transformations): + node.metadata["coordinateTransformations"] = transformations + LOGGER.info("datasets %s", datasets) for resolution in self.datasets: data: da.core.Array = self.array(resolution, version) From 2c204a86bd9406a9d887a8b0eb2dc53316714dd4 Mon Sep 17 00:00:00 2001 From: William Moore Date: Wed, 22 Mar 2023 21:57:53 +0000 Subject: [PATCH 2/3] Add test to assert Exception raised --- tests/test_reader.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_reader.py b/tests/test_reader.py index f5d4a3fd..556a2087 100644 --- a/tests/test_reader.py +++ b/tests/test_reader.py @@ -33,6 +33,24 @@ def test_label(self): assert len(list(reader())) == 3 +class TestInvalid: + @pytest.fixture(autouse=True) + def initdir(self, tmpdir): + self.path = tmpdir.mkdir("data") + + def test_invalid_version(self): + grp = create_zarr(str(self.path)) + # update version to something invalid + attrs = grp.attrs.asdict() + attrs["multiscales"][0]["version"] = "invalid" + grp.attrs.put(attrs) + # should raise exception + with pytest.raises(ValueError) as exe: + reader = Reader(parse_url(str(self.path))) + assert len(list(reader())) == 2 + assert str(exe.value) == "Version invalid not recognized" + + class TestHCSReader: @pytest.fixture(autouse=True) def initdir(self, tmpdir): From 7b56cb7aed9ff8770e5829fddc222e4b8cc73f0c Mon Sep 17 00:00:00 2001 From: William Moore Date: Wed, 22 Mar 2023 22:11:52 +0000 Subject: [PATCH 3/3] create_zarr() returns Group --- ome_zarr/data.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ome_zarr/data.py b/ome_zarr/data.py index d669459a..4bcd42fa 100644 --- a/ome_zarr/data.py +++ b/ome_zarr/data.py @@ -104,7 +104,7 @@ def create_zarr( label_name: str = "coins", fmt: Format = CurrentFormat(), chunks: Union[Tuple, List] = None, -) -> None: +) -> zarr.Group: """Generate a synthetic image pyramid with labels.""" pyramid, labels = method() @@ -192,3 +192,5 @@ def create_zarr( "properties": properties, "source": {"image": "../../"}, } + + return grp