Skip to content

Commit d8a0a55

Browse files
authored
Adds astype config for Delta in Zarr 3 wrapper (#664)
* fixes #663 * changelog
1 parent f43449b commit d8a0a55

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

docs/release.rst

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ Release notes
1313

1414
.. _unreleased:
1515

16+
Unreleased
17+
----------
18+
19+
Fixes
20+
~~~~~
21+
* Fixes issue with ``Delta`` Zarr 3 codec not working with ``astype``.
22+
By :user:`Norman Rzepka <normanrz>`, :issue:`664`
23+
24+
25+
Improvements
26+
~~~~~~~~~~~~
27+
28+
1629
0.14.1
1730
------
1831

docs/zarr3.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _Zarr 3 codecs:
2+
13
Zarr 3 codecs
24
=============
35
.. automodule:: numcodecs.zarr3

numcodecs/tests/test_zarr3.py

+21
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,24 @@ def test_generic_bytes_codec(store: StorePath, codec_class: type[numcodecs.zarr3
244244

245245
a[:, :] = data.copy()
246246
np.testing.assert_array_equal(data, a[:, :])
247+
248+
249+
def test_delta_astype(store: StorePath):
250+
data = np.linspace(0, 10, 256, dtype="i8").reshape((16, 16))
251+
252+
with pytest.warns(UserWarning, match=EXPECTED_WARNING_STR):
253+
a = Array.create(
254+
store / "generic",
255+
shape=data.shape,
256+
chunk_shape=(16, 16),
257+
dtype=data.dtype,
258+
fill_value=0,
259+
codecs=[
260+
numcodecs.zarr3.Delta(dtype="i8", astype="i2"), # type: ignore[arg-type]
261+
BytesCodec(),
262+
],
263+
)
264+
265+
a[:, :] = data.copy()
266+
a = Array.open(store / "generic")
267+
np.testing.assert_array_equal(data, a[:, :])

numcodecs/zarr3.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,19 @@ def evolve_from_array_spec(self, array_spec: ArraySpec) -> Shuffle:
266266

267267

268268
# array-to-array codecs ("filters")
269-
Delta = _add_docstring(_make_array_array_codec("delta", "Delta"), "numcodecs.delta.Delta")
269+
@_add_docstring_wrapper("numcodecs.delta.Delta")
270+
class Delta(_NumcodecsArrayArrayCodec):
271+
codec_name = f"{CODEC_PREFIX}delta"
272+
273+
def __init__(self, **codec_config: dict[str, JSON]) -> None:
274+
super().__init__(**codec_config)
275+
276+
def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec:
277+
if astype := self.codec_config.get("astype"):
278+
return replace(chunk_spec, dtype=np.dtype(astype)) # type: ignore[arg-type]
279+
return chunk_spec
280+
281+
270282
BitRound = _add_docstring(
271283
_make_array_array_codec("bitround", "BitRound"), "numcodecs.bitround.BitRound"
272284
)

0 commit comments

Comments
 (0)