Repository: scipy/scipy

Issue:
BUG: indexing error for sparse matrix with ix_
This is a rare bug which accurs when I tried to visit a block of size 1x1(assignment is ok) with ix_.

``` javascript
In [85]: d=csr_matrix((4,4))
In [86]: c=ix_(arange(1),arange(1))
In [87]: d[c]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-87-902607ad2243> in <module>()
----> 1 d[c]
/usr/local/lib/python2.7/dist-packages/scipy/sparse/csr.pyc in __getitem__(self, key)
    267             # [i, j]
    268             if isintlike(col):
--> 269                 return self._get_single_element(row, col)
    270             # [i, 1:2]
    271             elif isinstance(col, slice):

/usr/local/lib/python2.7/dist-packages/scipy/sparse/compressed.pyc in _get_single_element(self, row, col)
    826         # can use np.add(..., where) from numpy 1.7
    827         return np.compress(minor_index == self.indices[start:end],
--> 828                            self.data[start:end]).sum(dtype=self.dtype)
    829 
    830     def _get_slice(self, i, start, stop, stride, shape):

/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.pyc in compress(condition, a, axis, out)
   1559     except AttributeError:
   1560         return _wrapit(a, 'compress', condition, axis, out)
-> 1561     return compress(condition, axis, out)
   1562 
   1563 

ValueError: condition must be a 1-d array

In [88]: d[c]=2
In [89]: c=ix_(arange(2),arange(2))
In [90]: d[c]
Out[90]: 
<2x2 sparse matrix of type '<type 'numpy.float64'>'
    with 1 stored elements in Compressed Sparse Row format>
```



Discussion:
This comes down to 

```
In [21]: d[0, 0]
Out[21]: 0.0

In [22]: d[([[0]], [[0]])]
Out[22]: 
<1x1 sparse matrix of type '<type 'numpy.float64'>'
        with 0 stored elements in Compressed Sparse Row format>

In [23]: d[(array([[0]]), array([[0]]))]
---------------------------------------------------------------------------
ValueError 
```

So you're indexing with a tuple of 2-D arrays. It's supposed to work, but indeed a bit of a corner case.

it works as long as the constituent arrays are not 1x1 size. I can confirm the bug with 0.18.1
Additionally `sparse.csr_matrix((3, 3))[[[]], [[]]]` returns a 1x0 `csr_matrix` instead of 0x0. Seems to be a related problem.

Resolving patch:
diff --git a/scipy/sparse/sputils.py b/scipy/sparse/sputils.py
--- a/scipy/sparse/sputils.py
+++ b/scipy/sparse/sputils.py
@@ -189,7 +189,7 @@ def isintlike(x):
     """Is x appropriate as an index into a sparse matrix? Returns True
     if it can be cast safely to a machine int.
     """
-    if issequence(x):
+    if not isscalarlike(x):
         return False
     try:
         return bool(int(x) == x)


Test patch:
diff --git a/scipy/sparse/tests/test_base.py b/scipy/sparse/tests/test_base.py
--- a/scipy/sparse/tests/test_base.py
+++ b/scipy/sparse/tests/test_base.py
@@ -2645,6 +2645,13 @@ def test_fancy_indexing(self):
         assert_equal(A[s,:].todense(), B[2:4,:])
         assert_equal(A[:,s].todense(), B[:,2:4])
 
+        # Regression for gh-4917: index with tuple of 2D arrays
+        i = np.array([[1]], dtype=int)
+        assert_equal(A[i,i].todense(), B[i,i])
+
+        # Regression for gh-4917: index with tuple of empty nested lists
+        assert_equal(A[[[]], [[]]].todense(), B[[[]], [[]]])
+
     def test_fancy_indexing_randomized(self):
         np.random.seed(1234)  # make runs repeatable