Skip to content

Improved duck array wrapping #9798

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 30 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
fd6b339
lots more duck array compat, plus tests
slevang Nov 18, 2024
893408c
Merge branch 'main' into more-array-api-compat
slevang Nov 18, 2024
f7866ce
merge sliding_window_view
slevang Nov 18, 2024
90037fe
namespaces constant
slevang Nov 18, 2024
5ba1a2f
revert dask allowed
slevang Nov 18, 2024
6225ae3
fix up some tests
slevang Nov 19, 2024
e2911c2
backwards compat sparse mask
slevang Nov 19, 2024
2ac37f9
add as_array methods
slevang Nov 21, 2024
1cc344b
to_like_array helper
slevang Nov 21, 2024
69080a5
Merge branch 'main' into more-array-api-compat
slevang Nov 21, 2024
372439c
only cast non-numpy
slevang Nov 21, 2024
0eef2cb
better idxminmax approach
slevang Nov 21, 2024
6739504
fix mypy
slevang Nov 21, 2024
9e6d6f8
naming, add is_array_type
slevang Nov 21, 2024
e721011
add public doc and whats new
slevang Nov 21, 2024
1fe4131
update comments
slevang Nov 21, 2024
205c199
add support for chunked arrays in as_array_type
slevang Nov 21, 2024
7752088
Merge branch 'main' into more-array-api-compat
slevang Nov 21, 2024
c8d4e5e
revert array_type methods
slevang Nov 22, 2024
e67a819
Merge branch 'main' into more-array-api-compat
slevang Nov 22, 2024
f306768
fix up whats new
slevang Nov 22, 2024
18ebdcd
comment about bool_
slevang Nov 22, 2024
f51e3fb
Merge branch 'main' into more-array-api-compat
slevang Nov 22, 2024
121af9e
add jax to complete ci envs
slevang Nov 23, 2024
472ae7e
add pint and sparse to tests
slevang Nov 23, 2024
5aa4a39
remove from windows
slevang Nov 23, 2024
390df6f
mypy, xfail one more sparse
slevang Nov 23, 2024
f6074d2
add dask and a few other methods
slevang Nov 25, 2024
561f21b
Merge branch 'main' into more-array-api-compat
slevang Nov 25, 2024
bfd6aeb
move whats new
slevang Nov 25, 2024
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
2 changes: 2 additions & 0 deletions ci/requirements/environment-3.13.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ dependencies:
- toolz
- typing_extensions
- zarr
- pip:
- jax # no way to get cpu-only jaxlib from conda if gpu is present
2 changes: 2 additions & 0 deletions ci/requirements/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ dependencies:
- toolz
- typing_extensions
- zarr
- pip:
- jax # no way to get cpu-only jaxlib from conda if gpu is present
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Trying to install via conda on my local machine with a GPU automatically gave all the CUDA libs, which seems like unnecessary bloat for dev environments. If anyone knows a way around this, happy to move it back to conda. I also got a failure on windows, I think for trying to pip install anything, so left it off of there.

3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ v.2024.11.1 (unreleased)

New Features
~~~~~~~~~~~~
- Better support wrapping additional array types (e.g. ``cupy`` or ``jax``) by calling generalized
duck array operations throughout more xarray methods. (:issue:`7848`, :pull:`9798`).
By `Sam Levang <https://github.com/slevang>`_.


Breaking changes
Expand Down
38 changes: 38 additions & 0 deletions xarray/core/array_api_compat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import numpy as np

from xarray.namedarray.pycompat import array_type


def is_weak_scalar_type(t):
return isinstance(t, bool | int | float | complex | str | bytes)
Expand Down Expand Up @@ -42,3 +44,39 @@ def result_type(*arrays_and_dtypes, xp) -> np.dtype:
return xp.result_type(*arrays_and_dtypes)
else:
return _future_array_api_result_type(*arrays_and_dtypes, xp=xp)


def get_array_namespace(*values):
def _get_single_namespace(x):
if hasattr(x, "__array_namespace__"):
return x.__array_namespace__()
elif isinstance(x, array_type("cupy")):
# cupy is fully compliant from xarray's perspective, but will not expose
# __array_namespace__ until at least v14. Special case it for now
import cupy as cp

return cp
else:
return np

namespaces = {_get_single_namespace(t) for t in values}
non_numpy = namespaces - {np}

if len(non_numpy) > 1:
names = [module.__name__ for module in non_numpy]
raise TypeError(f"Mixed array types {names} are not supported.")
elif non_numpy:
[xp] = non_numpy
else:
xp = np

return xp


