Repository navigation
Open
Labels
arrayneeds attentionIt's been a while since this was pushed on. Needs attention from the owner or a maintainer.
Description
What happened:
I get a ValueError when I use a 1D dask index array to make an assignment on another dask array.
Minimal Complete Verifiable Example:
This
import dask.array as da
x = da.zeros(10)
x[da.arange(3)] = 1raises the following error:
ValueError Traceback (most recent call last)
~/Documents/FOSS/dask-tlouf/dask/array/core.py in __setitem__(self, key, value)
1720 try:
-> 1721 y = where(key, value, self)
1722 except ValueError as e:
~/Documents/FOSS/dask-tlouf/dask/array/routines.py in where(condition, x, y)
2037 else:
-> 2038 return elemwise(np.where, condition, x, y)
2039
~/Documents/FOSS/dask-tlouf/dask/array/core.py in elemwise(op, out, where, dtype, name, *args, **kwargs)
4441 out_ndim = len(
-> 4442 broadcast_shapes(*shapes)
4443 ) # Raises ValueError if dimensions mismatch
~/Documents/FOSS/dask-tlouf/dask/array/core.py in broadcast_shapes(*shapes)
4369 if any(i not in [-1, 0, 1, dim] and not np.isnan(i) for i in sizes):
-> 4370 raise ValueError(
4371 "operands could not be broadcast together with "
ValueError: operands could not be broadcast together with shapes (3,) () (10,)
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
<ipython-input-9-7ccc3d8dfd86> in <module>
1 x = da.zeros(10)
----> 2 x[da.arange(3)] = 1
~/Documents/FOSS/dask-tlouf/dask/array/core.py in __setitem__(self, key, value)
1721 y = where(key, value, self)
1722 except ValueError as e:
-> 1723 raise ValueError(
1724 "Boolean index assignment in Dask "
1725 "expects equally shaped arrays.\nExample: da1[da2] = da3 "
ValueError: Boolean index assignment in Dask expects equally shaped arrays.
Example: da1[da2] = da3 where da1.shape == (4,), da2.shape == (4,) and da3.shape == (4,).while curiously this does not:
x = da.zeros(10)
x[da.arange(3),] = 1Environment:
- Dask version: 2021.11.2+18.g43247806 (just pulled)
- Python version: 3.9.5
- Operating System: Pop!_OS 21.04 x86_64
- Install method (conda, pip, source): source
Reacted by Tommaso Comparin, Liam Bindle and Aaron Zuspan
Metadata
Metadata
Assignees
Labels
arrayneeds attentionIt's been a while since this was pushed on. Needs attention from the owner or a maintainer.
Activity
scharlottej13 commentedon Dec 8, 2021
@TLouf thank you for raising this issue! We were able to reproduce it easily thanks to your minimal example and it does indeed seem like this is a bug. The example you provided does work in Numpy:
ncclementi commentedon Jan 26, 2022
@davidhassell pinging you here as I noticed that you fixed this issue #8532 in this PR #8538 and they seem related. Would you like to take a crack at this issue too? It's completely ok to say no, no pressure : )
davidhassell commentedon Jan 26, 2022
Hi @ncclementi - sure, I can look at this. (edit: having looked at it and written some comments, see the last paragraph!)
This is case where there is a bit of conflict between the "original" assignment API, and the extended API that allows more general numpy-style indexed assignments.
The design of the extended API deliberately didn't overlap with cases already dealt with by the original API, to ensure that no existing code would break, even though the original API was not always consistent with numpy (as is seen in the example originally posted).
The original API only allowed a single index of type
dask.array.Arraywith the same shape asx. This is whyx[da.arange(3),] = 1works as expected - this index argument is atuple, and so is handled by the extended API.This situation is further complicated by the fact that the original API always interprets a
dask.array.Arrayindex in a boolean context, whereas the tuple-argument version behaves like numpy. e.g.Numpy behaves the same in both cases:
I'm sure that this could be "fixed" (by which I mean so that dask behaves like numpy in all cases - other meanings of "fixed" are probably available), but it'd be good to get some wider feedback on whether we should do so. Doing so would possibly break some existing applications that utilise the original
Array.__setitem__API, either by raising a new exception or, worse, running but giving incorrect results.Thanks,
David
pavithraes commentedon Apr 7, 2022
@jsignell Do you have thoughts on this? It'll be nice to get some direction on how to proceed... should we consider fixes, throw a warning, or document this better?
jsignell commentedon Apr 20, 2022
My initial reaction is that probably raising an informative error could be a first step if we aren't sure yet if this is worth implementing.