-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patharrayfuncs.py
More file actions
74 lines (53 loc) · 1.63 KB
/
arrayfuncs.py
File metadata and controls
74 lines (53 loc) · 1.63 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
# -*- coding: utf-8 -*-
"""
Array functions
---------------
Overview
^^^^^^^^
The :py:mod:`.arrayfuncs` module provides miscellaneous array functions.
The following functions are available:
* :py:func:`.get_nan_min`
* :py:func:`.get_nan_max`
* :py:func:`.get_nan_range`
Reference
^^^^^^^^^
.. autofunction:: get_nan_min
.. autofunction:: get_nan_max
.. autofunction:: get_nan_range
"""
from __future__ import annotations
import numpy as np
def get_nan_min(data: np.ndarray | np.ma.MaskedArray) -> float:
"""Return minimum value of data, ignoring NaNs
Args:
data: Data array (or masked array)
Returns:
float: Minimum value of data, ignoring NaNs
"""
if isinstance(data, np.ma.MaskedArray):
data = data.data
if data.dtype.name in ("float32", "float64", "float128"):
return np.nanmin(data)
else:
return data.min()
def get_nan_max(data: np.ndarray | np.ma.MaskedArray) -> float:
"""Return maximum value of data, ignoring NaNs
Args:
data: Data array (or masked array)
Returns:
float: Maximum value of data, ignoring NaNs
"""
if isinstance(data, np.ma.MaskedArray):
data = data.data
if data.dtype.name in ("float32", "float64", "float128"):
return np.nanmax(data)
else:
return data.max()
def get_nan_range(data: np.ndarray | np.ma.MaskedArray) -> tuple[float, float]:
"""Return range of data, i.e. (min, max), ignoring NaNs
Args:
data: Data array (or masked array)
Returns:
tuple: Minimum and maximum value of data, ignoring NaNs
"""
return get_nan_min(data), get_nan_max(data)