Skip to content

Commit

Permalink
Add zstd to old V3 supported codecs (#1914)
Browse files Browse the repository at this point in the history
* add zstd to old V3 supported codecs

* get to full test coverage and add release note

* fix pre-commit
  • Loading branch information
rabernat authored May 26, 2024
1 parent bf89533 commit 3e98c3b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
3 changes: 3 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Docs
Maintenance
~~~~~~~~~~~

* Add Zstd codec to old V3 code path.
By :user:`Ryan Abernathey <rabernat>`

Deprecations
~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion zarr/codecs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# flake8: noqa
from numcodecs import *
from numcodecs import get_codec, Blosc, Pickle, Zlib, Delta, AsType, BZ2
from numcodecs import get_codec, Blosc, Pickle, Zlib, Zstd, Delta, AsType, BZ2
from numcodecs.registry import codec_registry
4 changes: 4 additions & 0 deletions zarr/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ def _encode_codec_metadata(cls, codec: Codec) -> Optional[Mapping]:
uri = uri + "lz4/1.0"
elif isinstance(codec, numcodecs.LZMA):
uri = uri + "lzma/1.0"
elif isinstance(codec, numcodecs.Zstd):
uri = uri + "zstd/1.0"
meta = {
"codec": uri,
"configuration": config,
Expand All @@ -439,6 +441,8 @@ def _decode_codec_metadata(cls, meta: Optional[Mapping]) -> Optional[Codec]:
conf["id"] = "lz4"
elif meta["codec"].startswith(uri + "lzma/"):
conf["id"] = "lzma"
elif meta["codec"].startswith(uri + "zstd/"):
conf["id"] = "zstd"
else:
raise NotImplementedError

Expand Down
23 changes: 17 additions & 6 deletions zarr/tests/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
import pytest

from zarr.codecs import Blosc, Delta, Pickle, Zlib
from zarr.codecs import Blosc, Delta, Pickle, Zlib, Zstd
from zarr.errors import MetadataError
from zarr.meta import (
ZARR_FORMAT,
Expand Down Expand Up @@ -268,17 +268,23 @@ def test_encode_decode_array_dtype_shape():
assert meta_dec["filters"] is None


def test_encode_decode_array_dtype_shape_v3():
@pytest.mark.parametrize("cname", ["zlib", "zstd"])
def test_encode_decode_array_dtype_shape_v3(cname):
if cname == "zlib":
compressor = Zlib(1)
elif cname == "zstd":
compressor = Zstd(1)
meta = dict(
shape=(100,),
chunk_grid=dict(type="regular", chunk_shape=(10,), separator=("/")),
data_type=np.dtype("(10, 10)<f8"),
compressor=Zlib(1),
compressor=compressor,
fill_value=None,
chunk_memory_layout="C",
)

meta_json = """{
meta_json = (
"""{
"attributes": {},
"chunk_grid": {
"chunk_shape": [10],
Expand All @@ -287,7 +293,11 @@ def test_encode_decode_array_dtype_shape_v3():
},
"chunk_memory_layout": "C",
"compressor": {
"codec": "https://purl.org/zarr/spec/codec/zlib/1.0",
"""
+ f"""
"codec": "https://purl.org/zarr/spec/codec/{cname}/1.0",
"""
+ """
"configuration": {
"level": 1
}
Expand All @@ -297,6 +307,7 @@ def test_encode_decode_array_dtype_shape_v3():
"fill_value": null,
"shape": [100, 10, 10 ]
}"""
)

# test encoding
meta_enc = Metadata3.encode_array_metadata(meta)
Expand All @@ -315,7 +326,7 @@ def test_encode_decode_array_dtype_shape_v3():
assert "filters" not in meta_dec


@pytest.mark.parametrize("comp_id", ["gzip", "zlib", "blosc", "bz2", "lz4", "lzma"])
@pytest.mark.parametrize("comp_id", ["gzip", "zlib", "blosc", "bz2", "lz4", "lzma", "zstd"])
def test_decode_metadata_implicit_compressor_config_v3(comp_id):
meta = {
"attributes": {},
Expand Down

0 comments on commit 3e98c3b

Please sign in to comment.