Skip to content

Commit

Permalink
fixes #663
Browse files Browse the repository at this point in the history
  • Loading branch information
normanrz committed Dec 2, 2024
1 parent f43449b commit 6bc140f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Fixes
``crc32c`` is not installed. This has been changed to match
the behaviour of other optional dependencies/codecs.
By :user:`John Kirkham <jakirkham>`, :issue:`637`
* Fixes issue with ``Delta`` Zarr 3 codec not working with ``astype``.
By :user:`Norman Rzepka <normanrz>`, :issue:`664`

Improvements
~~~~~~~~~~~~
Expand Down
2 changes: 2 additions & 0 deletions docs/zarr3.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _Zarr 3 codecs:

Zarr 3 codecs
=============
.. automodule:: numcodecs.zarr3
Expand Down
21 changes: 21 additions & 0 deletions numcodecs/tests/test_zarr3.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,24 @@ def test_generic_bytes_codec(store: StorePath, codec_class: type[numcodecs.zarr3

a[:, :] = data.copy()
np.testing.assert_array_equal(data, a[:, :])


def test_delta_astype(store: StorePath):
data = np.linspace(0, 10, 256, dtype="i8").reshape((16, 16))

with pytest.warns(UserWarning, match=EXPECTED_WARNING_STR):
a = Array.create(
store / "generic",
shape=data.shape,
chunk_shape=(16, 16),
dtype=data.dtype,
fill_value=0,
codecs=[
numcodecs.zarr3.Delta(dtype="i8", astype="i2"), # type: ignore[arg-type]
BytesCodec(),
],
)

a[:, :] = data.copy()
a = Array.open(store / "generic")
np.testing.assert_array_equal(data, a[:, :])
14 changes: 13 additions & 1 deletion numcodecs/zarr3.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,19 @@ def evolve_from_array_spec(self, array_spec: ArraySpec) -> Shuffle:


# array-to-array codecs ("filters")
Delta = _add_docstring(_make_array_array_codec("delta", "Delta"), "numcodecs.delta.Delta")
@_add_docstring_wrapper("numcodecs.delta.Delta")
class Delta(_NumcodecsArrayArrayCodec):
codec_name = f"{CODEC_PREFIX}delta"

def __init__(self, **codec_config: dict[str, JSON]) -> None:
super().__init__(**codec_config)

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[arg-type]
return chunk_spec


BitRound = _add_docstring(
_make_array_array_codec("bitround", "BitRound"), "numcodecs.bitround.BitRound"
)
Expand Down

0 comments on commit 6bc140f

Please sign in to comment.