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
It's pretty crude, but basically this would need to be extended to implement many of these methods on for sparse arrays, too. Ideally we would define xarray's adapter logic into more cleanly separated submodules, perhaps using multiple dispatch. Even better, we would make this public API, so you can write something like xarray.register_data_type(MySparseArray) to register a type as valid for xarray's .data attribute.
It looks like __array_ufunc__ will actually finally land in NumPy 1.13, which might make this easier.
It depends on numpy and scipy.sparse and, with the exception of a bit of in-memory data movement and copies, should run at scipy speeds (though I haven't done any benchmarking).
@rabernat do you have an application that we could use to drive this?
In case you're still looking for an application, gene expression from single cells (see data/00_original/GSM162679$i_P14Retina_$j.digital_expression.txt.gz) is very sparse due to high gene dropout. The shape is expression.shape (49300, 24760) and it's mostly zeros or nans. A plain csv from this data was 2.5 gigs, which gzipped to 300 megs.
Here is an example of using xarray to combine these files but my kernel keeps dying when I do ds.to_netcdf() :(
Hope this is a good example for sparse arrays!
Reacted by Ambrose J Carr, Brian Aevermann and firdaus janoos
tokenizing textual data produces large sparse matrices where the column labels correspond to the vocabulary, while row labels correspond to document ids. Here is a minimal example using scikit-learn,
importos.pathfromsklearn.datasetsimportfetch_20newsgroupsfromsklearn.feature_extraction.textimportCountVectorizerds=fetch_20newsgroups()
vect=CountVectorizer()
X=vect.fit_transform(ds.data)
print(X) # Extracted tokens# Returns:# <11314x130107 sparse matrix of type '<class 'numpy.int64'>'# with 1787565 stored elements in Compressed Sparse Row format>column_labels=vect.get_feature_names()
print(np.asarray(column_labels))
# Returns:# array(['00', '000', '0000', ..., 'íålittin', 'ñaustin', 'ýé'], dtype='<U180')row_labels= [int(os.path.split(el)[1]) forelinds.filenames]
print(np.asarray(row_labels))
# Returns:# array([102994, 51861, 51879, ..., 60695, 38319, 104440])
I'm interested to see if there have been any developments on this. I currently have an application where I'm working with multiple dask arrays, some of which are sparse (text data). It'd be worth my time to move my project to xarray, so I'm be interested in contributing something here if there is a need.
Reacted by Roman Yurchak, Daniel Himmelstein and Florian R. Hölzlwimmer
Currently I have to work with both xarray and anndata to store counts in sparse arrays separate from other depending data which is a little bit annoying :)
How should these sparse arrays get stored in NetCDF4?
I know that NetCDF4 has some conventions how to store sparse data, but do we have to implement our own conversion mechanisms for each sparse type?
It will need some experimentation, but I think things should be pretty close after NumPy 1.17 is released. Potentially it could be as easy as adjusting the rules xarray uses for casting in xarray.core.variable.as_compatible_data.
@fjanoos there isn't any formal documentation yet but you can look at test_sparse.py for examples. That file will also tell you what works and doesn't work currently.
Activity
shoyer commentedon Apr 14, 2017
Yes, I would say this is in scope, as long as we can keep most of the data-type specific logic out of xarray's core (which seems doable).
Currently, we define most of our operations on duck arrays in https://github.com/pydata/xarray/blob/master/xarray/core/duck_array_ops.py
There are a few other hacks throughout the codebase, which can find by searching for "dask_array_type": https://github.com/pydata/xarray/search?p=1&q=dask_array_type&type=&utf8=%E2%9C%93
It's pretty crude, but basically this would need to be extended to implement many of these methods on for sparse arrays, too. Ideally we would define xarray's adapter logic into more cleanly separated submodules, perhaps using multiple dispatch. Even better, we would make this public API, so you can write something like
xarray.register_data_type(MySparseArray)to register a type as valid for xarray's.dataattribute.It looks like
__array_ufunc__will actually finally land in NumPy 1.13, which might make this easier.See also #1118
rabernat commentedon Apr 15, 2017
👍 to the scipy.sparse array suggestion
[While we are discussing supporting other array types, we should keep gpu arrays on the radar]
benbovy commentedon Apr 15, 2017
Although I don't know much about SciDB, it seems to be another possible application for
xarray.register_data_type.mrocklin commentedon Apr 16, 2017
Here is a brief attempt at a multi-dimensional sparse array: https://github.com/mrocklin/sparse
It depends on numpy and scipy.sparse and, with the exception of a bit of in-memory data movement and copies, should run at scipy speeds (though I haven't done any benchmarking).
@rabernat do you have an application that we could use to drive this?
rabernat commentedon Apr 17, 2017
Nothing comes to mind immediately. My data are unfortunately quite dense! 😜
olgabot commentedon Jun 26, 2017
In case you're still looking for an application, gene expression from single cells (see
data/00_original/GSM162679$i_P14Retina_$j.digital_expression.txt.gz) is very sparse due to high gene dropout. The shape isexpression.shape (49300, 24760)and it's mostly zeros or nans. A plain csv from this data was 2.5 gigs, which gzipped to 300 megs.Here is an example of using
xarrayto combine these files but my kernel keeps dying when I dods.to_netcdf():(Hope this is a good example for sparse arrays!
rth commentedon Sep 3, 2017
Other examples where labeled sparse arrays would be useful are,
rabernat commentedon Sep 3, 2017
Sparse Xarray DataArrays would be useful for the linear regridding operations discussed in JiaweiZhuang/xESMF#3.
lbybee commentedon Jan 4, 2018
I'm interested to see if there have been any developments on this. I currently have an application where I'm working with multiple dask arrays, some of which are sparse (text data). It'd be worth my time to move my project to xarray, so I'm be interested in contributing something here if there is a need.
Hoeze commentedon Jun 6, 2018
I'd know a project which could make perfect use of xarray, if it would support sparse tensors:
https://github.com/theislab/anndata
Currently I have to work with both xarray and anndata to store counts in sparse arrays separate from other depending data which is a little bit annoying :)
shoyer commentedon Jun 6, 2018
See also: #1938
The major challenge now is the dispatching mechanism, which hopefully http://www.numpy.org/neps/nep-0018-array-function-protocol.html will solve.
Hoeze commentedon Jul 5, 2018
Would it be an option to use dask's sparse support?
http://dask.pydata.org/en/latest/array-sparse.html
This way xarray could let dask do the main work.
Currently I load everything into a dask array by hand and pass this dask array to xarray.
This works pretty good.
Hoeze commentedon Jul 5, 2018
How should these sparse arrays get stored in NetCDF4?
I know that NetCDF4 has some conventions how to store sparse data, but do we have to implement our own conversion mechanisms for each sparse type?
shoyer commentedon Jul 6, 2018
In principle this would work, though I would prefer to support it directly in xarray, too.
Yes, we would need to implement a convention for handling sparse array data.
rabernat commentedon Jun 22, 2019
Given the recent improvements in numpy duck array typing, how close are we to being able to just wrap a pydata/sparse array in an xarray Dataset?
shoyer commentedon Jun 23, 2019
It will need some experimentation, but I think things should be pretty close after NumPy 1.17 is released. Potentially it could be as easy as adjusting the rules xarray uses for casting in
xarray.core.variable.as_compatible_data.rabernat commentedon Jul 12, 2019
If someone who is good at numpy shows up at our sprint tomorrow, this could be a good issue try out.
mrocklin commentedon Jul 12, 2019
@rgommers might be able to recommend someone
rgommers commentedon Jul 13, 2019
I haven't talked to anyone at SciPy'19 yet who was interested in sparse arrays, but I'll keep an eye out today.
And yes, this is a fun issue to work on and would be really nice to have!
rabernat commentedon Jul 13, 2019
I personally use the new sparse project for my day-to-day research. I am motivated on this, but I probably won't have time today to dive deep on this.
Maybe CuPy would be more exciting.
mrocklin commentedon Jul 14, 2019
@nvictus has been working on this at #3117
fjanoos commentedon Jul 21, 2019
Wondering what the status on this is ? Is there a branch with this functionality implemented - would love to give it a spin !
shoyer commentedon Aug 13, 2019
This is working now on the
masterbranch!Once we get a few more kinks worked out, it will be in the next release.
I've started another issue for discussing how xarray could integrate sparse arrays better into its API: #3213
fjanoos commentedon Aug 29, 2019
@shoyer
Is there documentation for using sparse arrays ? Could you point me to some example code ?
dcherian commentedon Aug 30, 2019
@fjanoos there isn't any formal documentation yet but you can look at test_sparse.py for examples. That file will also tell you what works and doesn't work currently.