-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Zarr V3 version based on numcodecs zarr v3
- Loading branch information
1 parent
5691fe0
commit 8af96ce
Showing
2 changed files
with
83 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from numcodecs.zarr3 import _make_bytes_bytes_codec | ||
|
||
Blosc2 = _make_bytes_bytes_codec("blosc2", "Blosc2") |
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,80 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
if TYPE_CHECKING: # pragma: no cover | ||
import zarr | ||
else: | ||
zarr = pytest.importorskip("zarr") | ||
|
||
import zarr.storage | ||
|
||
import numcodecs.zarr3 | ||
|
||
from ocf_blosc2.ocf_blosc2_v3 import Blosc2 | ||
|
||
pytestmark = [ | ||
pytest.mark.skipif(zarr.__version__ < "3.0.0", reason="zarr 3.0.0 or later is required"), | ||
pytest.mark.filterwarnings("ignore:Codec 'numcodecs.*' not configured in config.*:UserWarning"), | ||
pytest.mark.filterwarnings( | ||
"ignore:Numcodecs codecs are not in the Zarr version 3 specification and may not be supported by other zarr implementations." | ||
), | ||
] | ||
|
||
get_codec_class = zarr.registry.get_codec_class | ||
Array = zarr.Array | ||
BytesCodec = zarr.codecs.BytesCodec | ||
Store = zarr.abc.store.Store | ||
MemoryStore = zarr.storage.MemoryStore | ||
StorePath = zarr.storage.StorePath | ||
|
||
|
||
EXPECTED_WARNING_STR = "Numcodecs codecs are not in the Zarr version 3.*" | ||
|
||
|
||
@pytest.fixture | ||
def store() -> StorePath: | ||
return StorePath(MemoryStore(read_only=False)) | ||
|
||
|
||
ALL_CODECS = [getattr(numcodecs.zarr3, cls_name) for cls_name in numcodecs.zarr3.__all__] | ||
|
||
|
||
@pytest.mark.parametrize("codec_class", ALL_CODECS) | ||
def test_entry_points(codec_class: type[numcodecs.zarr3._NumcodecsCodec]): | ||
codec_name = codec_class.codec_name | ||
assert get_codec_class(codec_name) == codec_class | ||
|
||
|
||
@pytest.mark.parametrize("codec_class", ALL_CODECS) | ||
def test_docstring(codec_class: type[numcodecs.zarr3._NumcodecsCodec]): | ||
assert "See :class:`numcodecs." in codec_class.__doc__ # type: ignore[operator] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"codec_class", | ||
[ | ||
Blosc2 | ||
], | ||
) | ||
def test_generic_compressor( | ||
store: StorePath, codec_class: type[numcodecs.zarr3._NumcodecsBytesBytesCodec] | ||
): | ||
data = np.arange(0, 256, dtype="uint16").reshape((16, 16)) | ||
|
||
with pytest.warns(UserWarning, match=EXPECTED_WARNING_STR): | ||
a = zarr.create_array( | ||
store / "generic", | ||
shape=data.shape, | ||
chunks=(4, 4), | ||
shards=(16, 16), | ||
dtype=data.dtype, | ||
fill_value=0, | ||
compressors=[codec_class()], | ||
) | ||
|
||
a[:, :] = data.copy() | ||
np.testing.assert_array_equal(data, a[:, :]) |