Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: selection with zarr arrays #2137

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/zarr/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
IntSequence = list[int] | npt.NDArray[np.intp]
ArrayOfIntOrBool = npt.NDArray[np.intp] | npt.NDArray[np.bool_]
BasicSelector = int | slice | EllipsisType
Selector = BasicSelector | ArrayOfIntOrBool
Selector = (
BasicSelector | ArrayOfIntOrBool | Array
) # help! we want zarr.Array[np.intp] | zarr.Array[np.bool_]
Comment on lines +37 to +39
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dstansby and @normanrz - I believe we want something like I've written here but am not clear if we can do this yet. Any ideas?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a technical reason why it isn't possible to make Array a generic type over data types.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

happy to do this, can you point me in the right direction?


BasicSelection = BasicSelector | tuple[BasicSelector, ...] # also used for BlockIndex
CoordinateSelection = IntSequence | tuple[IntSequence, ...]
Expand Down Expand Up @@ -839,6 +841,10 @@ class OIndex:
array: Array

def __getitem__(self, selection: OrthogonalSelection) -> NDArrayLike:
from zarr.core.array import Array

if isinstance(selection, Array):
selection = np.asarray(selection)
fields, new_selection = pop_fields(selection)
new_selection = ensure_tuple(new_selection)
new_selection = replace_lists(new_selection)
Expand Down Expand Up @@ -1127,6 +1133,10 @@ class VIndex:
array: Array

def __getitem__(self, selection: CoordinateSelection | MaskSelection) -> NDArrayLike:
from zarr.core.array import Array

if isinstance(selection, Array):
selection = np.asarray(selection)
fields, new_selection = pop_fields(selection)
new_selection = ensure_tuple(new_selection)
new_selection = replace_lists(new_selection)
Expand Down
13 changes: 13 additions & 0 deletions tests/v3/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1780,3 +1780,16 @@ def test_orthogonal_bool_indexing_like_numpy_ix(store, selection):
# note: in python 3.10 z[*selection] is not valid unpacking syntax
actual = z[(*selection,)]
assert_array_equal(expected, actual, err_msg=f"{selection=}")


def test_indexing_with_zarr_array(store) -> None:
# regression test for https://github.com/zarr-developers/zarr-python/issues/2133
a = np.arange(10)
za = zarr.array(a, chunks=2)
ix = [False, True, False, True, False, True, False, True, False, True]

zix = zarr.array(ix, chunks=2)
za = zarr.array(a, chunks=2)
assert_array_equal(a[ix], za[zix])
assert_array_equal(a[ix], za.oindex[zix])
assert_array_equal(a[ix], za.vindex[zix])
Loading