Skip to content
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

Support for __array_function__ implementers (sparse arrays) [WIP] #3117

Merged
merged 31 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
5b5e245
Support for __array_function__ implementers
nvictus Jul 13, 2019
ad403fc
Pep8
nvictus Jul 13, 2019
df618ad
Consistent naming
nvictus Jul 14, 2019
1b62f6d
Check for NEP18 enabled and nep18 non-numpy arrays
nvictus Jul 14, 2019
21c0a21
Replace .values with .data
nvictus Jul 14, 2019
d62820b
Add initial test for nep18
nvictus Jul 14, 2019
0493263
Fix linting issues
nvictus Jul 14, 2019
ad42627
Add parameterized tests
nvictus Jul 14, 2019
ec01625
Internal clean-up of isnull() to avoid relying on pandas
shoyer Jul 15, 2019
fd05566
Merge branch 'master' into isnull-duck
shoyer Jul 15, 2019
5d8edfd
Add sparse to ci requirements
nvictus Jul 17, 2019
7c7a9f2
Moar tests
nvictus Jul 17, 2019
a231571
Two more patches for __array_function__ duck-arrays
nvictus Jul 17, 2019
4009379
Don't use coords attribute from duck-arrays that aren't derived from …
nvictus Jul 17, 2019
2c3b183
Improve checking for coords, and autopep8
nvictus Jul 17, 2019
23633b4
Skip tests if NEP-18 envvar is not set
nvictus Jul 17, 2019
38c4717
flake8
nvictus Jul 17, 2019
e9d9c41
Update xarray/core/dataarray.py
nvictus Jul 18, 2019
3f1eec2
Fix coords parsing
nvictus Jul 19, 2019
56864c3
More tests
nvictus Jul 19, 2019
1876892
Add align tests
nvictus Jul 19, 2019
ef51976
Replace nep18 tests with more extensive tests on pydata/sparse
nvictus Aug 2, 2019
86ee35c
Merge remote-tracking branch 'shoyer/isnull-duck' into sparse_xarrays
nvictus Aug 2, 2019
3afe7e2
Add xfails for missing np.result_type (fixed by pydata/sparse/pull/261)
nvictus Aug 2, 2019
33a07e7
Fix xpasses
nvictus Aug 2, 2019
5a817a8
Revert isnull/notnull
nvictus Aug 4, 2019
5675819
Fix as_like_arrays by coercing dense arrays to COO if any sparse
nvictus Aug 4, 2019
96e1346
Make Variable.load a no-op for non-dask duck arrays
nvictus Aug 5, 2019
563148c
Merge branch 'master' into sparse_xarrays
nvictus Aug 5, 2019
66c2f82
Add additional method tests
nvictus Aug 5, 2019
b353a0b
Fix utils.as_scalar to handle duck arrays with ndim>0
nvictus Aug 5, 2019
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
11 changes: 10 additions & 1 deletion xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ def isnull(data):
return _isnull(data)
except TypeError:
return np.zeros(data.shape, dtype=bool)
except Exception:
nvictus marked this conversation as resolved.
Show resolved Hide resolved
if hasattr(data, '__array_function__'):
return np.isnan(data)
else:
raise


transpose = _dask_or_eager_func('transpose')
Expand Down Expand Up @@ -121,7 +126,11 @@ def trapz(y, x, axis):


def asarray(data):
return data if isinstance(data, dask_array_type) else np.asarray(data)
return (
data if (isinstance(data, dask_array_type)
or hasattr(data, '__array_function__'))
else np.asarray(data)
)


def as_shared_dtype(scalars_or_arrays):
Expand Down
5 changes: 4 additions & 1 deletion xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,10 @@ def set_numpy_options(*args, **kwargs):


def short_array_repr(array):
array = np.asarray(array)

if not hasattr(array, '__array_function__'):
array = np.asarray(array)

# default to lower precision so a full (abbreviated) line can fit on
# one line with the default display_width
options = {
Expand Down
12 changes: 12 additions & 0 deletions xarray/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,9 @@ def as_indexable(array):
return PandasIndexAdapter(array)
if isinstance(array, dask_array_type):
return DaskIndexingAdapter(array)
if hasattr(array, '__array_function__'):
return NdArrayLikeIndexAdapter(array)

raise TypeError('Invalid array type: {}'.format(type(array)))


Expand Down Expand Up @@ -1180,6 +1183,15 @@ def __setitem__(self, key, value):
array[key] = value


class NdArrayLikeIndexAdapter(NumpyIndexingAdapter):
def __init__(self, array):
if not hasattr(array, '__array_function__'):
raise TypeError(
'NdArrayLikeIndexAdapter must wrap an object that implements '
'the __array_function__ protocol')
self.array = array


class DaskIndexingAdapter(ExplicitlyIndexedNDArrayMixin):
"""Wrap a dask array to support explicit indexing."""

Expand Down
5 changes: 5 additions & 0 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ def as_compatible_data(data, fastpath=False):
else:
data = np.asarray(data)

if hasattr(data, '__array_function__'):
return data
nvictus marked this conversation as resolved.
Show resolved Hide resolved

# validate whether the data is valid data types
data = np.asarray(data)

Expand Down Expand Up @@ -207,6 +210,8 @@ def _as_array_or_item(data):

TODO: remove this (replace with np.asarray) once these issues are fixed
"""
if hasattr(data, '__array_function__'):
nvictus marked this conversation as resolved.
Show resolved Hide resolved
return data
data = np.asarray(data)
if data.ndim == 0:
if data.dtype.kind == 'M':
Expand Down