Skip to content

Commit 6e23c22

Browse files
author
Luke Shaw
committed
Edited NDarray slice to use original codec/clevel for recompression by default
1 parent 200cea4 commit 6e23c22

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/blosc2/ndarray.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,11 @@ def slice(self, key: int | slice | Sequence[slice], **kwargs: Any) -> NDArray:
18371837
>>> print(type(c))
18381838
<class 'blosc2.ndarray.NDArray'>
18391839
"""
1840-
kwargs = _check_ndarray_kwargs(**kwargs)
1840+
original_codec = self.cparams.codec
1841+
original_clevel = self.cparams.clevel
1842+
if "cparams" not in kwargs:
1843+
kwargs["cparams"] = {"codec": original_codec, "clevel": original_clevel}
1844+
kwargs = _check_ndarray_kwargs(**kwargs) # sets cparams to defaults
18411845
key, mask = process_key(key, self.shape)
18421846
start, stop, step = get_ndarray_start_stop(self.ndim, key, self.shape)
18431847
key = (start, stop)

tests/ndarray/test_slice.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ def test_slice(shape, chunks, blocks, slices, dtype):
3030
np.testing.assert_almost_equal(b[...], np_slice)
3131

3232

33+
@pytest.mark.parametrize(argnames, argvalues)
34+
def test_slice_codec_and_clevel(shape, chunks, blocks, slices, dtype):
35+
size = int(np.prod(shape))
36+
nparray = np.arange(size, dtype=dtype).reshape(shape)
37+
a = blosc2.asarray(
38+
nparray, chunks=chunks, blocks=blocks, cparams={"codec": blosc2.Codec.LZ4, "clevel": 6}
39+
)
40+
41+
b = a.slice(slices)
42+
assert b.cparams.codec == a.cparams.codec
43+
assert b.cparams.clevel == a.cparams.clevel
44+
45+
3346
argnames = "shape, chunks, blocks, slices, dtype, chunks2, blocks2"
3447
argvalues = [
3548
([456], [258], [73], slice(0, 1), np.int32, [1], [1]),

0 commit comments

Comments
 (0)