From f8f4adad1dd92e3ce3d05156453db2c01e96a251 Mon Sep 17 00:00:00 2001 From: bendichter Date: Fri, 16 Jun 2023 20:17:54 -0400 Subject: [PATCH 1/4] add can_read classmethod to ZarrIO add test for new method --- src/hdmf_zarr/backend.py | 8 ++++++++ tests/unit/base_tests_zarrio.py | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/src/hdmf_zarr/backend.py b/src/hdmf_zarr/backend.py index 7c364864..fd3579a2 100644 --- a/src/hdmf_zarr/backend.py +++ b/src/hdmf_zarr/backend.py @@ -75,6 +75,14 @@ class ZarrIO(HDMFIO): + @classmethod + def can_read(cls, path): + try: + zarr.open(path, mode="r") + return True + except Exception: + return False + @docval({'name': 'path', 'type': (str, *SUPPORTED_ZARR_STORES), 'doc': 'the path to the Zarr file or a supported Zarr store'}, diff --git a/tests/unit/base_tests_zarrio.py b/tests/unit/base_tests_zarrio.py index 3e157413..9319928a 100644 --- a/tests/unit/base_tests_zarrio.py +++ b/tests/unit/base_tests_zarrio.py @@ -170,6 +170,9 @@ def createReferenceCompoundBuilder(self): 'ref_dataset': dataset_ref}) return builder + def test_cannot_read(self): + assert not ZarrIO.can_read("incorrect_path") + def read_test_dataset(self): reader = ZarrIO(self.store, manager=self.manager, mode='r') self.root = reader.read_builder() @@ -209,6 +212,7 @@ def test_write_int(self, test_data=None): writer = ZarrIO(self.store, manager=self.manager, mode='a') writer.write_builder(self.builder) writer.close() + assert ZarrIO.can_read(self.store) def test_write_compound(self, test_data=None): """ @@ -295,6 +299,7 @@ def test_write_reference_compound(self): def test_read_int(self): test_data = np.arange(100, 200, 10).reshape(5, 2) self.test_write_int(test_data=test_data) + dataset = self.read_test_dataset()['data'][:] self.assertTrue(np.all(test_data == dataset)) From d4c196488590bd1060979d7ba9eec1a12b217b53 Mon Sep 17 00:00:00 2001 From: bendichter Date: Fri, 16 Jun 2023 20:32:51 -0400 Subject: [PATCH 2/4] change to static method --- src/hdmf_zarr/backend.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hdmf_zarr/backend.py b/src/hdmf_zarr/backend.py index fd3579a2..153b703b 100644 --- a/src/hdmf_zarr/backend.py +++ b/src/hdmf_zarr/backend.py @@ -75,8 +75,8 @@ class ZarrIO(HDMFIO): - @classmethod - def can_read(cls, path): + @staticmethod + def can_read(path): try: zarr.open(path, mode="r") return True From fdf5475fae8129b6e688963f41947fb2366fd6dc Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Fri, 16 Jun 2023 20:33:21 -0400 Subject: [PATCH 3/4] Update tests/unit/base_tests_zarrio.py --- tests/unit/base_tests_zarrio.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/base_tests_zarrio.py b/tests/unit/base_tests_zarrio.py index 9319928a..6b1c7005 100644 --- a/tests/unit/base_tests_zarrio.py +++ b/tests/unit/base_tests_zarrio.py @@ -299,7 +299,6 @@ def test_write_reference_compound(self): def test_read_int(self): test_data = np.arange(100, 200, 10).reshape(5, 2) self.test_write_int(test_data=test_data) - dataset = self.read_test_dataset()['data'][:] self.assertTrue(np.all(test_data == dataset)) From 26cad6393170be8dcc6ef14a53dc91674c5a0912 Mon Sep 17 00:00:00 2001 From: Ryan Ly Date: Sun, 18 Jun 2023 00:48:20 -0700 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index acb3b0fb..595f43a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### Minor enhancements * Updated handling of references on read to simplify future integration of file-based Zarr stores (e.g., ZipStore or database stores) @oruebel [#62](https://github.com/hdmf-dev/hdmf-zarr/pull/62) +* Added can_read classmethod to ZarrIO. @bendichter [#97](https://github.com/hdmf-dev/hdmf-zarr/pull/97) ### Test suite enhancements * Modularized unit tests to simplify running tests for multiple Zarr storage backends