11from __future__ import annotations
22
3- from typing import TYPE_CHECKING , Any
3+ from typing import Any
44
55import numpy as np
6- import pytest
76
87from zarr .codecs .bytes import BytesCodec
98from zarr .codecs .gzip import GzipCodec
109from zarr .codecs .transpose import TransposeCodec
1110from zarr .codecs .zstd import ZstdCodec
1211from zarr .core .array_spec import ArrayConfig , ArraySpec
1312from zarr .core .buffer import NDBuffer , default_buffer_prototype
13+ from zarr .core .codec_pipeline import CodecChain
1414from zarr .core .dtype import get_data_type_from_native_dtype
1515
16- if TYPE_CHECKING :
17- from zarr .abc .codec import Codec
18-
1916
2017def _make_array_spec (shape : tuple [int , ...], dtype : np .dtype [np .generic ]) -> ArraySpec :
2118 zdtype = get_data_type_from_native_dtype (dtype )
@@ -33,124 +30,50 @@ def _make_nd_buffer(arr: np.ndarray[Any, np.dtype[Any]]) -> NDBuffer:
3330
3431
3532class TestCodecChain :
36- def test_from_codecs_bytes_only (self ) -> None :
37- from zarr .core .codec_pipeline import CodecChain
38-
39- chain = CodecChain .from_codecs ([BytesCodec ()])
40- assert chain .array_array_codecs == ()
41- assert isinstance (chain .array_bytes_codec , BytesCodec )
42- assert chain .bytes_bytes_codecs == ()
43- assert chain ._all_sync is True
44-
45- def test_from_codecs_with_compression (self ) -> None :
46- from zarr .core .codec_pipeline import CodecChain
47-
48- chain = CodecChain .from_codecs ([BytesCodec (), GzipCodec ()])
49- assert isinstance (chain .array_bytes_codec , BytesCodec )
50- assert len (chain .bytes_bytes_codecs ) == 1
51- assert isinstance (chain .bytes_bytes_codecs [0 ], GzipCodec )
52- assert chain ._all_sync is True
53-
54- def test_from_codecs_with_transpose (self ) -> None :
55- from zarr .core .codec_pipeline import CodecChain
56-
57- chain = CodecChain .from_codecs ([TransposeCodec (order = (1 , 0 )), BytesCodec ()])
58- assert len (chain .array_array_codecs ) == 1
59- assert isinstance (chain .array_array_codecs [0 ], TransposeCodec )
60- assert isinstance (chain .array_bytes_codec , BytesCodec )
61- assert chain ._all_sync is True
33+ def test_all_sync (self ) -> None :
34+ spec = _make_array_spec ((100 ,), np .dtype ("float64" ))
35+ chain = CodecChain ((BytesCodec (),), spec )
36+ assert chain .all_sync is True
6237
63- def test_from_codecs_full_chain (self ) -> None :
64- from zarr .core .codec_pipeline import CodecChain
38+ def test_all_sync_with_compression (self ) -> None :
39+ spec = _make_array_spec ((100 ,), np .dtype ("float64" ))
40+ chain = CodecChain ((BytesCodec (), GzipCodec ()), spec )
41+ assert chain .all_sync is True
6542
66- chain = CodecChain .from_codecs ([TransposeCodec (order = (1 , 0 )), BytesCodec (), ZstdCodec ()])
67- assert len (chain .array_array_codecs ) == 1
68- assert isinstance (chain .array_bytes_codec , BytesCodec )
69- assert len (chain .bytes_bytes_codecs ) == 1
70- assert chain ._all_sync is True
71-
72- def test_iter (self ) -> None :
73- from zarr .core .codec_pipeline import CodecChain
74-
75- codecs : list [Codec ] = [TransposeCodec (order = (1 , 0 )), BytesCodec (), GzipCodec ()]
76- chain = CodecChain .from_codecs (codecs )
77- assert list (chain ) == codecs
78-
79- def test_frozen (self ) -> None :
80- from zarr .core .codec_pipeline import CodecChain
81-
82- chain = CodecChain .from_codecs ([BytesCodec ()])
83- with pytest .raises (AttributeError ):
84- chain .array_bytes_codec = BytesCodec () # type: ignore[misc]
43+ def test_all_sync_full_chain (self ) -> None :
44+ spec = _make_array_spec ((3 , 4 ), np .dtype ("float64" ))
45+ chain = CodecChain ((TransposeCodec (order = (1 , 0 )), BytesCodec (), ZstdCodec ()), spec )
46+ assert chain .all_sync is True
8547
8648 def test_encode_decode_roundtrip_bytes_only (self ) -> None :
87- from zarr .core .codec_pipeline import CodecChain
88-
89- chain = CodecChain .from_codecs ([BytesCodec ()])
9049 arr = np .arange (100 , dtype = "float64" )
9150 spec = _make_array_spec (arr .shape , arr .dtype )
92- chain_evolved = CodecChain . from_codecs ([ c . evolve_from_array_spec ( spec ) for c in chain ] )
51+ chain = CodecChain (( BytesCodec (),), spec )
9352 nd_buf = _make_nd_buffer (arr )
9453
95- encoded = chain_evolved .encode_chunk (nd_buf , spec )
54+ encoded = chain .encode_chunk (nd_buf )
9655 assert encoded is not None
97- decoded = chain_evolved .decode_chunk (encoded , spec )
98- assert decoded is not None
56+ decoded = chain .decode_chunk (encoded )
9957 np .testing .assert_array_equal (arr , decoded .as_numpy_array ())
10058
10159 def test_encode_decode_roundtrip_with_compression (self ) -> None :
102- from zarr .core .codec_pipeline import CodecChain
103-
104- chain = CodecChain .from_codecs ([BytesCodec (), GzipCodec (level = 1 )])
10560 arr = np .arange (100 , dtype = "float64" )
10661 spec = _make_array_spec (arr .shape , arr .dtype )
107- chain_evolved = CodecChain . from_codecs ([ c . evolve_from_array_spec ( spec ) for c in chain ] )
62+ chain = CodecChain (( BytesCodec (), GzipCodec ( level = 1 )), spec )
10863 nd_buf = _make_nd_buffer (arr )
10964
110- encoded = chain_evolved .encode_chunk (nd_buf , spec )
65+ encoded = chain .encode_chunk (nd_buf )
11166 assert encoded is not None
112- decoded = chain_evolved .decode_chunk (encoded , spec )
113- assert decoded is not None
67+ decoded = chain .decode_chunk (encoded )
11468 np .testing .assert_array_equal (arr , decoded .as_numpy_array ())
11569
11670 def test_encode_decode_roundtrip_with_transpose (self ) -> None :
117- from zarr .core .codec_pipeline import CodecChain
118-
119- chain = CodecChain .from_codecs (
120- [TransposeCodec (order = (1 , 0 )), BytesCodec (), ZstdCodec (level = 1 )]
121- )
12271 arr = np .arange (12 , dtype = "float64" ).reshape (3 , 4 )
12372 spec = _make_array_spec (arr .shape , arr .dtype )
124- chain_evolved = CodecChain . from_codecs ([ c . evolve_from_array_spec ( spec ) for c in chain ] )
73+ chain = CodecChain (( TransposeCodec ( order = ( 1 , 0 )), BytesCodec (), ZstdCodec ( level = 1 )), spec )
12574 nd_buf = _make_nd_buffer (arr )
12675
127- encoded = chain_evolved .encode_chunk (nd_buf , spec )
76+ encoded = chain .encode_chunk (nd_buf )
12877 assert encoded is not None
129- decoded = chain_evolved .decode_chunk (encoded , spec )
130- assert decoded is not None
78+ decoded = chain .decode_chunk (encoded )
13179 np .testing .assert_array_equal (arr , decoded .as_numpy_array ())
132-
133- def test_resolve_metadata_chain (self ) -> None :
134- from zarr .core .codec_pipeline import CodecChain
135-
136- chain = CodecChain .from_codecs ([TransposeCodec (order = (1 , 0 )), BytesCodec (), GzipCodec ()])
137- arr = np .zeros ((3 , 4 ), dtype = "float64" )
138- spec = _make_array_spec (arr .shape , arr .dtype )
139- chain_evolved = CodecChain .from_codecs ([c .evolve_from_array_spec (spec ) for c in chain ])
140-
141- aa_chain , ab_pair , bb_chain = chain_evolved .resolve_metadata_chain (spec )
142- assert len (aa_chain ) == 1
143- assert aa_chain [0 ][1 ].shape == (3 , 4 ) # spec before transpose
144- _ab_codec , ab_spec = ab_pair
145- assert ab_spec .shape == (4 , 3 ) # spec after transpose
146- assert len (bb_chain ) == 1
147-
148- def test_resolve_metadata (self ) -> None :
149- from zarr .core .codec_pipeline import CodecChain
150-
151- chain = CodecChain .from_codecs ([TransposeCodec (order = (1 , 0 )), BytesCodec ()])
152- spec = _make_array_spec ((3 , 4 ), np .dtype ("float64" ))
153- chain_evolved = CodecChain .from_codecs ([c .evolve_from_array_spec (spec ) for c in chain ])
154- resolved = chain_evolved .resolve_metadata (spec )
155- # After transpose (1,0) + bytes, shape should reflect the transpose
156- assert resolved .shape == (4 , 3 )
0 commit comments