Skip to content

Commit 2092801

Browse files
committed
fixes #2501
1 parent 76904ea commit 2092801

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/zarr/codecs/_v2.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@
1414
import numcodecs.abc
1515

1616
from zarr.core.array_spec import ArraySpec
17-
from zarr.core.buffer import Buffer, NDBuffer
17+
from zarr.core.buffer import Buffer, NDArrayLike, NDBuffer
18+
19+
20+
def ensure_contiguous(arr: NDArrayLike) -> NDArrayLike:
21+
if arr.flags.c_contiguous or arr.flags.f_contiguous:
22+
return arr
23+
else:
24+
return arr.copy()
1825

1926

2027
@dataclass(frozen=True)
@@ -83,6 +90,7 @@ async def _encode_single(
8390
else:
8491
cdata = chunk
8592

93+
cdata = ensure_contiguous(ensure_ndarray_like(cdata))
8694
return chunk_spec.prototype.buffer.from_bytes(cdata)
8795

8896
def compute_encoded_size(self, _input_byte_length: int, _chunk_spec: ArraySpec) -> int:

tests/test_v2.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,19 @@ def test_v2_filters_codecs(filters: Any) -> None:
132132
arr[:] = array_fixture
133133
result = arr[:]
134134
np.testing.assert_array_equal(result, array_fixture)
135+
136+
137+
def test_v2_non_contiguous() -> None:
138+
arr = zarr.Array.create(
139+
MemoryStore({}),
140+
shape=(10, 8),
141+
chunks=(3, 3),
142+
fill_value=np.nan,
143+
dtype="float64",
144+
zarr_format=2,
145+
exists_ok=True,
146+
)
147+
a = np.ones(arr.shape)
148+
arr[slice(6, 9, None), slice(3, 6, None)] = a[
149+
slice(6, 9, None), slice(3, 6, None)
150+
] # The slice on the RHS is important

0 commit comments

Comments
 (0)