Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of tails in permutation test (issue 11511) #12216

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
17 changes: 12 additions & 5 deletions mne/stats/permutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ def _max_stat(X, X2, perms, dof_scaling):
n_samples = len(X)
mus = np.dot(perms, X) / float(n_samples)
stds = np.sqrt(X2[None, :] - mus * mus) * dof_scaling # std with splitting
max_abs = np.max(np.abs(mus) / (stds / sqrt(n_samples)), axis=1) # t-max
tvals = mus / (stds / sqrt(n_samples))
max_abs = np.squeeze(
np.take_along_axis(
tvals, np.argmax(np.abs(tvals), axis=1, keepdims=True), axis=1
)
)
return max_abs


Expand Down Expand Up @@ -86,22 +91,24 @@ def permutation_t_test(
T_obs = np.mean(X, axis=0) / (std0 / sqrt(n_samples))
rng = check_random_state(seed)
orders, _, extra = _get_1samp_orders(n_samples, n_permutations, tail, rng)
perms = 2 * np.array(orders) - 1 # from 0, 1 -> 1, -1
perms = 2 * np.array(orders) - 1 # from 0, 1 -> -1, 1
logger.info("Permuting %d times%s..." % (len(orders), extra))
parallel, my_max_stat, n_jobs = parallel_func(_max_stat, n_jobs)
max_abs = np.concatenate(
parallel(
my_max_stat(X, X2, p, dof_scaling) for p in np.array_split(perms, n_jobs)
)
)
max_abs = np.concatenate((max_abs, [np.abs(T_obs).max()]))
max_abs = np.concatenate(
(max_abs, np.atleast_1d(T_obs.flat[np.abs(T_obs).argmax()]))
)
H0 = np.sort(max_abs)
if tail == 0:
p_values = (H0 >= np.abs(T_obs[:, np.newaxis])).mean(-1)
p_values = (np.abs(H0) >= np.abs(T_obs[:, np.newaxis])).mean(-1)
elif tail == 1:
p_values = (H0 >= T_obs[:, np.newaxis]).mean(-1)
elif tail == -1:
p_values = (-H0 <= T_obs[:, np.newaxis]).mean(-1)
p_values = (H0 <= T_obs[:, np.newaxis]).mean(-1)
return T_obs, p_values, H0


Expand Down
10 changes: 1 addition & 9 deletions mne/stats/tests/test_permutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,7 @@ def test_permutation_t_test():

@pytest.mark.parametrize(
"tail_name,tail_code",
[
("two-sided", 0),
pytest.param(
"less", -1, marks=pytest.mark.xfail(reason="Bug in permutation function")
),
pytest.param(
"greater", 1, marks=pytest.mark.xfail(reason="Bug in permutation function")
),
],
[("two-sided", 0), ("less", -1), ("greater", 1)],
)
def test_permutation_t_test_tail(tail_name, tail_code):
"""Test that tails work properly."""
Expand Down
Loading