|
27 | 27 | ) |
28 | 28 | from plotpy.config import _ |
29 | 29 | from plotpy.constants import LUTAlpha |
| 30 | +from plotpy.coords import pixelround |
30 | 31 | from plotpy.interfaces import ( |
31 | 32 | IBaseImageItem, |
32 | 33 | IBasePlotItem, |
|
42 | 43 | ) |
43 | 44 | from plotpy.items.shape.rectangle import RectangleShape |
44 | 45 | from plotpy.lutrange import lut_range_threshold |
| 46 | +from plotpy.mathutils.arrayfuncs import get_nan_range |
45 | 47 | from plotpy.mathutils.colormap import FULLRANGE, get_cmap, get_cmap_name |
46 | 48 | from plotpy.styles.image import RawImageParam |
47 | 49 |
|
|
60 | 62 | LUT_MAX = float(LUT_SIZE - 1) |
61 | 63 |
|
62 | 64 |
|
63 | | -def _nanmin(data: np.ndarray) -> float: |
64 | | - """Return minimum value of data, ignoring NaNs |
65 | | -
|
66 | | - Args: |
67 | | - data: Data array |
68 | | -
|
69 | | - Returns: |
70 | | - float: Minimum value of data, ignoring NaNs |
71 | | - """ |
72 | | - if isinstance(data, np.ma.MaskedArray): |
73 | | - data = data.data |
74 | | - if data.dtype.name in ("float32", "float64", "float128"): |
75 | | - return np.nanmin(data) |
76 | | - else: |
77 | | - return data.min() |
78 | | - |
79 | | - |
80 | | -def _nanmax(data: np.ndarray) -> float: |
81 | | - """Return maximum value of data, ignoring NaNs |
82 | | -
|
83 | | - Args: |
84 | | - data: Data array |
85 | | -
|
86 | | - Returns: |
87 | | - float: Maximum value of data, ignoring NaNs |
88 | | - """ |
89 | | - if isinstance(data, np.ma.MaskedArray): |
90 | | - data = data.data |
91 | | - if data.dtype.name in ("float32", "float64", "float128"): |
92 | | - return np.nanmax(data) |
93 | | - else: |
94 | | - return data.max() |
95 | | - |
96 | | - |
97 | | -def pixelround(x: float, corner: str | None = None) -> int: |
98 | | - """Get pixel index from pixel coordinate |
99 | | -
|
100 | | - Args: |
101 | | - x: Pixel coordinate |
102 | | - corner: None (not a corner), 'TL' (top-left corner), |
103 | | - 'BR' (bottom-right corner) |
104 | | -
|
105 | | - Returns: |
106 | | - int: Pixel index |
107 | | - """ |
108 | | - assert corner is None or corner in ("TL", "BR") |
109 | | - if corner is None: |
110 | | - return np.floor(x) |
111 | | - elif corner == "BR": |
112 | | - return np.ceil(x) |
113 | | - elif corner == "TL": |
114 | | - return np.floor(x) |
115 | | - |
116 | | - |
117 | 65 | class BaseImageItem(QwtPlotItem): |
118 | 66 | """Base class for image items |
119 | 67 |
|
@@ -391,7 +339,7 @@ def set_data( |
391 | 339 | if lut_range is not None: |
392 | 340 | _min, _max = lut_range |
393 | 341 | else: |
394 | | - _min, _max = _nanmin(data), _nanmax(data) |
| 342 | + _min, _max = get_nan_range(data) |
395 | 343 |
|
396 | 344 | self.data = data |
397 | 345 | self.histogram_cache = None |
@@ -592,7 +540,7 @@ def get_lut_range_full(self) -> tuple[float, float]: |
592 | 540 | Returns: |
593 | 541 | tuple[float, float]: Lut range, tuple(min, max) |
594 | 542 | """ |
595 | | - return _nanmin(self.data), _nanmax(self.data) |
| 543 | + return get_nan_range(self.data) |
596 | 544 |
|
597 | 545 | def get_lut_range_max(self) -> tuple[float, float]: |
598 | 546 | """Get maximum range for this dataset |
@@ -1019,8 +967,7 @@ def get_histogram(self, nbins: int) -> tuple[np.ndarray, np.ndarray]: |
1019 | 967 | res = np.histogram(self.data[~np.isnan(self.data)], nbins) |
1020 | 968 | else: |
1021 | 969 | # TODO: _histogram is faster, but caching is buggy in this version |
1022 | | - _min = _nanmin(self.data) |
1023 | | - _max = _nanmax(self.data) |
| 970 | + _min, _max = get_nan_range(self.data) |
1024 | 971 | if self.data.dtype in (np.float64, np.float32): |
1025 | 972 | bins = np.unique( |
1026 | 973 | np.array( |
|
0 commit comments