Skip to content

chunk sparse arrays #3202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ def as_like_arrays(*data):
elif any(isinstance(d, sparse_array_type) for d in data):
from sparse import COO

return tuple(COO(d) for d in data)
out = []
for d in data:
if isinstance(d, dask_array_type):
d = d.compute()
out.append(COO(d))
return tuple(out)
else:
return tuple(np.asarray(d) for d in data)

Expand Down
31 changes: 18 additions & 13 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,21 +928,26 @@ def chunk(self, chunks=None, name=None, lock=False):
else:
if utils.is_dict_like(chunks):
chunks = tuple(chunks.get(n, s) for n, s in enumerate(self.shape))
# da.from_array works by using lazily indexing with a tuple of
# slices. Using OuterIndexer is a pragmatic choice: dask does not
# yet handle different indexing types in an explicit way:
# https://github.com/dask/dask/issues/2883
data = indexing.ImplicitToExplicitIndexingAdapter(
data, indexing.OuterIndexer
)

# For now, assume that all arrays that we wrap with dask (including
# our lazily loaded backend array classes) should use NumPy array
# operations.
if LooseVersion(dask.__version__) > "1.2.2":
kwargs = dict(meta=np.ndarray)
if not isinstance(data, np.ndarray) and hasattr(data, "__array_function__"):
# This should always be true after Variable.as_compatible_data
assert IS_NEP18_ACTIVE
kwargs = {}
else:
kwargs = dict()
# da.from_array works by using lazily indexing with a tuple of
# slices. Using OuterIndexer is a pragmatic choice: dask does not
# yet handle different indexing types in an explicit way:
# https://github.com/dask/dask/issues/2883
data = indexing.ImplicitToExplicitIndexingAdapter(
data, indexing.OuterIndexer
)

if LooseVersion(dask.__version__) < "2.0.0":
kwargs = {}
else:
# All of our lazily loaded backend array classes) should use NumPy
# array operations.
kwargs = {"meta": np.ndarray}

data = da.from_array(data, chunks, name=name, lock=lock, **kwargs)

Expand Down
18 changes: 14 additions & 4 deletions xarray/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ def test_variable_property(prop):
True,
marks=xfail(reason="'COO' object has no attribute 'argsort'"),
),
param(do("chunk", chunks=(5, 5)), True, marks=xfail),
param(
do(
"concat",
Expand Down Expand Up @@ -404,9 +403,6 @@ def test_dataarray_property(prop):
False,
marks=xfail(reason="Missing implementation for np.flip"),
),
param(
do("chunk", chunks=(5, 5)), False, marks=xfail(reason="Coercion to dense")
),
param(
do("combine_first", make_xrarray({"x": 10, "y": 5})),
True,
Expand Down Expand Up @@ -861,3 +857,17 @@ def test_sparse_coords(self):
dims=["x"],
coords={"x": COO.from_numpy([1, 2, 3, 4])},
)


def test_chunk():
s = sparse.COO.from_numpy(np.array([0, 0, 1, 2]))
a = DataArray(s)
ac = a.chunk(2)
assert ac.chunks == ((2, 2),)
assert isinstance(ac.data._meta, sparse.COO)
assert_identical(a, ac)

ds = a.to_dataset(name="a")
dsc = ds.chunk(2)
assert dsc.chunks == {"dim_0": (2, 2)}
assert_identical(ds, dsc)