From 2092801723febf73292123276ffa25bc9246ce85 Mon Sep 17 00:00:00 2001 From: Norman Rzepka Date: Sun, 24 Nov 2024 19:36:59 +0100 Subject: [PATCH 1/2] fixes #2501 --- src/zarr/codecs/_v2.py | 10 +++++++++- tests/test_v2.py | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/zarr/codecs/_v2.py b/src/zarr/codecs/_v2.py index 30504ad20..c0ba503d5 100644 --- a/src/zarr/codecs/_v2.py +++ b/src/zarr/codecs/_v2.py @@ -14,7 +14,14 @@ import numcodecs.abc from zarr.core.array_spec import ArraySpec - from zarr.core.buffer import Buffer, NDBuffer + from zarr.core.buffer import Buffer, NDArrayLike, NDBuffer + + +def ensure_contiguous(arr: NDArrayLike) -> NDArrayLike: + if arr.flags.c_contiguous or arr.flags.f_contiguous: + return arr + else: + return arr.copy() @dataclass(frozen=True) @@ -83,6 +90,7 @@ async def _encode_single( else: cdata = chunk + cdata = ensure_contiguous(ensure_ndarray_like(cdata)) return chunk_spec.prototype.buffer.from_bytes(cdata) def compute_encoded_size(self, _input_byte_length: int, _chunk_spec: ArraySpec) -> int: diff --git a/tests/test_v2.py b/tests/test_v2.py index e6b50ab2a..af1a14862 100644 --- a/tests/test_v2.py +++ b/tests/test_v2.py @@ -132,3 +132,19 @@ def test_v2_filters_codecs(filters: Any) -> None: arr[:] = array_fixture result = arr[:] np.testing.assert_array_equal(result, array_fixture) + + +def test_v2_non_contiguous() -> None: + arr = zarr.Array.create( + MemoryStore({}), + shape=(10, 8), + chunks=(3, 3), + fill_value=np.nan, + dtype="float64", + zarr_format=2, + exists_ok=True, + ) + a = np.ones(arr.shape) + arr[slice(6, 9, None), slice(3, 6, None)] = a[ + slice(6, 9, None), slice(3, 6, None) + ] # The slice on the RHS is important From ac61fae11fce03aa90185a6bbb940c9d2700d427 Mon Sep 17 00:00:00 2001 From: Norman Rzepka Date: Sun, 24 Nov 2024 19:45:36 +0100 Subject: [PATCH 2/2] typing --- src/zarr/codecs/_v2.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/zarr/codecs/_v2.py b/src/zarr/codecs/_v2.py index c0ba503d5..f1ac90321 100644 --- a/src/zarr/codecs/_v2.py +++ b/src/zarr/codecs/_v2.py @@ -18,7 +18,8 @@ def ensure_contiguous(arr: NDArrayLike) -> NDArrayLike: - if arr.flags.c_contiguous or arr.flags.f_contiguous: + flags = getattr(arr, "flags", None) + if flags is not None and (flags.c_contiguous or flags.f_contiguous): return arr else: return arr.copy()