From 5da55f81361067a002a6ae67f64a71abd57e9201 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 21 Oct 2023 22:17:30 +0200 Subject: [PATCH 1/4] Use namedarray repr --- xarray/namedarray/_array_api.py | 46 +++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/xarray/namedarray/_array_api.py b/xarray/namedarray/_array_api.py index 69f97305686..acb87132e42 100644 --- a/xarray/namedarray/_array_api.py +++ b/xarray/namedarray/_array_api.py @@ -1,3 +1,6 @@ +from __future__ import annotations + +import warnings from types import ModuleType from typing import Any @@ -13,12 +16,23 @@ ) from xarray.namedarray.core import NamedArray +with warnings.catch_warnings(): + warnings.filterwarnings( + "ignore", + r"The numpy.array_api submodule is still experimental", + category=UserWarning, + ) + import numpy.array_api as nxp + def _get_data_namespace(x: NamedArray[Any, Any]) -> ModuleType: if isinstance(x._data, _arrayapi): return x._data.__array_namespace__() - else: - return np + + return np + + +# %% Creation Functions def astype( @@ -49,18 +63,25 @@ def astype( Examples -------- - >>> narr = NamedArray(("x",), np.array([1.5, 2.5])) - >>> astype(narr, np.dtype(int)).data - array([1, 2]) + >>> narr = NamedArray(("x",), nxp.asarray([1.5, 2.5])) + >>> narr + + Array([1.5, 2.5], dtype=float64) + >>> astype(narr, np.dtype(np.int32)) + + Array([1, 2], dtype=int32) """ if isinstance(x._data, _arrayapi): xp = x._data.__array_namespace__() - return x._new(data=xp.astype(x, dtype, copy=copy)) + return x._new(data=xp.astype(x._data, dtype, copy=copy)) # np.astype doesn't exist yet: return x._new(data=x._data.astype(dtype, copy=copy)) # type: ignore[attr-defined] +# %% Elementwise Functions + + def imag( x: NamedArray[_ShapeType, np.dtype[_SupportsImag[_ScalarType]]], / # type: ignore[type-var] ) -> NamedArray[_ShapeType, np.dtype[_ScalarType]]: @@ -83,8 +104,9 @@ def imag( Examples -------- - >>> narr = NamedArray(("x",), np.array([1 + 2j, 2 + 4j])) - >>> imag(narr).data + >>> narr = NamedArray(("x",), np.asarray([1.0 + 2j, 2 + 4j])) # TODO: Use nxp + >>> imag(narr) + array([2., 4.]) """ xp = _get_data_namespace(x) @@ -114,9 +136,11 @@ def real( Examples -------- - >>> narr = NamedArray(("x",), np.array([1 + 2j, 2 + 4j])) - >>> real(narr).data + >>> narr = NamedArray(("x",), np.asarray([1.0 + 2j, 2 + 4j])) # TODO: Use nxp + >>> real(narr) + array([1., 2.]) """ xp = _get_data_namespace(x) - return x._new(data=xp.real(x._data)) + out = x._new(data=xp.real(x._data)) + return out From b23fb59543705beea215f6ea17a1735eab286955 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 21 Oct 2023 20:21:06 +0000 Subject: [PATCH 2/4] [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 acb87132e42..ead9b2357fd 100644 --- a/xarray/namedarray/_array_api.py +++ b/xarray/namedarray/_array_api.py @@ -22,7 +22,6 @@ r"The numpy.array_api submodule is still experimental", category=UserWarning, ) - import numpy.array_api as nxp def _get_data_namespace(x: NamedArray[Any, Any]) -> ModuleType: From 3896adb3fb340a7d18ef12d04e40a3a615b44f49 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 21 Oct 2023 22:27:01 +0200 Subject: [PATCH 3/4] Update _array_api.py --- xarray/namedarray/_array_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/namedarray/_array_api.py b/xarray/namedarray/_array_api.py index acb87132e42..e205c4d4efe 100644 --- a/xarray/namedarray/_array_api.py +++ b/xarray/namedarray/_array_api.py @@ -22,7 +22,7 @@ r"The numpy.array_api submodule is still experimental", category=UserWarning, ) - import numpy.array_api as nxp + import numpy.array_api as nxp # noqa: F401 def _get_data_namespace(x: NamedArray[Any, Any]) -> ModuleType: From 016016ca0937fa2d333b43db712ceee3d89d00d5 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 21 Oct 2023 22:30:40 +0200 Subject: [PATCH 4/4] Update formatting.py --- xarray/core/formatting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index 96a767f95ac..561b8d3cc0d 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -632,7 +632,7 @@ def short_data_repr(array): return short_array_repr(array) elif is_duck_array(internal_data): return limit_lines(repr(array.data), limit=40) - elif array._in_memory: + elif getattr(array, "_in_memory", None): return short_array_repr(array) else: # internal xarray array type