Skip to content
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

Add support for Array API in NamedArray #8344

Draft
wants to merge 469 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
469 commits
Select commit Hold shift + click to select a range
d3f4c14
Update _manipulation_functions.py
Illviljan Aug 25, 2024
ac20d2c
add linalg
Illviljan Aug 25, 2024
b799a9f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 25, 2024
c78031a
Add set functions
Illviljan Aug 25, 2024
b797472
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 25, 2024
d6911a2
Update _creation_functions.py
Illviljan Aug 25, 2024
91bb8fb
Merge branch 'add_array_api_mean' of https://github.com/Illviljan/xar…
Illviljan Aug 25, 2024
d9d23bb
Update __init__.py
Illviljan Aug 25, 2024
69f3061
Update _sorting_functions.py
Illviljan Aug 25, 2024
95c5bc4
Add fft
Illviljan Aug 25, 2024
4e7dffb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 25, 2024
09e1478
Update __init__.py
Illviljan Aug 25, 2024
9aca0cc
Update __init__.py
Illviljan Aug 25, 2024
a3f47a9
Update core.py
Illviljan Aug 25, 2024
3cebb62
Update _creation_functions.py
Illviljan Aug 25, 2024
042940b
Update _sorting_functions.py
Illviljan Aug 25, 2024
26fb6cb
Update core.py
Illviljan Aug 25, 2024
8e0af56
Update _utils.py
Illviljan Aug 25, 2024
3341e93
Revert "Update _utils.py"
Illviljan Aug 25, 2024
dc862c5
Update namedarray_array_api_skips.txt
Illviljan Aug 25, 2024
499bee9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 25, 2024
2d5e81c
Update _linalg.py
Illviljan Aug 25, 2024
3cee797
Merge branch 'add_array_api_mean' of https://github.com/Illviljan/xar…
Illviljan Aug 25, 2024
45abdfd
Update _indexing_functions.py
Illviljan Aug 25, 2024
d3efb66
Update _utils.py
Illviljan Aug 25, 2024
7b8592a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 25, 2024
02b4f41
Update core.py
Illviljan Aug 25, 2024
0470089
Update _utils.py
Illviljan Aug 25, 2024
5ff5f66
Update _sorting_functions.py
Illviljan Aug 25, 2024
3e1123b
fix linalg
Illviljan Aug 25, 2024
7215680
Use stable repr
Illviljan Aug 25, 2024
3c6e8c5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 25, 2024
9d0b142
Update __init__.py
Illviljan Aug 25, 2024
d277ab1
Merge branch 'add_array_api_mean' of https://github.com/Illviljan/xar…
Illviljan Aug 25, 2024
0b2f21b
stricter
Illviljan Aug 25, 2024
d54210c
stricter
Illviljan Aug 25, 2024
5622bd7
Update _elementwise_functions.py
Illviljan Aug 25, 2024
ee71d04
Update core.py
Illviljan Aug 25, 2024
ec55434
Update core.py
Illviljan Aug 25, 2024
93bd9ca
more
Illviljan Aug 25, 2024
ec0e79c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 25, 2024
1da91ea
Update _elementwise_functions.py
Illviljan Aug 25, 2024
bc4e69c
more
Illviljan Aug 25, 2024
dc37d5c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 25, 2024
b1d7a59
Update _statistical_functions.py
Illviljan Aug 25, 2024
48e5e9e
more
Illviljan Aug 25, 2024
813396a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 25, 2024
94eb7b2
Update core.py
Illviljan Aug 25, 2024
4a50a8a
Merge branch 'add_array_api_mean' of https://github.com/Illviljan/xar…
Illviljan Aug 25, 2024
ce8a60a
Update core.py
Illviljan Aug 25, 2024
bef1ead
Update core.py
Illviljan Aug 25, 2024
c13a8ae
Update _elementwise_functions.py
Illviljan Aug 25, 2024
e56e7cb
Update core.py
Illviljan Aug 26, 2024
acc4dc0
Simplify asarray
Illviljan Aug 27, 2024
2466035
promote scalars correctly
Illviljan Aug 27, 2024
a06972b
Merge branch 'main' into add_array_api_mean
Illviljan Aug 27, 2024
1dad0e3
fix
Illviljan Aug 27, 2024
a7b3a1c
all shapes less or equal than 1 are broadcastable
Illviljan Aug 28, 2024
8ad9d1f
Update _elementwise_functions.py
Illviljan Aug 28, 2024
a6091eb
Update _elementwise_functions.py
Illviljan Aug 28, 2024
9221751
simplify like functions
Illviljan Sep 1, 2024
60aa969
Update _utils.py
Illviljan Sep 1, 2024
451ac17
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 1, 2024
dc070d7
Update _creation_functions.py
Illviljan Sep 1, 2024
29fb043
Update _creation_functions.py
Illviljan Sep 1, 2024
9e36876
Update _utils.py
Illviljan Sep 1, 2024
9b681c9
add typing
Illviljan Sep 1, 2024
e9649ef
remove atleast_0d
Illviljan Sep 1, 2024
ac1265e
use set instead, if len>1 something is odd is happening
Illviljan Sep 1, 2024
407c8d6
Update _utils.py
Illviljan Sep 2, 2024
aab7b22
Update _elementwise_functions.py
Illviljan Sep 2, 2024
d82a7cc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 2, 2024
f73e7a1
typing
Illviljan Sep 2, 2024
43c81e3
Merge branch 'add_array_api_mean' of https://github.com/Illviljan/xar…
Illviljan Sep 2, 2024
bd083b2
Merge branch 'main' into add_array_api_mean
Illviljan Sep 2, 2024
b026b3c
Update _info.py
Illviljan Sep 2, 2024
8d3f192
methods
Illviljan Sep 2, 2024
221ba48
Update _manipulation_functions.py
Illviljan Sep 2, 2024
157c46d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 2, 2024
26182d2
Update _fft.py
Illviljan Sep 2, 2024
212dcdd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 2, 2024
d31fe95
Update _linalg.py
Illviljan Sep 2, 2024
d05791c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 2, 2024
5fccd5f
Update _typing.py
Illviljan Sep 2, 2024
39fa648
Update _utils.py
Illviljan Sep 2, 2024
7b80343
Merge branch 'main' into add_array_api_mean
Illviljan Sep 3, 2024
d37c44d
Make default unhashable
Illviljan Sep 3, 2024
beb3016
typing
Illviljan Sep 3, 2024
e993f3b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 3, 2024
b86ee93
Update _typing.py
Illviljan Sep 3, 2024
99426cb
Update _statistical_functions.py
Illviljan Sep 3, 2024
18d5001
Create a truly non hashable default by subclassing list
Illviljan Sep 6, 2024
4272a20
Update _utility_functions.py
Illviljan Sep 6, 2024
a6cf097
typing
Illviljan Sep 8, 2024
4fa029b
Merge branch 'main' into add_array_api_mean
Illviljan Sep 8, 2024
b1ad54c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 8, 2024
9657bf2
Merge branch 'add_array_api_mean' of https://github.com/Illviljan/xar…
Illviljan Sep 8, 2024
63db063
Update _typing.py
Illviljan Sep 8, 2024
e810921
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 8, 2024
d9b516d
Update _creation_functions.py
Illviljan Sep 8, 2024
686ef2b
Update _data_type_functions.py
Illviljan Sep 8, 2024
937724d
Update _linear_algebra_functions.py
Illviljan Sep 8, 2024
28dde6a
Update _set_functions.py
Illviljan Sep 8, 2024
9354771
Update _typing.py
Illviljan Sep 8, 2024
ee12ae5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 8, 2024
4de05f8
Using operators instead should handle inplace methods missing
Illviljan Sep 8, 2024
0328e44
Update _indexing_functions.py
Illviljan Sep 8, 2024
7f9c8bb
Update core.py
Illviljan Sep 8, 2024
6014235
Update _utils.py
Illviljan Sep 8, 2024
117548d
Update _utils.py
Illviljan Sep 8, 2024
64278e5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 8, 2024
783be0d
Update _utils.py
Illviljan Sep 8, 2024
652097b
Merge branch 'add_array_api_mean' of https://github.com/Illviljan/xar…
Illviljan Sep 8, 2024
5ce92c9
Update core.py
Illviljan Sep 8, 2024
1ba871a
Merge branch 'main' into add_array_api_mean
Illviljan Sep 8, 2024
de99d36
skip cumulative sum
Illviljan Sep 8, 2024
1835514
Update _typing.py
Illviljan Sep 8, 2024
68c271c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 8, 2024
92ee203
Update _typing.py
Illviljan Sep 8, 2024
e70fee7
Merge branch 'add_array_api_mean' of https://github.com/Illviljan/xar…
Illviljan Sep 8, 2024
019a610
Update core.py
Illviljan Sep 8, 2024
6e7de32
Update core.py
Illviljan Sep 9, 2024
c302e65
Update core.py
Illviljan Sep 9, 2024
ad6d7e4
Update _utils.py
Illviljan Sep 10, 2024
8fa6d58
Update _manipulation_functions.py
Illviljan Sep 10, 2024
b65de7a
Update _manipulation_functions.py
Illviljan Sep 11, 2024
0d0ea3b
normalize negative values to positive
Illviljan Sep 11, 2024
52343d9
more tests and fixes to broadcasted dims
Illviljan Sep 17, 2024
5e3da64
Add dims for basic array creation
Illviljan Sep 17, 2024
f45429d
add examples
Illviljan Sep 19, 2024
7efcef2
add dim keywords
Illviljan Sep 19, 2024
04b01b3
Update _manipulation_functions.py
Illviljan Sep 19, 2024
4a97364
Add arithmetic broadcasting
Illviljan Sep 19, 2024
f4f3268
Use arithmetic broadcasting
Illviljan Sep 19, 2024
656ab62
Add doctests
Illviljan Sep 19, 2024
95f8490
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 19, 2024
b9e6457
Merge branch 'main' into add_array_api_mean
Illviljan Sep 19, 2024
9d4b9d2
Update _utils.py
Illviljan Sep 19, 2024
f64f967
reflexive should broadcast as well
Illviljan Sep 19, 2024
5540702
typing
Illviljan Sep 19, 2024
b269650
Handle unordered dims
Illviljan Sep 20, 2024
218eba1
Update _manipulation_functions.py
Illviljan Sep 20, 2024
8ee6172
subclasses needs to be passed down
Illviljan Sep 20, 2024
90e4819
prioritize variable arithmetics
Illviljan Sep 20, 2024
c295100
Update indexing.py
Illviljan Sep 21, 2024
d02f4f2
add copy to __array__
Illviljan Sep 21, 2024
825805b
calculate dims from tuple indexing
Illviljan Sep 21, 2024
e29287d
Handle getitem better
Illviljan Sep 21, 2024
28df307
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 21, 2024
eb73c2d
Update _utils.py
Illviljan Sep 21, 2024
a450e8a
Update core.py
Illviljan Sep 21, 2024
30a4fea
Merge branch 'main' into add_array_api_mean
Illviljan Sep 21, 2024
9c3f6c1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 21, 2024
73a7d2f
improve dims handling
Illviljan Sep 21, 2024
e947757
Merge branch 'add_array_api_mean' of https://github.com/Illviljan/xar…
Illviljan Sep 21, 2024
45c6db0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 21, 2024
e90508e
typo
Illviljan Sep 21, 2024
93933d6
Merge branch 'add_array_api_mean' of https://github.com/Illviljan/xar…
Illviljan Sep 21, 2024
de0aa20
Update _utils.py
Illviljan Sep 21, 2024
17346f7
Update indexing.py
Illviljan Sep 21, 2024
1173cc0
Update indexing.py
Illviljan Sep 21, 2024
8a5a041
Update _manipulation_functions.py
Illviljan Sep 21, 2024
fae50aa
add examples
Illviljan Sep 21, 2024
52f7edf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 21, 2024
805a0c5
Update _manipulation_functions.py
Illviljan Sep 22, 2024
4621e26
Clarify the paths
Illviljan Sep 24, 2024
f6e90d0
Shorten name
Illviljan Sep 24, 2024
31ef484
Add atleast1d for dims, unique_values seems to need it
Illviljan Sep 24, 2024
c049f98
unique_values is atleast1d
Illviljan Sep 24, 2024
749235b
Update _manipulation_functions.py
Illviljan Sep 24, 2024
c77feb1
Update core.py
Illviljan Sep 24, 2024
fe17279
Merge branch 'main' into add_array_api_mean
Illviljan Sep 24, 2024
00115d1
Update _set_functions.py
Illviljan Sep 24, 2024
6cf9fae
Update _set_functions.py
Illviljan Sep 24, 2024
18400d5
indexing are always 1d?
Illviljan Sep 24, 2024
842b289
0D concatenation
Illviljan Sep 24, 2024
733a45a
lets try reshape again
Illviljan Sep 24, 2024
b7ceeb4
Update _utils.py
Illviljan Sep 24, 2024
43f757e
Ellipsis are often converted to slices
Illviljan Sep 25, 2024
e8b2b4e
Convert ellipsis to slices
Illviljan Sep 25, 2024
ec3880e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 25, 2024
4431175
Update _manipulation_functions.py
Illviljan Sep 25, 2024
a86d900
Merge branch 'add_array_api_mean' of https://github.com/Illviljan/xar…
Illviljan Sep 25, 2024
1267750
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 25, 2024
5bafb8c
Try simplify mean
Illviljan Sep 25, 2024
7c278dc
Merge branch 'add_array_api_mean' of https://github.com/Illviljan/xar…
Illviljan Sep 25, 2024
c59fb54
Seems successfull do it on the rest
Illviljan Sep 25, 2024
889feaa
more places to simplify
Illviljan Sep 25, 2024
f6ffdaf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 25, 2024
793d166
squeeze
Illviljan Sep 25, 2024
4ef9e1c
Merge branch 'add_array_api_mean' of https://github.com/Illviljan/xar…
Illviljan Sep 25, 2024
353d8bf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 25, 2024
0f26d51
Update _utils.py
Illviljan Sep 25, 2024
2cec2c7
typing
Illviljan Sep 25, 2024
c5a3054
Update _utils.py
Illviljan Sep 25, 2024
e3923f4
matrix_transpose
Illviljan Sep 26, 2024
b8c4ba5
Merge branch 'main' into add_array_api_mean
Illviljan Sep 26, 2024
31d8882
Update _linear_algebra_functions.py
Illviljan Sep 26, 2024
0c83ff6
simplify function names
Illviljan Sep 26, 2024
27ebb03
vecdot
Illviljan Sep 26, 2024
0022d04
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 26, 2024
b7d734a
Use normal repr
Illviljan Sep 27, 2024
1891a6c
Normal repr
Illviljan Sep 27, 2024
e77d38d
Merge branch 'main' into add_array_api_mean
Illviljan Sep 27, 2024
26de5e4
Update _statistical_functions.py
Illviljan Sep 27, 2024
5496da9
Update _linear_algebra_functions.py
Illviljan Sep 27, 2024
6499fb7
Update _set_functions.py
Illviljan Sep 27, 2024
4dba4bf
flip
Illviljan Sep 30, 2024
930c485
moveaxis
Illviljan Oct 5, 2024
2a83e70
repeat
Illviljan Oct 5, 2024
ef2843c
Update _manipulation_functions.py
Illviljan Oct 5, 2024
649118a
Update _manipulation_functions.py
Illviljan Oct 5, 2024
d7adbd8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2024
e88af20
roll
Illviljan Oct 5, 2024
557f654
Update _utils.py
Illviljan Oct 12, 2024
8c1c970
Update _utils.py
Illviljan Oct 12, 2024
19e1923
arange
Illviljan Oct 12, 2024
1844721
asarray
Illviljan Oct 12, 2024
22ed956
asarray
Illviljan Oct 12, 2024
02c1aa7
empty
Illviljan Oct 12, 2024
5f293b0
empty_like
Illviljan Oct 12, 2024
2e89630
eye
Illviljan Oct 12, 2024
e94ed05
Update _creation_functions.py
Illviljan Oct 12, 2024
468c90d
from_dlpack
Illviljan Oct 12, 2024
38b0c4b
full
Illviljan Oct 12, 2024
4163297
Update _creation_functions.py
Illviljan Oct 12, 2024
bbac8eb
full_like
Illviljan Oct 12, 2024
bab4856
linspace
Illviljan Oct 14, 2024
f01159c
Merge branch 'main' into add_array_api_mean
Illviljan Oct 14, 2024
20498ff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 14, 2024
3a0941c
meshgrid
Illviljan Oct 17, 2024
11e02c4
ones
Illviljan Oct 17, 2024
14108e2
ones_like
Illviljan Oct 17, 2024
ce633a8
tril
Illviljan Oct 17, 2024
8307e3e
triu
Illviljan Oct 17, 2024
69bd690
zeros
Illviljan Oct 17, 2024
b24d3b5
zeros_like
Illviljan Oct 17, 2024
d8a955f
Merge branch 'main' into add_array_api_mean
Illviljan Oct 17, 2024
0856ccb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 17, 2024
7ae8e68
Update core.py
Illviljan Oct 21, 2024
e8131ce
Update _utils.py
Illviljan Oct 21, 2024
437b3e0
all
Illviljan Oct 21, 2024
38cff61
any
Illviljan Oct 21, 2024
f7e870c
diff
Illviljan Oct 21, 2024
7d9918c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 21, 2024
befb4fe
Merge branch 'main' into add_array_api_mean
Illviljan Nov 17, 2024
05c2b78
handle 0 dim arrays in meshgrid
Illviljan Nov 17, 2024
1fdcea4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 17, 2024
ceb2b87
Merge branch 'main' into add_array_api_mean
Illviljan Nov 26, 2024
ad46646
Merge branch 'main' into add_array_api_mean
Illviljan Dec 10, 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
8 changes: 8 additions & 0 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ def __iter__(self: Any) -> Iterator[Any]:
raise TypeError("iteration over a 0-d array")
return self._iter()

@overload
def get_axis_num(self, dim: Iterable[Hashable]) -> tuple[int, ...]:
...

@overload
def get_axis_num(self, dim: Hashable) -> int:
...

def get_axis_num(self, dim: Hashable | Iterable[Hashable]) -> int | tuple[int, ...]:
"""Return axis number(s) corresponding to dimension(s) in this array.

Expand Down
80 changes: 79 additions & 1 deletion xarray/namedarray/_array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

from xarray.namedarray._typing import (
_arrayapi,
_AxisLike,
_Dims,
_DType,
_ScalarType,
_ShapeType,
_SupportsImag,
_SupportsReal,
)
from xarray.namedarray.core import NamedArray
from xarray.namedarray.core import NamedArray, _dims_to_axis, _get_remaining_dims

with warnings.catch_warnings():
warnings.filterwarnings(
Expand Down Expand Up @@ -144,3 +146,79 @@ def real(
xp = _get_data_namespace(x)
out = x._new(data=xp.real(x._data))
return out


# %% Statistical Functions
Copy link
Member

Choose a reason for hiding this comment

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

@Illviljan, i'm curious to hear your thoughts on adopting a similar structure/organization as that used in Numpy so as to it easier to incorporate new array API compliant functionality in the future. the new structure would group functionality under /namedarray/_array_api/*.py instead of of putting everything in _array_api.py.

e.g.

├── _creation_functions.py
├── _data_type_functions.py
├── _dtypes.py
├── _elementwise_functions.py
├── _indexing_functions.py
├── _manipulation_functions.py
├── _searching_functions.py
├── _set_functions.py
├── _sorting_functions.py
├── _statistical_functions.py
├── linalg.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! I just thought it was too few functions to bother with it right now.

Cubed has similar structure too.

Copy link
Member

Choose a reason for hiding this comment

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

perfect! i intend to add a few more functions to the array_api and i'm happy to put this structure in place



def mean(
x: NamedArray[Any, _DType],
/,
*,
dims: _Dims | None = None,
keepdims: bool = False,
axis: _AxisLike | None = None,
) -> NamedArray[Any, _DType]:
"""
Calculates the arithmetic mean of the input array x.

Parameters
----------
x :
Should have a real-valued floating-point data type.
dims :
Dim or dims along which arithmetic means must be computed. By default,
the mean must be computed over the entire array. If a tuple of hashables,
arithmetic means must be computed over multiple axes.
Default: None.
keepdims :
if True, the reduced axes (dimensions) must be included in the result
as singleton dimensions, and, accordingly, the result must be compatible
with the input array (see Broadcasting). Otherwise, if False, the
reduced axes (dimensions) must not be included in the result.
Default: False.
axis :
Axis or axes along which arithmetic means must be computed. By default,
the mean must be computed over the entire array. If a tuple of integers,
arithmetic means must be computed over multiple axes.
Default: None.

Returns
-------
out :
If the arithmetic mean was computed over the entire array,
a zero-dimensional array containing the arithmetic mean; otherwise,
a non-zero-dimensional array containing the arithmetic means.
The returned array must have the same data type as x.

Examples
--------
>>> x = NamedArray(("x", "y"), nxp.asarray([[1.0, 2.0], [3.0, 4.0]]))
>>> mean(x).data
Array(2.5, dtype=float64)
>>> mean(x, dims=("x",)).data
Array([2., 3.], dtype=float64)

Using keepdims:

>>> mean(x, dims=("x",), keepdims=True)
<xarray.NamedArray (x: 1, y: 2)>
Array([[2., 3.]], dtype=float64)
>>> mean(x, dims=("y",), keepdims=True)
<xarray.NamedArray (x: 2, y: 1)>
Array([[1.5],
[3.5]], dtype=float64)
"""
xp = _get_data_namespace(x)
axis_ = _dims_to_axis(x, dims, axis)
d = xp.mean(x._data, axis=axis_, keepdims=False) # We fix keepdims later
# TODO: Why do we need to do the keepdims ourselves?
dims_, data_ = _get_remaining_dims(x, d, axis_, keepdims=keepdims)
out = x._new(dims=dims_, data=data_)
return out


# if __name__ == "__main__":
# import doctest

# doctest.testmod()
71 changes: 63 additions & 8 deletions xarray/namedarray/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
import numpy as np

if TYPE_CHECKING:
from numpy.typing import NDArray
pass


# https://stackoverflow.com/questions/74633074/how-to-type-hint-a-generic-numpy-array
_T = TypeVar("_T")
_T_co = TypeVar("_T_co", covariant=True)


_dtype = np.dtype
_DType = TypeVar("_DType", bound=np.dtype[Any])
_DType_co = TypeVar("_DType_co", covariant=True, bound=np.dtype[Any])
# A subset of `npt.DTypeLike` that can be parametrized w.r.t. `np.generic`
Expand All @@ -48,21 +49,32 @@ def dtype(self) -> _DType_co:
]

# For unknown shapes Dask uses np.nan, array_api uses None:
_IntOrUnknown = int
_IntOrUnknown = int # Union[int, _Unknown]
Illviljan marked this conversation as resolved.
Show resolved Hide resolved
_Shape = tuple[_IntOrUnknown, ...]
_ShapeLike = Union[SupportsIndex, Sequence[SupportsIndex]]
_ShapeType = TypeVar("_ShapeType", bound=Any)
_ShapeType_co = TypeVar("_ShapeType_co", bound=Any, covariant=True)

_Axis = int
_Axes = tuple[_Axis, ...]
_AxisLike = Union[_Axis, _Axes]

_Chunks = tuple[_Shape, ...]

_Dim = Hashable
_Dims = tuple[_Dim, ...]

_DimsLike = Union[str, Iterable[_Dim]]
_AttrsLike = Union[Mapping[Any, Any], None]
_DimsLikeAgg = Union[_DimsLike, "ellipsis", None]
Illviljan marked this conversation as resolved.
Show resolved Hide resolved

_dtype = np.dtype

# https://data-apis.org/array-api/latest/API_specification/indexing.html
# TODO: np.array_api doesn't allow None for some reason, maybe they're
# recommending to use expand_dims?
_IndexKey = Union[int, slice, "ellipsis"]
_IndexKeys = tuple[Union[_IndexKey, None], ...]
_IndexKeyLike = Union[_IndexKey, _IndexKeys]

_AttrsLike = Union[Mapping[Any, Any], None]


class _SupportsReal(Protocol[_T_co]):
Expand Down Expand Up @@ -101,6 +113,11 @@ def __array__(self, dtype: None = ..., /) -> np.ndarray[Any, _DType_co]:
def __array__(self, dtype: _DType, /) -> np.ndarray[Any, _DType]:
...

def __array__(
self, dtype: _DType | None = ..., /
) -> np.ndarray[Any, _DType] | np.ndarray[Any, _DType_co]:
...


# Corresponds to np.typing.NDArray:
_Array = _array[Any, np.dtype[_ScalarType_co]]
Expand All @@ -116,6 +133,23 @@ class _arrayfunction(
Corresponds to np.ndarray.
"""

