diff --git a/mne/stats/permutations.py b/mne/stats/permutations.py index a49d3daca74..606f55e556e 100644 --- a/mne/stats/permutations.py +++ b/mne/stats/permutations.py @@ -9,7 +9,7 @@ import numpy as np from ..parallel import parallel_func -from ..utils import check_random_state, logger, verbose +from ..utils import _check_if_nan, check_random_state, logger, verbose def _max_stat(X, X2, perms, dof_scaling): @@ -77,6 +77,7 @@ def permutation_t_test( """ from .cluster_level import _get_1samp_orders + _check_if_nan(X) n_samples, n_tests = X.shape X2 = np.mean(X**2, axis=0) # precompute moments mu0 = np.mean(X, axis=0) diff --git a/mne/utils/check.py b/mne/utils/check.py index 85c5c62bdf7..fc3767bd92a 100644 --- a/mne/utils/check.py +++ b/mne/utils/check.py @@ -699,10 +699,14 @@ def _path_like(item): return False -def _check_if_nan(data, msg=" to be plotted"): +def _check_if_nan(data, on_nan="error", msg=" to be plotted"): """Raise if any of the values are NaN.""" + _check_option("on_nan", on_nan, ("error", "warn")) if not np.isfinite(data).all(): - raise ValueError(f"Some of the values {msg} are NaN.") + if on_nan == "error": + raise ValueError(f"Some of the values {msg} are NaN.") + elif on_nan == "warn": + warn(f"Some of the values {msg} are NaN") @verbose diff --git a/mne/utils/tests/test_check.py b/mne/utils/tests/test_check.py index af59ddf4e12..f64ae2d4658 100644 --- a/mne/utils/tests/test_check.py +++ b/mne/utils/tests/test_check.py @@ -20,6 +20,7 @@ Bunch, _check_ch_locs, _check_fname, + _check_if_nan, _check_info_inv, _check_option, _check_range, @@ -206,6 +207,24 @@ def test_check_option(): assert _check_option("option", "bad", ["valid"]) +def test_check_if_nan(): + """Test NaN handling and option validation.""" + msg = ( + "Invalid value for the 'on_nan' parameter. " + "Allowed values are 'error' and 'warn', but got 'er' instead." + ) + nan_error_msg = r"Some of the values\s+to be plotted are NaN\." + nan_warn_msg = r"Some of the values\s+to be plotted are NaN" + with pytest.raises(ValueError, match=msg): + _check_if_nan([0.0], on_nan="er") + + with pytest.raises(ValueError, match=nan_error_msg): + _check_if_nan([0.0, np.nan], on_nan="error") + + with pytest.warns(RuntimeWarning, match=nan_warn_msg): + _check_if_nan([0.0, np.nan], on_nan="warn") + + def test_path_like(): """Test _path_like().""" str_path = str(base_dir)