From 7c75cf729bdbb8c831023b6e24987fb557fa84f4 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:44:58 +0200 Subject: [PATCH 1/6] Use np.imag and np.real when possible --- xarray/namedarray/_array_api.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/xarray/namedarray/_array_api.py b/xarray/namedarray/_array_api.py index acbfc8af4f1..8aef2e42ad6 100644 --- a/xarray/namedarray/_array_api.py +++ b/xarray/namedarray/_array_api.py @@ -1,5 +1,6 @@ from __future__ import annotations +from packaging.version import Version from types import ModuleType from typing import Any @@ -71,7 +72,7 @@ def astype( xp = x._data.__array_namespace__() return x._new(data=xp.astype(x._data, dtype, copy=copy)) - # np.astype doesn't exist yet: + # TODO: np.astype only exists in np 2.0.0: return x._new(data=x._data.astype(dtype, copy=copy)) # type: ignore[attr-defined] @@ -105,9 +106,12 @@ def imag( Size: 16B array([2., 4.]) """ - xp = _get_data_namespace(x) - out = x._new(data=xp.imag(x._data)) - return out + if isinstance(x._data, _arrayapi): + xp = x._data.__array_namespace__() + return x._new(data=xp.imag(x._data)) + + # TODO: np.imag only exists in np 2.0.0: + return x._new(data=x._data.imag()) # type: ignore[attr-defined] def real( @@ -137,9 +141,12 @@ def real( Size: 16B array([1., 2.]) """ - xp = _get_data_namespace(x) - out = x._new(data=xp.real(x._data)) - return out + if isinstance(x._data, _arrayapi): + xp = x._data.__array_namespace__() + return x._new(data=xp.real(x._data)) + + # TODO: np.real only exists in np 2.0.0: + return x._new(data=x._data.real()) # type: ignore[attr-defined] # %% Manipulation functions From 8b7d852372c56e4fc2b49cacb43cec4927b1cf1e Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:45:22 +0200 Subject: [PATCH 2/6] Use array_api function --- xarray/namedarray/core.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/xarray/namedarray/core.py b/xarray/namedarray/core.py index c5841f6913e..b2bb097cefd 100644 --- a/xarray/namedarray/core.py +++ b/xarray/namedarray/core.py @@ -563,12 +563,9 @@ def imag( -------- numpy.ndarray.imag """ - if isinstance(self._data, _arrayapi): - from xarray.namedarray._array_api import imag + from xarray.namedarray._array_api import imag - return imag(self) - - return self._new(data=self._data.imag) + return imag(self) @property def real( @@ -581,11 +578,9 @@ def real( -------- numpy.ndarray.real """ - if isinstance(self._data, _arrayapi): - from xarray.namedarray._array_api import real + from xarray.namedarray._array_api import real - return real(self) - return self._new(data=self._data.real) + return real(self) def __dask_tokenize__(self) -> object: # Use v.data, instead of v._data, in order to cope with the wrappers From 7ade01603b718be77095ec01673f2684889dce7e Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:45:52 +0200 Subject: [PATCH 3/6] Remove imag and real requirement from duckarray protocol --- xarray/namedarray/_typing.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/xarray/namedarray/_typing.py b/xarray/namedarray/_typing.py index 57b17385558..960d1dea4a8 100644 --- a/xarray/namedarray/_typing.py +++ b/xarray/namedarray/_typing.py @@ -184,12 +184,6 @@ def __array_function__( kwargs: Mapping[str, Any], ) -> Any: ... - @property - def imag(self) -> _arrayfunction[_ShapeType_co, Any]: ... - - @property - def real(self) -> _arrayfunction[_ShapeType_co, Any]: ... - @runtime_checkable class _arrayapi(_array[_ShapeType_co, _DType_co], Protocol[_ShapeType_co, _DType_co]): From 3a9a57d48d61afd44557b8450c5bb2982d3cfd53 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 15 Aug 2024 20:47:26 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/namedarray/_array_api.py | 1 - 1 file changed, 1 deletion(-) diff --git a/xarray/namedarray/_array_api.py b/xarray/namedarray/_array_api.py index 8aef2e42ad6..53ef7172a70 100644 --- a/xarray/namedarray/_array_api.py +++ b/xarray/namedarray/_array_api.py @@ -1,6 +1,5 @@ from __future__ import annotations -from packaging.version import Version from types import ModuleType from typing import Any From 7390807fcf84a0621fcee4ebfe8790660aa45495 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:52:15 +0200 Subject: [PATCH 5/6] Update _array_api.py --- xarray/namedarray/_array_api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/namedarray/_array_api.py b/xarray/namedarray/_array_api.py index 8aef2e42ad6..8fd3c544f0b 100644 --- a/xarray/namedarray/_array_api.py +++ b/xarray/namedarray/_array_api.py @@ -111,7 +111,7 @@ def imag( return x._new(data=xp.imag(x._data)) # TODO: np.imag only exists in np 2.0.0: - return x._new(data=x._data.imag()) # type: ignore[attr-defined] + return x._new(data=x._data.imag) # type: ignore[attr-defined] def real( @@ -146,7 +146,7 @@ def real( return x._new(data=xp.real(x._data)) # TODO: np.real only exists in np 2.0.0: - return x._new(data=x._data.real()) # type: ignore[attr-defined] + return x._new(data=x._data.real) # type: ignore[attr-defined] # %% Manipulation functions From 1a4d3e8e047cac3e4faaf6dcbdf2fa6efe101c9e Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 15 Aug 2024 22:54:06 +0200 Subject: [PATCH 6/6] np 2 --- xarray/namedarray/_array_api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/namedarray/_array_api.py b/xarray/namedarray/_array_api.py index c757b98e926..b2434cb4cde 100644 --- a/xarray/namedarray/_array_api.py +++ b/xarray/namedarray/_array_api.py @@ -71,7 +71,7 @@ def astype( xp = x._data.__array_namespace__() return x._new(data=xp.astype(x._data, dtype, copy=copy)) - # TODO: np.astype only exists in np 2.0.0: + # TODO: np.astype only exists in np 2: return x._new(data=x._data.astype(dtype, copy=copy)) # type: ignore[attr-defined] @@ -109,7 +109,7 @@ def imag( xp = x._data.__array_namespace__() return x._new(data=xp.imag(x._data)) - # TODO: np.imag only exists in np 2.0.0: + # TODO: np.imag only exists in np 2: return x._new(data=x._data.imag) # type: ignore[attr-defined] @@ -144,7 +144,7 @@ def real( xp = x._data.__array_namespace__() return x._new(data=xp.real(x._data)) - # TODO: np.real only exists in np 2.0.0: + # TODO: np.real only exists in np 2: return x._new(data=x._data.real) # type: ignore[attr-defined]