Skip to content

Commit 692ecce

Browse files
1himanpre-commit-ci[bot]nordme
authored
Raise warning and errors when np.nan provided to mne.stats.permutation_t_test(). (mne-tools#13813)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Erica R Peterson <38704848+nordme@users.noreply.github.com>
1 parent a6fd5cd commit 692ecce

4 files changed

Lines changed: 28 additions & 3 deletions

File tree

doc/changes/dev/13813.other.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Adding checks in :func:`mne.stats.permutation_t_test()` to warn the user when provided with bad input values, by `Himanshu Mahor`_.

mne/stats/permutations.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import numpy as np
1010

1111
from ..parallel import parallel_func
12-
from ..utils import check_random_state, logger, verbose
12+
from ..utils import _check_if_nan, check_random_state, logger, verbose
1313

1414

1515
def _max_stat(X, X2, perms, dof_scaling):
@@ -77,6 +77,7 @@ def permutation_t_test(
7777
"""
7878
from .cluster_level import _get_1samp_orders
7979

80+
_check_if_nan(X, msg="in the data array for permutations testing")
8081
n_samples, n_tests = X.shape
8182
X2 = np.mean(X**2, axis=0) # precompute moments
8283
mu0 = np.mean(X, axis=0)

mne/utils/check.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,10 +704,14 @@ def _path_like(item):
704704
return False
705705

706706

707-
def _check_if_nan(data, msg=" to be plotted"):
707+
def _check_if_nan(data, on_nan="error", msg=" to be plotted"):
708708
"""Raise if any of the values are NaN."""
709+
_check_option("on_nan", on_nan, ("error", "warn"))
709710
if not np.isfinite(data).all():
710-
raise ValueError(f"Some of the values {msg} are NaN.")
711+
if on_nan == "error":
712+
raise ValueError(f"Some of the values {msg} are NaN.")
713+
elif on_nan == "warn":
714+
warn(f"Some of the values {msg} are NaN")
711715

712716

713717
@verbose

mne/utils/tests/test_check.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
Bunch,
2121
_check_ch_locs,
2222
_check_fname,
23+
_check_if_nan,
2324
_check_info_inv,
2425
_check_option,
2526
_check_range,
@@ -206,6 +207,24 @@ def test_check_option():
206207
assert _check_option("option", "bad", ["valid"])
207208

208209

210+
def test_check_if_nan():
211+
"""Test NaN handling and option validation."""
212+
msg = (
213+
"Invalid value for the 'on_nan' parameter. "
214+
"Allowed values are 'error' and 'warn', but got 'er' instead."
215+
)
216+
nan_error_msg = r"Some of the values\s+to be plotted are NaN\."
217+
nan_warn_msg = r"Some of the values\s+to be plotted are NaN"
218+
with pytest.raises(ValueError, match=msg):
219+
_check_if_nan([0.0], on_nan="er")
220+
221+
with pytest.raises(ValueError, match=nan_error_msg):
222+
_check_if_nan([0.0, np.nan], on_nan="error")
223+
224+
with pytest.warns(RuntimeWarning, match=nan_warn_msg):
225+
_check_if_nan([0.0, np.nan], on_nan="warn")
226+
227+
209228
def test_path_like():
210229
"""Test _path_like()."""
211230
str_path = str(base_dir)

0 commit comments

Comments
 (0)