Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mne/stats/permutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions mne/utils/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions mne/utils/tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Bunch,
_check_ch_locs,
_check_fname,
_check_if_nan,
_check_info_inv,
_check_option,
_check_range,
Expand Down Expand Up @@ -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)
Expand Down
Loading