From b825666b13d925dc9f878444c7a7102c353635f0 Mon Sep 17 00:00:00 2001 From: Don Yin Date: Thu, 22 Oct 2026 22:56:51 +0100 Subject: [PATCH 1/2] fix: preserve Numcodecs filter shape and decode order Restore the shape declared by the filter and normalize decode input to C order after a transpose. Extend the existing round-trip cases to cover a following TransposeCodec without relaxing exact array equality. The shape-restoration approach follows ghuls's draft in #4553. Signed-off-by: Don Yin --- changes/4553.bugfix.md | 1 + src/zarr/codecs/numcodecs/_codecs.py | 3 ++- tests/test_codecs/test_numcodecs.py | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 changes/4553.bugfix.md diff --git a/changes/4553.bugfix.md b/changes/4553.bugfix.md new file mode 100644 index 0000000000..07a384c15a --- /dev/null +++ b/changes/4553.bugfix.md @@ -0,0 +1 @@ +Preserve the declared chunk shape after Numcodecs filters and decode transposed chunks in logical C order. diff --git a/src/zarr/codecs/numcodecs/_codecs.py b/src/zarr/codecs/numcodecs/_codecs.py index c980743166..bb2f0a3ab2 100644 --- a/src/zarr/codecs/numcodecs/_codecs.py +++ b/src/zarr/codecs/numcodecs/_codecs.py @@ -155,7 +155,7 @@ def __init__(self, **codec_config: JSON) -> None: super().__init__(**codec_config) def _decode_sync(self, chunk_data: NDBuffer, chunk_spec: ArraySpec) -> NDBuffer: - chunk_ndarray = chunk_data.as_ndarray_like() + chunk_ndarray = np.ascontiguousarray(chunk_data.as_ndarray_like()) out = self._codec.decode(chunk_ndarray) return chunk_spec.prototype.nd_buffer.from_ndarray_like(out.reshape(chunk_spec.shape)) @@ -164,6 +164,7 @@ def _encode_sync(self, chunk_data: NDBuffer, chunk_spec: ArraySpec) -> NDBuffer: # would be encoded in transposed element order (gh-3558) chunk_ndarray = np.ascontiguousarray(chunk_data.as_ndarray_like()) out = self._codec.encode(chunk_ndarray) + out = out.reshape(self.resolve_metadata(chunk_spec).shape) return chunk_spec.prototype.nd_buffer.from_ndarray_like(out) async def _encode_single(self, chunk_data: NDBuffer, chunk_spec: ArraySpec) -> NDBuffer: diff --git a/tests/test_codecs/test_numcodecs.py b/tests/test_codecs/test_numcodecs.py index d78b73f34c..93a03cedfc 100644 --- a/tests/test_codecs/test_numcodecs.py +++ b/tests/test_codecs/test_numcodecs.py @@ -10,6 +10,7 @@ from zarr import config, create_array, open_array from zarr.abc.numcodec import _is_numcodec, _is_numcodec_cls +from zarr.codecs import TransposeCodec from zarr.codecs import numcodecs as _numcodecs from zarr.errors import UnknownCodecError from zarr.registry import get_codec_class, get_numcodec @@ -135,9 +136,11 @@ def test_generic_compressor(codec_class: type[_numcodecs._NumcodecsBytesBytesCod "astype", ], ) +@pytest.mark.parametrize("transpose", [False, True]) def test_generic_filter( codec_class: type[_numcodecs._NumcodecsArrayArrayCodec], codec_config: dict[str, JSON], + transpose: bool, ) -> None: data = np.linspace(0, 10, 256, dtype="float32").reshape((16, 16)) @@ -149,6 +152,7 @@ def test_generic_filter( fill_value=0, filters=[ codec_class(**codec_config), + *([TransposeCodec(order=(1, 0))] if transpose else []), ], ) From ffcfe1f01f3cef6107f198630ae1649ee5185cde Mon Sep 17 00:00:00 2001 From: Davis Vann Bennett Date: Mon, 16 Nov 2026 18:26:54 +0100 Subject: [PATCH 2/2] docs(changes): rename the fragment to the PR number and scope it to decode order The shape fix on encode is already described by changes/4560.bugfix.md. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude Opus 5.5 --- changes/4553.bugfix.md | 1 - changes/4558.bugfix.md | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) delete mode 100644 changes/4553.bugfix.md create mode 100644 changes/4558.bugfix.md diff --git a/changes/4553.bugfix.md b/changes/4553.bugfix.md deleted file mode 100644 index 07a384c15a..0000000000 --- a/changes/4553.bugfix.md +++ /dev/null @@ -1 +0,0 @@ -Preserve the declared chunk shape after Numcodecs filters and decode transposed chunks in logical C order. diff --git a/changes/4558.bugfix.md b/changes/4558.bugfix.md new file mode 100644 index 0000000000..1aaea5f423 --- /dev/null +++ b/changes/4558.bugfix.md @@ -0,0 +1,3 @@ +Arrays that apply a Numcodecs `Delta` or `FixedScaleOffset` filter followed by a +`TransposeCodec` now read back the values that were written. The filter now +decodes the transposed chunk in logical (C) order rather than memory order.