Skip to content

Remove dask_array_type checks #7023

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 8 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions xarray/core/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .common import ImplementsArrayReduce, ImplementsDatasetReduce
from .ops import IncludeCumMethods, IncludeNumpySameMethods, IncludeReduceMethods
from .options import OPTIONS, _get_keep_attrs
from .pycompat import dask_array_type
from .pycompat import is_duck_array


class SupportsArithmetic:
Expand All @@ -33,20 +33,21 @@ class SupportsArithmetic:

# TODO: allow extending this with some sort of registration system
_HANDLED_TYPES = (
np.ndarray,
np.generic,
numbers.Number,
bytes,
str,
) + 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,)):
if not is_duck_array(x) and not isinstance(
x, self._HANDLED_TYPES + (SupportsArithmetic,)
):
return NotImplemented

if ufunc.signature is not None:
Expand Down
62 changes: 0 additions & 62 deletions xarray/core/dask_array_compat.py

This file was deleted.

10 changes: 6 additions & 4 deletions xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
from numpy import take, tensordot, transpose, unravel_index # noqa
from numpy import where as _where

from . import dask_array_compat, dask_array_ops, dtypes, npcompat, nputils
from . import dask_array_ops, dtypes, npcompat, nputils
from .nputils import nanfirst, nanlast
from .pycompat import cupy_array_type, dask_array_type, is_duck_dask_array
from .pycompat import cupy_array_type, is_duck_dask_array
from .utils import is_duck_array

try:
Expand Down Expand Up @@ -113,7 +113,7 @@ def isnull(data):
return zeros_like(data, dtype=bool)
else:
# at this point, array should have dtype=object
if isinstance(data, (np.ndarray, dask_array_type)):
if isinstance(data, np.ndarray):
return pandas_isnull(data)
else:
# Not reachable yet, but intended for use with other duck array
Expand Down Expand Up @@ -631,7 +631,9 @@ def sliding_window_view(array, window_shape, axis):
The rolling dimension will be placed at the last dimension.
"""
if is_duck_dask_array(array):
return dask_array_compat.sliding_window_view(array, window_shape, axis)
import dask.array as da

return da.lib.stride_tricks.sliding_window_view(array, window_shape, axis)
else:
return npcompat.sliding_window_view(array, window_shape, axis)

Expand Down
23 changes: 4 additions & 19 deletions xarray/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@

from . import dtypes, nputils, utils
from .duck_array_ops import count, fillna, isnull, where, where_method
from .pycompat import dask_array_type

try:
import dask.array as dask_array

from . import dask_array_compat
except ImportError:
dask_array = None # type: ignore[assignment]
dask_array_compat = None # type: ignore[assignment]


def _maybe_null_out(result, axis, mask, min_count=1):
Expand Down Expand Up @@ -65,34 +56,30 @@ def nanmin(a, axis=None, out=None):
if a.dtype.kind == "O":
return _nan_minmax_object("min", dtypes.get_pos_infinity(a.dtype), a, axis)

module = dask_array if isinstance(a, dask_array_type) else nputils
return module.nanmin(a, axis=axis)
return nputils.nanmin(a, axis=axis)


def nanmax(a, axis=None, out=None):
if a.dtype.kind == "O":
return _nan_minmax_object("max", dtypes.get_neg_infinity(a.dtype), a, axis)

module = dask_array if isinstance(a, dask_array_type) else nputils
return module.nanmax(a, axis=axis)
return nputils.nanmax(a, axis=axis)


def nanargmin(a, axis=None):
if a.dtype.kind == "O":
fill_value = dtypes.get_pos_infinity(a.dtype)
return _nan_argminmax_object("argmin", fill_value, a, axis=axis)

module = dask_array if isinstance(a, dask_array_type) else nputils
return module.nanargmin(a, axis=axis)
return nputils.nanargmin(a, axis=axis)


def nanargmax(a, axis=None):
if a.dtype.kind == "O":
fill_value = dtypes.get_neg_infinity(a.dtype)
return _nan_argminmax_object("argmax", fill_value, a, axis=axis)

module = dask_array if isinstance(a, dask_array_type) else nputils
return module.nanargmax(a, axis=axis)
return nputils.nanargmax(a, axis=axis)


def nansum(a, axis=None, dtype=None, out=None, min_count=None):
Expand Down Expand Up @@ -128,8 +115,6 @@ def nanmean(a, axis=None, dtype=None, out=None):
warnings.filterwarnings(
"ignore", r"Mean of empty slice", category=RuntimeWarning
)
if isinstance(a, dask_array_type):
return dask_array.nanmean(a, axis=axis, dtype=dtype)

return np.nanmean(a, axis=axis, dtype=dtype)

Expand Down
11 changes: 3 additions & 8 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
from .pycompat import (
DuckArrayModule,
cupy_array_type,
dask_array_type,
integer_types,
is_duck_dask_array,
sparse_array_type,
Expand All @@ -59,12 +58,8 @@
)

NON_NUMPY_SUPPORTED_ARRAY_TYPES = (
(
indexing.ExplicitlyIndexed,
pd.Index,
)
+ dask_array_type
+ cupy_array_type
indexing.ExplicitlyIndexed,
pd.Index,
)
# https://github.com/python/mypy/issues/224
BASIC_INDEXING_TYPES = integer_types + (slice,)
Expand Down Expand Up @@ -1150,7 +1145,7 @@ def to_numpy(self) -> np.ndarray:
data = self.data

# TODO first attempt to call .to_numpy() once some libraries implement it
if isinstance(data, dask_array_type):
if hasattr(data, "chunks"):
data = data.compute()
if isinstance(data, cupy_array_type):
data = data.get()
Expand Down