-
Notifications
You must be signed in to change notification settings - Fork 101
Handle zarr 3.1.0 #766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
d-v-b
wants to merge
10
commits into
zarr-developers:main
Choose a base branch
from
d-v-b:handle-zarr-3.1.0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+39
−11
Open
Handle zarr 3.1.0 #766
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
998579a
Update pyproject.toml
d-v-b f843142
dtype adaptor for zarr 3.1
d-v-b 20d089f
revert change to pyproject.toml
d-v-b 5cad56f
Merge branch 'main' into handle-zarr-3.1.0
d-v-b 171e5a5
versionify version
d-v-b ca19044
Merge branch 'handle-zarr-3.1.0' of https://github.com/d-v-b/numcodec…
d-v-b 6c44c4e
versionify another version
d-v-b 41e72eb
lint
d-v-b 0e57a02
lint
d-v-b 2557245
dodge test coverage
d-v-b File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||||
---|---|---|---|---|---|---|
|
@@ -29,17 +29,19 @@ | |||||
import math | ||||||
from dataclasses import dataclass, replace | ||||||
from functools import cached_property | ||||||
from importlib.metadata import version | ||||||
from typing import Any, Self | ||||||
from warnings import warn | ||||||
|
||||||
import numpy as np | ||||||
from packaging.version import Version | ||||||
|
||||||
import numcodecs | ||||||
|
||||||
try: | ||||||
import zarr | ||||||
import zarr # noqa: F401 | ||||||
|
||||||
if zarr.__version__ < "3.0.0": # pragma: no cover | ||||||
if Version(version('zarr')) < Version("3.0.0"): # pragma: no cover | ||||||
raise ImportError("zarr 3.0.0 or later is required to use the numcodecs zarr integration.") | ||||||
except ImportError as e: # pragma: no cover | ||||||
raise ImportError( | ||||||
|
@@ -56,6 +58,23 @@ | |||||
CODEC_PREFIX = "numcodecs." | ||||||
|
||||||
|
||||||
def from_zarr_dtype(dtype: Any) -> np.dtype: | ||||||
""" | ||||||
Get a numpy data type from an array spec, depending on the zarr version. | ||||||
""" | ||||||
if Version(version('zarr')) >= Version("3.1.0"): | ||||||
return dtype.to_native_dtype() | ||||||
return dtype # pragma: no cover | ||||||
|
||||||
|
||||||
def to_zarr_dtype(dtype: np.dtype) -> Any: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if Version(version('zarr')) >= Version("3.1.0"): | ||||||
from zarr.dtype import parse_data_type | ||||||
|
||||||
return parse_data_type(dtype, zarr_format=3) | ||||||
return dtype # pragma: no cover | ||||||
|
||||||
|
||||||
def _expect_name_prefix(codec_name: str) -> str: | ||||||
if not codec_name.startswith(CODEC_PREFIX): | ||||||
raise ValueError( | ||||||
|
@@ -224,15 +243,17 @@ class LZMA(_NumcodecsBytesBytesCodec, codec_name="lzma"): | |||||
class Shuffle(_NumcodecsBytesBytesCodec, codec_name="shuffle"): | ||||||
def evolve_from_array_spec(self, array_spec: ArraySpec) -> Shuffle: | ||||||
if self.codec_config.get("elementsize") is None: | ||||||
return Shuffle(**{**self.codec_config, "elementsize": array_spec.dtype.itemsize}) | ||||||
dtype = from_zarr_dtype(array_spec.dtype) | ||||||
return Shuffle(**{**self.codec_config, "elementsize": dtype.itemsize}) | ||||||
return self # pragma: no cover | ||||||
|
||||||
|
||||||
# array-to-array codecs ("filters") | ||||||
class Delta(_NumcodecsArrayArrayCodec, codec_name="delta"): | ||||||
def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec: | ||||||
if astype := self.codec_config.get("astype"): | ||||||
return replace(chunk_spec, dtype=np.dtype(astype)) # type: ignore[call-overload] | ||||||
dtype = to_zarr_dtype(np.dtype(astype)) # type: ignore[call-overload] | ||||||
return replace(chunk_spec, dtype=dtype) | ||||||
return chunk_spec | ||||||
|
||||||
|
||||||
|
@@ -243,12 +264,14 @@ class BitRound(_NumcodecsArrayArrayCodec, codec_name="bitround"): | |||||
class FixedScaleOffset(_NumcodecsArrayArrayCodec, codec_name="fixedscaleoffset"): | ||||||
def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec: | ||||||
if astype := self.codec_config.get("astype"): | ||||||
return replace(chunk_spec, dtype=np.dtype(astype)) # type: ignore[call-overload] | ||||||
dtype = to_zarr_dtype(np.dtype(astype)) # type: ignore[call-overload] | ||||||
return replace(chunk_spec, dtype=dtype) | ||||||
return chunk_spec | ||||||
|
||||||
def evolve_from_array_spec(self, array_spec: ArraySpec) -> FixedScaleOffset: | ||||||
if self.codec_config.get("dtype") is None: | ||||||
return FixedScaleOffset(**{**self.codec_config, "dtype": str(array_spec.dtype)}) | ||||||
dtype = from_zarr_dtype(array_spec.dtype) | ||||||
return FixedScaleOffset(**{**self.codec_config, "dtype": str(dtype)}) | ||||||
return self | ||||||
|
||||||
|
||||||
|
@@ -258,7 +281,8 @@ def __init__(self, **codec_config: JSON) -> None: | |||||
|
||||||
def evolve_from_array_spec(self, array_spec: ArraySpec) -> Quantize: | ||||||
if self.codec_config.get("dtype") is None: | ||||||
return Quantize(**{**self.codec_config, "dtype": str(array_spec.dtype)}) | ||||||
dtype = from_zarr_dtype(array_spec.dtype) | ||||||
return Quantize(**{**self.codec_config, "dtype": str(dtype)}) | ||||||
return self | ||||||
|
||||||
|
||||||
|
@@ -267,21 +291,25 @@ def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec: | |||||
return replace( | ||||||
chunk_spec, | ||||||
shape=(1 + math.ceil(product(chunk_spec.shape) / 8),), | ||||||
dtype=np.dtype("uint8"), | ||||||
dtype=to_zarr_dtype(np.dtype("uint8")), | ||||||
) | ||||||
|
||||||
def validate(self, *, dtype: np.dtype[Any], **_kwargs) -> None: | ||||||
if dtype != np.dtype("bool"): | ||||||
_dtype = from_zarr_dtype(dtype) | ||||||
if _dtype != np.dtype("bool"): | ||||||
raise ValueError(f"Packbits filter requires bool dtype. Got {dtype}.") | ||||||
|
||||||
|
||||||
class AsType(_NumcodecsArrayArrayCodec, codec_name="astype"): | ||||||
def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec: | ||||||
return replace(chunk_spec, dtype=np.dtype(self.codec_config["encode_dtype"])) # type: ignore[arg-type] | ||||||
dtype = to_zarr_dtype(np.dtype(self.codec_config["encode_dtype"])) # type: ignore[arg-type] | ||||||
return replace(chunk_spec, dtype=dtype) | ||||||
|
||||||
def evolve_from_array_spec(self, array_spec: ArraySpec) -> AsType: | ||||||
if self.codec_config.get("decode_dtype") is None: | ||||||
return AsType(**{**self.codec_config, "decode_dtype": str(array_spec.dtype)}) | ||||||
# TODO: remove these coverage exemptions the correct way, i.e. with tests | ||||||
dtype = from_zarr_dtype(array_spec.dtype) # pragma: no cover | ||||||
return AsType(**{**self.codec_config, "decode_dtype": str(dtype)}) # pragma: no cover | ||||||
return self | ||||||
|
||||||
|
||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.