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
dask.array.asarray should recognize dask duck-types #2912
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.
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.
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?
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...
Activity
jakirkham commentedon Nov 20, 2017
So at least for me personally, I would expect
asarrayto always return a daskArrayinstance even if it may be a bad decision (e.g. loss of data, weird wrapping, etc.). Am basing this in part on howasarrayworks in NumPy with say aMaskedArray. 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
asanyarrayto 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 daskArraywould 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 ofasanyarrayin 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
DataArrayabove, it appears this does not inherit from daskArray. So callingasanyarrayon aDataArrayinstance will give us a daskArraythat wraps theDataArrayinstance (just like withasarray). Ideallyasanyarraywould preserve theDataArray. There are two options I can think of and have no idea from the outset which is better and/or more feasible. FirstDataArray(and friends) can inherit from daskArray. Second we broaden outasanyarrayto handle things likeDataArray.Thoughts?
shoyer commentedon Nov 20, 2017
Since
xarray.DataArraydoes not inherit from daskArray, bothasarrayandasanyarrayshould convert it in a daskArray. That part of the existing functionality is exactly right.But if a
xarray.DataArraycontains 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?
jcrist commentedon Nov 20, 2017
A few ideas:
to_dask_array? Something else?jakirkham commentedon Mar 30, 2018
Does
xarray.DataArraysupport the custom collections interface currently? If not, is there an open issue/PR that we should be aware of?shoyer commentedon Mar 31, 2018
xarray.DataArraydoes 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...