Skip to content

Commit aa5db9f

Browse files
jhammanrabernat
andauthored
[V3] Allow for incomplete codec metadata using numcodecs.get_codec (#1447)
* refactor(v3): Allow for incomplete codec metadata using numcodecs.get_codec * add test * lint * add release note --------- Co-authored-by: Ryan Abernathey <[email protected]>
1 parent 98f74d5 commit aa5db9f

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

docs/release.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ Release notes
1818
Unreleased
1919
----------
2020

21-
Bug fixes
22-
~~~~~~~~~
21+
Enhancements
22+
~~~~~~~~~~~~
23+
24+
* Allow for partial codec specification in V3 array metadata.
25+
By :user:`Joe Hamman <jhamman>` :issue:`1443`.
2326

24-
* Add ``__contains__`` method to ``KVStore``. By :user:`Christoph Gohlke <cgohlke>` :issue:`1454`.
27+
* Add ``__contains__`` method to ``KVStore``.
28+
By :user:`Christoph Gohlke <cgohlke>` :issue:`1454`.
2529

2630
* **Block Indexing**: Implemented blockwise (chunk blocks) indexing to ``zarr.Array``.
2731
By :user:`Altay Sansal <tasansal>` :issue:`1428`

zarr/meta.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -441,26 +441,22 @@ def _decode_codec_metadata(cls, meta: Optional[Mapping]) -> Optional[Codec]:
441441
uri = 'https://purl.org/zarr/spec/codec/'
442442
conf = meta['configuration']
443443
if meta['codec'].startswith(uri + 'gzip/'):
444-
codec = numcodecs.GZip(level=conf['level'])
444+
conf["id"] = "gzip"
445445
elif meta['codec'].startswith(uri + 'zlib/'):
446-
codec = numcodecs.Zlib(level=conf['level'])
446+
conf["id"] = "zlib"
447447
elif meta['codec'].startswith(uri + 'blosc/'):
448-
codec = numcodecs.Blosc(clevel=conf['clevel'],
449-
shuffle=conf['shuffle'],
450-
blocksize=conf['blocksize'],
451-
cname=conf['cname'])
448+
conf["id"] = "blosc"
452449
elif meta['codec'].startswith(uri + 'bz2/'):
453-
codec = numcodecs.BZ2(level=conf['level'])
450+
conf["id"] = "bz2"
454451
elif meta['codec'].startswith(uri + 'lz4/'):
455-
codec = numcodecs.LZ4(acceleration=conf['acceleration'])
452+
conf["id"] = "lz4"
456453
elif meta['codec'].startswith(uri + 'lzma/'):
457-
codec = numcodecs.LZMA(format=conf['format'],
458-
check=conf['check'],
459-
preset=conf['preset'],
460-
filters=conf['filters'])
454+
conf["id"] = "lzma"
461455
else:
462456
raise NotImplementedError
463457

458+
codec = numcodecs.get_codec(conf)
459+
464460
return codec
465461

466462
@classmethod

zarr/tests/test_meta.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,34 @@ def test_encode_decode_array_dtype_shape_v3():
314314
assert 'filters' not in meta_dec
315315

316316

317+
@pytest.mark.parametrize("comp_id", ["gzip", "zlib", "blosc", "bz2", "lz4", "lzma"])
318+
def test_decode_metadata_implicit_compressor_config_v3(comp_id):
319+
meta = {
320+
"attributes": {},
321+
"chunk_grid": {
322+
"chunk_shape": [10],
323+
"separator": "/",
324+
"type": "regular"
325+
},
326+
"chunk_memory_layout": "C",
327+
"compressor": {
328+
"codec": f"https://purl.org/zarr/spec/codec/{comp_id}/1.0",
329+
"configuration": {
330+
# intentionally left empty
331+
}
332+
},
333+
"data_type": "<f8",
334+
"extensions": [],
335+
"fill_value": None,
336+
"shape": [100, 10, 10]
337+
}
338+
meta_json = json.dumps(meta)
339+
340+
# test decoding
341+
meta_dec = Metadata3.decode_array_metadata(meta_json)
342+
assert meta_dec['compressor'].codec_id == comp_id
343+
344+
317345
def test_encode_decode_array_structured():
318346

319347
meta = dict(

0 commit comments

Comments
 (0)