Skip to content

Commit 251cd14

Browse files
committed
WIP fix: array creation in tests
1 parent e9b3350 commit 251cd14

6 files changed

Lines changed: 116 additions & 200 deletions

File tree

tests/test_codecs.py

Lines changed: 37 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55

66
import numpy as np
77
import pytest
8-
from zarr import Array, AsyncArray, config
8+
from zarr import Array, AsyncArray, config, create_array
9+
from zarr.api.asynchronous import create_array as create_async_array
910
from zarr.codecs import (
10-
BytesCodec,
11-
ShardingCodec,
1211
TransposeCodec,
1312
)
1413
from zarr.core.buffer import default_buffer_prototype
14+
from zarr.core.chunk_key_encodings import ChunkKeyEncodingParams
1515
from zarr.storage import StorePath
1616

1717
if TYPE_CHECKING:
18-
from zarr.abc.codec import Codec
1918
from zarr.abc.store import Store
2019
from zarr.core.buffer.core import NDArrayLike
2120
from zarr.core.common import MemoryOrder
@@ -73,32 +72,17 @@ async def test_order(
7372
data = np.arange(0, 256, dtype="uint16").reshape((32, 8), order=input_order)
7473
path = "order"
7574
spath = StorePath(store, path=path)
76-
codecs_: list[Codec] = (
77-
[
78-
ShardingCodec(
79-
chunk_shape=(16, 8),
80-
codecs=[
81-
TransposeCodec(order=order_from_dim(store_order, data.ndim)),
82-
BytesCodec(),
83-
],
84-
)
85-
]
86-
if with_sharding
87-
else [
88-
TransposeCodec(order=order_from_dim(store_order, data.ndim)),
89-
BytesCodec(),
90-
]
91-
)
9275

9376
with config.set({"array.order": runtime_write_order}):
94-
a = await AsyncArray.create(
77+
a = await create_async_array(
9578
spath,
9679
shape=data.shape,
97-
chunk_shape=(32, 8),
80+
chunks=(32, 8),
81+
shards=(16, 8) if with_sharding else None,
9882
dtype=data.dtype,
9983
fill_value=0,
100-
chunk_key_encoding=("v2", "."),
101-
codecs=codecs_,
84+
chunk_key_encoding=ChunkKeyEncodingParams(name="v2", separator="."),
85+
filters=[TransposeCodec(order=order_from_dim(store_order, data.ndim))],
10286
)
10387

10488
await _AsyncArrayProxy(a)[:, :].set(data)
@@ -135,18 +119,15 @@ def test_order_implicit(
135119
data = np.arange(0, 256, dtype="uint16").reshape((16, 16), order=input_order)
136120
path = "order_implicit"
137121
spath = StorePath(store, path)
138-
codecs_: list[Codec] | None = (
139-
[ShardingCodec(chunk_shape=(8, 8))] if with_sharding else None
140-
)
141122

142123
with config.set({"array.order": runtime_write_order}):
143-
a = Array.create(
124+
a = create_array(
144125
spath,
145126
shape=data.shape,
146-
chunk_shape=(16, 16),
127+
chunks=(16, 16),
128+
shards=(8, 8) if with_sharding else None,
147129
dtype=data.dtype,
148130
fill_value=0,
149-
codecs=codecs_,
150131
)
151132

152133
a[:, :] = data
@@ -167,10 +148,10 @@ def test_order_implicit(
167148
def test_write_partial_chunks(store: Store) -> None:
168149
data = np.arange(0, 256, dtype="uint16").reshape((16, 16))
169150
spath = StorePath(store)
170-
a = Array.create(
151+
a = create_array(
171152
spath,
172153
shape=data.shape,
173-
chunk_shape=(20, 20),
154+
chunks=(20, 20),
174155
dtype=data.dtype,
175156
fill_value=1,
176157
)
@@ -182,10 +163,10 @@ async def test_delete_empty_chunks(store: Store) -> None:
182163
data = np.ones((16, 16))
183164
path = "delete_empty_chunks"
184165
spath = StorePath(store, path)
185-
a = await AsyncArray.create(
166+
a = await create_async_array(
186167
spath,
187168
shape=data.shape,
188-
chunk_shape=(32, 32),
169+
chunks=(32, 32),
189170
dtype=data.dtype,
190171
fill_value=1,
191172
)
@@ -199,97 +180,89 @@ def test_invalid_metadata(store: Store) -> None:
199180
# LD: Disabled for `zarrs`. Including endianness for a single-byte data type is not invalid.
200181
# spath2 = StorePath(store, "invalid_endian")
201182
# with pytest.raises(TypeError):
202-
# Array.create(
183+
# create_array(
203184
# spath2,
204185
# shape=(16, 16),
205-
# chunk_shape=(16, 16),
186+
# chunks=(16, 16),
206187
# dtype=np.dtype("uint8"),
207188
# fill_value=0,
208-
# codecs=[
209-
# BytesCodec(endian="big"),
189+
# filters=[
210190
# TransposeCodec(order=order_from_dim("F", 2)),
211191
# ],
212192
# )
213193
spath3 = StorePath(store, "invalid_order")
214194
with pytest.raises(TypeError):
215-
Array.create(
195+
create_array(
216196
spath3,
217197
shape=(16, 16),
218-
chunk_shape=(16, 16),
198+
chunks=(16, 16),
219199
dtype=np.dtype("uint8"),
220200
fill_value=0,
221-
codecs=[
222-
BytesCodec(),
201+
filters=[
223202
TransposeCodec(order="F"), # type: ignore[arg-type]
224203
],
225204
)
226205
spath4 = StorePath(store, "invalid_missing_bytes_codec")
227206
with pytest.raises(ValueError, match=r".*[Cc]odec.*required"):
228-
Array.create(
207+
create_array(
229208
spath4,
230209
shape=(16, 16),
231-
chunk_shape=(16, 16),
210+
chunks=(16, 16),
232211
dtype=np.dtype("uint8"),
233212
fill_value=0,
234-
codecs=[
213+
filters=[
235214
TransposeCodec(order=order_from_dim("F", 2)),
236215
],
237216
)
238217
spath5 = StorePath(store, "invalid_inner_chunk_shape")
239218
with pytest.raises(
240219
ValueError, match=r".*shard.*chunk_shape.*array.*shape.*need.*same.*dimensions"
241220
):
242-
Array.create(
221+
create_array(
243222
spath5,
244223
shape=(16, 16),
245-
chunk_shape=(16, 16),
224+
chunks=(16, 16),
225+
shards=(8,),
246226
dtype=np.dtype("uint8"),
247227
fill_value=0,
248-
codecs=[
249-
ShardingCodec(chunk_shape=(8,)),
250-
],
251228
)
252229
spath6 = StorePath(store, "invalid_inner_chunk_shape")
253230
with pytest.raises(
254231
ValueError, match=r".*array.*chunk_shape.*divisible.*shard.*chunk_shape"
255232
):
256-
Array.create(
233+
create_array(
257234
spath6,
258235
shape=(16, 16),
259-
chunk_shape=(16, 16),
236+
chunks=(16, 16),
237+
shards=(8, 7),
260238
dtype=np.dtype("uint8"),
261239
fill_value=0,
262-
codecs=[
263-
ShardingCodec(chunk_shape=(8, 7)),
264-
],
265240
)
266241
# LD: Disabled for `zarrs`. Such checks do not exist.
267242
# Also this is not invalid metadata, should be a separate test.
268243
# spath7 = StorePath(store, "warning_inefficient_codecs")
269244
# with pytest.warns(UserWarning):
270-
# Array.create(
245+
# create_array(
271246
# spath7,
272247
# shape=(16, 16),
273-
# chunk_shape=(16, 16),
248+
# chunks=(16, 16),
249+
# shards=(8, 8),
274250
# dtype=np.dtype("uint8"),
275251
# fill_value=0,
276-
# codecs=[
277-
# ShardingCodec(chunk_shape=(8, 8)),
278-
# GzipCodec(),
279-
# ],
252+
# compressors=[GzipCodec()],
280253
# )
281254

282255

283256
async def test_resize(store: Store) -> None:
284257
data = np.zeros((16, 18), dtype="uint16")
285258
path = "resize"
286259
spath = StorePath(store, path)
287-
a = await AsyncArray.create(
260+
a = await create_async_array(
288261
spath,
289262
shape=data.shape,
290-
chunk_shape=(10, 10),
263+
chunks=(10, 10),
291264
dtype=data.dtype,
292-
chunk_key_encoding=("v2", "."),
265+
chunk_key_encoding=ChunkKeyEncodingParams(name="v2", separator="."),
293266
fill_value=1,
294267
)
295268

tests/test_endian.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import numpy as np
44
import pytest
5-
from zarr import AsyncArray
65
from zarr.abc.store import Store
6+
from zarr.api.asynchronous import create_array as create_async_array
77
from zarr.codecs import BytesCodec
8+
from zarr.core.chunk_key_encodings import ChunkKeyEncodingParams
89
from zarr.storage import StorePath
910

1011
from .test_codecs import _AsyncArrayProxy
@@ -15,14 +16,14 @@ async def test_endian(store: Store, endian: Literal["big", "little"]) -> None:
1516
data = np.arange(0, 256, dtype="uint16").reshape((16, 16))
1617
path = "endian"
1718
spath = StorePath(store, path)
18-
a = await AsyncArray.create(
19+
a = await create_async_array(
1920
spath,
2021
shape=data.shape,
21-
chunk_shape=(16, 16),
22+
chunks=(16, 16),
2223
dtype=data.dtype,
2324
fill_value=0,
24-
chunk_key_encoding=("v2", "."),
25-
codecs=[BytesCodec(endian=endian)],
25+
chunk_key_encoding=ChunkKeyEncodingParams(name="v2", separator="."),
26+
serializer=BytesCodec(endian=endian),
2627
)
2728

2829
await _AsyncArrayProxy(a)[:, :].set(data)
@@ -40,14 +41,14 @@ async def test_endian_write(
4041
data = np.arange(0, 256, dtype=dtype_input_endian).reshape((16, 16))
4142
path = "endian"
4243
spath = StorePath(store, path)
43-
a = await AsyncArray.create(
44+
a = await create_async_array(
4445
spath,
4546
shape=data.shape,
46-
chunk_shape=(16, 16),
47+
chunks=(16, 16),
4748
dtype="uint16",
4849
fill_value=0,
49-
chunk_key_encoding=("v2", "."),
50-
codecs=[BytesCodec(endian=dtype_store_endian)],
50+
chunk_key_encoding=ChunkKeyEncodingParams(name="v2", separator="."),
51+
serializer=BytesCodec(endian=dtype_store_endian),
5152
)
5253

5354
await _AsyncArrayProxy(a)[:, :].set(data)

tests/test_gzip.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import numpy as np
2-
from zarr import Array
2+
from zarr import create_array
33
from zarr.abc.store import Store
4-
from zarr.codecs import BytesCodec, GzipCodec
4+
from zarr.codecs import GzipCodec
55
from zarr.storage import StorePath
66

77

88
def test_gzip(store: Store) -> None:
99
data = np.arange(0, 256, dtype="uint16").reshape((16, 16))
1010

11-
a = Array.create(
11+
a = create_array(
1212
StorePath(store),
1313
shape=data.shape,
14-
chunk_shape=(16, 16),
14+
chunks=(16, 16),
1515
dtype=data.dtype,
1616
fill_value=0,
17-
codecs=[BytesCodec(), GzipCodec()],
17+
compressors=[GzipCodec()],
1818
)
1919

2020
a[:, :] = data

0 commit comments

Comments
 (0)