forked from zarr-developers/zarr-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_v2.py
315 lines (284 loc) · 10.4 KB
/
test_v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from __future__ import annotations
import json
from typing import TYPE_CHECKING, Literal
import numpy as np
import pytest
import zarr.api.asynchronous
import zarr.storage
from zarr.core.buffer import cpu
from zarr.core.buffer.core import default_buffer_prototype
from zarr.core.group import ConsolidatedMetadata, GroupMetadata
from zarr.core.metadata import ArrayV2Metadata
from zarr.core.metadata.v2 import parse_zarr_format
if TYPE_CHECKING:
from typing import Any
from zarr.abc.codec import Codec
import numcodecs
def test_parse_zarr_format_valid() -> None:
assert parse_zarr_format(2) == 2
@pytest.mark.parametrize("data", [None, 1, 3, 4, 5, "3"])
def test_parse_zarr_format_invalid(data: Any) -> None:
with pytest.raises(ValueError, match=f"Invalid value. Expected 2. Got {data}"):
parse_zarr_format(data)
@pytest.mark.parametrize("attributes", [None, {"foo": "bar"}])
@pytest.mark.parametrize("filters", [None, (), (numcodecs.GZip(),)])
@pytest.mark.parametrize("compressor", [None, numcodecs.GZip()])
@pytest.mark.parametrize("fill_value", [None, 0, 1])
@pytest.mark.parametrize("order", ["C", "F"])
@pytest.mark.parametrize("dimension_separator", [".", "/", None])
def test_metadata_to_dict(
compressor: Codec | None,
filters: tuple[Codec] | None,
fill_value: Any,
order: Literal["C", "F"],
dimension_separator: Literal[".", "/"] | None,
attributes: dict[str, Any] | None,
) -> None:
shape = (1, 2, 3)
chunks = (1,) * len(shape)
data_type = "|u1"
metadata_dict = {
"zarr_format": 2,
"shape": shape,
"chunks": chunks,
"dtype": data_type,
"order": order,
"compressor": compressor,
"filters": filters,
"fill_value": fill_value,
}
if attributes is not None:
metadata_dict["attributes"] = attributes
if dimension_separator is not None:
metadata_dict["dimension_separator"] = dimension_separator
metadata = ArrayV2Metadata.from_dict(metadata_dict)
observed = metadata.to_dict()
expected = metadata_dict.copy()
if attributes is None:
assert observed["attributes"] == {}
observed.pop("attributes")
if dimension_separator is None:
expected_dimension_sep = "."
assert observed["dimension_separator"] == expected_dimension_sep
observed.pop("dimension_separator")
assert observed == expected
class TestConsolidated:
@pytest.fixture
async def v2_consolidated_metadata(
self, memory_store: zarr.storage.MemoryStore
) -> zarr.storage.MemoryStore:
zmetadata = {
"metadata": {
".zattrs": {
"Conventions": "COARDS",
},
".zgroup": {"zarr_format": 2},
"air/.zarray": {
"chunks": [730],
"compressor": None,
"dtype": "<i2",
"fill_value": 0,
"filters": None,
"order": "C",
"shape": [730],
"zarr_format": 2,
},
"air/.zattrs": {
"_ARRAY_DIMENSIONS": ["time"],
"dataset": "NMC Reanalysis",
},
"time/.zarray": {
"chunks": [730],
"compressor": None,
"dtype": "<f4",
"fill_value": "0.0",
"filters": None,
"order": "C",
"shape": [730],
"zarr_format": 2,
},
"time/.zattrs": {
"_ARRAY_DIMENSIONS": ["time"],
"calendar": "standard",
"long_name": "Time",
"standard_name": "time",
"units": "hours since 1800-01-01",
},
"nested/.zattrs": {"key": "value"},
"nested/.zgroup": {"zarr_format": 2},
"nested/array/.zarray": {
"chunks": [730],
"compressor": None,
"dtype": "<f4",
"fill_value": "0.0",
"filters": None,
"order": "C",
"shape": [730],
"zarr_format": 2,
},
"nested/array/.zattrs": {
"calendar": "standard",
},
},
"zarr_consolidated_format": 1,
}
store_dict = {}
store = zarr.storage.MemoryStore(store_dict=store_dict)
await store.set(
".zattrs", cpu.Buffer.from_bytes(json.dumps({"Conventions": "COARDS"}).encode())
)
await store.set(".zgroup", cpu.Buffer.from_bytes(json.dumps({"zarr_format": 2}).encode()))
await store.set(".zmetadata", cpu.Buffer.from_bytes(json.dumps(zmetadata).encode()))
await store.set(
"air/.zarray",
cpu.Buffer.from_bytes(json.dumps(zmetadata["metadata"]["air/.zarray"]).encode()),
)
await store.set(
"air/.zattrs",
cpu.Buffer.from_bytes(json.dumps(zmetadata["metadata"]["air/.zattrs"]).encode()),
)
await store.set(
"time/.zarray",
cpu.Buffer.from_bytes(json.dumps(zmetadata["metadata"]["time/.zarray"]).encode()),
)
await store.set(
"time/.zattrs",
cpu.Buffer.from_bytes(json.dumps(zmetadata["metadata"]["time/.zattrs"]).encode()),
)
# and a nested group for fun
await store.set(
"nested/.zattrs", cpu.Buffer.from_bytes(json.dumps({"key": "value"}).encode())
)
await store.set(
"nested/.zgroup", cpu.Buffer.from_bytes(json.dumps({"zarr_format": 2}).encode())
)
await store.set(
"nested/array/.zarray",
cpu.Buffer.from_bytes(
json.dumps(zmetadata["metadata"]["nested/array/.zarray"]).encode()
),
)
await store.set(
"nested/array/.zattrs",
cpu.Buffer.from_bytes(
json.dumps(zmetadata["metadata"]["nested/array/.zattrs"]).encode()
),
)
return store
async def test_read_consolidated_metadata(
self, v2_consolidated_metadata: zarr.storage.MemoryStore
):
# .zgroup, .zattrs, .metadata
store = v2_consolidated_metadata
group = zarr.open_consolidated(store=store, zarr_format=2)
assert group.metadata.consolidated_metadata is not None
expected = ConsolidatedMetadata(
metadata={
"air": ArrayV2Metadata(
shape=(730,),
fill_value=0,
chunks=(730,),
attributes={"_ARRAY_DIMENSIONS": ["time"], "dataset": "NMC Reanalysis"},
dtype=np.dtype("int16"),
order="C",
filters=None,
dimension_separator=".",
compressor=None,
),
"time": ArrayV2Metadata(
shape=(730,),
fill_value=0.0,
chunks=(730,),
attributes={
"_ARRAY_DIMENSIONS": ["time"],
"calendar": "standard",
"long_name": "Time",
"standard_name": "time",
"units": "hours since 1800-01-01",
},
dtype=np.dtype("float32"),
order="C",
filters=None,
dimension_separator=".",
compressor=None,
),
"nested": GroupMetadata(
attributes={"key": "value"},
zarr_format=2,
consolidated_metadata=ConsolidatedMetadata(
metadata={
"array": ArrayV2Metadata(
shape=(730,),
fill_value=0.0,
chunks=(730,),
attributes={
"calendar": "standard",
},
dtype=np.dtype("float32"),
order="C",
filters=None,
dimension_separator=".",
compressor=None,
)
}
),
),
},
kind="inline",
must_understand=False,
)
result = group.metadata.consolidated_metadata
assert result == expected
async def test_getitem_consolidated(self, v2_consolidated_metadata):
store = v2_consolidated_metadata
group = await zarr.api.asynchronous.open_consolidated(store=store, zarr_format=2)
air = await group.getitem("air")
assert air.metadata.shape == (730,)
def test_from_dict_extra_fields() -> None:
data = {
"_nczarr_array": {"dimrefs": ["/dim1", "/dim2"], "storage": "chunked"},
"attributes": {"key": "value"},
"chunks": [8],
"compressor": None,
"dtype": "<f8",
"fill_value": 0.0,
"filters": None,
"order": "C",
"shape": [8],
"zarr_format": 2,
}
result = ArrayV2Metadata.from_dict(data)
expected = ArrayV2Metadata(
attributes={"key": "value"},
shape=(8,),
dtype="float64",
chunks=(8,),
fill_value=0.0,
order="C",
)
assert result == expected
def test_zstd_checksum() -> None:
arr = zarr.create_array(
{},
shape=(10,),
chunks=(10,),
dtype="int32",
compressors={"id": "zstd", "level": 5, "checksum": False},
zarr_format=2,
)
metadata = json.loads(
arr.metadata.to_buffer_dict(default_buffer_prototype())[".zarray"].to_bytes()
)
assert "checksum" not in metadata["compressor"]
def test_0_fill_str_type():
array = zarr.create_array(
store=zarr.storage.MemoryStore(),
dtype=str,
shape=(5,),
chunks=(2,),
fill_value=0,
zarr_format=2,
overwrite=True,
)
# Ensure the array initializes correctly with the fill value
np.testing.assert_array_equal(array[:], ["", "", "", "", ""])