44import pytest
55
66from numcodecs .registry import get_codec
7+ from numcodecs .zarr3 import (
8+ BloscCodec ,
9+ BZ2Codec ,
10+ GZipCodec ,
11+ LZ4Codec ,
12+ LZMACodec ,
13+ NumcodecsCodec ,
14+ ShuffleCodec ,
15+ ZlibCodec ,
16+ ZstdCodec ,
17+ )
718
819zarr = pytest .importorskip ("zarr" )
920
@@ -29,9 +40,42 @@ def store() -> Store:
2940
3041
3142@pytest .mark .parametrize (
32- "codec_id" , ["blosc" , "lz4" , "zstd" , "zlib" , "gzip" , "bz2" , "lzma" , "shuffle" ]
43+ "codec_name" , ["blosc" , "lz4" , "zstd" , "zlib" , "gzip" , "bz2" , "lzma" , "shuffle" ]
44+ )
45+ def test_generic_codec (store : Store , codec_name : str ):
46+ data = np .arange (0 , 256 , dtype = "uint16" ).reshape ((16 , 16 ))
47+
48+ with pytest .warns (UserWarning , match = EXPECTED_WARNING_STR ):
49+ a = Array .create (
50+ store / "generic" ,
51+ shape = data .shape ,
52+ chunk_shape = (16 , 16 ),
53+ dtype = data .dtype ,
54+ fill_value = 0 ,
55+ codecs = [
56+ BytesCodec (),
57+ get_codec_class (f"numcodecs.{ codec_name } " )({"id" : codec_name }),
58+ ],
59+ )
60+
61+ a [:, :] = data .copy ()
62+ np .testing .assert_array_equal (data , a [:, :])
63+
64+
65+ @pytest .mark .parametrize (
66+ "codec_class" ,
67+ [
68+ BloscCodec ,
69+ LZ4Codec ,
70+ ZstdCodec ,
71+ ZlibCodec ,
72+ GZipCodec ,
73+ BZ2Codec ,
74+ LZMACodec ,
75+ ShuffleCodec ,
76+ ],
3377)
34- def test_generic_codec (store : Store , codec_id : str ):
78+ def test_generic_codec_class (store : Store , codec_class : type [ NumcodecsCodec ] ):
3579 data = np .arange (0 , 256 , dtype = "uint16" ).reshape ((16 , 16 ))
3680
3781 with pytest .warns (UserWarning , match = EXPECTED_WARNING_STR ):
@@ -43,7 +87,7 @@ def test_generic_codec(store: Store, codec_id: str):
4387 fill_value = 0 ,
4488 codecs = [
4589 BytesCodec (),
46- get_codec_class ( f"numcodecs. { codec_id } " )({ "id" : codec_id } ),
90+ codec_class ( ),
4791 ],
4892 )
4993
@@ -69,7 +113,7 @@ def test_generic_codec(store: Store, codec_id: str):
69113def test_generic_filter (store : Store , codec_config : dict [str , JSON ]):
70114 data = np .linspace (0 , 10 , 256 , dtype = "float32" ).reshape ((16 , 16 ))
71115
72- codec_id = codec_config ["id" ]
116+ codec_name = codec_config ["id" ]
73117 del codec_config ["id" ]
74118
75119 with pytest .warns (UserWarning , match = EXPECTED_WARNING_STR ):
@@ -80,7 +124,7 @@ def test_generic_filter(store: Store, codec_config: dict[str, JSON]):
80124 dtype = data .dtype ,
81125 fill_value = 0 ,
82126 codecs = [
83- get_codec_class (f"numcodecs.{ codec_id } " )(codec_config ),
127+ get_codec_class (f"numcodecs.{ codec_name } " )(codec_config ),
84128 BytesCodec (),
85129 ],
86130 )
@@ -167,8 +211,8 @@ def test_generic_filter_packbits(store: Store):
167211 )
168212
169213
170- @pytest .mark .parametrize ("codec_id " , ["crc32" , "adler32" , "fletcher32" , "jenkins_lookup3" ])
171- def test_generic_checksum (store : Store , codec_id : str ):
214+ @pytest .mark .parametrize ("codec_name " , ["crc32" , "adler32" , "fletcher32" , "jenkins_lookup3" ])
215+ def test_generic_checksum (store : Store , codec_name : str ):
172216 data = np .linspace (0 , 10 , 256 , dtype = "float32" ).reshape ((16 , 16 ))
173217
174218 with pytest .warns (UserWarning , match = EXPECTED_WARNING_STR ):
@@ -180,7 +224,7 @@ def test_generic_checksum(store: Store, codec_id: str):
180224 fill_value = 0 ,
181225 codecs = [
182226 BytesCodec (),
183- get_codec_class (f"numcodecs.{ codec_id } " )(),
227+ get_codec_class (f"numcodecs.{ codec_name } " )(),
184228 ],
185229 )
186230
@@ -189,17 +233,17 @@ def test_generic_checksum(store: Store, codec_id: str):
189233 np .testing .assert_array_equal (data , a [:, :])
190234
191235
192- @pytest .mark .parametrize ("codec_id " , ["pcodec" , "zfpy" ])
193- def test_generic_bytes_codec (store : Store , codec_id : str ):
236+ @pytest .mark .parametrize ("codec_name " , ["pcodec" , "zfpy" ])
237+ def test_generic_bytes_codec (store : Store , codec_name : str ):
194238 try :
195- get_codec ({"id" : codec_id })
239+ get_codec ({"id" : codec_name })
196240 except ValueError as e :
197241 if "codec not available" in str (e ):
198- pytest .xfail (f"{ codec_id } is not available: { e } " )
242+ pytest .xfail (f"{ codec_name } is not available: { e } " )
199243 else :
200244 raise # pragma: no cover
201245 except ImportError as e :
202- pytest .xfail (f"{ codec_id } is not available: { e } " )
246+ pytest .xfail (f"{ codec_name } is not available: { e } " )
203247
204248 data = np .arange (0 , 256 , dtype = "float32" ).reshape ((16 , 16 ))
205249
@@ -211,7 +255,7 @@ def test_generic_bytes_codec(store: Store, codec_id: str):
211255 dtype = data .dtype ,
212256 fill_value = 0 ,
213257 codecs = [
214- get_codec_class (f"numcodecs.{ codec_id } " )({"id" : codec_id }),
258+ get_codec_class (f"numcodecs.{ codec_name } " )({"id" : codec_name }),
215259 ],
216260 )
217261
0 commit comments