def to_like_array(array, like):
# Mostly for cupy compatibility, because cupy binary ops require all cupy arrays
xp = get_array_namespace(like)
if xp is not np:
return xp.asarray(array)
# avoid casting things like pint quantities to numpy arrays
return array
4 changes: 2 additions & 2 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def clip(
keep_attrs = _get_keep_attrs(default=True)

return apply_ufunc(
np.clip, self, min, max, keep_attrs=keep_attrs, dask="allowed"
duck_array_ops.clip, self, min, max, keep_attrs=keep_attrs, dask="allowed"
)

def get_index(self, key: Hashable) -> pd.Index:
Expand Down Expand Up @@ -1760,7 +1760,7 @@ def _full_like_variable(
**from_array_kwargs,
)
else:
data = np.full_like(other.data, fill_value, dtype=dtype)
data = duck_array_ops.full_like(other.data, fill_value, dtype=dtype)

return Variable(dims=other.dims, data=data, attrs=other.attrs)

Expand Down
16 changes: 9 additions & 7 deletions xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from xarray.core import dtypes, duck_array_ops, utils
from xarray.core.alignment import align, deep_align
from xarray.core.array_api_compat import to_like_array
from xarray.core.common import zeros_like
from xarray.core.duck_array_ops import datetime_to_numeric
from xarray.core.formatting import limit_lines
Expand Down Expand Up @@ -1702,7 +1703,7 @@ def cross(
)

c = apply_ufunc(
np.cross,
duck_array_ops.cross,
a,
b,
input_core_dims=[[dim], [dim]],
Expand Down Expand Up @@ -2170,13 +2171,14 @@ def _calc_idxminmax(
chunks = dict(zip(array.dims, array.chunks, strict=True))
dask_coord = chunkmanager.from_array(array[dim].data, chunks=chunks[dim])
data = dask_coord[duck_array_ops.ravel(indx.data)]
res = indx.copy(data=duck_array_ops.reshape(data, indx.shape))
# we need to attach back the dim name
res.name = dim
else:
res = array[dim][(indx,)]
# The dim is gone but we need to remove the corresponding coordinate.
del res.coords[dim]
arr_coord = to_like_array(array[dim].data, array.data)
data = arr_coord[duck_array_ops.ravel(indx.data)]

# rebuild like the argmin/max output, and rename as the dim name
data = duck_array_ops.reshape(data, indx.shape)
res = indx.copy(data=data)
res.name = dim

if skipna or (skipna is None and array.dtype.kind in na_dtypes):
# Put the NaN values back in after removing them
Expand Down
10 changes: 6 additions & 4 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
align,
)
from xarray.core.arithmetic import DatasetArithmetic
from xarray.core.array_api_compat import to_like_array
from xarray.core.common import (
DataWithCoords,
_contains_datetime_like_objects,
Expand Down Expand Up @@ -127,7 +128,7 @@
calculate_dimensions,
)
from xarray.namedarray.parallelcompat import get_chunked_array_type, guess_chunkmanager
from xarray.namedarray.pycompat import array_type, is_chunked_array
from xarray.namedarray.pycompat import array_type, is_chunked_array, to_numpy
from xarray.plot.accessor import DatasetPlotAccessor
from xarray.util.deprecation_helpers import _deprecate_positional_args, deprecate_dims

Expand Down Expand Up @@ -6622,7 +6623,7 @@ def dropna(
array = self._variables[k]
if dim in array.dims:
dims = [d for d in array.dims if d != dim]
count += np.asarray(array.count(dims))
count += to_numpy(array.count(dims).data)
size += math.prod([self.sizes[d] for d in dims])

if thresh is not None:
Expand Down Expand Up @@ -8736,16 +8737,17 @@ def _integrate_one(self, coord, datetime_unit=None, cumulative=False):
coord_names.add(k)
else:
if k in self.data_vars and dim in v.dims:
coord_data = to_like_array(coord_var.data, like=v.data)
if _contains_datetime_like_objects(v):
v = datetime_to_numeric(v, datetime_unit=datetime_unit)
if cumulative:
integ = duck_array_ops.cumulative_trapezoid(
v.data, coord_var.data, axis=v.get_axis_num(dim)
v.data, coord_data, axis=v.get_axis_num(dim)
)
v_dims = v.dims
else:
integ = duck_array_ops.trapz(
v.data, coord_var.data, axis=v.get_axis_num(dim)
v.data, coord_data, axis=v.get_axis_num(dim)
)
v_dims = list(v.dims)
v_dims.remove(dim)
Expand Down
Loading
Loading