Skip to content

Support boolean indexing with a COO sparse matrix #304

Closed

Description

@tomwhite

While investigating scverse/scanpy#921 I ran into the following limitation when using a boolean array as an index.

This is the expected behaviour for regular NumPy:

>>> import numpy as np
x = np.array([1, 2, 3, 4])
>>> x[x % 2 == 0]
array([2, 4])

But the same thing fails for sparse:

>>> import sparse
>>> s = sparse.COO.from_numpy(x)
>>> s
<COO: shape=(4,), dtype=int64, nnz=4, fill_value=0>
>>> [s % 2 == 0]
[<COO: shape=(4,), dtype=bool, nnz=2, fill_value=True>]
>>> s[s % 2 == 0]
<class 'sparse._coo.core.COO'>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/tom/workspace/scanpy/venv/lib/python3.7/site-packages/sparse/_coo/indexing.py", line 60, in getitem
    index = normalize_index(index, x.shape)
  File "/Users/tom/workspace/scanpy/venv/lib/python3.7/site-packages/sparse/_slicing.py", line 58, in normalize_index
    check_index(i, d)
  File "/Users/tom/workspace/scanpy/venv/lib/python3.7/site-packages/sparse/_slicing.py", line 123, in check_index
    raise IndexError("only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and "
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

It would be good to fix it for GCXS if that's a problem too. I couldn't reproduce it there since from sparse import GCXS fails.

Activity

  1. hameerabbasi commented on Nov 22, 2019

    @hameerabbasi
    Collaborator

    This works:

    import sparse
    import numpy as np
    x = np.array([1, 2, 3, 4])
    s = sparse.COO.from_numpy(x)
    s % 2 == 0
    <COO: shape=(4,), dtype=bool, nnz=2, fill_value=True>
    s[(s % 2 == 0).todense()]
    <COO: shape=(2,), dtype=int64, nnz=2, fill_value=0>

    But when you do s % 2, that leads to an array with an FV of 0, and doing it with s % 2 == 0 leads to a FV of True. This basically means every 0 in x is transformed to True. I could add this sort of indexing for a fill-value of False, if that covers your use-cases. But with a FV of True, you're filtering out very little, so being explicit about it is nice vs having an explosion of memory.

  2. hameerabbasi commented on Nov 22, 2019

    @hameerabbasi
    Collaborator

    You could also do this:

    s[np.nonzero(s % 2)]
    <COO: shape=(2,), dtype=int64, nnz=2, fill_value=0>

    Which is just as fast and works across everything.

  3. tomwhite commented on Nov 22, 2019

    @tomwhite
    Author

    The modulo operation was just an example - the actual operation is checking s[s > min_number] for a fixed min_number value. The workarounds are fine, but it's not always easy to use them in code that also supports regular arrays. It should be possible to support boolean indexing with a COO sparse matrix though, without converting to the dense representation, shouldn't it?

  4. hameerabbasi commented on Nov 22, 2019

    @hameerabbasi
    Collaborator

    It’s easy to add it for 1D, although for ND we would have to revamp a lot of things.

  5. tomwhite commented on Nov 22, 2019

    @tomwhite
    Author

    Good to know. Thanks!

  6. jakirkham commented on Nov 22, 2019

    @jakirkham

    It’s easy to add it for 1D, although for ND we would have to revamp a lot of things.

    Could you ravel both the array and the mask before selection? Would this make things more tractable?

  7. hameerabbasi commented on Nov 22, 2019

    @hameerabbasi
    Collaborator

    Could you ravel both the array and the mask before selection? Would this make things more tractable?

    ravel/reshape isn't O(1) for COO. It's O(n).

  8. jakirkham commented on Nov 23, 2019

    @jakirkham

    So will applying the mask to select things, no?

  9. hameerabbasi commented on Nov 23, 2019

    @hameerabbasi
    Collaborator

    If the mask is sparser than the original array... That could blow up complexity.

  10. added
    enhancement
    on Jul 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions