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 5 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
36 changes: 21 additions & 15 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,23 +926,29 @@ def chunk(self, chunks=None, name=None, lock=False):
if isinstance(data, da.Array):
data = data.rechunk(chunks)
else:
if isinstance(data, indexing.ExplicitlyIndexed):
# Unambiguously handle array storage backends (like NetCDF4 and h5py)
# that can't handle general array indexing. For example, in netCDF4 you
# can do "outer" indexing along two dimensions independent, which works
# differently from how NumPy handles it.
# da.from_array works by using lazy 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}
else:
kwargs = {}

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)
else:
kwargs = dict()

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

Expand Down
33 changes: 16 additions & 17 deletions xarray/tests/test_sparse.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
from collections import OrderedDict
from contextlib import suppress
from distutils.version import LooseVersion
from textwrap import dedent
import pickle
import numpy as np
import pandas as pd

from xarray import DataArray, Dataset, Variable
from xarray.tests import mock
from xarray import DataArray, Variable
from xarray.core.npcompat import IS_NEP18_ACTIVE
import xarray as xr
import xarray.ufuncs as xu

from . import (
assert_allclose,
assert_array_equal,
assert_equal,
assert_frame_equal,
assert_identical,
raises_regex,
)
from . import assert_equal, assert_identical

import pytest

Expand Down Expand Up @@ -148,7 +137,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 @@ -422,9 +410,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 @@ -879,3 +864,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(ac, a)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shoyer After #3204, assert_identical(a, ac) does not work anymore - not even with the latest sparse git head

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this is due to dask/dask#5259


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