Skip to content

Commit df7aae6

Browse files
committed
more tests
1 parent 5625e94 commit df7aae6

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

src/zarr/codecs/_v2.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
from typing import TYPE_CHECKING
66

77
import numcodecs
8-
import numpy as np
9-
from numcodecs.compat import ensure_ndarray_like
8+
from numcodecs.compat import ensure_bytes, ensure_ndarray_like
109

1110
from zarr.abc.codec import ArrayBytesCodec
1211
from zarr.registry import get_ndbuffer_class
@@ -15,15 +14,7 @@
1514
import numcodecs.abc
1615

1716
from zarr.core.array_spec import ArraySpec
18-
from zarr.core.buffer import Buffer, NDArrayLike, NDBuffer
19-
20-
21-
def ensure_contiguous(arr: NDArrayLike) -> NDArrayLike:
22-
flags = getattr(arr, "flags", None)
23-
if flags is not None and flags.c_contiguous:
24-
return arr
25-
else:
26-
return np.ascontiguousarray(arr)
17+
from zarr.core.buffer import Buffer, NDBuffer
2718

2819

2920
@dataclass(frozen=True)
@@ -77,6 +68,9 @@ async def _encode_single(
7768
) -> Buffer | None:
7869
chunk = chunk_array.as_ndarray_like()
7970

71+
# ensure contiguous and correct order
72+
chunk = chunk.astype(chunk_spec.dtype, order=chunk_spec.order, copy=False)
73+
8074
# apply filters
8175
if self.filters:
8276
for f in self.filters:
@@ -92,7 +86,7 @@ async def _encode_single(
9286
else:
9387
cdata = chunk
9488

95-
cdata = ensure_contiguous(ensure_ndarray_like(cdata))
89+
cdata = ensure_bytes(cdata)
9690
return chunk_spec.prototype.buffer.from_bytes(cdata)
9791

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

tests/test_v2.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
from collections.abc import Iterator
3-
from typing import Any
3+
from typing import Any, Literal
44

55
import numcodecs.vlen
66
import numpy as np
@@ -126,15 +126,17 @@ async def test_create_dtype_str(dtype: Any) -> None:
126126

127127

128128
@pytest.mark.parametrize("filters", [[], [numcodecs.Delta(dtype="<i4")], [numcodecs.Zlib(level=2)]])
129-
def test_v2_filters_codecs(filters: Any) -> None:
129+
@pytest.mark.parametrize("order", ["C", "F"])
130+
def test_v2_filters_codecs(filters: Any, order: Literal["C", "F"]) -> None:
130131
array_fixture = [42]
131-
arr = zarr.create(shape=1, dtype="<i4", zarr_format=2, filters=filters)
132+
arr = zarr.create(shape=1, dtype="<i4", zarr_format=2, filters=filters, order=order)
132133
arr[:] = array_fixture
133134
result = arr[:]
134135
np.testing.assert_array_equal(result, array_fixture)
135136

136137

137-
def test_v2_non_contiguous() -> None:
138+
@pytest.mark.parametrize("order", ["C", "F"])
139+
def test_v2_non_contiguous(order: Literal["C", "F"]) -> None:
138140
arr = zarr.Array.create(
139141
MemoryStore({}),
140142
shape=(10, 8),
@@ -143,16 +145,22 @@ def test_v2_non_contiguous() -> None:
143145
dtype="float64",
144146
zarr_format=2,
145147
exists_ok=True,
148+
order=order,
146149
)
147-
a = np.ones(arr.shape)
150+
a = np.arange(arr.shape[0] * arr.shape[1]).reshape(arr.shape)
148151
arr[slice(6, 9, None), slice(3, 6, None)] = a[
149152
slice(6, 9, None), slice(3, 6, None)
150153
] # The slice on the RHS is important
154+
np.testing.assert_array_equal(
155+
arr[slice(6, 9, None), slice(3, 6, None)], a[slice(6, 9, None), slice(3, 6, None)]
156+
)
151157

152-
a = np.ones((3, 3), order="F")
158+
a = np.arange(9).reshape((3, 3), order="F")
153159
assert a.flags.f_contiguous
154160
arr[slice(6, 9, None), slice(3, 6, None)] = a
161+
np.testing.assert_array_equal(arr[slice(6, 9, None), slice(3, 6, None)], a)
155162

156-
a = np.ones((3, 3), order="C")
163+
a = np.arange(9).reshape((3, 3), order="C")
157164
assert a.flags.c_contiguous
158165
arr[slice(6, 9, None), slice(3, 6, None)] = a
166+
np.testing.assert_array_equal(arr[slice(6, 9, None), slice(3, 6, None)], a)

0 commit comments

Comments
 (0)