Skip to content

Commit

Permalink
fixes #2501
Browse files Browse the repository at this point in the history
  • Loading branch information
normanrz committed Nov 24, 2024
1 parent 76904ea commit 2092801
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/zarr/codecs/_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2092801

Please sign in to comment.