Like assert_all_finite, but only for ndarray.
(
X, allow_nan=False, msg_dtype=None, estimator_name=None, input_name=""
)
| 105 | |
| 106 | |
| 107 | def _assert_all_finite( |
| 108 | X, allow_nan=False, msg_dtype=None, estimator_name=None, input_name="" |
| 109 | ): |
| 110 | """Like assert_all_finite, but only for ndarray.""" |
| 111 | |
| 112 | xp, is_array_api = get_namespace(X) |
| 113 | |
| 114 | if _get_config()["assume_finite"]: |
| 115 | return |
| 116 | |
| 117 | X = xp.asarray(X) |
| 118 | |
| 119 | # for object dtype data, we only check for NaNs (GH-13254) |
| 120 | if not is_array_api and X.dtype == np.dtype("object") and not allow_nan: |
| 121 | if _object_dtype_isnan(X).any(): |
| 122 | raise ValueError("Input contains NaN") |
| 123 | |
| 124 | # We need only consider float arrays, hence can early return for all else. |
| 125 | if not xp.isdtype(X.dtype, ("real floating", "complex floating")): |
| 126 | return |
| 127 | |
| 128 | # First try an O(n) time, O(1) space solution for the common case that |
| 129 | # everything is finite; fall back to O(n) space `np.isinf/isnan` or custom |
| 130 | # Cython implementation to prevent false positives and provide a detailed |
| 131 | # error message. |
| 132 | with np.errstate(over="ignore"): |
| 133 | first_pass_isfinite = xp.isfinite(xp.sum(X)) |
| 134 | if first_pass_isfinite: |
| 135 | return |
| 136 | |
| 137 | _assert_all_finite_element_wise( |
| 138 | X, |
| 139 | xp=xp, |
| 140 | allow_nan=allow_nan, |
| 141 | msg_dtype=msg_dtype, |
| 142 | estimator_name=estimator_name, |
| 143 | input_name=input_name, |
| 144 | ) |
| 145 | |
| 146 | |
| 147 | def _assert_all_finite_element_wise( |
no test coverage detected
searching dependent graphs…