From ab1cd64a74d97908aeca78754a702c918c194d61 Mon Sep 17 00:00:00 2001 From: 1himan Date: Sun, 5 Apr 2026 15:45:33 +0530 Subject: [PATCH 1/3] Fixing issue-13767 --- mne/stats/permutations.py | 3 ++- mne/utils/check.py | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) 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..359b21f9a2c 100644 --- a/mne/utils/check.py +++ b/mne/utils/check.py @@ -699,10 +699,13 @@ 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.""" 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 From b8b2e472f2a3932e9993b3ab9071f14f85674a60 Mon Sep 17 00:00:00 2001 From: 1himan Date: Sun, 5 Apr 2026 16:09:19 +0530 Subject: [PATCH 2/3] Adding test --- mne/utils/check.py | 1 + mne/utils/tests/test_check.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/mne/utils/check.py b/mne/utils/check.py index 359b21f9a2c..fc3767bd92a 100644 --- a/mne/utils/check.py +++ b/mne/utils/check.py @@ -701,6 +701,7 @@ def _path_like(item): 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(): if on_nan == "error": raise ValueError(f"Some of the values {msg} are NaN.") diff --git a/mne/utils/tests/test_check.py b/mne/utils/tests/test_check.py index af59ddf4e12..1328dfbe918 100644 --- a/mne/utils/tests/test_check.py +++ b/mne/utils/tests/test_check.py @@ -21,6 +21,7 @@ _check_ch_locs, _check_fname, _check_info_inv, + _check_if_nan, _check_option, _check_range, _check_sphere, @@ -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) From 34007b542816355e9fb90f5ecea5aba80ae29360 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 5 Apr 2026 10:39:44 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/utils/tests/test_check.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mne/utils/tests/test_check.py b/mne/utils/tests/test_check.py index 1328dfbe918..f64ae2d4658 100644 --- a/mne/utils/tests/test_check.py +++ b/mne/utils/tests/test_check.py @@ -20,8 +20,8 @@ Bunch, _check_ch_locs, _check_fname, - _check_info_inv, _check_if_nan, + _check_info_inv, _check_option, _check_range, _check_sphere,