|
30 | 30 | """ # noqa: E501 |
31 | 31 |
|
32 | 32 | import inspect |
33 | | -import math |
34 | 33 | import textwrap |
35 | 34 | from functools import wraps |
36 | 35 |
|
@@ -119,17 +118,20 @@ def val_in_range(self, val): |
119 | 118 | """ |
120 | 119 | Return whether the value(s) are within the valid range for this scale. |
121 | 120 |
|
122 | | - This method is a generic implementation. Subclasses may implement more |
123 | | - efficient solutions for their domain. |
124 | | - """ |
125 | | - try: |
126 | | - if not math.isfinite(val): |
127 | | - return False |
| 121 | + Accepts a scalar or array-like ``val``. For a scalar, returns a |
| 122 | + Python ``bool``. For an array, returns a bool ndarray of the same |
| 123 | + shape. This is a generic implementation, and subclasses may implement |
| 124 | + more efficient solutions for their domain. |
| 125 | + """ |
| 126 | + arr = np.asarray(val) |
| 127 | + with np.errstate(invalid='ignore'): |
| 128 | + try: |
| 129 | + vmin, vmax = self.limit_range_for_scale(arr, arr, minpos=1e-300) |
| 130 | + except (TypeError, ValueError): |
| 131 | + result = np.zeros(arr.shape, dtype=bool) |
128 | 132 | else: |
129 | | - vmin, vmax = self.limit_range_for_scale(val, val, minpos=1e-300) |
130 | | - return vmin == val and vmax == val |
131 | | - except (TypeError, ValueError): |
132 | | - return False |
| 133 | + result = np.isfinite(arr) & (vmin == arr) & (vmax == arr) |
| 134 | + return bool(result) if arr.ndim == 0 else result |
133 | 135 |
|
134 | 136 |
|
135 | 137 | def _make_axis_parameter_optional(init_func): |
@@ -219,11 +221,13 @@ def get_transform(self): |
219 | 221 |
|
220 | 222 | def val_in_range(self, val): |
221 | 223 | """ |
222 | | - Return whether the value is within the valid range for this scale. |
| 224 | + Return whether the value(s) are within the valid range for this scale. |
223 | 225 |
|
224 | 226 | This is True for all values, except +-inf and NaN. |
225 | 227 | """ |
226 | | - return math.isfinite(val) |
| 228 | + arr = np.asarray(val) |
| 229 | + result = np.isfinite(arr) |
| 230 | + return bool(result) if arr.ndim == 0 else result |
227 | 231 |
|
228 | 232 |
|
229 | 233 | class FuncTransform(Transform): |
@@ -431,11 +435,14 @@ def limit_range_for_scale(self, vmin, vmax, minpos): |
431 | 435 |
|
432 | 436 | def val_in_range(self, val): |
433 | 437 | """ |
434 | | - Return whether the value is within the valid range for this scale. |
| 438 | + Return whether the value(s) are within the valid range for this scale. |
435 | 439 |
|
436 | 440 | This is True for value(s) > 0 except +inf and NaN. |
437 | 441 | """ |
438 | | - return math.isfinite(val) and val > 0 |
| 442 | + arr = np.asarray(val) |
| 443 | + with np.errstate(invalid='ignore'): |
| 444 | + result = np.isfinite(arr) & (arr > 0) |
| 445 | + return bool(result) if arr.ndim == 0 else result |
439 | 446 |
|
440 | 447 |
|
441 | 448 | class FuncScaleLog(LogScale): |
@@ -625,11 +632,13 @@ def get_transform(self): |
625 | 632 |
|
626 | 633 | def val_in_range(self, val): |
627 | 634 | """ |
628 | | - Return whether the value is within the valid range for this scale. |
| 635 | + Return whether the value(s) are within the valid range for this scale. |
629 | 636 |
|
630 | 637 | This is True for all values, except +-inf and NaN. |
631 | 638 | """ |
632 | | - return math.isfinite(val) |
| 639 | + arr = np.asarray(val) |
| 640 | + result = np.isfinite(arr) |
| 641 | + return bool(result) if arr.ndim == 0 else result |
633 | 642 |
|
634 | 643 |
|
635 | 644 | class AsinhTransform(Transform): |
@@ -759,11 +768,13 @@ def set_default_locators_and_formatters(self, axis): |
759 | 768 |
|
760 | 769 | def val_in_range(self, val): |
761 | 770 | """ |
762 | | - Return whether the value is within the valid range for this scale. |
| 771 | + Return whether the value(s) are within the valid range for this scale. |
763 | 772 |
|
764 | 773 | This is True for all values, except +-inf and NaN. |
765 | 774 | """ |
766 | | - return math.isfinite(val) |
| 775 | + arr = np.asarray(val) |
| 776 | + result = np.isfinite(arr) |
| 777 | + return bool(result) if arr.ndim == 0 else result |
767 | 778 |
|
768 | 779 |
|
769 | 780 | class LogitTransform(Transform): |
@@ -880,11 +891,14 @@ def limit_range_for_scale(self, vmin, vmax, minpos): |
880 | 891 |
|
881 | 892 | def val_in_range(self, val): |
882 | 893 | """ |
883 | | - Return whether the value is within the valid range for this scale. |
| 894 | + Return whether the value(s) are within the valid range for this scale. |
884 | 895 |
|
885 | 896 | This is True for value(s) which are between 0 and 1 (excluded). |
886 | 897 | """ |
887 | | - return 0 < val < 1 |
| 898 | + arr = np.asarray(val) |
| 899 | + with np.errstate(invalid='ignore'): |
| 900 | + result = (0 < arr) & (arr < 1) |
| 901 | + return bool(result) if arr.ndim == 0 else result |
888 | 902 |
|
889 | 903 |
|
890 | 904 | _scale_mapping = { |
|
0 commit comments