|
10 | 10 | import numcodecs |
11 | 11 | import numpy as np |
12 | 12 | import pytest |
| 13 | +from packaging.version import Version |
13 | 14 |
|
14 | 15 | import zarr.api.asynchronous |
15 | 16 | import zarr.api.synchronous as sync_api |
@@ -1346,3 +1347,47 @@ async def test_orthogonal_set_total_slice() -> None: |
1346 | 1347 | array = zarr.create_array(store, shape=(20, 20), chunks=(1, 2), dtype=int, fill_value=-1) |
1347 | 1348 | with mock.patch("zarr.storage.MemoryStore.get", side_effect=ValueError): |
1348 | 1349 | array[0, slice(4, 10)] = np.arange(6) |
| 1350 | + |
| 1351 | + |
| 1352 | +@pytest.mark.skipif( |
| 1353 | + Version(numcodecs.__version__) < Version("0.15.1"), |
| 1354 | + reason="codec configuration is overwritten on older versions. GH2800", |
| 1355 | +) |
| 1356 | +def test_roundtrip_numcodecs() -> None: |
| 1357 | + store = MemoryStore() |
| 1358 | + |
| 1359 | + compressors = [ |
| 1360 | + {"name": "numcodecs.shuffle", "configuration": {"elementsize": 2}}, |
| 1361 | + {"name": "numcodecs.zlib", "configuration": {"level": 4}}, |
| 1362 | + ] |
| 1363 | + filters = [ |
| 1364 | + { |
| 1365 | + "name": "numcodecs.fixedscaleoffset", |
| 1366 | + "configuration": { |
| 1367 | + "scale": 100.0, |
| 1368 | + "offset": 0.0, |
| 1369 | + "dtype": "<f8", |
| 1370 | + "astype": "<i2", |
| 1371 | + }, |
| 1372 | + }, |
| 1373 | + ] |
| 1374 | + |
| 1375 | + # Create the array with the correct codecs |
| 1376 | + root = zarr.group(store) |
| 1377 | + root.create_array( |
| 1378 | + "test", |
| 1379 | + shape=(720, 1440), |
| 1380 | + chunks=(720, 1440), |
| 1381 | + dtype="float64", |
| 1382 | + compressors=compressors, |
| 1383 | + filters=filters, |
| 1384 | + fill_value=-9.99, |
| 1385 | + dimension_names=["lat", "lon"], |
| 1386 | + ) |
| 1387 | + |
| 1388 | + BYTES_CODEC = {"name": "bytes", "configuration": {"endian": "little"}} |
| 1389 | + # Read in the array again and check compressor config |
| 1390 | + root = zarr.open_group(store, mode="r") |
| 1391 | + metadata = root["test"].metadata.to_dict() |
| 1392 | + expected = (*filters, BYTES_CODEC, *compressors) |
| 1393 | + assert metadata["codecs"] == expected |
0 commit comments