Repository navigation
Closed
Labels
Description
An example
>>> x = np.arange(12).reshape(4, 3)
>>> y = np.array([1, 1, 2, 3])
>>> dx = da.from_array(x, chunks=2)
>>> dy = da.from_array(y, chunks=2)
>>> x[y == 1] # NumPy
array([[0, 1, 2],
[3, 4, 5]])
In [43]: dx[dy == 1]---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-43-9a0439f73673> in <module>()
----> 1 dx[dy == 1]
/Users/taugspurger/Envs/dask-dev/lib/python3.6/site-packages/dask/dask/array/core.py in __getitem__(self, index)
1196 # Slicing
1197 if isinstance(index, Array):
-> 1198 return slice_with_dask_array(self, index)
1199
1200 if not isinstance(index, tuple):
/Users/taugspurger/Envs/dask-dev/lib/python3.6/site-packages/dask/dask/array/core.py in slice_with_dask_array(x, index)
3868
3869 def slice_with_dask_array(x, index):
-> 3870 y = elemwise(getitem, x, index, dtype=x.dtype)
3871
3872 name = 'getitem-' + tokenize(x, index)
/Users/taugspurger/Envs/dask-dev/lib/python3.6/site-packages/dask/dask/array/core.py in elemwise(op, *args, **kwargs)
2624 shapes = [getattr(arg, 'shape', ()) for arg in args]
2625 shapes = [s if isinstance(s, Iterable) else () for s in shapes]
-> 2626 out_ndim = len(broadcast_shapes(*shapes)) # Raises ValueError if dimensions mismatch
2627 expr_inds = tuple(range(out_ndim))[::-1]
2628
/Users/taugspurger/Envs/dask-dev/lib/python3.6/site-packages/dask/dask/array/core.py in broadcast_shapes(*shapes)
2597 if any(i != -1 and i != 1 and i != dim and not np.isnan(i) for i in sizes):
2598 raise ValueError("operands could not be broadcast together with "
-> 2599 "shapes {0}".format(' '.join(map(str, shapes))))
2600 out.append(dim)
2601 return tuple(reversed(out))
ValueError: operands could not be broadcast together with shapes (4, 3) (4,)A solution is for the user to manually reshape to y to a 2-d array, with length 1 on the second dimension. Then the broadcasting lines up. I think we want to follow NumPy here (if possible?)
Activity
mrocklin commentedon Jun 5, 2017
Doing the broadcasting automatically makes sense to me.
TomAugspurger commentedon Jun 5, 2017
TomAugspurger commentedon Jun 6, 2017
@mrocklin would you (or anyone else familiar with the slicing / atop code) take a look at master...TomAugspurger:array-slice-array and let me know if I'm starting down the wrong track? (apologies for the messy WIP code).
My current goal is to get
dx[dy == 1]working, wheredxanddyare dask arrays with the correct shapesdx.shape[0] == dy.shape[0]dx.ndim > dy.ndimCurrently, it constructs a graph, built incorrectly; running
pytest dask/array/tests/test_slicing.py -k test_slice_array_mask_simpleraises on the compute withdask/array/tests/test_slicing.py:587: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ dask/array/utils.py:60: in assert_eq a = a.compute(get=get_sync) dask/base.py:97: in compute (result,) = compute(self, traverse=False, **kwargs) dask/base.py:202: in compute dsk = collections_to_dsk(variables, optimize_graph, **kwargs) dask/base.py:461: in collections_to_dsk for opt, (dsk, keys) in groups.items()]) dask/base.py:461: in <listcomp> for opt, (dsk, keys) in groups.items()]) dask/context.py:84: in f return func(*args, **kwargs) dask/array/optimization.py:30: in optimize dsk2, dependencies = cull(dsk, keys) dask/optimize.py:47: in cull for k in work] dask/optimize.py:47: in <listcomp> for k in work] _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ dsk = {('getitem-a3ae0bd91a3873b743f1663f57fc66be', 0, 0): (<built-in function getitem>, ('array-2d68ac816440984399abba3b570... function getitem>, ('array-2d68ac816440984399abba3b570ca725', 1, 1), ('eq-7f02bb319ba45387432c1112e13587e1', 1)), ...} key = ('getitem-3e4c700d35ba80a7fbb3387f1ea0c076', 1, 1), task = None, as_list = True def get_dependencies(dsk, key=None, task=None, as_list=False): """ Get the immediate tasks on which this task depends """ if key is not None: > arg = dsk[key] E KeyError: ('getitem-3e4c700d35ba80a7fbb3387f1ea0c076', 1, 1)I'll keep going down this route a bit further to see what I can get working.
mrocklin commentedon Jun 6, 2017
When I look at the result of the elemwise call everything seems fine. I checked this by using
result.visualize('dask.pdf')andresult.compute()within a pdb call.However when I look at the full output graph and keys I see that there is a mismatch
The
getitem-3e4keys have two indexes in our keys but only one index in the graph. Looking at this code we see thatslice_with_dask_arraylinearizes the keys. This made sense in the case we had before but no longer makes sense here.TomAugspurger commentedon Jun 6, 2017
Great, thanks for the pointer. I'll go there next.
TomAugspurger commentedon Jun 7, 2017
@mrocklin one more request for guidance if you don't mind :)
master...TomAugspurger:array-slice-array is working correctly with
dx[mask]wheremaskis a 1-d boolean mask. We should be able to supportdx[mask]for any dimensionmask(<=dx.ndim).Constructs the DAG (correctly I think) but fails on executation with
So the is that
args2[0]andargs2[1]are length 1 lists, rather than ndarrays. In the 1-d case, these lists are unpacked down to just the arrays (I think, may be wrong about this), possibly inconcatenate3.My question: does it sound like the issue is with the constructed DAG, or is there something I need to do on the compute side to make this work? Actually, typing this up has given my some ideas of where to look (based on the visualization). Going to check out rechunking stage next.
mrocklin commentedon Jun 7, 2017
OK, so atop handles contractions (when some of the input indices are not present in the output) by giving the function a list of chunks as an input. That is what is happening in our case.
Perhaps you want to have
expr_indsbe the full index set? You might do a bit of atop work in this way and then collapse dimensions afterwards?TomAugspurger commentedon Jun 7, 2017
I see, thanks.
mrocklin commentedon Jun 7, 2017
Another way to think of this is that atop simultaneously handles broadcasting and reductions. In your case you probably just want broadcasting, in which case you'll want the output index set to be full. You might want to do some sort of axis shuffling afterwards (I'm not clear on
array[array]semantics).jakirkham commentedon Dec 4, 2017
So the example raised in the OP appears to be solved at least in 0.16.0. Though admittedly the fix may have come earlier, but I don't recall which PR. This is the result from running the OP example below.
Were there other things we were trying to solve here or was that it?
TomAugspurger commentedon Jun 29, 2018
I think that was it.