Skip to content

Commit

Permalink
NDArray.__getitem__ accepts (1-dim) NDArray as keys
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescAlted committed Nov 27, 2024
1 parent 504dbff commit 9349182
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/blosc2/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,8 @@ def blocksize(self) -> int:
return self._schunk.blocksize

def __getitem__( # noqa: C901
self, key: int | slice | Sequence[slice | int] | np.ndarray[np.bool_] | blosc2.LazyExpr | str
self,
key: int | slice | Sequence[slice | int] | np.ndarray[np.bool_] | NDArray | blosc2.LazyExpr | str,
) -> np.ndarray | blosc2.LazyExpr:
"""Retrieve a (multidimensional) slice as specified by the key.
Expand Down
15 changes: 15 additions & 0 deletions tests/ndarray/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# This source code is licensed under a BSD-style license (found in the
# LICENSE file in the root directory of this source tree)
#######################################################################
import math

import numpy as np
import pytest
Expand Down Expand Up @@ -142,3 +143,17 @@ def test_iter(shape, chunks, blocks):
for _i, (a, b) in enumerate(zip(b2a, npa, strict=False)):
np.testing.assert_equal(a, b)
assert _i == shape[0] - 1


@pytest.mark.parametrize("dtype", [np.int32, np.float32, np.float64])
def test_ndarray(dtype):
# Check that we can slice a blosc2 array with a NDArray
shape = (10,)
size = math.prod(shape)
ndarray = blosc2.arange(size - 1, -1, -1, dtype=np.int64, shape=shape)
a = blosc2.linspace(0, 10, size, shape=shape, dtype=dtype)
a_slice = a[ndarray]
na = np.linspace(0, 10, size, dtype=dtype).reshape(shape)
nparray = np.arange(size - 1, -1, -1, dtype=np.int64).reshape(shape)
na_slice = na[nparray]
np.testing.assert_almost_equal(a_slice, na_slice)

0 comments on commit 9349182

Please sign in to comment.