Skip to content

Commit

Permalink
First check for compatibility for zarr version 3 in storage backend
Browse files Browse the repository at this point in the history
  • Loading branch information
david-zwicker committed Jan 27, 2025
1 parent f9f8f93 commit 453811c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
22 changes: 18 additions & 4 deletions modelrunner/storage/backend/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@
import numpy as np
import zarr
from numpy.typing import ArrayLike, DTypeLike
from zarr._storage.store import Store

try:
# try import path from zarr version 3
from zarr.abc.store import Store

is_zarr2 = False

Check warning on line 22 in modelrunner/storage/backend/zarr.py

View check run for this annotation

Codecov / codecov/patch

modelrunner/storage/backend/zarr.py#L22

Added line #L22 was not covered by tests

except ImportError:
# try import path from zarr version 2
from zarr._storage.store import Store

is_zarr2 = True

from ..access_modes import ModeType
from ..attributes import AttrsLike
Expand Down Expand Up @@ -55,7 +66,10 @@ def __init__(self, store_or_path: str | Path | Store, *, mode: ModeType = "read"
if self.mode.file_mode == "r":
self._logger.info("DirectoryStore is always opened writable")

self._store = zarr.DirectoryStore(path)
if is_zarr2:
self._store = zarr.DirectoryStore(path)
else:
self._store = zarr.LocalStore(path)

Check warning on line 72 in modelrunner/storage/backend/zarr.py

View check run for this annotation

Codecov / codecov/patch

modelrunner/storage/backend/zarr.py#L72

Added line #L72 was not covered by tests

elif path.suffix == ".zip":
# create a ZipStore
Expand All @@ -65,7 +79,7 @@ def __init__(self, store_or_path: str | Path | Store, *, mode: ModeType = "read"
path.unlink()
self._store = zarr.storage.ZipStore(path, mode=file_mode)

elif path.suffix == ".sqldb":
elif is_zarr2 and path.suffix == ".sqldb":
# create a SQLiteStore
if self.mode.file_mode == "w" and path.exists():
self._logger.info("Delete file `%s`", path)
Expand Down Expand Up @@ -135,7 +149,7 @@ def keys(self, loc: Sequence[str] | None = None) -> Collection[str]:
return self._root.keys() # type: ignore

def is_group(self, loc: Sequence[str], *, ignore_cls: bool = False) -> bool:
return isinstance(self[loc], zarr.hierarchy.Group)
return isinstance(self[loc], zarr.Group)

def _create_group(self, loc: Sequence[str]) -> None:
parent, name = self._get_parent(loc)
Expand Down
6 changes: 4 additions & 2 deletions tests/helpers/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def storage_extensions(
dot (bool):
Indicates whether the returned extensions are prepended with a dot (`.`)
exclude (list, optional):
Extensions (without dots) that should be explicitely excluded
Extensions (without dots) that should be explicitly excluded
Returns:
sequence: sorted list of extensions
Expand All @@ -53,10 +53,12 @@ def storage_extensions(
if module_available("h5py"):
exts.append("hdf")
if module_available("zarr"):
from modelrunner.storage.backend.zarr import is_zarr2

exts.append("zip")
if incl_folder:
exts.extend(["", "zarr"])
if module_available("sqlite3"):
if is_zarr2 and module_available("sqlite3"):
exts.append("sqldb")

if dot:
Expand Down

0 comments on commit 453811c

Please sign in to comment.