Repository navigation
Closed
Description
Short sample for reproduction and traceback follow.
import numpy as np
import sparse
import dask.array as da
from numpy.testing import assert_array_equal
x = da.random.random((2, 3, 4), chunks=(1, 2, 2))
x[x < 0.8] = 0
y = x.map_blocks(sparse.COO.from_numpy)
xx = x.mean()
yy = y.mean()
assert_array_equal(xx, yy)Traceback (most recent call last):
File "dask_sparse.py", line 14, in <module>
assert_array_equal(xx, yy)
File "/home/pentschev/.local/lib/python3.5/site-packages/numpy/testing/_private/utils.py", line 894, in assert_array_equal
verbose=verbose, header='Arrays are not equal')
File "/home/pentschev/.local/lib/python3.5/site-packages/numpy/testing/_private/utils.py", line 697, in assert_array_compare
y = array(y, copy=False, subok=True)
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/array/core.py", line 998, in __array__
x = self.compute()
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/base.py", line 156, in compute
(result,) = compute(self, traverse=False, **kwargs)
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/base.py", line 398, in compute
results = schedule(dsk, keys, **kwargs)
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/threaded.py", line 76, in get
pack_exception=pack_exception, **kwargs)
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/local.py", line 475, in get_async
finish(dsk, state, not succeeded)
File "/usr/lib/python3.5/contextlib.py", line 77, in __exit__
self.gen.throw(type, value, traceback)
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/callbacks.py", line 99, in local_callbacks
yield callbacks or ()
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/local.py", line 460, in get_async
raise_exception(exc, tb)
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/compatibility.py", line 112, in reraise
raise exc
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/local.py", line 230, in execute_task
result = _execute_task(task, data)
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/core.py", line 119, in _execute_task
return func(*args2)
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/optimization.py", line 942, in __call__
dict(zip(self.inkeys, args)))
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/core.py", line 149, in get
result = _execute_task(task, cache)
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/core.py", line 119, in _execute_task
return func(*args2)
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/compatibility.py", line 93, in apply
return func(*args, **kwargs)
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/array/reductions.py", line 332, in mean_chunk
n = numel(x, dtype=dtype, **kwargs)
File "/home/pentschev/.local/lib/python3.5/site-packages/dask/array/reductions.py", line 323, in numel
return chunk.sum(np.ones_like(x), **kwargs)
File "/home/pentschev/.local/lib/python3.5/site-packages/numpy/core/overrides.py", line 151, in public_api
implementation, public_api, relevant_args, args, kwargs)
File "/home/pentschev/.local/lib/python3.5/site-packages/numpy/core/numeric.py", line 289, in ones_like
res = empty_like(a, dtype=dtype, order=order, subok=subok)
File "/home/pentschev/.local/lib/python3.5/site-packages/numpy/core/overrides.py", line 151, in public_api
implementation, public_api, relevant_args, args, kwargs)
File "/home/pentschev/.local/lib/python3.5/site-packages/sparse/sparse_array.py", line 213, in __array__
raise RuntimeError('Cannot convert a sparse array to dense automatically. '
RuntimeError: Cannot convert a sparse array to dense automatically. To manually densify, use the todense method.Another function that fails with same error besides mean() is std(). After fixing the error, we have to make sure to add both functions to sparse tests, inside the functions list.
Metadata
Metadata
Assignees
Labels
No labels
Activity
Fixed mean() and moment() on sparse arrays
Fixed mean() and moment() on sparse arrays
Hoeze commentedon Mar 25, 2019
Also not working:
y.compute()pentschev commentedon Mar 25, 2019
@Hoeze that works for me, could you post a traceback?
Hoeze commentedon Mar 25, 2019
@pentschev
Dask v1.1.4:
Hoeze commentedon Mar 25, 2019
The sad thing here is the fact that I got no idea how to execute
todense()on that:Hoeze commentedon Mar 25, 2019
Maybe related?
Autmatic densification seems like an important feature to use
sparsewith dask, especially when mixing different types of arrays...EDIT:
The problem seems to be that
np.asarray()fails on sparse matrices.pentschev commentedon Mar 25, 2019
You can try this:
or, alternatively without a numpy array:
Would that be an option for your use case?
Hoeze commentedon Mar 25, 2019
Unfortunately, this does not work for me.
I need to load a huge sparse matrix from disk and combine this with another dense matrix.
Therefore, I planned something like that:
My hope was that Dask then hides all the
if data is sparse; thenstuff from me.Hoeze commentedon Mar 25, 2019
@pentschev
@hameerabbasi just stated that disallowing
np.asarraywas intentionally.This means that Dask needs to do its own check at every point whether the current chunk is a sparse matrix (e.g. to calculate the concatenation of a sparse and a dense array). Is this doable?
pentschev commentedon Mar 25, 2019
Yes, I see your point, unfortunately, this won't be the case, due to Dask's design to prevent accidental densification of a sparse matrix.
I think the only alternative for your case is to densify your matrix manually, from your prior example, you would need to do the following:
hameerabbasi commentedon Mar 25, 2019
Copying relevant comment of mine from elsewhere: pydata/sparse#10 (comment)
Hoeze commentedon Mar 25, 2019
Setting
SPARSE_AUTO_DENSIFY=1works!Of course, it's a quick and dirty way, but that's still the only option in my case, since I cannot convert the full dataset into a dense format.
Thanks again @hameerabbasi and @pentschev
hameerabbasi commentedon Mar 25, 2019
@Hoeze I added
stdandvarin sparsemaster(pydata/sparse#244). 🙂 You should be able to use those without any kind of densification now.meanwas already there.Fixed mean() and moment() on sparse arrays (#4525)
Fixed mean() and moment() on sparse arrays (dask#4525)