You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Expected behavior of dask.array.full() for non-scalar fill values? #6107
The issue is that when the chunk sizes change, it won't work anymore, as it starts broadcasting per chunk:
a2=dask.array.full((2, 3), [[1, 2, 3], [4, 5, 6]], chunks=(1, 3))
a2.compute() # ValueError: could not broadcast input array from shape (2,3) into shape (1,3)a3=dask.array.full((2, 3), [1, 2, 3], chunks=(1, 3))
a3.compute()
My feeling is that choosing different chunk sizes shouldn't lead to different behavior here. I think I'd prefer a ValueError for a non-scalar fill_value.
Thanks for raising this issue. I think your proposal of raising an Error when the fill_value is not a scalar is quite reasonable but I wouldn't want to break people unnecessarily. Using full with a list works with chunks as long as the fill_value can be contained in one chunk.
I do think it'd be better if we can raise an error before calling .compute. I'm just not sure how to go about that. For the record that would happen here:
I think we should just error if fill_value isn't a scalar. (I didn't even know numpy supported broadcasting of fill_value). The broadcasting behavior only works for dask in extremely specific cases, and only works by accident, not by design.
@Huite, would you be interested in making a PR adding a nice error message for this case?
This requires a specific check on fill_value, so I don't think there's an elegant solution within the wrapping functions. This is the easiest way I could special case it:
# full() requires special casing due to argument check on fill_value_full=w(np.full) # Generate only oncedeffull(shape, fill_value, dtype=None, order='C'):
# np.isscalar has somewhat strange behavior: # https://docs.scipy.org/doc/numpy/reference/generated/numpy.isscalar.html ifnp.ndim(fill_value) !=0:
raiseValueError(f"fill_value must be scalar. Received {fill_value} instead.")
return_full(shape, fill_value, dtype, order)
full.__doc__=_full.__doc__full.__name__=_full.__name__
Not sure if that's what you had in mind, more or less?
The most elegant fix seems tackling it upstream in numpy. But I'd argue it's not a real issue in numpy anyway (even though the numpy signature explicitly says "scalar"), and it would likely cause significant breakage.
Activity
jsignell commentedon Apr 17, 2020
Thanks for raising this issue. I think your proposal of raising an Error when the fill_value is not a scalar is quite reasonable but I wouldn't want to break people unnecessarily. Using
fullwith a list works with chunks as long as thefill_valuecan be contained in one chunk.I do think it'd be better if we can raise an error before calling
.compute. I'm just not sure how to go about that. For the record that would happen here:dask/dask/array/wrap.py
Line 58 in 8217ab0
Huite commentedon Apr 19, 2020
A few thoughts:
If I take a look at the dask.array API (https://docs.dask.org/en/latest/array-api.html#dask.array), I don't think many of the functions implicitly do this kind of broadcasting? -- So this "chunkwise broadcasting" maybe only affects
full?This is probably a naive idea, but:
broadcast_todoes raise the kind of error we'd be after:(From your examples):
The last line raises an error immediately, without
.compute:(Ignoring the
orderanddtypearguments offullin this example)jcrist commentedon Apr 20, 2020
I think we should just error if
fill_valueisn't a scalar. (I didn't even know numpy supported broadcasting offill_value). The broadcasting behavior only works for dask in extremely specific cases, and only works by accident, not by design.@Huite, would you be interested in making a PR adding a nice error message for this case?
Huite commentedon Apr 21, 2020
I'd be happy to oblige, but I'm not sure what the best way of approaching this is, since
fullis currently justs wrapsnp.full:dask/dask/array/wrap.py
Line 140 in 8217ab0
This requires a specific check on
fill_value, so I don't think there's an elegant solution within the wrapping functions. This is the easiest way I could special case it:Not sure if that's what you had in mind, more or less?
The most elegant fix seems tackling it upstream in numpy. But I'd argue it's not a real issue in numpy anyway (even though the numpy signature explicitly says "scalar"), and it would likely cause significant breakage.
TomAugspurger commentedon Apr 22, 2020
@Huite that looks good. Could you submit a pull request with it?
Huite commentedon Apr 22, 2020
Yes, will do!