Skip to content

Assignment using 1D dask Array index #8460

Open

Description

@TLouf

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)] = 1

raises 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),] = 1

Environment:

  • 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

Activity

  1. scharlottej13 commented on Dec 8, 2021

    @scharlottej13
    Contributor

    @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:

    import numpy as np
    x = np.zeros(10)
    x[np.arange(3)] = 1
  2. added
    good second issue
    on Dec 15, 2021
  3. ncclementi commented on Jan 26, 2022

    @ncclementi
    Member

    @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 : )

  4. davidhassell commented on Jan 26, 2022

    @davidhassell
    Contributor

    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.Array with the same shape as x. This is why x[da.arange(3),] = 1 works as expected - this index argument is a tuple, 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.Array index in a boolean context, whereas the tuple-argument version behaves like numpy. e.g.

    >>> import dask.array as da
    >>> index = da.from_array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0])
    
    >>> # Using the original API
    >>> x = da.zeros(10)
    >>> x[index] = -99 # indexed by an Array & elements of index are interpreted as booleans
    >>> x.compute()
    array([  0., -99., -99.,   0., -99., -99.,   0., -99., -99.,   0.])  # puts a -99 where index.astype(bool) is True
    
    >>> # Using the extended API
    >>> x = da.zeros(10)
    >>> x[(index,)] = -99  # indexed by a tuple & elements of index are interpreted as integers
    >>> x.compute()
    array([-99., -99., -99.,   0.,   0.,   0.,   0.,   0.,   0.,   0.])  # puts a -99 at indices 0, 1, and 2

    Numpy behaves the same in both cases:

    >>> import numpy as np
    >>> index = [0, 1, 2, 0, 1, 2, 0, 1, 2, 0]
    >>> a = np.zeros(10)
    >>> a[index] = -99
    >>> a
    array([-99., -99., -99.,   0.,   0.,   0.,   0.,   0.,   0.,   0.])
    >>> a = np.zeros(10)
    >>> a[(index,)] = -99
    >>> a
    array([-99., -99., -99.,   0.,   0.,   0.,   0.,   0.,   0.,   0.])

    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

  5. removed
    good second issue
    on Jan 26, 2022
  6. removed their assignment
    on Mar 9, 2022
  7. pavithraes commented on Apr 7, 2022

    @pavithraes
    Member

    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.

    @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?

  8. jsignell commented on Apr 20, 2022

    @jsignell
    Member

    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.

  9. self-assigned this
    on Apr 21, 2022
  10. removed their assignment
    on May 10, 2022
  11. added
    needs attention
    on Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrayneeds attention

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions