-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path__init__.py
More file actions
293 lines (246 loc) · 7.34 KB
/
__init__.py
File metadata and controls
293 lines (246 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# SPDX-License-Identifier: MPL-2.0
"""Statistics utilities for 2D arrays.
All of these allow you to specify an ``axis``,
which allows you to choose whether to compute the statistic across rows, columns, or all elements.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, overload
from .._checks import check_dask_sparray_support
from .._validation import validate_axis
from ..typing import CpuArray, DiskArray, GpuArray # noqa: TC001
if TYPE_CHECKING:
from typing import Any, Literal
import numpy as np
from numpy.typing import DTypeLike, NDArray
from optype.numpy import ToDType
from .. import types
__all__ = ["is_constant", "mean", "mean_var", "sum"]
@overload
def is_constant(
x: NDArray[Any] | types.CSBase | types.CupyArray, /, *, axis: None = None
) -> bool: ...
@overload
def is_constant(x: NDArray[Any] | types.CSBase, /, *, axis: Literal[0, 1]) -> NDArray[np.bool]: ...
@overload
def is_constant(x: types.CupyArray, /, *, axis: Literal[0, 1]) -> types.CupyArray: ...
@overload
def is_constant(x: types.DaskArray, /, *, axis: Literal[0, 1, None] = None) -> types.DaskArray: ...
@check_dask_sparray_support
def is_constant(
x: NDArray[Any] | types.CSBase | types.CupyArray | types.DaskArray,
/,
*,
axis: Literal[0, 1, None] = None,
) -> bool | NDArray[np.bool] | types.CupyArray | types.DaskArray:
"""Check whether values in array are constant.
Parameters
----------
x
Array to check.
axis
Axis to reduce over.
Returns
-------
If ``axis`` is :data:`None`, return if all values were constant.
Else returns a boolean array with :data:`True` representing constant columns/rows.
Example
-------
>>> import numpy as np
>>> x = np.array([
... [0, 1, 2],
... [0, 0, 0],
... ])
>>> is_constant(x)
False
>>> is_constant(x, axis=0)
array([ True, False, False])
>>> is_constant(x, axis=1)
array([False, True])
"""
from ._is_constant import is_constant_
validate_axis(axis)
return is_constant_(x, axis=axis)
# TODO(flying-sheep): support CSDataset (TODO)
# https://github.com/scverse/fast-array-utils/issues/52
@overload
def mean(
x: CpuArray | GpuArray | DiskArray,
/,
*,
axis: Literal[None] = None,
dtype: DTypeLike | None = None,
) -> np.number[Any]: ...
@overload
def mean(
x: CpuArray | DiskArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None
) -> NDArray[np.number[Any]]: ...
@overload
def mean(
x: GpuArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None
) -> types.CupyArray: ...
@overload
def mean(
x: types.DaskArray, /, *, axis: Literal[0, 1], dtype: ToDType[Any] | None = None
) -> types.DaskArray: ...
@check_dask_sparray_support
def mean(
x: CpuArray | GpuArray | DiskArray | types.DaskArray,
/,
*,
axis: Literal[0, 1, None] = None,
dtype: DTypeLike | None = None,
) -> NDArray[np.number[Any]] | types.CupyArray | np.number[Any] | types.DaskArray:
"""Mean over both or one axis.
Parameters
----------
x
Array to calculate mean(s) for.
axis
Axis to reduce over.
Returns
-------
If ``axis`` is :data:`None`, then the sum over all elements is returned as a scalar.
Otherwise, the sum over the given axis is returned as a 1D array.
Example
-------
>>> import numpy as np
>>> x = np.array([
... [0, 1, 2],
... [0, 0, 0],
... ])
>>> mean(x)
np.float64(0.5)
>>> mean(x, axis=0)
array([0. , 0.5, 1. ])
>>> mean(x, axis=1)
array([1., 0.])
See Also
--------
:func:`numpy.mean`
"""
from ._mean import mean_
validate_axis(axis)
return mean_(x, axis=axis, dtype=dtype) # type: ignore[no-any-return] # literally the same type, wtf mypy
@overload
def mean_var(
x: CpuArray | GpuArray, /, *, axis: Literal[None] = None, correction: int = 0
) -> tuple[np.float64, np.float64]: ...
@overload
def mean_var(
x: CpuArray, /, *, axis: Literal[0, 1], correction: int = 0
) -> tuple[NDArray[np.float64], NDArray[np.float64]]: ...
@overload
def mean_var(
x: GpuArray, /, *, axis: Literal[0, 1], correction: int = 0
) -> tuple[types.CupyArray, types.CupyArray]: ...
@overload
def mean_var(
x: types.DaskArray, /, *, axis: Literal[0, 1, None] = None, correction: int = 0
) -> tuple[types.DaskArray, types.DaskArray]: ...
@check_dask_sparray_support
def mean_var(
x: CpuArray | GpuArray | types.DaskArray,
/,
*,
axis: Literal[0, 1, None] = None,
correction: int = 0,
) -> (
tuple[np.float64, np.float64]
| tuple[NDArray[np.float64], NDArray[np.float64]]
| tuple[types.CupyArray, types.CupyArray]
| tuple[types.DaskArray, types.DaskArray]
):
"""Mean and variance over both or one axis.
Parameters
----------
x
Array to compute mean and variance for.
axis
Axis to reduce over.
correction
Degrees of freedom correction.
Returns
-------
mean
See below:
var
If ``axis`` is :data:`None`,
the mean and variance over all elements are returned as scalars.
Otherwise, the means and variances over the given axis are returned as 1D arrays.
Example
-------
>>> import numpy as np
>>> x = np.array([
... [0, 1, 2],
... [0, 0, 0],
... ])
>>> mean_var(x) # doctest: +FLOAT_CMP
(np.float64(0.5), np.float64(0.5833333333333334))
>>> mean_var(x, axis=0)
(array([0. , 0.5, 1. ]), array([0. , 0.25, 1. ]))
>>> mean_var(x, axis=1)
(array([1., 0.]), array([0.66666667, 0. ]))
See Also
--------
:func:`numpy.mean`
:func:`numpy.var`
"""
from ._mean_var import mean_var_
return mean_var_(x, axis=axis, correction=correction) # type: ignore[no-any-return]
# TODO(flying-sheep): support CSDataset (TODO)
# https://github.com/scverse/fast-array-utils/issues/52
@overload
def sum(
x: CpuArray | GpuArray | DiskArray, /, *, axis: None = None, dtype: DTypeLike | None = None
) -> np.number[Any]: ...
@overload
def sum(
x: CpuArray | DiskArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None
) -> NDArray[Any]: ...
@overload
def sum(
x: GpuArray, /, *, axis: Literal[0, 1], dtype: DTypeLike | None = None
) -> types.CupyArray: ...
@overload
def sum(
x: types.DaskArray, /, *, axis: Literal[0, 1, None] = None, dtype: DTypeLike | None = None
) -> types.DaskArray: ...
@check_dask_sparray_support
def sum(
x: CpuArray | GpuArray | DiskArray | types.DaskArray,
/,
*,
axis: Literal[0, 1, None] = None,
dtype: DTypeLike | None = None,
) -> NDArray[Any] | types.CupyArray | np.number[Any] | types.DaskArray:
"""Sum over both or one axis.
Parameters
----------
x
Array to sum up.
axis
Axis to reduce over.
Returns
-------
If ``axis`` is :data:`None`, then the sum over all elements is returned as a scalar.
Otherwise, the sum over the given axis is returned as a 1D array.
Example
-------
>>> import numpy as np
>>> x = np.array([
... [0, 1, 2],
... [0, 0, 0],
... ])
>>> sum(x)
np.int64(3)
>>> sum(x, axis=0)
array([0, 1, 2])
>>> sum(x, axis=1)
array([3, 0])
See Also
--------
:func:`numpy.sum`
"""
from ._sum import sum_
validate_axis(axis)
return sum_(x, axis=axis, dtype=dtype)