10
10
import numcodecs
11
11
12
12
from zarr .abc .codec import ArrayArrayCodec , BytesBytesCodec
13
- from zarr .buffer import NDBuffer , Buffer , as_numpy_array_wrapper
13
+ from zarr .buffer import NDBuffer , Buffer , BufferPrototype , as_numpy_array_wrapper
14
14
from zarr .array_spec import ArraySpec
15
15
from zarr .common import (
16
16
JSON ,
@@ -64,7 +64,6 @@ def __init__(
64
64
65
65
@cached_property
66
66
def _codec (self ) -> numcodecs .abc .Codec :
67
- print (self .codec_config )
68
67
return numcodecs .get_codec (self .codec_config )
69
68
70
69
@classmethod
@@ -92,20 +91,25 @@ def __init__(self, *, codec_id: str, codec_config: dict[str, JSON]) -> None:
92
91
super ().__init__ (codec_id = codec_id , codec_config = codec_config )
93
92
94
93
async def _decode_single (
95
- self , chunk_bytes : Buffer , _chunk_spec : ArraySpec
94
+ self , chunk_bytes : Buffer , chunk_spec : ArraySpec
96
95
) -> Buffer :
97
- return await to_thread (as_numpy_array_wrapper , self ._codec .decode , chunk_bytes )
96
+ return await to_thread (
97
+ as_numpy_array_wrapper ,
98
+ self ._codec .decode ,
99
+ chunk_bytes ,
100
+ chunk_spec .prototype ,
101
+ )
98
102
99
- def _encode (self , chunk_bytes : Buffer ) -> Buffer :
103
+ def _encode (self , chunk_bytes : Buffer , prototype : BufferPrototype ) -> Buffer :
100
104
encoded = self ._codec .encode (chunk_bytes .as_array_like ())
101
105
if isinstance (encoded , np .ndarray ): # Required for checksum codecs
102
- return encoded .tobytes ()
103
- return Buffer .from_bytes (encoded )
106
+ return prototype . buffer . from_bytes ( encoded .tobytes () )
107
+ return prototype . buffer .from_bytes (encoded )
104
108
105
109
async def _encode_single (
106
- self , chunk_bytes : Buffer , _chunk_spec : ArraySpec
110
+ self , chunk_bytes : Buffer , chunk_spec : ArraySpec
107
111
) -> Buffer :
108
- return await to_thread (self ._encode , chunk_bytes )
112
+ return await to_thread (self ._encode , chunk_bytes , chunk_spec . prototype )
109
113
110
114
111
115
class NumcodecsArrayArrayCodec (NumcodecsCodec , ArrayArrayCodec ):
@@ -117,14 +121,16 @@ async def _decode_single(
117
121
) -> NDBuffer :
118
122
chunk_ndarray = chunk_array .as_ndarray_like ()
119
123
out = await to_thread (self ._codec .decode , chunk_ndarray )
120
- return NDBuffer .from_ndarray_like (out .reshape (chunk_spec .shape ))
124
+ return chunk_spec .prototype .nd_buffer .from_ndarray_like (
125
+ out .reshape (chunk_spec .shape )
126
+ )
121
127
122
128
async def _encode_single (
123
- self , chunk_array : NDBuffer , _chunk_spec : ArraySpec
129
+ self , chunk_array : NDBuffer , chunk_spec : ArraySpec
124
130
) -> NDBuffer :
125
131
chunk_ndarray = chunk_array .as_ndarray_like ()
126
132
out = await to_thread (self ._codec .encode , chunk_ndarray )
127
- return NDBuffer .from_ndarray_like (out )
133
+ return chunk_spec . prototype . nd_buffer .from_ndarray_like (out )
128
134
129
135
130
136
def make_bytes_bytes_codec (
0 commit comments