Repository: dask/dask

Issue:
dask.array.stack problem since 0.18
dask.stack is failing for me with arrays made with 'from_array(..., asarray=False)', and anything other than whole-array chunks.
At this point, I can't be more general than that.

Short example:
```
>>> import dask
>>> import dask.array as da
>>> import numpy as np
>>> 
>>> print('dask version = ', dask.__version__)
('dask version = ', u'0.19.2')
>>> 
>>> a1 = da.from_array(np.zeros(3,), chunks=1, asarray=False)
>>> a2 = da.from_array(np.ones(3,), chunks=1, asarray=False)
>>> al = da.stack([a1, a2])
>>> al.compute()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/local/itpp/miniconda/envs/dask0x19/lib/python2.7/site-packages/dask/base.py", line 156, in compute
    (result,) = compute(self, traverse=False, **kwargs)
  File "/data/local/itpp/miniconda/envs/dask0x19/lib/python2.7/site-packages/dask/base.py", line 395, in compute
    results = schedule(dsk, keys, **kwargs)
  File "/data/local/itpp/miniconda/envs/dask0x19/lib/python2.7/site-packages/dask/threaded.py", line 75, in get
    pack_exception=pack_exception, **kwargs)
  File "/data/local/itpp/miniconda/envs/dask0x19/lib/python2.7/site-packages/dask/local.py", line 501, in get_async
    raise_exception(exc, tb)
  File "/data/local/itpp/miniconda/envs/dask0x19/lib/python2.7/site-packages/dask/local.py", line 272, in execute_task
    result = _execute_task(task, data)
  File "/data/local/itpp/miniconda/envs/dask0x19/lib/python2.7/site-packages/dask/local.py", line 252, in _execute_task
    args2 = [_execute_task(a, cache) for a in args]
  File "/data/local/itpp/miniconda/envs/dask0x19/lib/python2.7/site-packages/dask/local.py", line 253, in _execute_task
    return func(*args2)
TypeError: op_getitem expected 2 arguments, got 4
>>>
```

Notes:
  * it works ok if you remove the 'asarray=False'
    * ( the 'asarray=False' operation is important to us in https://github.com/SciTools/iris )
  * it works ok with chunks == array.shape, but not anything smaller (that I have yet tried)
  * it works ok at 0.17.1, but not with 0.18.1, 0.18.2 or 0.19.2




Discussion:

Resolving patch:
diff --git a/dask/array/core.py b/dask/array/core.py
--- a/dask/array/core.py
+++ b/dask/array/core.py
@@ -183,7 +183,7 @@ def getem(arr, chunks, getitem=getter, shape=None, out_name=None, lock=False,
     keys = list(product([out_name], *[range(len(bds)) for bds in chunks]))
     slices = slices_from_chunks(chunks)
 
-    if not asarray or lock:
+    if getitem is not operator.getitem and (not asarray or lock):
         values = [(getitem, arr, x, asarray, lock) for x in slices]
     else:
         # Common case, drop extra parameters
@@ -2389,7 +2389,7 @@ def from_array(x, chunks, name=None, lock=False, asarray=True, fancy=True,
         dsk = {(name, ) + (0, ) * x.ndim: x}
     else:
         if getitem is None:
-            if type(x) is np.ndarray:
+            if type(x) is np.ndarray and not lock:
                 # simpler and cleaner, but missing all the nuances of getter
                 getitem = operator.getitem
             elif fancy:


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
@@ -820,3 +820,13 @@ def test_setitem_with_different_chunks_preserves_shape(params):
 def test_gh3579():
     assert_eq(np.arange(10)[0::-1], da.arange(10, chunks=3)[0::-1])
     assert_eq(np.arange(10)[::-1], da.arange(10, chunks=3)[::-1])
+
+
+@pytest.mark.parametrize('lock', [True, False])
+@pytest.mark.parametrize('asarray', [True, False])
+@pytest.mark.parametrize('fancy', [True, False])
+def test_gh4043(lock, asarray, fancy):
+    a1 = da.from_array(np.zeros(3,), chunks=1, asarray=asarray, lock=lock, fancy=fancy)
+    a2 = da.from_array(np.ones(3,), chunks=1, asarray=asarray, lock=lock, fancy=fancy)
+    al = da.stack([a1, a2])
+    assert_eq(al, al)