Repository: dask/dask

Issue:
List indexing followed by newaxis indexing fails
``` python
import dask
import dask.array as da

da.set_options(get=dask.get)
x = da.zeros(shape=(5, 5), blockshape=(3, 3))
x[[0, 1]][None].compute()
```

```
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
```

I think I might need to actually write those futz tests for indexing :).



Discussion:
Hrm, what concerns me more is that two dask arrays with the same blockdims behave differently.  That breaks a decently strong dask.array principle.

``` python
In [35]: x[[0, 1]][None].compute()
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

     60         if not args:
---> 61             val = func(*results)
     62             if len(stack) == 1:

ipdb> print func
<built-in function getitem>
ipdb> print results
[array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]]), (None, (0, 1, slice(0, None, 1)), slice(0, None, None))]
```

Hrm, something isn't right with the slices there.  It looks like this is a problem with the fuse_slice optimization.  It's joining nested getitems in a case where it shouldn't.  Turning it off fixes the error.  I'll take a look at this tomorrow.


Resolving patch:
diff --git a/dask/array/slicing.py b/dask/array/slicing.py
--- a/dask/array/slicing.py
+++ b/dask/array/slicing.py
@@ -407,7 +407,7 @@ def take(outname, inname, blockdims, index, axis=0):
 
     colon = slice(None, None, None)
 
-    rev_index = tuple(map(sorted(index).index, index))
+    rev_index = list(map(sorted(index).index, index))
     vals = [(getitem, (np.concatenate,
                   (list, [(getitem, ((inname,) + d[:axis] + (i,) + d[axis+1:]),
                                    ((colon,)*axis + (IL,) + (colon,)*(n-axis-1)))


Test patch:
diff --git a/dask/array/tests/test_slicing.py b/dask/array/tests/test_slicing.py
--- a/dask/array/tests/test_slicing.py
+++ b/dask/array/tests/test_slicing.py
@@ -253,7 +253,7 @@ def test_take():
                 [(getitem, ('x', 0), ([1, 3, 5],)),
                  (getitem, ('x', 2), ([7],))]),
                0),
-             ((2, 0, 3, 1),))}
+             ([2, 0, 3, 1],))}
     assert result == expected
 
     result = take('y', 'x', [(20, 20, 20, 20), (20, 20)], [5, 1, 47, 3], axis=0)
@@ -263,7 +263,7 @@ def test_take():
                 [(getitem, ('x', 0, j), ([1, 3, 5], slice(None, None, None))),
                  (getitem, ('x', 2, j), ([7], slice(None, None, None)))]),
                 0),
-              ((2, 0, 3, 1), slice(None, None, None))))
+              ([2, 0, 3, 1], slice(None, None, None))))
             for j in range(2))
     assert result == expected
 
@@ -274,7 +274,7 @@ def test_take():
                 [(getitem, ('x', i, 0), (slice(None, None, None), [1, 3, 5])),
                  (getitem, ('x', i, 1), (slice(None, None, None), [17]))]),
                 1),
-             (slice(None, None, None), (2, 0, 3, 1))))
+             (slice(None, None, None), [2, 0, 3, 1])))
            for i in range(4))
 
     assert result == expected
@@ -295,7 +295,7 @@ def test_slice_lists():
                            ([0], slice(None, None, None))),
                            ]),
                         0),
-                       ((0, 1, 2), slice(None, None, None))))
+                       ([0, 1, 2], slice(None, None, None))))
                 for i in range(4))
 
     assert blockdims == ((3,), (3, 3, 3, 1))
@@ -343,3 +343,10 @@ def test_slice_stop_0():
     a = da.ones(10, blockshape=(10,))[:0].compute()
     b = np.ones(10)[:0]
     assert eq(a, b)
+
+
+def test_slice_list_then_None():
+    x = da.zeros(shape=(5, 5), blockshape=(3, 3))
+    y = x[[2, 1]][None]
+
+    assert eq(y, np.zeros((1, 2, 5)))