Skip to content

dask.array.asarray should recognize dask duck-types #2912

Open

Description

@shoyer

With master for dask and xarray (with dask duck array support):

In [25]: import dask.array as da

In [26]: import xarray

In [27]: dask_obj = da.ones((3, 3), chunks=-1)

In [28]: xarray_obj = xarray.DataArray(x, dims=['x', 'y'])

In [29]: dict(da.asarray(xarray_obj).dask)
Out[29]:
{('array-5ce95599636c17df4c5746fb5f3c220b',
  0,
  0): (<function dask.array.core.getter_inline>, 'array-original-5ce95599636c17df4c5746fb5f3c220b', (slice(0, 3, None),
   slice(0, 3, None))),
 'array-original-5ce95599636c17df4c5746fb5f3c220b': <xarray.DataArray 'wrapped-8015d128b19387281f3d8bd3a663300a' (x: 3, y: 3)>
 dask.array<shape=(3, 3), dtype=float64, chunksize=(3, 3)>
 Dimensions without coordinates: x, y}

The dask graph from asarray() has an xarray.DataArray object in it, with its own nested dask graph. This is pretty messy: asarray() should really recognize the dask duck-type and combine the argument's dask graph into its own.

Activity

  1. jakirkham commented on Nov 20, 2017

    @jakirkham
    Member

    So at least for me personally, I would expect asarray to always return a dask Array instance even if it may be a bad decision (e.g. loss of data, weird wrapping, etc.). Am basing this in part on how asarray works in NumPy with say a MaskedArray. Also basing this on how we have documented it and how it has been used internally before it was exposed in the public API.

    OTOH I would expect asanyarray to try to preserve the original type as much as possible. In NumPy, this seems to have been done by inheritance (and we do the same thing here currently). Namely all types inheriting from a dask Array would be left unchanged. Admittedly our documentation in dask is a bit unclear on this; so, it could be improved. FWIW we have been trying to make use of asanyarray in functions needing to enforce some sort of Dask Array as input, but with the intent of keeping them more flexible.

    Now I know next to nothing about xarray. So please correct me if I speak incorrectly in what follows. For the DataArray above, it appears this does not inherit from dask Array. So calling asanyarray on a DataArray instance will give us a dask Array that wraps the DataArray instance (just like with asarray). Ideally asanyarray would preserve the DataArray. There are two options I can think of and have no idea from the outset which is better and/or more feasible. First DataArray (and friends) can inherit from dask Array. Second we broaden out asanyarray to handle things like DataArray.

    Thoughts?

  2. shoyer commented on Nov 20, 2017

    @shoyer
    MemberAuthor

    Since xarray.DataArray does not inherit from dask Array, both asarray and asanyarray should convert it in a dask Array. That part of the existing functionality is exactly right.

    But if a xarray.DataArray contains a dask graph (via the custom collections interface), it would be nice to merge its dask graph directly into the resulting dask Array, instead of having nested dask arrays. The later is pretty messy, and can result in various problems like the inability to optimize and resource contention.

    It's not entirely clear to me that there is a good way to do this with the current custom collections interface. @mrocklin @jcrist any thoughts?

  3. jcrist commented on Nov 20, 2017

    @jcrist
    Member

    It's not entirely clear to me that there is a good way to do this with the current custom collections interface.

    A few ideas:

    • Special case xarray for now. This is easy to do correctly, and since it's the only dask array-like thing currently, I see no issue in doing this.
    • Look for a specific method on the class, and call that if found. to_dask_array? Something else?
  4. jakirkham commented on Mar 30, 2018

    @jakirkham
    Member

    Does xarray.DataArray support the custom collections interface currently? If not, is there an open issue/PR that we should be aware of?

  5. shoyer commented on Mar 31, 2018

    @shoyer
    MemberAuthor

    xarray.DataArray does support the custom collections interface.

    But merely being a dask object isn't enough here. We also need some way to indicate how xarray objects can be converted into dask arrays.

    Coincidentally I've been working this past week with @njsmith on a proposal for how exactly this sort of thing should be done. We'll hopefully be sending it out very soon...

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions