|
| 1 | +"""Base classes implementing arithmetic for xarray objects.""" |
| 2 | +from __future__ import absolute_import, division, print_function |
| 3 | + |
| 4 | +import numbers |
| 5 | + |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +from .options import OPTIONS |
| 9 | +from .pycompat import bytes_type, dask_array_type, unicode_type |
| 10 | +from .utils import not_implemented |
| 11 | + |
| 12 | + |
| 13 | +class SupportsArithmetic(object): |
| 14 | + """Base class for xarray types that support arithmetic. |
| 15 | +
|
| 16 | + Used by Dataset, DataArray, Variable and GroupBy. |
| 17 | + """ |
| 18 | + |
| 19 | + # TODO: implement special methods for arithmetic here rather than injecting |
| 20 | + # them in xarray/core/ops.py. Ideally, do so by inheriting from |
| 21 | + # numpy.lib.mixins.NDArrayOperatorsMixin. |
| 22 | + |
| 23 | + # TODO: allow extending this with some sort of registration system |
| 24 | + _HANDLED_TYPES = (np.ndarray, np.generic, numbers.Number, bytes_type, |
| 25 | + unicode_type) + dask_array_type |
| 26 | + |
| 27 | + def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): |
| 28 | + from .computation import apply_ufunc |
| 29 | + |
| 30 | + # See the docstring example for numpy.lib.mixins.NDArrayOperatorsMixin. |
| 31 | + out = kwargs.get('out', ()) |
| 32 | + for x in inputs + out: |
| 33 | + if not isinstance(x, self._HANDLED_TYPES + (SupportsArithmetic,)): |
| 34 | + return NotImplemented |
| 35 | + |
| 36 | + if ufunc.signature is not None: |
| 37 | + raise NotImplementedError( |
| 38 | + '{} not supported: xarray objects do not directly implement ' |
| 39 | + 'generalized ufuncs. Instead, use xarray.apply_ufunc.' |
| 40 | + .format(ufunc)) |
| 41 | + |
| 42 | + if method != '__call__': |
| 43 | + # TODO: support other methods, e.g., reduce and accumulate. |
| 44 | + raise NotImplementedError( |
| 45 | + '{} method for ufunc {} is not implemented on xarray objects, ' |
| 46 | + 'which currently only support the __call__ method.' |
| 47 | + .format(method, ufunc)) |
| 48 | + |
| 49 | + if any(isinstance(o, SupportsArithmetic) for o in out): |
| 50 | + # TODO: implement this with logic like _inplace_binary_op. This |
| 51 | + # will be necessary to use NDArrayOperatorsMixin. |
| 52 | + raise NotImplementedError( |
| 53 | + 'xarray objects are not yet supported in the `out` argument ' |
| 54 | + 'for ufuncs.') |
| 55 | + |
| 56 | + join = dataset_join = OPTIONS['arithmetic_join'] |
| 57 | + |
| 58 | + return apply_ufunc(ufunc, *inputs, |
| 59 | + input_core_dims=((),) * ufunc.nin, |
| 60 | + output_core_dims=((),) * ufunc.nout, |
| 61 | + join=join, |
| 62 | + dataset_join=dataset_join, |
| 63 | + dataset_fill_value=np.nan, |
| 64 | + kwargs=kwargs, |
| 65 | + dask='allowed') |
| 66 | + |
| 67 | + # this has no runtime function - these are listed so IDEs know these |
| 68 | + # methods are defined and don't warn on these operations |
| 69 | + __lt__ = __le__ = __ge__ = __gt__ = __add__ = __sub__ = __mul__ = \ |
| 70 | + __truediv__ = __floordiv__ = __mod__ = __pow__ = __and__ = __xor__ = \ |
| 71 | + __or__ = __div__ = __eq__ = __ne__ = not_implemented |
0 commit comments