-
Notifications
You must be signed in to change notification settings - Fork 87
/
_statistical_functions.py
335 lines (283 loc) · 8.32 KB
/
_statistical_functions.py
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
from __future__ import annotations
from ._dtypes import (
_real_floating_dtypes,
_real_numeric_dtypes,
_numeric_dtypes,
# _complex_floating_dtypes,
_signed_integer_dtypes,
uint64,
int64,
float64,
# complex128,
)
from ._array_object import Array, implements_numpy
from ._manipulation_functions import squeeze
from typing import TYPE_CHECKING, Optional, Tuple, Union
if TYPE_CHECKING:
from ._typing import Dtype
from arkouda.numeric import cast as akcast
from arkouda.client import generic_msg
from arkouda.pdarrayclass import parse_single_value, create_pdarray
from arkouda.pdarraycreation import scalar_array
import numpy as np
@implements_numpy(np.max)
@implements_numpy(np.nanmax)
def max(
x: Array,
/,
*,
axis: Optional[Union[int, Tuple[int, ...]]] = None,
keepdims: bool = False,
) -> Array:
if x.dtype not in _real_numeric_dtypes:
raise TypeError("Only real numeric dtypes are allowed in max")
axis_list = []
if axis is not None:
axis_list = list(axis) if isinstance(axis, tuple) else [axis]
resp = generic_msg(
cmd=f"reduce{x.ndim}D",
args={
"x": x._array,
"op": "max",
"nAxes": len(axis_list),
"axis": axis_list,
"skipNan": True,
},
)
if axis is None or x.ndim == 1:
return Array._new(scalar_array(parse_single_value(resp)))
else:
arr = Array._new(create_pdarray(resp))
if keepdims:
return arr
else:
return squeeze(arr, axis)
# this is a temporary fix to get mean working with XArray
@implements_numpy(np.nanmean)
@implements_numpy(np.mean)
def mean_shim(x: Array, axis=None, dtype=None, out=None, keepdims=False):
return mean(x, axis=axis, keepdims=keepdims)
def mean(
x: Array,
/,
*,
axis: Optional[Union[int, Tuple[int, ...]]] = None,
keepdims: bool = False,
) -> Array:
if x.dtype not in _real_floating_dtypes:
raise TypeError("Only real floating-point dtypes are allowed in mean")
axis_list = []
if axis is not None:
axis_list = list(axis) if isinstance(axis, tuple) else [axis]
resp = generic_msg(
cmd=f"stats{x.ndim}D",
args={
"x": x._array,
"comp": "mean",
"nAxes": len(axis_list),
"axis": axis_list,
"ddof": 0,
"skipNan": True, # TODO: handle all-nan slices
},
)
if axis is None or x.ndim == 1:
return Array._new(scalar_array(parse_single_value(resp)))
else:
arr = Array._new(create_pdarray(resp))
if keepdims:
return arr
else:
return squeeze(arr, axis)
@implements_numpy(np.min)
@implements_numpy(np.nanmin)
def min(
x: Array,
/,
*,
axis: Optional[Union[int, Tuple[int, ...]]] = None,
keepdims: bool = False,
) -> Array:
if x.dtype not in _real_numeric_dtypes:
raise TypeError("Only real numeric dtypes are allowed in min")
axis_list = []
if axis is not None:
axis_list = list(axis) if isinstance(axis, tuple) else [axis]
resp = generic_msg(
cmd=f"reduce{x.ndim}D",
args={
"x": x._array,
"op": "min",
"nAxes": len(axis_list),
"axis": axis_list,
"skipNan": True,
},
)
if axis is None or x.ndim == 1:
return Array._new(scalar_array(parse_single_value(resp)))
else:
arr = Array._new(create_pdarray(resp))
if keepdims:
return arr
else:
return squeeze(arr, axis)
@implements_numpy(np.prod)
@implements_numpy(np.nanprod)
def prod(
x: Array,
/,
*,
axis: Optional[Union[int, Tuple[int, ...]]] = None,
dtype: Optional[Dtype] = None,
keepdims: bool = False,
) -> Array:
if x.dtype not in _numeric_dtypes:
raise TypeError("Only numeric dtypes are allowed in prod")
axis_list = []
if axis is not None:
axis_list = list(axis) if isinstance(axis, tuple) else [axis]
# cast to the appropriate dtype if necessary
cast_to = prod_sum_dtype(x.dtype) if dtype is None else dtype
if cast_to != x.dtype:
x_op = akcast(x._array, cast_to)
else:
x_op = x._array
resp = generic_msg(
cmd=f"reduce{x.ndim}D",
args={
"x": x_op,
"op": "prod",
"nAxes": len(axis_list),
"axis": axis_list,
"skipNan": True,
},
)
if axis is None or x.ndim == 1:
return Array._new(scalar_array(parse_single_value(resp)))
else:
arr = Array._new(create_pdarray(resp))
if keepdims:
return arr
else:
return squeeze(arr, axis)
@implements_numpy(np.nanmax)
def std(
x: Array,
/,
*,
axis: Optional[Union[int, Tuple[int, ...]]] = None,
correction: Union[int, float] = 0.0,
keepdims: bool = False,
) -> Array:
if x.dtype not in _real_floating_dtypes:
raise TypeError("Only real floating-point dtypes are allowed in std")
if correction < 0:
raise ValueError("Correction must be non-negative in std")
axis_list = []
if axis is not None:
axis_list = list(axis) if isinstance(axis, tuple) else [axis]
resp = generic_msg(
cmd=f"stats{x.ndim}D",
args={
"x": x._array,
"comp": "std",
"ddof": correction,
"nAxes": len(axis_list),
"axis": axis_list,
"skipNan": True,
},
)
if axis is None or x.ndim == 1:
return Array._new(scalar_array(parse_single_value(resp)))
else:
arr = Array._new(create_pdarray(resp))
if keepdims:
return arr
else:
return squeeze(arr, axis)
# @implements_numpy(np.sum)
# @implements_numpy(np.nansum)
def sum(
x: Array,
/,
*,
axis: Optional[Union[int, Tuple[int, ...]]] = None,
dtype: Optional[Dtype] = None,
keepdims: bool = False,
) -> Array:
if x.dtype not in _numeric_dtypes:
raise TypeError("Only numeric dtypes are allowed in sum")
axis_list = []
if axis is not None:
axis_list = list(axis) if isinstance(axis, tuple) else [axis]
# cast to the appropriate dtype if necessary
cast_to = prod_sum_dtype(x.dtype) if dtype is None else dtype
if cast_to != x.dtype:
x_op = akcast(x._array, cast_to)
else:
x_op = x._array
resp = generic_msg(
cmd=f"reduce{x.ndim}D",
args={
"x": x_op,
"op": "sum",
"nAxes": len(axis_list),
"axis": axis_list,
"skipNan": True,
},
)
if axis is None or x.ndim == 1:
return Array._new(scalar_array(parse_single_value(resp)))
else:
arr = Array._new(create_pdarray(resp))
if keepdims:
return arr
else:
return squeeze(arr, axis)
@implements_numpy(np.var)
@implements_numpy(np.nanvar)
def var(
x: Array,
/,
*,
axis: Optional[Union[int, Tuple[int, ...]]] = None,
correction: Union[int, float] = 0.0,
keepdims: bool = False,
) -> Array:
# Note: the keyword argument correction is different here
if x.dtype not in _real_floating_dtypes:
raise TypeError("Only real floating-point dtypes are allowed in var")
if correction < 0:
raise ValueError("Correction must be non-negative in std")
axis_list = []
if axis is not None:
axis_list = list(axis) if isinstance(axis, tuple) else [axis]
resp = generic_msg(
cmd=f"stats{x.ndim}D",
args={
"x": x._array,
"comp": "var",
"ddof": correction,
"nAxes": len(axis_list),
"axis": axis_list,
"skipNan": True,
},
)
if axis is None or x.ndim == 1:
return Array._new(scalar_array(parse_single_value(resp)))
else:
arr = Array._new(create_pdarray(resp))
if keepdims:
return arr
else:
return squeeze(arr, axis)
def prod_sum_dtype(dtype: Dtype) -> Dtype:
if dtype == uint64:
return dtype
elif dtype in _real_floating_dtypes:
return float64
# elif dtype in _complex_floating_dtypes:
# return complex128
elif dtype in _signed_integer_dtypes:
return int64
else:
return uint64