Skip to content

Mask based indexing works when the mask is a list, but not a numpy array #1490

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

Closed
ivirshup opened this issue Aug 7, 2023 · 2 comments
Closed
Labels
bug Potential issues with the zarr-python library

Comments

@ivirshup
Copy link

ivirshup commented Aug 7, 2023

Zarr version

2.16.1.dev4,2.15.0

Numcodecs version

0.10.2

Python Version

3.9.16,3.10.12

Operating System

Mac

Installation

Dev environment + pip install from pypi

Description

Indexing with a boolean mask doesn't work if the mask is a numpy array. I expect this to work because the behavior is available for python lists, and has an implementation in zarr/indexing.py

Steps to reproduce

import numpy as np
import zarr

z = zarr.array(np.arange(6).reshape((3, 2)))


z[[True, False, True], :]
# array([[0, 1],
#        [4, 5]])

That works, this doesn't:

z[np.array([True, False, True]), :]
File ~/github/zarr-python/zarr/indexing.py:348, in BasicIndexer.__init__(self, selection, array)
    345         dim_indexer = SliceDimIndexer(dim_sel, dim_len, dim_chunk_len)
    347     else:
--> 348         raise IndexError(
    349             "unsupported selection item for basic indexing; "
    350             "expected integer or slice, got {!r}".format(type(dim_sel))
    351         )
    353     dim_indexers.append(dim_indexer)
    355 self.dim_indexers = dim_indexers

IndexError: unsupported selection item for basic indexing; expected integer or slice, got <class 'numpy.ndarray'>
Full traceback
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[5], line 1
----> 1 z[np.array([True, False, True]), :]

File ~/github/zarr-python/zarr/core.py:844, in Array.__getitem__(self, selection)
    842     result = self.get_orthogonal_selection(pure_selection, fields=fields)
    843 else:
--> 844     result = self.get_basic_selection(pure_selection, fields=fields)
    845 return result

File ~/github/zarr-python/zarr/core.py:970, in Array.get_basic_selection(self, selection, out, fields)
    968     return self._get_basic_selection_zd(selection=selection, out=out, fields=fields)
    969 else:
--> 970     return self._get_basic_selection_nd(selection=selection, out=out, fields=fields)

File ~/github/zarr-python/zarr/core.py:1010, in Array._get_basic_selection_nd(self, selection, out, fields)
   1006 def _get_basic_selection_nd(self, selection, out=None, fields=None):
   1007     # implementation of basic selection for array with at least one dimension
   1008 
   1009     # setup indexer
-> 1010     indexer = BasicIndexer(selection, self)
   1012     return self._get_selection(indexer=indexer, out=out, fields=fields)

File ~/github/zarr-python/zarr/indexing.py:348, in BasicIndexer.__init__(self, selection, array)
    345         dim_indexer = SliceDimIndexer(dim_sel, dim_len, dim_chunk_len)
    347     else:
--> 348         raise IndexError(
    349             "unsupported selection item for basic indexing; "
    350             "expected integer or slice, got {!r}".format(type(dim_sel))
    351         )
    353     dim_indexers.append(dim_indexer)
    355 self.dim_indexers = dim_indexers

IndexError: unsupported selection item for basic indexing; expected integer or slice, got <class 'numpy.ndarray'>

Additional output

This is also the case for 1 boolean arrays:

zarr.array(np.arange(3))[np.array([True, False, True])]
# IndexError: unsupported selection item for basic indexing; expected integer or slice, got <class 'numpy.ndarray'>
Full traceback
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[18], line 1
----> 1 zarr.array(np.arange(3))[np.array([True, False, True])]

File ~/github/zarr-python/zarr/core.py:844, in Array.__getitem__(self, selection)
    842     result = self.get_orthogonal_selection(pure_selection, fields=fields)
    843 else:
--> 844     result = self.get_basic_selection(pure_selection, fields=fields)
    845 return result

File ~/github/zarr-python/zarr/core.py:970, in Array.get_basic_selection(self, selection, out, fields)
    968     return self._get_basic_selection_zd(selection=selection, out=out, fields=fields)
    969 else:
--> 970     return self._get_basic_selection_nd(selection=selection, out=out, fields=fields)

File ~/github/zarr-python/zarr/core.py:1010, in Array._get_basic_selection_nd(self, selection, out, fields)
   1006 def _get_basic_selection_nd(self, selection, out=None, fields=None):
   1007     # implementation of basic selection for array with at least one dimension
   1008 
   1009     # setup indexer
-> 1010     indexer = BasicIndexer(selection, self)
   1012     return self._get_selection(indexer=indexer, out=out, fields=fields)

File ~/github/zarr-python/zarr/indexing.py:348, in BasicIndexer.__init__(self, selection, array)
    345         dim_indexer = SliceDimIndexer(dim_sel, dim_len, dim_chunk_len)
    347     else:
--> 348         raise IndexError(
    349             "unsupported selection item for basic indexing; "
    350             "expected integer or slice, got {!r}".format(type(dim_sel))
    351         )
    353     dim_indexers.append(dim_indexer)
    355 self.dim_indexers = dim_indexers

IndexError: unsupported selection item for basic indexing; expected integer or slice, got <class 'numpy.ndarray'>

And I can get an error message telling me that mask based indexing is valid:

z = zarr.array(np.arange(6).reshape((3, 2)))
z[[True, False, True], np.array([1])]
VindexInvalidSelectionError: unsupported selection type for vectorized indexing; only coordinate selection 
(tuple of integer arrays) and mask selection (single Boolean array) are supported; got 
(array([ True, False,  True]), array([1]))
@ivirshup ivirshup added the bug Potential issues with the zarr-python library label Aug 7, 2023
@joshmoore
Copy link
Member

+1: Seems like widening the isinstance in https://github.com/zarr-developers/zarr-python/blob/main/zarr/indexing.py#L33 would be straight-forward.

cc: @jni re 7c31f04

@jhamman
Copy link
Member

jhamman commented Oct 18, 2024

This seems to have been fixed in the v3 refactor. Feel free to reopen if you hit this on 3.0.0.beta or later.

@jhamman jhamman closed this as completed Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Potential issues with the zarr-python library
Projects
None yet
Development

No branches or pull requests

3 participants