Skip to content

Scalar array-like with asarray=False passed though #2823

@djkirkham

Description

@djkirkham

Using the new asarray=False functionality with dask.from_array on a scalar array-like (shape=()) results in the array-like itself being returned by compute():

>>> import numpy as np
>>> import dask.array as da
>>> class MyArray:
...     shape = ()
...     dtype = np.dtype('i4')
...     def __getitem__(self, idx):
...         # This is never called by Dask
...         pass
... 
>>> a = MyArray()
>>> d = da.from_array(a, 1, asarray=False)
>>> d.compute()
<__main__.MyArray instance at 0x7f36bc726998>

This is preumably because __getitem__ is never called by Dask for scalar arrays. This results in errors if you try to process the Dask object, for example reshaping it:

>>> d = d.reshape((1,))
>>> d.compute()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../dask/base.py", line 99, in compute
    (result,) = compute(self, traverse=False, **kwargs)
  File ".../dask/base.py", line 206, in compute
    results = get(dsk, keys, **kwargs)
  File ".../dask/local.py", line 562, in get_sync
    return get_async(apply_sync, 1, dsk, keys, **kwargs)
  File ".../dask/local.py", line 529, in get_async
    fire_task()
  File ".../dask/local.py", line 504, in fire_task
    callback=queue.put)
  File ".../local.py", line 551, in apply_sync
    res = func(*args, **kwds)
  File ".../dask/local.py", line 295, in execute_task
    result = pack_exception(e, dumps)
  File ".../dask/local.py", line 290, in execute_task
    result = _execute_task(task, data)
  File ".../dask/local.py", line 270, in _execute_task
    args2 = [_execute_task(a, cache) for a in args]
  File ".../dask/local.py", line 271, in _execute_task
    return func(*args2)
  File ".../dask/utils.py", line 688, in __call__
    return getattr(obj, self.method)(*args, **kwargs)
AttributeError: MyArray instance has no attribute 'reshape'

The asarray keyword was added as part of the work to allow Dask to return masked arrays, introduced in #2301 and released in v0.15.3, though I believe v0.15.4 is more stable. I don't think it was intended to ever return the original array-like (though I could be mistaken).

@jcrist

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions