Open mangecoeur opened 1 year ago
the reason that's failing is that scipy.sparse.coo_array
does not implement __array_function__
or __array_namespace__
. As soon as that's the case I'd expect it to work.
In fact, setting s.__array_namespace__ = lambda x: x
will result in xr.DataArray(s)
printing as
<xarray.DataArray (dim_0: 4, dim_1: 4)>
<4x4 sparse array of type '<class 'numpy.float64'>'
with 4 stored elements in COOrdinate format>
Dimensions without coordinates: dim_0, dim_1
which I think is what you were after? Though of course that does not mean that we can actually use it...
Ok I had assumed that scipy would have directly implemented the array interface, I will see if there is already an issue open there. Then we can slowly see what else does/doesn't work.
@keewis using your solution things seem to more or less work, except that every operation of course 'loses' the __array_namespace__
attr so anything like slicing only half works, plus a lot of indexing operations are not implemented on scipy sparse arrays.
changing the assignment to:
s.__class__.__array_namespace__ = ...
should fix this, but indeed it is better to just wait on scipy.sparse
to implement the array API (NEP47)
What happened?
Now that Scipy is moving to support sparse NDarrays, we would expect that Xarray should work with them as any other array like data.
What did you expect to happen?
Doesn't work. It seems that why trying to use a scipy sparse array as the data, Xarray wraps the the sparse array in a 0-D dense array. (there are likely more issues after this but this was the first hurdle)
With sparse array s:
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment