-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 __array_ufunc__ for xarray objects. #1962
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a427b80
Support __array_ufunc__ for xarray objects.
shoyer 661c5b4
add TODO note on xarray objects in out argument
shoyer 0ebbcb6
Satisfy stickler for __eq__ overload
shoyer 561ac77
Move dummy arithmetic implementations to SupportsArithemtic
shoyer 52c750d
Try again to disable flake8 warning
shoyer 0369786
Disable py3k tool on stickler-ci
shoyer 4e6ac28
Move arithmetic to its own file.
shoyer 1fc2844
Merge branch 'master' into array-ufunc
shoyer b6bed5b
Remove unused imports
shoyer 259a109
Add note on backwards incompatible changes from apply_ufunc
shoyer 8f4840e
Merge branch 'master' into array-ufunc
shoyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"""Base classes implementing arithmetic for xarray objects.""" | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import numbers | ||
|
||
import numpy as np | ||
|
||
from .options import OPTIONS | ||
from .pycompat import bytes_type, dask_array_type, unicode_type | ||
from .utils import not_implemented | ||
|
||
|
||
class SupportsArithmetic(object): | ||
"""Base class for xarray types that support arithmetic. | ||
|
||
Used by Dataset, DataArray, Variable and GroupBy. | ||
""" | ||
|
||
# TODO: implement special methods for arithmetic here rather than injecting | ||
# them in xarray/core/ops.py. Ideally, do so by inheriting from | ||
# numpy.lib.mixins.NDArrayOperatorsMixin. | ||
|
||
# TODO: allow extending this with some sort of registration system | ||
_HANDLED_TYPES = (np.ndarray, np.generic, numbers.Number, bytes_type, | ||
unicode_type) + dask_array_type | ||
|
||
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): | ||
from .computation import apply_ufunc | ||
|
||
# See the docstring example for numpy.lib.mixins.NDArrayOperatorsMixin. | ||
out = kwargs.get('out', ()) | ||
for x in inputs + out: | ||
if not isinstance(x, self._HANDLED_TYPES + (SupportsArithmetic,)): | ||
return NotImplemented | ||
|
||
if ufunc.signature is not None: | ||
raise NotImplementedError( | ||
'{} not supported: xarray objects do not directly implement ' | ||
'generalized ufuncs. Instead, use xarray.apply_ufunc.' | ||
.format(ufunc)) | ||
|
||
if method != '__call__': | ||
# TODO: support other methods, e.g., reduce and accumulate. | ||
raise NotImplementedError( | ||
'{} method for ufunc {} is not implemented on xarray objects, ' | ||
'which currently only support the __call__ method.' | ||
.format(method, ufunc)) | ||
|
||
if any(isinstance(o, SupportsArithmetic) for o in out): | ||
# TODO: implement this with logic like _inplace_binary_op. This | ||
# will be necessary to use NDArrayOperatorsMixin. | ||
raise NotImplementedError( | ||
'xarray objects are not yet supported in the `out` argument ' | ||
'for ufuncs.') | ||
|
||
join = dataset_join = OPTIONS['arithmetic_join'] | ||
|
||
return apply_ufunc(ufunc, *inputs, | ||
input_core_dims=((),) * ufunc.nin, | ||
output_core_dims=((),) * ufunc.nout, | ||
join=join, | ||
dataset_join=dataset_join, | ||
dataset_fill_value=np.nan, | ||
kwargs=kwargs, | ||
dask='allowed') | ||
|
||
# this has no runtime function - these are listed so IDEs know these | ||
# methods are defined and don't warn on these operations | ||
__lt__ = __le__ = __ge__ = __gt__ = __add__ = __sub__ = __mul__ = \ | ||
__truediv__ = __floordiv__ = __mod__ = __pow__ = __and__ = __xor__ = \ | ||
__or__ = __div__ = __eq__ = __ne__ = not_implemented |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import numbers | ||
import warnings | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
from . import dtypes, formatting, ops | ||
from .pycompat import OrderedDict, basestring, dask_array_type, suppress | ||
from .arithmetic import SupportsArithmetic | ||
from .options import OPTIONS | ||
from .pycompat import ( | ||
OrderedDict, basestring, bytes_type, dask_array_type, suppress, | ||
unicode_type) | ||
from .utils import Frozen, SortedKeysDict, not_implemented | ||
|
||
|
||
|
@@ -235,7 +240,7 @@ def get_squeeze_dims(xarray_obj, dim, axis=None): | |
return dim | ||
|
||
|
||
class BaseDataObject(AttrAccessMixin): | ||
class DataWithCoords(SupportsArithmetic, AttrAccessMixin): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W1641 Implementing eq without also implementing hash |
||
"""Shared base class for Dataset and DataArray.""" | ||
|
||
def squeeze(self, dim=None, drop=False, axis=None): | ||
|
@@ -749,12 +754,6 @@ def __enter__(self): | |
def __exit__(self, exc_type, exc_value, traceback): | ||
self.close() | ||
|
||
# this has no runtime function - these are listed so IDEs know these | ||
# methods are defined and don't warn on these operations | ||
__lt__ = __le__ = __ge__ = __gt__ = __add__ = __sub__ = __mul__ = \ | ||
__truediv__ = __floordiv__ = __mod__ = __pow__ = __and__ = __xor__ = \ | ||
__or__ = __div__ = __eq__ = __ne__ = not_implemented | ||
|
||
|
||
def full_like(other, fill_value, dtype=None): | ||
"""Return a new object with the same shape and type as a given object. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is
OPTIONS
ever used or is it needed in this scope for some reason?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops we don't need it here.