Skip to content

Minimal example with sparse arrays #2562

Closed

Description

@rth

I was wondering if there is a minimal example of using sparse arrays with Dask, along the lines of,

import dask.array as da
y = da.from_array(x, chunks=(100))
y.sum().compute()

where x is a 2D sparse array saved on disk, then partially loaded by some package supporting Numpy slicing syntax. I am aware of the Sparse array section in the documentation, however, I'm not certain how the proposed approach of swapping out in-memory numpy arrays with in-memory sparse arrays could apply with sparse arrays saved on disk.

I have posted a question on SO about it earlier, without receiving any suggestions on how this could be achievable. There I focused on scipy CSR arrays, loaded with joblib.load(.. , mmap_mode="r") because this allows partial loading of sparse arrays on disk and supports numpy slicing. However, I'm open to any other suggestions regarding the sparse array format / io package (e.g. using the mrocklin/sparse package).

I find the suggestions in the documentation about the storage format very useful (e.g. parquet for dataframes, more loosely hdf5 for dense arrays, etc). Is there any consensus yet as to what format could be used (possibly in the future) to store sparse arrays with dask? I'm not sure if anyone has worked on a language independent sparse array format (akin to feather or parquet for dataframes), or if it's possible to read/write the distributed sparse format from Spark in Python without installing PySpark...

If this is not yet possible in dask, is there a development roadmap for getting there? In particular, there were a number of issues on this, but at present, all of them are closed.

Thank you. Sorry for all the questions.

Activity

  1. mrocklin commented on Jul 27, 2017

    @mrocklin
    Member

    I personally do not know of a well-accepted on-disk format for sparse nd-arrays. If this is something that interests you then your research and efforts here would certainly be appreciated.

  2. rth commented on Jul 27, 2017

    @rth
    ContributorAuthor

    @mrocklin Thanks for your response!

    So, assuming there is an on disk format for sparse arrays (using joblib.dump/load on the sparse.COO works well enough) I was wondering what would be necessary to make da.from_array(X) work for such an object? The X[slice] could be a lazily loaded sparse array in any format, as needed.

    Here are some things I have tried (using the sparse.COO format): https://github.com/rth/ipynb/blob/master/dask/On_disk_sparse_storage_dask.ipynb

    and the only thing that worked was to,

    1. Allocate a dense array on disk with the same shape as the sparse array
    2. Copy the data from the sparse array to the dense array
    3. Load the dense array with dask
    4. Copy the data back into the sparse format with map_blocks(sparse.COO)

    which is terribly inefficient. Is there a way around creating this initial dense array and doing the in memory replacement?

    In particular, since I'm not (yet) familiar with the dask code base, I have a hard time estimating how difficult it would be to make a dask.array.from_sparse_array that would create a dask array directly from a sparse.COO or making dask.array.from_array work in this case. Unless there is a better way of doing this?
    I would appreciate any suggestions regarding where to start on this. Thank you!

  3. mrocklin commented on Jul 27, 2017

    @mrocklin
    Member
  4. rth commented on Jul 27, 2017

    @rth
    ContributorAuthor

    Slicing into an on-disk storage object should produce an ndarray object of some form (numpy, sparse, other).

    That is the case for the on disk storage I am using, see above-linked notebook Out [10]:, however running da.from_array on this object fails. For that matter even applying da.from_array on an in memory sparse.COO array fails (with the same error), cf. above linked notebook sections 3.1, 3.2...

  5. mrocklin commented on Jul 27, 2017

    @mrocklin
    Member

    Two changes:

    1. chunks is short for chunksize. You'll want to include the shape of the chunks you with to cut out
    2. use the asarray=False keyword argument to from_array to stop coercion to numpy arrays
    In [1]: import dask
    
    In [2]: import dask.array as da
    
    In [3]: import sparse
    
    In [4]: x = sparse.COO({(10000, 10000): 1})
    
    In [5]: x = da.from_array(x, chunks=(1000, 1000), asarray=False)
    
    In [6]: x.compute()
    Out[6]: <COO: shape=(10001, 10001), dtype=int64, nnz=1, sorted=False, duplicates=False>
  6. rth commented on Jul 27, 2017

    @rth
    ContributorAuthor

    @mrocklin That's exactly what I was looking for. Thanks a lot!

  7. seanlaw commented on Jul 25, 2019

    @seanlaw

    In [1]: import dask
    In [2]: import dask.array as da
    In [3]: import sparse
    In [4]: x = sparse.COO({(10000, 10000): 1})
    In [5]: x = da.from_array(x, chunks=(1000, 1000), asarray=False)
    In [6]: x.compute()
    Out[6]: <COO: shape=(10001, 10001), dtype=int64, nnz=1, sorted=False, duplicates=False>

    @mrocklin In this example, how/when would you persist the array to a distributed cluster? It's probably due to a faulty mental model but I'm having a tough time figuring out how I could push multiple dask arrays to the cluster so that I could concatenate them afterward. I've tried scattering the sparse.COO array and then da.concatenating the futures but it doesn't seem to like that.

  8. mrocklin commented on Jul 25, 2019

    @mrocklin
    Member

    Ideally you would load your data lazily as tasks, perhaps using something like dask.delayed and da.from_delayed. This blogpost might be helpful: https://blog.dask.org/2019/06/20/load-image-data

    If you absolutely need to load data locally then you might consider using scatter and then da.from_delayed.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions