{"action":"created","issue":{"url":"https://api.github.com/repos/pydata/sparse/issues/304","repository_url":"https://api.github.com/repos/pydata/sparse","labels_url":"https://api.github.com/repos/pydata/sparse/issues/304/labels{/name}","comments_url":"https://api.github.com/repos/pydata/sparse/issues/304/comments","events_url":"https://api.github.com/repos/pydata/sparse/issues/304/events","html_url":"https://github.com/pydata/sparse/issues/304","id":527114329,"node_id":"MDU6SXNzdWU1MjcxMTQzMjk=","number":304,"title":"Support boolean indexing with a COO sparse matrix","user":{"login":"tomwhite","id":85085,"node_id":"MDQ6VXNlcjg1MDg1","avatar_url":"https://avatars3.githubusercontent.com/u/85085?v=4","gravatar_id":"","url":"https://api.github.com/users/tomwhite","html_url":"https://github.com/tomwhite","followers_url":"https://api.github.com/users/tomwhite/followers","following_url":"https://api.github.com/users/tomwhite/following{/other_user}","gists_url":"https://api.github.com/users/tomwhite/gists{/gist_id}","starred_url":"https://api.github.com/users/tomwhite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tomwhite/subscriptions","organizations_url":"https://api.github.com/users/tomwhite/orgs","repos_url":"https://api.github.com/users/tomwhite/repos","events_url":"https://api.github.com/users/tomwhite/events{/privacy}","received_events_url":"https://api.github.com/users/tomwhite/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2019-11-22T10:29:17Z","updated_at":"2019-11-22T10:45:59Z","closed_at":null,"author_association":"NONE","body":"While investigating https://github.com/theislab/scanpy/issues/921 I ran into the following limitation when using a boolean array as an index.\r\n\r\nThis is the expected behaviour for regular NumPy:\r\n\r\n```python\r\n>>> import numpy as np\r\nx = np.array([1, 2, 3, 4])\r\n>>> x[x % 2 == 0]\r\narray([2, 4])\r\n```\r\nBut the same thing fails for sparse:\r\n\r\n```python\r\n>>> import sparse\r\n>>> s = sparse.COO.from_numpy(x)\r\n>>> s\r\n<COO: shape=(4,), dtype=int64, nnz=4, fill_value=0>\r\n>>> [s % 2 == 0]\r\n[<COO: shape=(4,), dtype=bool, nnz=2, fill_value=True>]\r\n>>> s[s % 2 == 0]\r\n<class 'sparse._coo.core.COO'>\r\nTraceback (most recent call last):\r\n  File \"<stdin>\", line 1, in <module>\r\n  File \"/Users/tom/workspace/scanpy/venv/lib/python3.7/site-packages/sparse/_coo/indexing.py\", line 60, in getitem\r\n    index = normalize_index(index, x.shape)\r\n  File \"/Users/tom/workspace/scanpy/venv/lib/python3.7/site-packages/sparse/_slicing.py\", line 58, in normalize_index\r\n    check_index(i, d)\r\n  File \"/Users/tom/workspace/scanpy/venv/lib/python3.7/site-packages/sparse/_slicing.py\", line 123, in check_index\r\n    raise IndexError(\"only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and \"\r\nIndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices\r\n```\r\nIt 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."},"comment":{"url":"https://api.github.com/repos/pydata/sparse/issues/comments/557484632","html_url":"https://github.com/pydata/sparse/issues/304#issuecomment-557484632","issue_url":"https://api.github.com/repos/pydata/sparse/issues/304","id":557484632,"node_id":"MDEyOklzc3VlQ29tbWVudDU1NzQ4NDYzMg==","user":{"login":"hameerabbasi","id":2190658,"node_id":"MDQ6VXNlcjIxOTA2NTg=","avatar_url":"https://avatars3.githubusercontent.com/u/2190658?v=4","gravatar_id":"","url":"https://api.github.com/users/hameerabbasi","html_url":"https://github.com/hameerabbasi","followers_url":"https://api.github.com/users/hameerabbasi/followers","following_url":"https://api.github.com/users/hameerabbasi/following{/other_user}","gists_url":"https://api.github.com/users/hameerabbasi/gists{/gist_id}","starred_url":"https://api.github.com/users/hameerabbasi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hameerabbasi/subscriptions","organizations_url":"https://api.github.com/users/hameerabbasi/orgs","repos_url":"https://api.github.com/users/hameerabbasi/repos","events_url":"https://api.github.com/users/hameerabbasi/events{/privacy}","received_events_url":"https://api.github.com/users/hameerabbasi/received_events","type":"User","site_admin":false},"created_at":"2019-11-22T10:45:59Z","updated_at":"2019-11-22T10:45:59Z","author_association":"COLLABORATOR","body":"This works:\r\n```python\r\nimport sparse\r\nimport numpy as np\r\nx = np.array([1, 2, 3, 4])\r\ns = sparse.COO.from_numpy(x)\r\ns % 2 == 0\r\n<COO: shape=(4,), dtype=bool, nnz=2, fill_value=True>\r\ns[(s % 2 == 0).todense()]\r\n<COO: shape=(2,), dtype=int64, nnz=2, fill_value=0>\r\n```\r\n\r\nBut 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."}}
{"actor":{"display_login":"hameerabbasi"}}