|
45 | 45 | if TYPE_CHECKING: |
46 | 46 | from zarr.abc.numcodec import Numcodec |
47 | 47 | from zarr.core.array_spec import ArraySpec |
48 | | - from zarr.core.buffer import Buffer, BufferPrototype, NDBuffer |
| 48 | + from zarr.core.buffer import Buffer, NDBuffer |
49 | 49 |
|
50 | 50 | CODEC_PREFIX = "numcodecs." |
51 | 51 |
|
@@ -132,53 +132,63 @@ class _NumcodecsBytesBytesCodec(_NumcodecsCodec, BytesBytesCodec): |
132 | 132 | def __init__(self, **codec_config: JSON) -> None: |
133 | 133 | super().__init__(**codec_config) |
134 | 134 |
|
135 | | - async def _decode_single(self, chunk_data: Buffer, chunk_spec: ArraySpec) -> Buffer: |
136 | | - return await asyncio.to_thread( |
137 | | - as_numpy_array_wrapper, |
138 | | - self._codec.decode, |
139 | | - chunk_data, |
140 | | - chunk_spec.prototype, |
141 | | - ) |
| 135 | + def _decode_sync(self, chunk_data: Buffer, chunk_spec: ArraySpec) -> Buffer: |
| 136 | + return as_numpy_array_wrapper(self._codec.decode, chunk_data, chunk_spec.prototype) |
142 | 137 |
|
143 | | - def _encode(self, chunk_data: Buffer, prototype: BufferPrototype) -> Buffer: |
| 138 | + def _encode_sync(self, chunk_data: Buffer, chunk_spec: ArraySpec) -> Buffer: |
144 | 139 | encoded = self._codec.encode(chunk_data.as_array_like()) |
145 | 140 | if isinstance(encoded, np.ndarray): # Required for checksum codecs |
146 | | - return prototype.buffer.from_bytes(encoded.tobytes()) |
147 | | - return prototype.buffer.from_bytes(encoded) |
| 141 | + return chunk_spec.prototype.buffer.from_bytes(encoded.tobytes()) |
| 142 | + return chunk_spec.prototype.buffer.from_bytes(encoded) |
| 143 | + |
| 144 | + async def _decode_single(self, chunk_data: Buffer, chunk_spec: ArraySpec) -> Buffer: |
| 145 | + return await asyncio.to_thread(self._decode_sync, chunk_data, chunk_spec) |
148 | 146 |
|
149 | 147 | async def _encode_single(self, chunk_data: Buffer, chunk_spec: ArraySpec) -> Buffer: |
150 | | - return await asyncio.to_thread(self._encode, chunk_data, chunk_spec.prototype) |
| 148 | + return await asyncio.to_thread(self._encode_sync, chunk_data, chunk_spec) |
151 | 149 |
|
152 | 150 |
|
153 | 151 | class _NumcodecsArrayArrayCodec(_NumcodecsCodec, ArrayArrayCodec): |
154 | 152 | def __init__(self, **codec_config: JSON) -> None: |
155 | 153 | super().__init__(**codec_config) |
156 | 154 |
|
157 | | - async def _decode_single(self, chunk_data: NDBuffer, chunk_spec: ArraySpec) -> NDBuffer: |
| 155 | + def _decode_sync(self, chunk_data: NDBuffer, chunk_spec: ArraySpec) -> NDBuffer: |
158 | 156 | chunk_ndarray = chunk_data.as_ndarray_like() |
159 | | - out = await asyncio.to_thread(self._codec.decode, chunk_ndarray) |
| 157 | + out = self._codec.decode(chunk_ndarray) |
160 | 158 | return chunk_spec.prototype.nd_buffer.from_ndarray_like(out.reshape(chunk_spec.shape)) |
161 | 159 |
|
162 | | - async def _encode_single(self, chunk_data: NDBuffer, chunk_spec: ArraySpec) -> NDBuffer: |
| 160 | + def _encode_sync(self, chunk_data: NDBuffer, chunk_spec: ArraySpec) -> NDBuffer: |
163 | 161 | chunk_ndarray = chunk_data.as_ndarray_like() |
164 | | - out = await asyncio.to_thread(self._codec.encode, chunk_ndarray) |
| 162 | + out = self._codec.encode(chunk_ndarray) |
165 | 163 | return chunk_spec.prototype.nd_buffer.from_ndarray_like(out) |
166 | 164 |
|
| 165 | + async def _decode_single(self, chunk_data: NDBuffer, chunk_spec: ArraySpec) -> NDBuffer: |
| 166 | + return await asyncio.to_thread(self._decode_sync, chunk_data, chunk_spec) |
| 167 | + |
| 168 | + async def _encode_single(self, chunk_data: NDBuffer, chunk_spec: ArraySpec) -> NDBuffer: |
| 169 | + return await asyncio.to_thread(self._encode_sync, chunk_data, chunk_spec) |
| 170 | + |
167 | 171 |
|
168 | 172 | class _NumcodecsArrayBytesCodec(_NumcodecsCodec, ArrayBytesCodec): |
169 | 173 | def __init__(self, **codec_config: JSON) -> None: |
170 | 174 | super().__init__(**codec_config) |
171 | 175 |
|
172 | | - async def _decode_single(self, chunk_data: Buffer, chunk_spec: ArraySpec) -> NDBuffer: |
| 176 | + def _decode_sync(self, chunk_data: Buffer, chunk_spec: ArraySpec) -> NDBuffer: |
173 | 177 | chunk_bytes = chunk_data.to_bytes() |
174 | | - out = await asyncio.to_thread(self._codec.decode, chunk_bytes) |
| 178 | + out = self._codec.decode(chunk_bytes) |
175 | 179 | return chunk_spec.prototype.nd_buffer.from_ndarray_like(out.reshape(chunk_spec.shape)) |
176 | 180 |
|
177 | | - async def _encode_single(self, chunk_data: NDBuffer, chunk_spec: ArraySpec) -> Buffer: |
| 181 | + def _encode_sync(self, chunk_data: NDBuffer, chunk_spec: ArraySpec) -> Buffer: |
178 | 182 | chunk_ndarray = chunk_data.as_ndarray_like() |
179 | | - out = await asyncio.to_thread(self._codec.encode, chunk_ndarray) |
| 183 | + out = self._codec.encode(chunk_ndarray) |
180 | 184 | return chunk_spec.prototype.buffer.from_bytes(out) |
181 | 185 |
|
| 186 | + async def _decode_single(self, chunk_data: Buffer, chunk_spec: ArraySpec) -> NDBuffer: |
| 187 | + return await asyncio.to_thread(self._decode_sync, chunk_data, chunk_spec) |
| 188 | + |
| 189 | + async def _encode_single(self, chunk_data: NDBuffer, chunk_spec: ArraySpec) -> Buffer: |
| 190 | + return await asyncio.to_thread(self._encode_sync, chunk_data, chunk_spec) |
| 191 | + |
182 | 192 |
|
183 | 193 | # bytes-to-bytes codecs |
184 | 194 | class Blosc(_NumcodecsBytesBytesCodec, codec_name="blosc"): |
|
0 commit comments