@overload
def __getitem__(self, key: _IndexKeyLike) -> Any:
...

@overload
def __getitem__(
self, key: _arrayfunction[Any, Any] | tuple[_arrayfunction[Any, Any], ...]
) -> _arrayfunction[Any, _DType_co]:
...

def __getitem__(
self,
key: _IndexKeyLike | _arrayfunction[Any, Any],
/,
) -> _arrayfunction[Any, _DType_co] | Any:
...

# TODO: Should return the same subclass but with a new dtype generic.
# https://github.com/python/typing/issues/548
def __array_ufunc__(
Expand Down Expand Up @@ -151,6 +185,27 @@ class _arrayapi(_array[_ShapeType_co, _DType_co], Protocol[_ShapeType_co, _DType
Corresponds to np.ndarray.
"""

# TODO: Only integer _arrayapi:
# def __getitem__(
# self,
# key: Union[
# int,
# slice,
# "ellipsis",
# tuple[Union[int, slice, "ellipsis", None], ...],
# _arrayapi[Any, Any],
# ],
# /,
# ) -> _arrayapi[Any, _DType_co]:
# ...

def __getitem__(
self,
key: Any,
/,
) -> _arrayapi[Any, _DType_co]:
...

def __array_namespace__(self) -> ModuleType:
...

Expand Down Expand Up @@ -238,7 +293,7 @@ class _sparsearray(
Corresponds to np.ndarray.
"""

def todense(self) -> NDArray[_ScalarType_co]:
def todense(self) -> np.ndarray[Any, _DType_co]:
...


Expand All @@ -256,7 +311,7 @@ class _sparsearrayfunction(
Corresponds to np.ndarray.
"""

def todense(self) -> NDArray[_ScalarType_co]:
def todense(self) -> np.ndarray[Any, _DType_co]:
...


Expand All @@ -274,7 +329,7 @@ class _sparsearrayapi(
Corresponds to np.ndarray.
"""

def todense(self) -> NDArray[_ScalarType_co]:
def todense(self) -> np.ndarray[Any, _DType_co]:
...


Expand Down
Loading
Loading