Skip to content

Commit fafd0bf

Browse files
Update V2 codec pipeline to use concrete classes (#2244)
The previous implementation used the codec config, rather than the codec itself.
1 parent 4cbb17e commit fafd0bf

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/zarr/codecs/_v2.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88

99
from zarr.abc.codec import ArrayArrayCodec, ArrayBytesCodec
1010
from zarr.core.buffer import Buffer, NDBuffer, default_buffer_prototype
11-
from zarr.core.common import JSON, to_thread
11+
from zarr.core.common import to_thread
1212
from zarr.registry import get_ndbuffer_class
1313

1414
if TYPE_CHECKING:
15+
import numcodecs.abc
16+
1517
from zarr.core.array_spec import ArraySpec
1618

1719

1820
@dataclass(frozen=True)
1921
class V2Compressor(ArrayBytesCodec):
20-
compressor: dict[str, JSON] | None
22+
compressor: numcodecs.abc.Codec | None
2123

2224
is_fixed_size = False
2325

@@ -27,9 +29,8 @@ async def _decode_single(
2729
chunk_spec: ArraySpec,
2830
) -> NDBuffer:
2931
if self.compressor is not None:
30-
compressor = numcodecs.get_codec(self.compressor)
3132
chunk_numpy_array = ensure_ndarray(
32-
await to_thread(compressor.decode, chunk_bytes.as_array_like())
33+
await to_thread(self.compressor.decode, chunk_bytes.as_array_like())
3334
)
3435
else:
3536
chunk_numpy_array = ensure_ndarray(chunk_bytes.as_array_like())
@@ -47,14 +48,13 @@ async def _encode_single(
4748
) -> Buffer | None:
4849
chunk_numpy_array = chunk_array.as_numpy_array()
4950
if self.compressor is not None:
50-
compressor = numcodecs.get_codec(self.compressor)
5151
if (
5252
not chunk_numpy_array.flags.c_contiguous
5353
and not chunk_numpy_array.flags.f_contiguous
5454
):
5555
chunk_numpy_array = chunk_numpy_array.copy(order="A")
5656
encoded_chunk_bytes = ensure_bytes(
57-
await to_thread(compressor.encode, chunk_numpy_array)
57+
await to_thread(self.compressor.encode, chunk_numpy_array)
5858
)
5959
else:
6060
encoded_chunk_bytes = ensure_bytes(chunk_numpy_array)

src/zarr/core/metadata/v2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ def _json_convert(
100100
return o.str
101101
else:
102102
return o.descr
103+
if isinstance(o, numcodecs.abc.Codec):
104+
return o.get_config()
103105
if np.isscalar(o):
104106
out: Any
105107
if hasattr(o, "dtype") and o.dtype.kind == "M" and hasattr(o, "view"):

tests/v3/test_v2.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import numpy as np
44
import pytest
5+
from numcodecs import Delta
6+
from numcodecs.blosc import Blosc
57

8+
import zarr
69
from zarr import Array
710
from zarr.store import MemoryStore, StorePath
811

@@ -26,3 +29,20 @@ def test_simple(store: StorePath) -> None:
2629

2730
a[:, :] = data
2831
assert np.array_equal(data, a[:, :])
32+
33+
34+
def test_codec_pipeline() -> None:
35+
# https://github.com/zarr-developers/zarr-python/issues/2243
36+
store = MemoryStore(mode="w")
37+
array = zarr.create(
38+
store=store,
39+
shape=(1,),
40+
dtype="i4",
41+
zarr_format=2,
42+
filters=[Delta(dtype="i4").get_config()],
43+
compressor=Blosc().get_config(),
44+
)
45+
array[:] = 1
46+
result = array[:]
47+
expected = np.ones(1)
48+
np.testing.assert_array_equal(result, expected)

0 commit comments

Comments
 (0)