Skip to content

Commit 1822576

Browse files
committed
update to latest zarr-python interfaces
1 parent 2dc9281 commit 1822576

File tree

2 files changed

+41
-58
lines changed

2 files changed

+41
-58
lines changed

numcodecs/zarr3.py

Lines changed: 40 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from dataclasses import dataclass
3+
from dataclasses import dataclass, replace
44
from functools import cached_property
55
import math
66
from typing_extensions import Self
@@ -9,16 +9,15 @@
99
import numpy as np
1010
import numcodecs
1111

12-
from zarr.abc.codec import ArrayArrayCodec, BytesBytesCodec, Codec
12+
from zarr.abc.codec import ArrayArrayCodec, BytesBytesCodec
13+
from zarr.buffer import NDBuffer, Buffer, as_numpy_array_wrapper
1314
from zarr.common import (
1415
JSON,
1516
ArraySpec,
16-
BytesLike,
1717
parse_named_configuration,
1818
product,
1919
to_thread,
2020
)
21-
from zarr.config import RuntimeConfiguration
2221
from zarr.metadata import ArrayMetadata
2322

2423

@@ -33,12 +32,12 @@ def parse_codec_configuration(
3332
raise ValueError(
3433
f"Expected name to start with '{expected_name_prefix}'. Got {parsed_name} instead."
3534
)
36-
id = parsed_name[len(expected_name_prefix):]
35+
id = parsed_name[len(expected_name_prefix) :]
3736
return {"id": id, **parsed_configuration}
3837

3938

4039
@dataclass(frozen=True)
41-
class NumcodecsCodec(Codec):
40+
class NumcodecsCodec:
4241
codec_config: dict[str, JSON]
4342

4443
def __init__(
@@ -65,6 +64,7 @@ def __init__(
6564

6665
@cached_property
6766
def _codec(self) -> numcodecs.abc.Codec:
67+
print(self.codec_config)
6868
return numcodecs.get_codec(self.codec_config)
6969

7070
@classmethod
@@ -91,49 +91,40 @@ class NumcodecsBytesBytesCodec(NumcodecsCodec, BytesBytesCodec):
9191
def __init__(self, *, codec_id: str, codec_config: dict[str, JSON]) -> None:
9292
super().__init__(codec_id=codec_id, codec_config=codec_config)
9393

94-
async def decode(
95-
self,
96-
chunk_bytes: BytesLike,
97-
_chunk_spec: ArraySpec,
98-
_runtime_configuration: RuntimeConfiguration,
99-
) -> BytesLike:
100-
return await to_thread(self._codec.decode, chunk_bytes)
94+
async def _decode_single(
95+
self, chunk_bytes: Buffer, _chunk_spec: ArraySpec
96+
) -> Buffer:
97+
return await to_thread(as_numpy_array_wrapper, self._codec.decode, chunk_bytes)
10198

102-
def _encode(self, chunk_bytes: BytesLike) -> BytesLike:
103-
encoded = self._codec.encode(chunk_bytes)
99+
def _encode(self, chunk_bytes: Buffer) -> Buffer:
100+
encoded = self._codec.encode(chunk_bytes.as_array_like())
104101
if isinstance(encoded, np.ndarray): # Required for checksum codecs
105102
return encoded.tobytes()
106-
return encoded
107-
108-
async def encode(
109-
self,
110-
chunk_bytes: BytesLike,
111-
_chunk_spec: ArraySpec,
112-
_runtime_configuration: RuntimeConfiguration,
113-
) -> BytesLike:
103+
return Buffer.from_bytes(encoded)
104+
105+
async def _encode_single(
106+
self, chunk_bytes: Buffer, _chunk_spec: ArraySpec
107+
) -> Buffer:
114108
return await to_thread(self._encode, chunk_bytes)
115109

116110

117111
class NumcodecsArrayArrayCodec(NumcodecsCodec, ArrayArrayCodec):
118112
def __init__(self, *, codec_id: str, codec_config: dict[str, JSON]) -> None:
119113
super().__init__(codec_id=codec_id, codec_config=codec_config)
120114

121-
async def decode(
122-
self,
123-
chunk_array: np.ndarray,
124-
chunk_spec: ArraySpec,
125-
_runtime_configuration: RuntimeConfiguration,
126-
) -> np.ndarray:
127-
out = await to_thread(self._codec.decode, chunk_array)
128-
return out.reshape(chunk_spec.shape)
115+
async def _decode_single(
116+
self, chunk_array: NDBuffer, chunk_spec: ArraySpec
117+
) -> NDBuffer:
118+
chunk_ndarray = chunk_array.as_ndarray_like()
119+
out = await to_thread(self._codec.decode, chunk_ndarray)
120+
return NDBuffer.from_ndarray_like(out.reshape(chunk_spec.shape))
129121

130-
async def encode(
131-
self,
132-
chunk_array: np.ndarray,
133-
_chunk_spec: ArraySpec,
134-
_runtime_configuration: RuntimeConfiguration,
135-
) -> np.ndarray:
136-
return await to_thread(self._codec.encode, chunk_array)
122+
async def _encode_single(
123+
self, chunk_array: NDBuffer, _chunk_spec: ArraySpec
124+
) -> NDBuffer:
125+
chunk_ndarray = chunk_array.as_ndarray_like()
126+
out = await to_thread(self._codec.encode, chunk_ndarray)
127+
return NDBuffer.from_ndarray_like(out)
137128

138129

139130
def make_bytes_bytes_codec(
@@ -185,7 +176,7 @@ class ShuffleCodec(NumcodecsBytesBytesCodec):
185176
def __init__(self, codec_config: dict[str, JSON] = {}) -> None:
186177
super().__init__(codec_id="shuffle", codec_config=codec_config)
187178

188-
def evolve(self, array_spec: ArraySpec) -> Self:
179+
def evolve_from_array_spec(self, array_spec: ArraySpec) -> Self:
189180
if array_spec.dtype.itemsize != self.codec_config.get("elementsize"):
190181
return self.__class__(
191182
{**self.codec_config, "elementsize": array_spec.dtype.itemsize}
@@ -199,14 +190,10 @@ def __init__(self, codec_config: dict[str, JSON] = {}) -> None:
199190

200191
def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec:
201192
if astype := self.codec_config.get("astype"):
202-
return ArraySpec(
203-
chunk_spec.shape,
204-
np.dtype(astype),
205-
chunk_spec.fill_value,
206-
)
193+
return replace(chunk_spec, dtype=np.dtype(astype))
207194
return chunk_spec
208195

209-
def evolve(self, array_spec: ArraySpec) -> Self:
196+
def evolve_from_array_spec(self, array_spec: ArraySpec) -> Self:
210197
if str(array_spec.dtype) != self.codec_config.get("dtype"):
211198
return self.__class__({**self.codec_config, "dtype": str(array_spec.dtype)})
212199
return self
@@ -216,7 +203,7 @@ class QuantizeCodec(NumcodecsArrayArrayCodec):
216203
def __init__(self, codec_config: dict[str, JSON] = {}) -> None:
217204
super().__init__(codec_id="quantize", codec_config=codec_config)
218205

219-
def evolve(self, array_spec: ArraySpec) -> Self:
206+
def evolve_from_array_spec(self, array_spec: ArraySpec) -> Self:
220207
if str(array_spec.dtype) != self.codec_config.get("dtype"):
221208
return self.__class__({**self.codec_config, "dtype": str(array_spec.dtype)})
222209
return self
@@ -227,13 +214,9 @@ def __init__(self, codec_config: dict[str, JSON] = {}) -> None:
227214
super().__init__(codec_id="astype", codec_config=codec_config)
228215

229216
def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec:
230-
return ArraySpec(
231-
chunk_spec.shape,
232-
np.dtype(self.codec_config["encode_dtype"]),
233-
chunk_spec.fill_value,
234-
)
217+
return replace(chunk_spec, dtype=np.dtype(self.codec_config["encode_dtype"]))
235218

236-
def evolve(self, array_spec: ArraySpec) -> Self:
219+
def evolve_from_array_spec(self, array_spec: ArraySpec) -> Self:
237220
decode_dtype = self.codec_config.get("decode_dtype")
238221
if str(array_spec.dtype) != decode_dtype:
239222
return self.__class__(
@@ -247,10 +230,10 @@ def __init__(self, codec_config: dict[str, JSON] = {}) -> None:
247230
super().__init__(codec_id="packbits", codec_config=codec_config)
248231

249232
def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec:
250-
return ArraySpec(
251-
(1 + math.ceil(product(chunk_spec.shape) / 8),),
252-
np.dtype("uint8"),
253-
chunk_spec.fill_value,
233+
return replace(
234+
chunk_spec,
235+
shape=(1 + math.ceil(product(chunk_spec.shape) / 8),),
236+
dtype=np.dtype("uint8"),
254237
)
255238

256239
def validate(self, array_metadata: ArrayMetadata) -> None:
@@ -282,4 +265,4 @@ def validate(self, array_metadata: ArrayMetadata) -> None:
282265
Crc32Codec = make_checksum_codec("crc32", "Crc32Codec")
283266
Adler32Codec = make_checksum_codec("adler32", "Adler32Codec")
284267
Fletcher32Codec = make_checksum_codec("fletcher32", "Fletcher32Codec")
285-
JenkinsLookup3 = make_checksum_codec("jenkinks_lookup3", "JenkinsLookup3")
268+
JenkinsLookup3 = make_checksum_codec("jenkins_lookup3", "JenkinsLookup3")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pcodec = [
8989
"https://zarr.dev/numcodecs/crc32" = "numcodecs.zarr3:Crc32Codec"
9090
"https://zarr.dev/numcodecs/adler32" = "numcodecs.zarr3:Adler32Codec"
9191
"https://zarr.dev/numcodecs/fletcher32" = "numcodecs.zarr3:Fletcher32Codec"
92-
"https://zarr.dev/numcodecs/jenkinks_lookup3" = "numcodecs.zarr3:JenkinsLookup3"
92+
"https://zarr.dev/numcodecs/jenkins_lookup3" = "numcodecs.zarr3:JenkinsLookup3"
9393

9494
[tool.setuptools]
9595
license-files = ["LICENSE.txt"]

0 commit comments

Comments
 (0)