|
12 | 12 | from zarr.abc.numcodec import _is_numcodec, _is_numcodec_cls |
13 | 13 | from zarr.codecs import numcodecs as _numcodecs |
14 | 14 | from zarr.codecs.transpose import TransposeCodec |
| 15 | +from zarr.core.array_spec import ArrayConfig, ArraySpec |
| 16 | +from zarr.core.buffer import default_buffer_prototype |
| 17 | +from zarr.core.dtype import get_data_type_from_native_dtype |
15 | 18 | from zarr.errors import UnknownCodecError |
16 | 19 | from zarr.registry import get_codec_class, get_numcodec |
17 | 20 |
|
@@ -214,22 +217,38 @@ def test_generic_filter_flattened_output_reshaped() -> None: |
214 | 217 | np.testing.assert_array_equal(data, b[:, :]) |
215 | 218 |
|
216 | 219 |
|
217 | | -def test_generic_filter_packedbits_output_shape() -> None: |
218 | | - # PackBits changes the size of the chunk, so `_encode_sync` must reshape its |
219 | | - # output to the shape from `resolve_metadata`, not the input chunk shape |
220 | | - data = np.tril(np.ones((16, 16), dtype=bool)) |
221 | | - |
222 | | - a = create_array( |
223 | | - {}, |
224 | | - shape=data.shape, |
225 | | - chunks=(16, 16), |
226 | | - dtype=data.dtype, |
227 | | - fill_value=0, |
228 | | - filters=[_numcodecs.PackBits()], |
| 220 | +@pytest.mark.parametrize( |
| 221 | + ("codec", "dtype", "shape"), |
| 222 | + [ |
| 223 | + (_numcodecs.Delta(dtype="int64"), "int64", (4, 6)), |
| 224 | + (_numcodecs.FixedScaleOffset(offset=0, scale=1), "int64", (4, 6)), |
| 225 | + (_numcodecs.Quantize(digits=3), "float32", (4, 6)), |
| 226 | + (_numcodecs.BitRound(keepbits=10), "float32", (4, 6)), |
| 227 | + (_numcodecs.AsType(encode_dtype="float32", decode_dtype="float64"), "float64", (4, 6)), |
| 228 | + (_numcodecs.PackBits(), "bool", (16, 16)), |
| 229 | + # the packed length (1 + ceil(2 / 8) == 2) equals the chunk size |
| 230 | + (_numcodecs.PackBits(), "bool", (1, 2)), |
| 231 | + ], |
| 232 | + ids=["delta", "fixedscaleoffset", "quantize", "bitround", "astype", "packbits", "packbits-2"], |
| 233 | +) |
| 234 | +def test_generic_filter_encoded_shape( |
| 235 | + codec: _numcodecs._NumcodecsArrayArrayCodec, dtype: str, shape: tuple[int, ...] |
| 236 | +) -> None: |
| 237 | + # gh-4553: the next codec receives the chunk shape that resolve_metadata declares |
| 238 | + zdtype = get_data_type_from_native_dtype(np.dtype(dtype)) |
| 239 | + spec = ArraySpec( |
| 240 | + shape=shape, |
| 241 | + dtype=zdtype, |
| 242 | + fill_value=zdtype.cast_scalar(0), |
| 243 | + config=ArrayConfig(order="C", write_empty_chunks=True), |
| 244 | + prototype=default_buffer_prototype(), |
229 | 245 | ) |
| 246 | + codec = codec.evolve_from_array_spec(spec) |
| 247 | + chunk = default_buffer_prototype().nd_buffer.from_ndarray_like(np.ones(shape, dtype=dtype)) |
230 | 248 |
|
231 | | - a[:, :] = data |
232 | | - np.testing.assert_array_equal(data, a[:, :]) |
| 249 | + encoded = codec._encode_sync(chunk, spec) |
| 250 | + |
| 251 | + assert encoded.shape == codec.resolve_metadata(spec).shape |
233 | 252 |
|
234 | 253 |
|
235 | 254 | def test_generic_filter_bitround() -> None: |
|
0 commit comments