-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a9b48d
commit 31f074c
Showing
12 changed files
with
170 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
"""Collection of helper functions to work with Zarr groups.""" | ||
|
||
from zarr import Group | ||
from zarr.store.common import StoreLike | ||
|
||
from ngio.io._zarr_group_utils import StoreOrGroup, open_group_wrapper | ||
from ngio.io._zarr import AccessModeLiteral, StoreLike, StoreOrGroup | ||
from ngio.io._zarr_group_utils import ( | ||
open_group_wrapper, | ||
) | ||
|
||
# Zarr V3 imports | ||
# from zarr.store.common import StoreLike | ||
|
||
__all__ = [ | ||
"Group", | ||
"StoreLike", | ||
"AccessModeLiteral", | ||
"StoreOrGroup", | ||
"open_group_wrapper", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from importlib.metadata import version | ||
from pathlib import Path | ||
from typing import Literal | ||
|
||
import zarr | ||
from packaging.version import Version | ||
|
||
zarr_verison = version("zarr") | ||
ZARR_PYTHON_V = 2 if Version(zarr_verison) < Version("3.0.0a") else 3 | ||
|
||
# Zarr v3 Imports | ||
# import zarr.store | ||
# from zarr.core.common import AccessModeLiteral, ZarrFormat | ||
# from zarr.store.common import StoreLike | ||
|
||
AccessModeLiteral = Literal["r", "r+", "w", "w-", "a"] | ||
ZarrFormat = Literal[2, 3] | ||
StoreLike = str | Path # This type alias more narrrow than necessary | ||
StoreOrGroup = StoreLike | zarr.Group | ||
|
||
|
||
class ZarrV3Error(Exception): | ||
pass | ||
|
||
|
||
def _pass_through_group( | ||
group: zarr.Group, mode: AccessModeLiteral, zarr_format: ZarrFormat = 2 | ||
) -> zarr.Group: | ||
if ZARR_PYTHON_V == 2: | ||
if zarr_format == 3: | ||
raise ZarrV3Error("Zarr v3 is not supported in when using zarr-python v2.") | ||
else: | ||
return group | ||
|
||
else: | ||
if group.metadata.zarr_format != zarr_format: | ||
raise ValueError( | ||
f"Zarr format mismatch. Expected {zarr_format}, " | ||
"got {store.metadata.zarr_format}." | ||
) | ||
else: | ||
return group | ||
|
||
raise ValueError("This should never be reached.") | ||
|
||
|
||
def _open_group_v2_v3( | ||
store: StoreOrGroup, mode: AccessModeLiteral, zarr_format: ZarrFormat = 2 | ||
) -> zarr.Group: | ||
"""Wrapper around zarr.open_group with some additional checks. | ||
Args: | ||
store (StoreOrGroup): The store (can also be a Path/str) or group to open. | ||
mode (ReadOrEdirLiteral): The mode to open the group in. | ||
zarr_format (ZarrFormat): The Zarr format to use. | ||
Returns: | ||
zarr.Group: The opened Zarr group. | ||
""" | ||
if ZARR_PYTHON_V == 3: | ||
return zarr.open_group(store=store, mode=mode, zarr_format=zarr_format) | ||
else: | ||
return zarr.open_group(store=store, mode=mode, zarr_version=zarr_format) | ||
|
||
|
||
def _is_group_readonly(group: zarr.Group) -> bool: | ||
if ZARR_PYTHON_V == 3: | ||
return group.store_path.store.mode.readonly | ||
|
||
else: | ||
return not group.store.is_writeable() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.