33import numpy as np
44import pytest
55
6- from 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- )
6+ import numcodecs .zarr3
187
198zarr = pytest .importorskip ("zarr" )
209
@@ -40,42 +29,50 @@ def store() -> Store:
4029
4130
4231@pytest .mark .parametrize (
43- "codec_name" , ["blosc" , "lz4" , "zstd" , "zlib" , "gzip" , "bz2" , "lzma" , "shuffle" ]
32+ ("codec_name" , "codec_class" ),
33+ [
34+ ("numcodecs.blosc" , numcodecs .zarr3 .Blosc ),
35+ ("numcodecs.lz4" , numcodecs .zarr3 .LZ4 ),
36+ ("numcodecs.zstd" , numcodecs .zarr3 .Zstd ),
37+ ("numcodecs.zlib" , numcodecs .zarr3 .Zlib ),
38+ ("numcodecs.gzip" , numcodecs .zarr3 .GZip ),
39+ ("numcodecs.bz2" , numcodecs .zarr3 .BZ2 ),
40+ ("numcodecs.lzma" , numcodecs .zarr3 .LZMA ),
41+ ("numcodecs.shuffle" , numcodecs .zarr3 .Shuffle ),
42+ ("numcodecs.delta" , numcodecs .zarr3 .Delta ),
43+ ("numcodecs.bitround" , numcodecs .zarr3 .BitRound ),
44+ ("numcodecs.fixedscaleoffset" , numcodecs .zarr3 .FixedScaleOffset ),
45+ ("numcodecs.quantize" , numcodecs .zarr3 .Quantize ),
46+ ("numcodecs.packbits" , numcodecs .zarr3 .PackBits ),
47+ ("numcodecs.astype" , numcodecs .zarr3 .AsType ),
48+ ("numcodecs.crc32" , numcodecs .zarr3 .CRC32 ),
49+ ("numcodecs.crc32c" , numcodecs .zarr3 .CRC32C ),
50+ ("numcodecs.adler32" , numcodecs .zarr3 .Adler32 ),
51+ ("numcodecs.fletcher32" , numcodecs .zarr3 .Fletcher32 ),
52+ ("numcodecs.jenkins_lookup3" , numcodecs .zarr3 .JenkinsLookup3 ),
53+ ("numcodecs.pcodec" , numcodecs .zarr3 .PCodec ),
54+ ("numcodecs.zfpy" , numcodecs .zarr3 .ZFPY ),
55+ ],
4456)
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 [:, :])
57+ def test_entry_points (codec_name : str , codec_class : type [numcodecs .zarr3 ._NumcodecsCodec ]):
58+ assert codec_class .codec_name == codec_name
59+ assert get_codec_class (codec_name ) == codec_class
6360
6461
6562@pytest .mark .parametrize (
6663 "codec_class" ,
6764 [
68- BloscCodec ,
69- LZ4Codec ,
70- ZstdCodec ,
71- ZlibCodec ,
72- GZipCodec ,
73- BZ2Codec ,
74- LZMACodec ,
75- ShuffleCodec ,
65+ numcodecs . zarr3 . Blosc ,
66+ numcodecs . zarr3 . LZ4 ,
67+ numcodecs . zarr3 . Zstd ,
68+ numcodecs . zarr3 . Zlib ,
69+ numcodecs . zarr3 . GZip ,
70+ numcodecs . zarr3 . BZ2 ,
71+ numcodecs . zarr3 . LZMA ,
72+ numcodecs . zarr3 . Shuffle ,
7673 ],
7774)
78- def test_generic_codec_class (store : Store , codec_class : type [NumcodecsCodec ]):
75+ def test_generic_codec_class (store : Store , codec_class : type [numcodecs . zarr3 . _NumcodecsCodec ]):
7976 data = np .arange (0 , 256 , dtype = "uint16" ).reshape ((16 , 16 ))
8077
8178 with pytest .warns (UserWarning , match = EXPECTED_WARNING_STR ):
@@ -85,23 +82,20 @@ def test_generic_codec_class(store: Store, codec_class: type[NumcodecsCodec]):
8582 chunk_shape = (16 , 16 ),
8683 dtype = data .dtype ,
8784 fill_value = 0 ,
88- codecs = [
89- BytesCodec (),
90- codec_class (),
91- ],
85+ codecs = [BytesCodec (), codec_class ()],
9286 )
9387
9488 a [:, :] = data .copy ()
9589 np .testing .assert_array_equal (data , a [:, :])
9690
9791
9892@pytest .mark .parametrize (
99- " codec_config" ,
93+ ( "codec_class" , " codec_config") ,
10094 [
101- { "id" : "delta" , "dtype" : "float32" },
102- { "id" : "fixedscaleoffset" , "offset" : 0 , "scale" : 25.5 },
103- { "id" : "fixedscaleoffset" , "offset" : 0 , "scale" : 51 , "astype" : "uint16" },
104- { "id" : "astype" , "encode_dtype" : "float32" , "decode_dtype" : "float64" },
95+ ( numcodecs . zarr3 . Delta , { "dtype" : "float32" }) ,
96+ ( numcodecs . zarr3 . FixedScaleOffset , { "offset" : 0 , "scale" : 25.5 }) ,
97+ ( numcodecs . zarr3 . FixedScaleOffset , { "offset" : 0 , "scale" : 51 , "astype" : "uint16" }) ,
98+ ( numcodecs . zarr3 . AsType , { "encode_dtype" : "float32" , "decode_dtype" : "float64" }) ,
10599 ],
106100 ids = [
107101 "delta" ,
@@ -110,12 +104,11 @@ def test_generic_codec_class(store: Store, codec_class: type[NumcodecsCodec]):
110104 "astype" ,
111105 ],
112106)
113- def test_generic_filter (store : Store , codec_config : dict [str , JSON ]):
107+ def test_generic_filter (
108+ store : Store , codec_class : type [numcodecs .zarr3 ._NumcodecsCodec ], codec_config : dict [str , JSON ]
109+ ):
114110 data = np .linspace (0 , 10 , 256 , dtype = "float32" ).reshape ((16 , 16 ))
115111
116- codec_name = codec_config ["id" ]
117- del codec_config ["id" ]
118-
119112 with pytest .warns (UserWarning , match = EXPECTED_WARNING_STR ):
120113 a = Array .create (
121114 store / "generic" ,
@@ -124,7 +117,7 @@ def test_generic_filter(store: Store, codec_config: dict[str, JSON]):
124117 dtype = data .dtype ,
125118 fill_value = 0 ,
126119 codecs = [
127- get_codec_class ( f"numcodecs. { codec_name } " )( codec_config ),
120+ codec_class ( ** codec_config ),
128121 BytesCodec (),
129122 ],
130123 )
@@ -144,10 +137,7 @@ def test_generic_filter_bitround(store: Store):
144137 chunk_shape = (16 , 16 ),
145138 dtype = data .dtype ,
146139 fill_value = 0 ,
147- codecs = [
148- get_codec_class ("numcodecs.bitround" )({"keepbits" : 3 }),
149- BytesCodec (),
150- ],
140+ codecs = [numcodecs .zarr3 .BitRound (keepbits = 3 ), BytesCodec ()],
151141 )
152142
153143 a [:, :] = data .copy ()
@@ -165,10 +155,7 @@ def test_generic_filter_quantize(store: Store):
165155 chunk_shape = (16 , 16 ),
166156 dtype = data .dtype ,
167157 fill_value = 0 ,
168- codecs = [
169- get_codec_class ("numcodecs.quantize" )({"digits" : 3 }),
170- BytesCodec (),
171- ],
158+ codecs = [numcodecs .zarr3 .Quantize (digits = 3 ), BytesCodec ()],
172159 )
173160
174161 a [:, :] = data .copy ()
@@ -187,10 +174,7 @@ def test_generic_filter_packbits(store: Store):
187174 chunk_shape = (16 , 16 ),
188175 dtype = data .dtype ,
189176 fill_value = 0 ,
190- codecs = [
191- get_codec_class ("numcodecs.packbits" )(),
192- BytesCodec (),
193- ],
177+ codecs = [numcodecs .zarr3 .PackBits (), BytesCodec ()],
194178 )
195179
196180 a [:, :] = data .copy ()
@@ -204,15 +188,21 @@ def test_generic_filter_packbits(store: Store):
204188 chunk_shape = (16 , 16 ),
205189 dtype = "uint32" ,
206190 fill_value = 0 ,
207- codecs = [
208- get_codec_class ("numcodecs.packbits" )(),
209- BytesCodec (),
210- ],
191+ codecs = [numcodecs .zarr3 .PackBits (), BytesCodec ()],
211192 )
212193
213194
214- @pytest .mark .parametrize ("codec_name" , ["crc32" , "adler32" , "fletcher32" , "jenkins_lookup3" ])
215- def test_generic_checksum (store : Store , codec_name : str ):
195+ @pytest .mark .parametrize (
196+ "codec_class" ,
197+ [
198+ numcodecs .zarr3 .CRC32 ,
199+ numcodecs .zarr3 .CRC32C ,
200+ numcodecs .zarr3 .Adler32 ,
201+ numcodecs .zarr3 .Fletcher32 ,
202+ numcodecs .zarr3 .JenkinsLookup3 ,
203+ ],
204+ )
205+ def test_generic_checksum (store : Store , codec_class : type [numcodecs .zarr3 ._NumcodecsCodec ]):
216206 data = np .linspace (0 , 10 , 256 , dtype = "float32" ).reshape ((16 , 16 ))
217207
218208 with pytest .warns (UserWarning , match = EXPECTED_WARNING_STR ):
@@ -222,28 +212,25 @@ def test_generic_checksum(store: Store, codec_name: str):
222212 chunk_shape = (16 , 16 ),
223213 dtype = data .dtype ,
224214 fill_value = 0 ,
225- codecs = [
226- BytesCodec (),
227- get_codec_class (f"numcodecs.{ codec_name } " )(),
228- ],
215+ codecs = [BytesCodec (), codec_class ()],
229216 )
230217
231218 a [:, :] = data .copy ()
232219 a = Array .open (store / "generic_checksum" )
233220 np .testing .assert_array_equal (data , a [:, :])
234221
235222
236- @pytest .mark .parametrize ("codec_name " , ["pcodec" , "zfpy" ])
237- def test_generic_bytes_codec (store : Store , codec_name : str ):
223+ @pytest .mark .parametrize ("codec_class " , [numcodecs . zarr3 . PCodec , numcodecs . zarr3 . ZFPY ])
224+ def test_generic_bytes_codec (store : Store , codec_class : type [ numcodecs . zarr3 . _NumcodecsCodec ] ):
238225 try :
239- get_codec ({ "id" : codec_name } )
226+ codec_class ( )
240227 except ValueError as e :
241228 if "codec not available" in str (e ):
242- pytest .xfail (f"{ codec_name } is not available: { e } " )
229+ pytest .xfail (f"{ codec_class . codec_name } is not available: { e } " )
243230 else :
244231 raise # pragma: no cover
245232 except ImportError as e :
246- pytest .xfail (f"{ codec_name } is not available: { e } " )
233+ pytest .xfail (f"{ codec_class . codec_name } is not available: { e } " )
247234
248235 data = np .arange (0 , 256 , dtype = "float32" ).reshape ((16 , 16 ))
249236
@@ -255,7 +242,7 @@ def test_generic_bytes_codec(store: Store, codec_name: str):
255242 dtype = data .dtype ,
256243 fill_value = 0 ,
257244 codecs = [
258- get_codec_class ( f"numcodecs. { codec_name } " )({ "id" : codec_name } ),
245+ codec_class ( ),
259246 ],
260247 )
261248
0 commit comments