Spatiotemporal permutation F-test on full sensor data#

Tests for differential evoked responses in at least one condition using a permutation clustering test. The FieldTrip neighbor templates will be used to determine the adjacency between sensors. This serves as a spatial prior to the clustering. Spatiotemporal clusters will then be visualized using custom matplotlib code.

Here, the unit of observation is epochs from a specific study subject. However, the same logic applies when the unit observation is a number of study subject each of whom contribute their own averaged data (i.e., an average of their epochs). This would then be considered an analysis at the “2nd level”.

See the FieldTrip tutorial for a caveat regarding the possible interpretation of “significant” clusters.

For more information on cluster-based permutation testing in MNE-Python, see also: Non-parametric 1 sample cluster statistic on single trial power

# Authors: Denis Engemann <denis.engemann@gmail.com>
#          Jona Sassenhagen <jona.sassenhagen@gmail.com>
#          Alex Rockhill <aprockhill@mailbox.org>
#          Stefan Appelhoff <stefan.appelhoff@mailbox.org>
#
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats
from mpl_toolkits.axes_grid1 import make_axes_locatable

import mne
from mne.channels import find_ch_adjacency
from mne.datasets import sample
from mne.stats import combine_adjacency, spatio_temporal_cluster_test
from mne.viz import plot_compare_evokeds

Set parameters#

data_path = sample.data_path()
meg_path = data_path / "MEG" / "sample"
raw_fname = meg_path / "sample_audvis_filt-0-40_raw.fif"
event_fname = meg_path / "sample_audvis_filt-0-40_raw-eve.fif"
event_id = {"Aud/L": 1, "Aud/R": 2, "Vis/L": 3, "Vis/R": 4}
tmin = -0.2
tmax = 0.5

# Setup for reading the raw data
raw = mne.io.read_raw_fif(raw_fname, preload=True)
raw.filter(1, 25)
events = mne.read_events(event_fname)
Opening raw data file /home/circleci/mne_data/MNE-sample-data/MEG/sample/sample_audvis_filt-0-40_raw.fif...
    Read a total of 4 projection items:
        PCA-v1 (1 x 102)  idle
        PCA-v2 (1 x 102)  idle
        PCA-v3 (1 x 102)  idle
        Average EEG reference (1 x 60)  idle
    Range : 6450 ... 48149 =     42.956 ...   320.665 secs
Ready.
Reading 0 ... 41699  =      0.000 ...   277.709 secs...
Filtering raw data in 1 contiguous segment
Setting up band-pass filter from 1 - 25 Hz

FIR filter parameters
---------------------
Designing a one-pass, zero-phase, non-causal bandpass filter:
- Windowed time-domain design (firwin) method
- Hamming window with 0.0194 passband ripple and 53 dB stopband attenuation
- Lower passband edge: 1.00
- Lower transition bandwidth: 1.00 Hz (-6 dB cutoff frequency: 0.50 Hz)
- Upper passband edge: 25.00 Hz
- Upper transition bandwidth: 6.25 Hz (-6 dB cutoff frequency: 28.12 Hz)
- Filter length: 497 samples (3.310 s)

[Parallel(n_jobs=1)]: Done  17 tasks      | elapsed:    0.0s
[Parallel(n_jobs=1)]: Done  71 tasks      | elapsed:    0.2s
[Parallel(n_jobs=1)]: Done 161 tasks      | elapsed:    0.3s
[Parallel(n_jobs=1)]: Done 287 tasks      | elapsed:    0.5s

Read epochs for the channel of interest#

picks = mne.pick_types(raw.info, meg="mag", eog=True)

reject = dict(mag=4e-12, eog=150e-6)
epochs = mne.Epochs(
    raw,
    events,
    event_id,
    tmin,
    tmax,
    picks=picks,
    decim=2,  # just for speed!
    baseline=None,
    reject=reject,
    preload=True,
)

epochs.drop_channels(["EOG 061"])
epochs.equalize_event_counts(event_id)

# Obtain the data as a 3D matrix and transpose it such that
# the dimensions are as expected for the cluster permutation test:
# n_epochs × n_times × n_channels
X = [epochs[event_name].get_data(copy=False) for event_name in event_id]
X = [np.transpose(x, (0, 2, 1)) for x in X]
Not setting metadata
288 matching events found
No baseline correction applied
Created an SSP operator (subspace dimension = 3)
3 projection items activated
Using data from preloaded Raw for 288 events and 106 original time points (prior to decimation) ...
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on MAG : ['MEG 1711']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on MAG : ['MEG 1711']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
    Rejecting  epoch based on EOG : ['EOG 061']
48 bad epochs dropped
Dropped 16 epochs: 50, 51, 84, 93, 95, 96, 129, 146, 149, 154, 158, 195, 201, 203, 211, 212

Find the FieldTrip neighbor definition to setup sensor adjacency#

75 cluster ftest spatiotemporal
Reading adjacency matrix for neuromag306mag.
<class 'scipy.sparse._csr.csr_matrix'>

Compute permutation statistic#

How does it work? We use clustering to “bind” together features which are similar. Our features are the magnetic fields measured over our sensor array at different times. This reduces the multiple comparison problem. To compute the actual test-statistic, we first sum all F-values in all clusters. We end up with one statistic for each cluster. Then we generate a distribution from the data by shuffling our conditions between our samples and recomputing our clusters and the test statistics. We test for the significance of a given cluster by computing the probability of observing a cluster of that size [1][2].

# We are running an F test, so we look at the upper tail
# see also: https://stats.stackexchange.com/a/73993
tail = 1

# We want to set a critical test statistic (here: F), to determine when
# clusters are being formed. Using Scipy's percent point function of the F
# distribution, we can conveniently select a threshold that corresponds to
# some alpha level that we arbitrarily pick.
alpha_cluster_forming = 0.001

# For an F test we need the degrees of freedom for the numerator
# (number of conditions - 1) and the denominator (number of observations
# - number of conditions):
n_conditions = len(event_id)
n_observations = len(X[0])
dfn = n_conditions - 1
dfd = n_observations - n_conditions

# Note: we calculate 1 - alpha_cluster_forming to get the critical value
# on the right tail
f_thresh = scipy.stats.f.ppf(1 - alpha_cluster_forming, dfn=dfn, dfd=dfd)

# run the cluster based permutation analysis
cluster_stats = spatio_temporal_cluster_test(
    X,
    n_permutations=1000,
    threshold=f_thresh,
    tail=tail,
    n_jobs=None,
    buffer_size=None,
    adjacency=adjacency,
)
F_obs, clusters, p_values, _ = cluster_stats
stat_fun(H1): min=0.0033787374718191373 max=207.77535362731717
Running initial clustering …
Found 22 clusters

  0%|          | Permuting : 0/999 [00:00<?,       ?it/s]
  0%|          | Permuting : 3/999 [00:00<00:11,   87.21it/s]
  1%|          | Permuting : 8/999 [00:00<00:08,  117.86it/s]
  1%|▏         | Permuting : 14/999 [00:00<00:07,  138.45it/s]
  2%|▏         | Permuting : 20/999 [00:00<00:06,  148.77it/s]
  3%|▎         | Permuting : 25/999 [00:00<00:06,  148.50it/s]
  3%|▎         | Permuting : 31/999 [00:00<00:06,  153.90it/s]
  4%|▎         | Permuting : 37/999 [00:00<00:06,  157.83it/s]
  4%|▍         | Permuting : 43/999 [00:00<00:05,  160.69it/s]
  5%|▍         | Permuting : 49/999 [00:00<00:05,  162.91it/s]
  6%|▌         | Permuting : 56/999 [00:00<00:05,  168.35it/s]
  6%|▌         | Permuting : 62/999 [00:00<00:05,  169.37it/s]
  7%|▋         | Permuting : 68/999 [00:00<00:05,  170.29it/s]
  7%|▋         | Permuting : 74/999 [00:00<00:05,  171.05it/s]
  8%|▊         | Permuting : 80/999 [00:00<00:05,  171.69it/s]
  9%|▊         | Permuting : 86/999 [00:00<00:05,  172.23it/s]
  9%|▉         | Permuting : 92/999 [00:00<00:05,  172.70it/s]
 10%|▉         | Permuting : 98/999 [00:00<00:05,  173.08it/s]
 10%|█         | Permuting : 104/999 [00:00<00:05,  173.47it/s]
 11%|█         | Permuting : 110/999 [00:00<00:05,  173.80it/s]
 12%|█▏        | Permuting : 115/999 [00:00<00:05,  171.80it/s]
 12%|█▏        | Permuting : 120/999 [00:00<00:05,  170.01it/s]
 13%|█▎        | Permuting : 125/999 [00:00<00:05,  168.36it/s]
 13%|█▎        | Permuting : 130/999 [00:00<00:05,  166.90it/s]
 14%|█▎        | Permuting : 135/999 [00:00<00:05,  165.55it/s]
 14%|█▍        | Permuting : 139/999 [00:00<00:05,  162.26it/s]
 14%|█▍        | Permuting : 144/999 [00:00<00:05,  161.21it/s]
 15%|█▍        | Permuting : 149/999 [00:00<00:05,  160.33it/s]
 15%|█▌        | Permuting : 153/999 [00:00<00:05,  157.59it/s]
 16%|█▌        | Permuting : 158/999 [00:00<00:05,  156.97it/s]
 16%|█▋        | Permuting : 163/999 [00:01<00:05,  156.38it/s]
 17%|█▋        | Permuting : 167/999 [00:01<00:05,  153.99it/s]
 17%|█▋        | Permuting : 172/999 [00:01<00:05,  153.57it/s]
 18%|█▊        | Permuting : 177/999 [00:01<00:05,  153.23it/s]
 18%|█▊        | Permuting : 182/999 [00:01<00:05,  152.92it/s]
 19%|█▊        | Permuting : 186/999 [00:01<00:05,  150.86it/s]
 19%|█▉        | Permuting : 191/999 [00:01<00:05,  150.66it/s]
 20%|█▉        | Permuting : 196/999 [00:01<00:05,  150.51it/s]
 20%|██        | Permuting : 201/999 [00:01<00:05,  150.36it/s]
 21%|██        | Permuting : 206/999 [00:01<00:05,  150.23it/s]
 21%|██        | Permuting : 211/999 [00:01<00:05,  150.11it/s]
 22%|██▏       | Permuting : 216/999 [00:01<00:05,  150.00it/s]
 22%|██▏       | Permuting : 221/999 [00:01<00:05,  149.88it/s]
 23%|██▎       | Permuting : 226/999 [00:01<00:05,  149.78it/s]
 23%|██▎       | Permuting : 232/999 [00:01<00:05,  151.33it/s]
 24%|██▎       | Permuting : 237/999 [00:01<00:05,  151.15it/s]
 24%|██▍       | Permuting : 243/999 [00:01<00:04,  152.59it/s]
 25%|██▍       | Permuting : 249/999 [00:01<00:04,  153.97it/s]
 26%|██▌       | Permuting : 256/999 [00:01<00:04,  156.88it/s]
 26%|██▌       | Permuting : 262/999 [00:01<00:04,  158.01it/s]
 27%|██▋       | Permuting : 268/999 [00:01<00:04,  159.08it/s]
 27%|██▋       | Permuting : 274/999 [00:01<00:04,  160.09it/s]
 28%|██▊       | Permuting : 280/999 [00:01<00:04,  161.04it/s]
 29%|██▊       | Permuting : 286/999 [00:01<00:04,  161.93it/s]
 29%|██▉       | Permuting : 292/999 [00:01<00:04,  162.76it/s]
 30%|██▉       | Permuting : 297/999 [00:01<00:04,  161.98it/s]
 30%|███       | Permuting : 303/999 [00:01<00:04,  162.80it/s]
 31%|███       | Permuting : 309/999 [00:01<00:04,  163.58it/s]
 32%|███▏      | Permuting : 315/999 [00:01<00:04,  164.28it/s]
 32%|███▏      | Permuting : 321/999 [00:01<00:04,  164.98it/s]
 33%|███▎      | Permuting : 326/999 [00:02<00:04,  164.06it/s]
 33%|███▎      | Permuting : 332/999 [00:02<00:04,  164.76it/s]
 34%|███▍      | Permuting : 338/999 [00:02<00:03,  165.40it/s]
 34%|███▍      | Permuting : 343/999 [00:02<00:03,  164.47it/s]
 35%|███▍      | Permuting : 348/999 [00:02<00:03,  163.60it/s]
 35%|███▌      | Permuting : 354/999 [00:02<00:03,  164.33it/s]
 36%|███▌      | Permuting : 359/999 [00:02<00:03,  163.49it/s]
 36%|███▋      | Permuting : 364/999 [00:02<00:03,  162.67it/s]
 37%|███▋      | Permuting : 370/999 [00:02<00:03,  163.43it/s]
 38%|███▊      | Permuting : 375/999 [00:02<00:03,  162.62it/s]
 38%|███▊      | Permuting : 380/999 [00:02<00:03,  161.87it/s]
 39%|███▊      | Permuting : 386/999 [00:02<00:03,  162.67it/s]
 39%|███▉      | Permuting : 391/999 [00:02<00:03,  161.89it/s]
 40%|███▉      | Permuting : 397/999 [00:02<00:03,  162.69it/s]
 40%|████      | Permuting : 403/999 [00:02<00:03,  163.45it/s]
 41%|████      | Permuting : 409/999 [00:02<00:03,  164.16it/s]
 41%|████▏     | Permuting : 414/999 [00:02<00:03,  163.34it/s]
 42%|████▏     | Permuting : 420/999 [00:02<00:03,  164.07it/s]
 43%|████▎     | Permuting : 426/999 [00:02<00:03,  164.73it/s]
 43%|████▎     | Permuting : 432/999 [00:02<00:03,  165.36it/s]
 44%|████▎     | Permuting : 437/999 [00:02<00:03,  164.46it/s]
 44%|████▍     | Permuting : 442/999 [00:02<00:03,  163.61it/s]
 45%|████▍     | Permuting : 447/999 [00:02<00:03,  162.80it/s]
 45%|████▌     | Permuting : 451/999 [00:02<00:03,  160.54it/s]
 46%|████▌     | Permuting : 456/999 [00:02<00:03,  159.91it/s]
 46%|████▌     | Permuting : 461/999 [00:02<00:03,  159.28it/s]
 47%|████▋     | Permuting : 466/999 [00:02<00:03,  158.71it/s]
 47%|████▋     | Permuting : 470/999 [00:02<00:03,  156.65it/s]
 48%|████▊     | Permuting : 475/999 [00:02<00:03,  156.18it/s]
 48%|████▊     | Permuting : 480/999 [00:03<00:03,  155.75it/s]
 49%|████▊     | Permuting : 486/999 [00:03<00:03,  155.95it/s]
 49%|████▉     | Permuting : 491/999 [00:03<00:03,  155.54it/s]
 50%|████▉     | Permuting : 495/999 [00:03<00:03,  153.64it/s]
 50%|█████     | Permuting : 500/999 [00:03<00:03,  153.32it/s]
 51%|█████     | Permuting : 505/999 [00:03<00:03,  153.03it/s]
 51%|█████     | Permuting : 510/999 [00:03<00:03,  152.69it/s]
 52%|█████▏    | Permuting : 515/999 [00:03<00:03,  152.45it/s]
 52%|█████▏    | Permuting : 520/999 [00:03<00:03,  152.20it/s]
 53%|█████▎    | Permuting : 525/999 [00:03<00:03,  151.99it/s]
 53%|█████▎    | Permuting : 530/999 [00:03<00:03,  151.78it/s]
 54%|█████▎    | Permuting : 535/999 [00:03<00:03,  151.55it/s]
 54%|█████▍    | Permuting : 539/999 [00:03<00:03,  149.88it/s]
 54%|█████▍    | Permuting : 544/999 [00:03<00:03,  149.78it/s]
 55%|█████▍    | Permuting : 549/999 [00:03<00:03,  149.69it/s]
 55%|█████▌    | Permuting : 554/999 [00:03<00:02,  149.60it/s]
 56%|█████▌    | Permuting : 559/999 [00:03<00:02,  149.51it/s]
 56%|█████▋    | Permuting : 563/999 [00:03<00:02,  147.86it/s]
 57%|█████▋    | Permuting : 568/999 [00:03<00:02,  147.87it/s]
 57%|█████▋    | Permuting : 573/999 [00:03<00:02,  147.85it/s]
 58%|█████▊    | Permuting : 578/999 [00:03<00:02,  147.83it/s]
 58%|█████▊    | Permuting : 583/999 [00:03<00:02,  147.83it/s]
 59%|█████▉    | Permuting : 588/999 [00:03<00:02,  147.82it/s]
 59%|█████▉    | Permuting : 593/999 [00:03<00:02,  147.82it/s]
 60%|█████▉    | Permuting : 599/999 [00:03<00:02,  149.30it/s]
 60%|██████    | Permuting : 603/999 [00:03<00:02,  147.72it/s]
 61%|██████    | Permuting : 609/999 [00:03<00:02,  149.20it/s]
 61%|██████▏   | Permuting : 614/999 [00:03<00:02,  149.14it/s]
 62%|██████▏   | Permuting : 620/999 [00:03<00:02,  150.53it/s]
 63%|██████▎   | Permuting : 625/999 [00:03<00:02,  150.41it/s]
 63%|██████▎   | Permuting : 631/999 [00:04<00:02,  151.77it/s]
 64%|██████▎   | Permuting : 636/999 [00:04<00:02,  151.58it/s]
 64%|██████▍   | Permuting : 642/999 [00:04<00:02,  152.83it/s]
 65%|██████▍   | Permuting : 647/999 [00:04<00:02,  152.57it/s]
 65%|██████▌   | Permuting : 652/999 [00:04<00:02,  152.34it/s]
 66%|██████▌   | Permuting : 658/999 [00:04<00:02,  153.59it/s]
 66%|██████▋   | Permuting : 664/999 [00:04<00:02,  154.78it/s]
 67%|██████▋   | Permuting : 669/999 [00:04<00:02,  154.44it/s]
 68%|██████▊   | Permuting : 675/999 [00:04<00:02,  155.60it/s]
 68%|██████▊   | Permuting : 681/999 [00:04<00:02,  156.68it/s]
 69%|██████▊   | Permuting : 686/999 [00:04<00:02,  156.23it/s]
 69%|██████▉   | Permuting : 691/999 [00:04<00:01,  155.80it/s]
 70%|██████▉   | Permuting : 697/999 [00:04<00:01,  156.89it/s]
 70%|███████   | Permuting : 702/999 [00:04<00:01,  156.43it/s]
 71%|███████   | Permuting : 707/999 [00:04<00:01,  156.01it/s]
 71%|███████▏  | Permuting : 713/999 [00:04<00:01,  157.09it/s]
 72%|███████▏  | Permuting : 717/999 [00:04<00:01,  155.12it/s]
 72%|███████▏  | Permuting : 721/999 [00:04<00:01,  153.25it/s]
 73%|███████▎  | Permuting : 726/999 [00:04<00:01,  152.95it/s]
 73%|███████▎  | Permuting : 732/999 [00:04<00:01,  154.14it/s]
 74%|███████▍  | Permuting : 737/999 [00:04<00:01,  153.80it/s]
 74%|███████▍  | Permuting : 743/999 [00:04<00:01,  154.95it/s]
 75%|███████▍  | Permuting : 748/999 [00:04<00:01,  154.57it/s]
 75%|███████▌  | Permuting : 754/999 [00:04<00:01,  155.67it/s]
 76%|███████▌  | Permuting : 759/999 [00:04<00:01,  155.25it/s]
 77%|███████▋  | Permuting : 765/999 [00:04<00:01,  156.31it/s]
 77%|███████▋  | Permuting : 770/999 [00:04<00:01,  155.86it/s]
 78%|███████▊  | Permuting : 775/999 [00:04<00:01,  155.42it/s]
 78%|███████▊  | Permuting : 780/999 [00:04<00:01,  155.01it/s]
 79%|███████▊  | Permuting : 785/999 [00:05<00:01,  154.63it/s]
 79%|███████▉  | Permuting : 790/999 [00:05<00:01,  154.21it/s]
 80%|███████▉  | Permuting : 796/999 [00:05<00:01,  155.33it/s]
 80%|████████  | Permuting : 801/999 [00:05<00:01,  154.92it/s]
 81%|████████  | Permuting : 806/999 [00:05<00:01,  154.53it/s]
 81%|████████▏ | Permuting : 812/999 [00:05<00:01,  155.65it/s]
 82%|████████▏ | Permuting : 817/999 [00:05<00:01,  155.24it/s]
 82%|████████▏ | Permuting : 822/999 [00:05<00:01,  154.86it/s]
 83%|████████▎ | Permuting : 826/999 [00:05<00:01,  153.04it/s]
 83%|████████▎ | Permuting : 832/999 [00:05<00:01,  154.04it/s]
 84%|████████▎ | Permuting : 836/999 [00:05<00:01,  152.25it/s]
 84%|████████▍ | Permuting : 841/999 [00:05<00:01,  152.04it/s]
 85%|████████▍ | Permuting : 846/999 [00:05<00:01,  151.83it/s]
 85%|████████▌ | Permuting : 851/999 [00:05<00:00,  151.62it/s]
 86%|████████▌ | Permuting : 857/999 [00:05<00:00,  152.92it/s]
 86%|████████▋ | Permuting : 863/999 [00:05<00:00,  154.15it/s]
 87%|████████▋ | Permuting : 868/999 [00:05<00:00,  153.85it/s]
 87%|████████▋ | Permuting : 874/999 [00:05<00:00,  155.00it/s]
 88%|████████▊ | Permuting : 880/999 [00:05<00:00,  156.10it/s]
 89%|████████▊ | Permuting : 885/999 [00:05<00:00,  155.68it/s]
 89%|████████▉ | Permuting : 891/999 [00:05<00:00,  156.74it/s]
 90%|████████▉ | Permuting : 896/999 [00:05<00:00,  156.31it/s]
 90%|█████████ | Permuting : 901/999 [00:05<00:00,  155.89it/s]
 91%|█████████ | Permuting : 907/999 [00:05<00:00,  156.96it/s]
 91%|█████████▏| Permuting : 912/999 [00:05<00:00,  156.50it/s]
 92%|█████████▏| Permuting : 917/999 [00:05<00:00,  156.08it/s]
 92%|█████████▏| Permuting : 922/999 [00:05<00:00,  155.58it/s]
 93%|█████████▎| Permuting : 928/999 [00:05<00:00,  156.65it/s]
 93%|█████████▎| Permuting : 934/999 [00:05<00:00,  157.70it/s]
 94%|█████████▍| Permuting : 940/999 [00:05<00:00,  158.67it/s]
 95%|█████████▍| Permuting : 945/999 [00:06<00:00,  158.11it/s]
 95%|█████████▌| Permuting : 950/999 [00:06<00:00,  157.57it/s]
 96%|█████████▌| Permuting : 955/999 [00:06<00:00,  157.09it/s]
 96%|█████████▌| Permuting : 959/999 [00:06<00:00,  155.14it/s]
 96%|█████████▋| Permuting : 964/999 [00:06<00:00,  154.71it/s]
 97%|█████████▋| Permuting : 969/999 [00:06<00:00,  154.37it/s]
 97%|█████████▋| Permuting : 974/999 [00:06<00:00,  153.79it/s]
 98%|█████████▊| Permuting : 978/999 [00:06<00:00,  152.04it/s]
 98%|█████████▊| Permuting : 984/999 [00:06<00:00,  153.29it/s]
 99%|█████████▉| Permuting : 988/999 [00:06<00:00,  151.51it/s]
 99%|█████████▉| Permuting : 993/999 [00:06<00:00,  151.31it/s]
100%|█████████▉| Permuting : 998/999 [00:06<00:00,  151.12it/s]
100%|██████████| Permuting : 999/999 [00:06<00:00,  155.85it/s]

Note

Note how we only specified an adjacency for sensors! However, because we used mne.stats.spatio_temporal_cluster_test(), an adjacency for time points was automatically taken into account. That is, at time point N, the time points N - 1 and N + 1 were considered as adjacent (this is also called “lattice adjacency”). This is only possible because we ran the analysis on 2D data (times × channels) per observation … for 3D data per observation (e.g., times × frequencies × channels), we will need to use mne.stats.combine_adjacency(), as shown further below.

Note also that the same functions work with source estimates. The only differences are the origin of the data, the size, and the adjacency definition. It can be used for single trials or for groups of subjects.

Visualize clusters#

# We subselect clusters that we consider significant at an arbitrarily
# picked alpha level: "p_accept".
# NOTE: remember the caveats with respect to "significant" clusters that
# we mentioned in the introduction of this tutorial!
p_accept = 0.01
good_cluster_inds = np.where(p_values < p_accept)[0]

# configure variables for visualization
colors = {"Aud": "crimson", "Vis": "steelblue"}
linestyles = {"L": "-", "R": "--"}

# organize data for plotting
evokeds = {cond: epochs[cond].average() for cond in event_id}

# loop over clusters
for i_clu, clu_idx in enumerate(good_cluster_inds):
    # unpack cluster information, get unique indices
    time_inds, space_inds = np.squeeze(clusters[clu_idx])
    ch_inds = np.unique(space_inds)
    time_inds = np.unique(time_inds)

    # get topography for F stat
    f_map = F_obs[time_inds, ...].mean(axis=0)

    # get signals at the sensors contributing to the cluster
    sig_times = epochs.times[time_inds]

    # create spatial mask
    mask = np.zeros((f_map.shape[0], 1), dtype=bool)
    mask[ch_inds, :] = True

    # initialize figure
    fig, ax_topo = plt.subplots(1, 1, figsize=(10, 3), layout="constrained")

    # plot average test statistic and mark significant sensors
    f_evoked = mne.EvokedArray(f_map[:, np.newaxis], epochs.info, tmin=0)
    f_evoked.plot_topomap(
        times=0,
        mask=mask,
        axes=ax_topo,
        cmap="Reds",
        vlim=(np.min, np.max),
        show=False,
        colorbar=False,
        mask_params=dict(markersize=10),
    )
    image = ax_topo.images[0]

    # remove the title that would otherwise say "0.000 s"
    ax_topo.set_title("")

    # create additional axes (for ERF and colorbar)
    divider = make_axes_locatable(ax_topo)

    # add axes for colorbar
    ax_colorbar = divider.append_axes("right", size="5%", pad=0.05)
    plt.colorbar(image, cax=ax_colorbar)
    ax_topo.set_xlabel(
        "Averaged F-map ({:0.3f} - {:0.3f} s)".format(*sig_times[[0, -1]])
    )

    # add new axis for time courses and plot time courses
    ax_signals = divider.append_axes("right", size="300%", pad=1.2)
    title = f"Cluster #{i_clu + 1}, {len(ch_inds)} sensor"
    if len(ch_inds) > 1:
        title += "s (mean)"
    plot_compare_evokeds(
        evokeds,
        title=title,
        picks=ch_inds,
        axes=ax_signals,
        colors=colors,
        linestyles=linestyles,
        show=False,
        split_legend=True,
        truncate_yaxis="auto",
    )

    # plot temporal cluster extent
    ymin, ymax = ax_signals.get_ylim()
    ax_signals.fill_betweenx(
        (ymin, ymax), sig_times[0], sig_times[-1], color="orange", alpha=0.3
    )

plt.show()
  • Cluster #1, 4 sensors (mean) (RMS)
  • Cluster #2, 85 sensors (mean) (RMS)
  • Cluster #3, 7 sensors (mean) (RMS)
  • Cluster #4, 8 sensors (mean) (RMS)
  • Cluster #5, 34 sensors (mean) (RMS)
  • Cluster #6, 5 sensors (mean) (RMS)
  • Cluster #7, 13 sensors (mean) (RMS)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)
combining channels using RMS (mag channels)

Permutation statistic for time-frequencies#

Let’s do the same thing with the time-frequency decomposition of the data (see Frequency and time-frequency sensor analysis for a tutorial and Time-frequency on simulated data (Multitaper vs. Morlet vs. Stockwell vs. Hilbert) for a comparison of time-frequency methods) to show how cluster permutations can be done on higher-dimensional data.

decim = 4
freqs = np.arange(7, 30, 3)  # define frequencies of interest
n_cycles = freqs / freqs[0]

epochs_power = list()
for condition in [epochs[k] for k in ("Aud/L", "Vis/L")]:
    this_tfr = condition.compute_tfr(
        method="morlet",
        freqs=freqs,
        n_cycles=n_cycles,
        decim=decim,
        average=False,
        return_itc=False,
    )
    this_tfr.apply_baseline(mode="ratio", baseline=(None, 0))
    epochs_power.append(this_tfr.data)

# transpose again to (epochs, frequencies, times, channels)
X = [np.transpose(x, (0, 2, 3, 1)) for x in epochs_power]
[Parallel(n_jobs=1)]: Done  17 tasks      | elapsed:    0.2s
[Parallel(n_jobs=1)]: Done  71 tasks      | elapsed:    0.8s
Applying baseline correction (mode: ratio)
[Parallel(n_jobs=1)]: Done  17 tasks      | elapsed:    0.2s
[Parallel(n_jobs=1)]: Done  71 tasks      | elapsed:    0.6s
Applying baseline correction (mode: ratio)

Remember the note on the adjacency matrix from above: For 3D data, as here, we must use mne.stats.combine_adjacency() to extend the sensor-based adjacency to incorporate the time-frequency plane as well.

Here, the integer inputs are converted into a lattice and combined with the sensor adjacency matrix so that data at similar times and with similar frequencies and at close sensor locations are clustered together.

# our data at each observation is of shape frequencies × times × channels
tfr_adjacency = combine_adjacency(len(freqs), len(this_tfr.times), adjacency)

Now we can run the cluster permutation test, but first we have to set a threshold. This example decimates in time and uses few frequencies so we need to increase the threshold from the default value in order to have differentiated clusters (i.e., so that our algorithm doesn’t just find one large cluster). For a more principled method of setting this parameter, threshold-free cluster enhancement may be used. See Statistical inference for a discussion.

# This time we don't calculate a threshold based on the F distribution.
# We might as well select an arbitrary threshold for cluster forming
tfr_threshold = 15.0

# run cluster based permutation analysis
cluster_stats = spatio_temporal_cluster_test(
    X,
    n_permutations=1000,
    threshold=tfr_threshold,
    tail=1,
    n_jobs=None,
    buffer_size=None,
    adjacency=tfr_adjacency,
)
stat_fun(H1): min=6.752417805734057e-07 max=38.408365559505015
Running initial clustering …
Found 3 clusters

  0%|          | Permuting : 0/999 [00:00<?,       ?it/s]
  0%|          | Permuting : 2/999 [00:00<00:17,   57.98it/s]
  0%|          | Permuting : 4/999 [00:00<00:17,   58.52it/s]
  1%|          | Permuting : 10/999 [00:00<00:09,   99.95it/s]
  1%|          | Permuting : 12/999 [00:00<00:11,   85.57it/s]
  2%|▏         | Permuting : 17/999 [00:00<00:09,   98.89it/s]
  2%|▏         | Permuting : 22/999 [00:00<00:09,  107.87it/s]
  2%|▏         | Permuting : 24/999 [00:00<00:09,   99.87it/s]
  3%|▎         | Permuting : 26/999 [00:00<00:10,   92.80it/s]
  3%|▎         | Permuting : 28/999 [00:00<00:11,   87.92it/s]
  3%|▎         | Permuting : 29/999 [00:00<00:11,   80.87it/s]
  3%|▎         | Permuting : 30/999 [00:00<00:12,   75.06it/s]
  3%|▎         | Permuting : 32/999 [00:00<00:13,   72.22it/s]
  3%|▎         | Permuting : 33/999 [00:00<00:14,   66.95it/s]
  3%|▎         | Permuting : 34/999 [00:00<00:15,   63.46it/s]
  4%|▎         | Permuting : 36/999 [00:01<00:44,   21.81it/s]
  4%|▍         | Permuting : 38/999 [00:01<00:41,   22.99it/s]
  4%|▍         | Permuting : 43/999 [00:01<00:35,   26.99it/s]
  5%|▍         | Permuting : 48/999 [00:01<00:30,   30.94it/s]
  5%|▌         | Permuting : 50/999 [00:01<00:29,   31.85it/s]
  5%|▌         | Permuting : 51/999 [00:01<00:29,   31.78it/s]
  5%|▌         | Permuting : 54/999 [00:01<00:28,   33.61it/s]
  6%|▌         | Permuting : 56/999 [00:01<00:27,   34.43it/s]
  6%|▌         | Permuting : 61/999 [00:01<00:24,   38.42it/s]
  7%|▋         | Permuting : 65/999 [00:01<00:22,   41.18it/s]
  7%|▋         | Permuting : 66/999 [00:01<00:23,   40.54it/s]
  7%|▋         | Permuting : 70/999 [00:01<00:21,   43.37it/s]
  7%|▋         | Permuting : 73/999 [00:01<00:20,   44.90it/s]
  7%|▋         | Permuting : 74/999 [00:01<00:20,   44.08it/s]
  8%|▊         | Permuting : 77/999 [00:01<00:20,   45.50it/s]
  8%|▊         | Permuting : 80/999 [00:01<00:19,   46.86it/s]
  8%|▊         | Permuting : 81/999 [00:01<00:19,   45.94it/s]
  8%|▊         | Permuting : 83/999 [00:01<00:19,   46.13it/s]
  9%|▊         | Permuting : 85/999 [00:01<00:19,   46.63it/s]
  9%|▊         | Permuting : 87/999 [00:01<00:19,   46.85it/s]
  9%|▉         | Permuting : 89/999 [00:01<00:19,   47.33it/s]
  9%|▉         | Permuting : 91/999 [00:02<00:18,   47.80it/s]
  9%|▉         | Permuting : 92/999 [00:02<00:19,   47.07it/s]
 10%|▉         | Permuting : 95/999 [00:02<00:18,   48.45it/s]
 10%|▉         | Permuting : 99/999 [00:02<00:17,   51.28it/s]
 10%|█         | Permuting : 104/999 [00:02<00:16,   55.24it/s]
 11%|█         | Permuting : 106/999 [00:02<00:16,   55.05it/s]
 11%|█         | Permuting : 108/999 [00:02<00:16,   55.21it/s]
 11%|█         | Permuting : 112/999 [00:02<00:15,   57.85it/s]
 11%|█▏        | Permuting : 114/999 [00:02<00:15,   57.90it/s]
 12%|█▏        | Permuting : 116/999 [00:02<00:15,   57.87it/s]
 12%|█▏        | Permuting : 120/999 [00:02<00:14,   60.45it/s]
 13%|█▎        | Permuting : 125/999 [00:02<00:13,   64.21it/s]
 13%|█▎        | Permuting : 128/999 [00:02<00:13,   64.84it/s]
 13%|█▎        | Permuting : 131/999 [00:02<00:13,   65.44it/s]
 14%|█▎        | Permuting : 135/999 [00:02<00:12,   67.73it/s]
 14%|█▎        | Permuting : 136/999 [00:02<00:13,   66.06it/s]
 14%|█▍        | Permuting : 141/999 [00:02<00:12,   69.65it/s]
 14%|█▍        | Permuting : 143/999 [00:02<00:12,   69.19it/s]
 15%|█▍        | Permuting : 145/999 [00:02<00:12,   68.74it/s]
 15%|█▌        | Permuting : 150/999 [00:02<00:11,   72.28it/s]
 15%|█▌        | Permuting : 153/999 [00:02<00:11,   72.69it/s]
 16%|█▌        | Permuting : 156/999 [00:02<00:11,   72.91it/s]
 16%|█▌        | Permuting : 157/999 [00:02<00:11,   70.61it/s]
 16%|█▌        | Permuting : 160/999 [00:02<00:11,   71.36it/s]
 16%|█▌        | Permuting : 162/999 [00:02<00:11,   70.80it/s]
 16%|█▋        | Permuting : 164/999 [00:02<00:11,   70.26it/s]
 17%|█▋        | Permuting : 167/999 [00:03<00:17,   48.57it/s]
 17%|█▋        | Permuting : 170/999 [00:03<00:16,   49.85it/s]
 17%|█▋        | Permuting : 172/999 [00:03<00:16,   50.15it/s]
 18%|█▊        | Permuting : 177/999 [00:03<00:15,   53.37it/s]
 18%|█▊        | Permuting : 179/999 [00:03<00:15,   53.56it/s]
 18%|█▊        | Permuting : 181/999 [00:03<00:15,   53.75it/s]
 18%|█▊        | Permuting : 184/999 [00:03<00:14,   54.84it/s]
 19%|█▊        | Permuting : 186/999 [00:03<00:14,   54.67it/s]
 19%|█▉        | Permuting : 191/999 [00:03<00:13,   57.95it/s]
 19%|█▉        | Permuting : 193/999 [00:03<00:13,   57.91it/s]
 19%|█▉        | Permuting : 194/999 [00:03<00:14,   56.57it/s]
 20%|█▉        | Permuting : 198/999 [00:03<00:13,   58.83it/s]
 20%|██        | Permuting : 200/999 [00:03<00:13,   58.84it/s]
 20%|██        | Permuting : 201/999 [00:03<00:13,   57.41it/s]
 20%|██        | Permuting : 202/999 [00:03<00:14,   56.04it/s]
 21%|██        | Permuting : 205/999 [00:03<00:13,   57.07it/s]
 21%|██        | Permuting : 210/999 [00:03<00:13,   60.57it/s]
 21%|██        | Permuting : 212/999 [00:03<00:13,   60.44it/s]
 22%|██▏       | Permuting : 216/999 [00:03<00:12,   62.72it/s]
 22%|██▏       | Permuting : 219/999 [00:03<00:12,   63.59it/s]
 22%|██▏       | Permuting : 221/999 [00:03<00:12,   63.35it/s]
 23%|██▎       | Permuting : 225/999 [00:04<00:11,   65.56it/s]
 23%|██▎       | Permuting : 230/999 [00:04<00:11,   68.90it/s]
 23%|██▎       | Permuting : 232/999 [00:04<00:11,   68.50it/s]
 24%|██▎       | Permuting : 237/999 [00:04<00:10,   71.79it/s]
 24%|██▍       | Permuting : 240/999 [00:04<00:10,   72.17it/s]
 24%|██▍       | Permuting : 241/999 [00:04<00:10,   70.37it/s]
 24%|██▍       | Permuting : 243/999 [00:04<00:10,   69.89it/s]
 24%|██▍       | Permuting : 244/999 [00:04<00:11,   67.73it/s]
 25%|██▍       | Permuting : 247/999 [00:04<00:10,   68.63it/s]
 25%|██▍       | Permuting : 248/999 [00:04<00:11,   66.94it/s]
 25%|██▍       | Permuting : 249/999 [00:04<00:11,   65.32it/s]
 25%|██▌       | Permuting : 250/999 [00:04<00:11,   63.77it/s]
 26%|██▌       | Permuting : 255/999 [00:04<00:11,   67.46it/s]
 26%|██▌       | Permuting : 257/999 [00:04<00:11,   67.10it/s]
 26%|██▌       | Permuting : 260/999 [00:04<00:10,   67.61it/s]
 26%|██▋       | Permuting : 264/999 [00:04<00:10,   69.85it/s]
 27%|██▋       | Permuting : 267/999 [00:04<00:10,   70.38it/s]
 27%|██▋       | Permuting : 272/999 [00:04<00:09,   73.85it/s]
 27%|██▋       | Permuting : 274/999 [00:04<00:09,   73.18it/s]
 28%|██▊       | Permuting : 276/999 [00:04<00:09,   72.55it/s]
 28%|██▊       | Permuting : 278/999 [00:05<00:14,   49.28it/s]
 28%|██▊       | Permuting : 279/999 [00:05<00:14,   48.42it/s]
 28%|██▊       | Permuting : 283/999 [00:05<00:14,   50.66it/s]
 29%|██▉       | Permuting : 288/999 [00:05<00:13,   53.83it/s]
 29%|██▉       | Permuting : 289/999 [00:05<00:13,   53.03it/s]
 29%|██▉       | Permuting : 292/999 [00:05<00:13,   54.24it/s]
 29%|██▉       | Permuting : 294/999 [00:05<00:12,   54.40it/s]
 30%|██▉       | Permuting : 295/999 [00:05<00:13,   53.43it/s]
 30%|██▉       | Permuting : 298/999 [00:05<00:12,   54.68it/s]
 30%|██▉       | Permuting : 299/999 [00:05<00:13,   53.49it/s]
 30%|███       | Permuting : 301/999 [00:05<00:13,   53.44it/s]
 30%|███       | Permuting : 303/999 [00:05<00:12,   53.65it/s]
 31%|███       | Permuting : 308/999 [00:05<00:12,   57.14it/s]
 31%|███       | Permuting : 310/999 [00:05<00:12,   57.22it/s]
 31%|███       | Permuting : 312/999 [00:05<00:11,   57.30it/s]
 32%|███▏      | Permuting : 317/999 [00:05<00:11,   60.79it/s]
 32%|███▏      | Permuting : 319/999 [00:05<00:11,   60.72it/s]
 32%|███▏      | Permuting : 321/999 [00:05<00:11,   60.65it/s]
 32%|███▏      | Permuting : 323/999 [00:05<00:11,   60.59it/s]
 33%|███▎      | Permuting : 325/999 [00:05<00:11,   60.18it/s]
 33%|███▎      | Permuting : 330/999 [00:05<00:10,   63.73it/s]
 34%|███▎      | Permuting : 335/999 [00:05<00:09,   67.16it/s]
 34%|███▍      | Permuting : 340/999 [00:05<00:09,   70.49it/s]
 34%|███▍      | Permuting : 343/999 [00:05<00:09,   71.25it/s]
 35%|███▍      | Permuting : 345/999 [00:05<00:09,   70.72it/s]
 35%|███▌      | Permuting : 350/999 [00:05<00:08,   73.98it/s]
 36%|███▌      | Permuting : 355/999 [00:05<00:08,   77.12it/s]
 36%|███▌      | Permuting : 359/999 [00:05<00:08,   78.38it/s]
 36%|███▋      | Permuting : 363/999 [00:06<00:07,   80.09it/s]
 37%|███▋      | Permuting : 365/999 [00:06<00:08,   79.19it/s]
 37%|███▋      | Permuting : 370/999 [00:06<00:07,   82.17it/s]
 38%|███▊      | Permuting : 375/999 [00:06<00:07,   85.05it/s]
 38%|███▊      | Permuting : 379/999 [00:06<00:07,   86.51it/s]
 38%|███▊      | Permuting : 384/999 [00:06<00:06,   89.24it/s]
 39%|███▊      | Permuting : 387/999 [00:06<00:06,   89.21it/s]
 39%|███▉      | Permuting : 392/999 [00:06<00:06,   91.83it/s]
 40%|███▉      | Permuting : 395/999 [00:06<00:06,   91.39it/s]
 40%|███▉      | Permuting : 397/999 [00:06<00:06,   89.32it/s]
 40%|████      | Permuting : 401/999 [00:06<00:06,   90.02it/s]
 40%|████      | Permuting : 403/999 [00:06<00:06,   88.18it/s]
 41%|████      | Permuting : 408/999 [00:06<00:06,   90.87it/s]
 41%|████      | Permuting : 409/999 [00:06<00:09,   59.73it/s]
 41%|████      | Permuting : 410/999 [00:06<00:10,   58.50it/s]
 41%|████▏     | Permuting : 414/999 [00:06<00:09,   60.39it/s]
 42%|████▏     | Permuting : 417/999 [00:06<00:09,   61.04it/s]
 42%|████▏     | Permuting : 418/999 [00:06<00:09,   60.01it/s]
 42%|████▏     | Permuting : 423/999 [00:07<00:09,   62.93it/s]
 43%|████▎     | Permuting : 427/999 [00:07<00:08,   64.47it/s]
 43%|████▎     | Permuting : 428/999 [00:07<00:09,   62.94it/s]
 43%|████▎     | Permuting : 433/999 [00:07<00:08,   65.88it/s]
 44%|████▎     | Permuting : 435/999 [00:07<00:08,   65.64it/s]
 44%|████▎     | Permuting : 437/999 [00:07<00:08,   65.41it/s]
 44%|████▍     | Permuting : 440/999 [00:07<00:08,   65.89it/s]
 44%|████▍     | Permuting : 444/999 [00:07<00:08,   67.80it/s]
 45%|████▍     | Permuting : 447/999 [00:07<00:08,   68.48it/s]
 45%|████▌     | Permuting : 452/999 [00:07<00:07,   71.46it/s]
 46%|████▌     | Permuting : 455/999 [00:07<00:07,   71.86it/s]
 46%|████▌     | Permuting : 457/999 [00:07<00:07,   70.89it/s]
 46%|████▌     | Permuting : 460/999 [00:07<00:07,   71.58it/s]
 46%|████▋     | Permuting : 463/999 [00:07<00:07,   71.87it/s]
 47%|████▋     | Permuting : 465/999 [00:07<00:07,   70.96it/s]
 47%|████▋     | Permuting : 467/999 [00:07<00:07,   70.49it/s]
 47%|████▋     | Permuting : 469/999 [00:07<00:07,   70.03it/s]
 47%|████▋     | Permuting : 471/999 [00:07<00:07,   69.58it/s]
 48%|████▊     | Permuting : 475/999 [00:07<00:07,   71.13it/s]
 48%|████▊     | Permuting : 476/999 [00:07<00:07,   69.37it/s]
 48%|████▊     | Permuting : 479/999 [00:07<00:07,   70.15it/s]
 48%|████▊     | Permuting : 484/999 [00:07<00:07,   73.37it/s]
 49%|████▊     | Permuting : 486/999 [00:07<00:07,   72.78it/s]
 49%|████▉     | Permuting : 491/999 [00:07<00:06,   75.95it/s]
 49%|████▉     | Permuting : 494/999 [00:07<00:06,   76.12it/s]
 50%|████▉     | Permuting : 496/999 [00:07<00:06,   75.40it/s]
 50%|████▉     | Permuting : 498/999 [00:07<00:06,   74.71it/s]
 50%|█████     | Permuting : 502/999 [00:07<00:06,   76.34it/s]
 51%|█████     | Permuting : 505/999 [00:08<00:06,   76.86it/s]
 51%|█████     | Permuting : 509/999 [00:08<00:09,   54.23it/s]
 51%|█████▏    | Permuting : 512/999 [00:08<00:08,   55.29it/s]
 51%|█████▏    | Permuting : 514/999 [00:08<00:08,   55.41it/s]
 52%|█████▏    | Permuting : 516/999 [00:08<00:08,   55.53it/s]
 52%|█████▏    | Permuting : 518/999 [00:08<00:08,   55.64it/s]
 52%|█████▏    | Permuting : 520/999 [00:08<00:08,   55.76it/s]
 52%|█████▏    | Permuting : 522/999 [00:08<00:08,   55.86it/s]
 52%|█████▏    | Permuting : 523/999 [00:08<00:08,   54.60it/s]
 53%|█████▎    | Permuting : 527/999 [00:08<00:08,   56.79it/s]
 53%|█████▎    | Permuting : 529/999 [00:08<00:08,   56.87it/s]
 53%|█████▎    | Permuting : 532/999 [00:08<00:08,   57.68it/s]
 53%|█████▎    | Permuting : 534/999 [00:08<00:08,   57.72it/s]
 54%|█████▎    | Permuting : 536/999 [00:08<00:08,   57.76it/s]
 54%|█████▍    | Permuting : 539/999 [00:08<00:07,   58.57it/s]
 54%|█████▍    | Permuting : 542/999 [00:08<00:07,   59.35it/s]
 55%|█████▍    | Permuting : 547/999 [00:08<00:07,   62.66it/s]
 55%|█████▌    | Permuting : 550/999 [00:08<00:07,   63.57it/s]
 55%|█████▌    | Permuting : 552/999 [00:08<00:07,   63.39it/s]
 55%|█████▌    | Permuting : 554/999 [00:09<00:07,   63.22it/s]
 56%|█████▌    | Permuting : 555/999 [00:09<00:07,   61.89it/s]
 56%|█████▌    | Permuting : 556/999 [00:09<00:07,   60.61it/s]
 56%|█████▌    | Permuting : 559/999 [00:09<00:07,   61.74it/s]
 56%|█████▌    | Permuting : 561/999 [00:09<00:07,   61.63it/s]
 56%|█████▋    | Permuting : 564/999 [00:09<00:06,   62.36it/s]
 57%|█████▋    | Permuting : 568/999 [00:09<00:06,   64.64it/s]
 57%|█████▋    | Permuting : 571/999 [00:09<00:06,   65.22it/s]
 57%|█████▋    | Permuting : 573/999 [00:09<00:06,   64.55it/s]
 58%|█████▊    | Permuting : 576/999 [00:09<00:06,   65.14it/s]
 58%|█████▊    | Permuting : 580/999 [00:09<00:06,   67.36it/s]
 58%|█████▊    | Permuting : 584/999 [00:09<00:06,   69.06it/s]
 59%|█████▉    | Permuting : 587/999 [00:09<00:05,   69.77it/s]
 59%|█████▉    | Permuting : 589/999 [00:09<00:05,   69.17it/s]
 59%|█████▉    | Permuting : 590/999 [00:09<00:06,   67.05it/s]
 59%|█████▉    | Permuting : 591/999 [00:09<00:06,   65.04it/s]
 60%|█████▉    | Permuting : 595/999 [00:09<00:06,   67.31it/s]
 60%|█████▉    | Permuting : 596/999 [00:09<00:06,   65.25it/s]
 60%|█████▉    | Permuting : 598/999 [00:09<00:06,   64.98it/s]
 60%|██████    | Permuting : 603/999 [00:09<00:05,   68.57it/s]
 61%|██████    | Permuting : 606/999 [00:10<00:08,   47.84it/s]
 61%|██████    | Permuting : 610/999 [00:10<00:07,   50.00it/s]
 61%|██████▏   | Permuting : 612/999 [00:10<00:07,   50.05it/s]
 62%|██████▏   | Permuting : 616/999 [00:10<00:07,   52.21it/s]
 62%|██████▏   | Permuting : 618/999 [00:10<00:07,   52.43it/s]
 62%|██████▏   | Permuting : 619/999 [00:10<00:07,   51.68it/s]
 62%|██████▏   | Permuting : 621/999 [00:10<00:07,   51.93it/s]
 62%|██████▏   | Permuting : 624/999 [00:10<00:07,   52.91it/s]
 63%|██████▎   | Permuting : 626/999 [00:10<00:07,   52.90it/s]
 63%|██████▎   | Permuting : 631/999 [00:10<00:06,   56.21it/s]
 63%|██████▎   | Permuting : 632/999 [00:10<00:06,   55.26it/s]
 63%|██████▎   | Permuting : 633/999 [00:10<00:06,   54.04it/s]
 63%|██████▎   | Permuting : 634/999 [00:10<00:06,   52.87it/s]
 64%|██████▍   | Permuting : 638/999 [00:10<00:06,   55.26it/s]
 64%|██████▍   | Permuting : 643/999 [00:10<00:06,   58.69it/s]
 65%|██████▍   | Permuting : 648/999 [00:10<00:05,   62.04it/s]
 65%|██████▌   | Permuting : 651/999 [00:10<00:05,   62.86it/s]
 66%|██████▌   | Permuting : 656/999 [00:10<00:05,   66.12it/s]
 66%|██████▌   | Permuting : 658/999 [00:10<00:05,   65.84it/s]
 66%|██████▌   | Permuting : 661/999 [00:10<00:05,   66.34it/s]
 66%|██████▋   | Permuting : 664/999 [00:10<00:05,   66.82it/s]
 67%|██████▋   | Permuting : 667/999 [00:10<00:04,   67.40it/s]
 67%|██████▋   | Permuting : 669/999 [00:10<00:04,   67.06it/s]
 67%|██████▋   | Permuting : 671/999 [00:10<00:04,   66.74it/s]
 67%|██████▋   | Permuting : 674/999 [00:10<00:04,   67.37it/s]
 68%|██████▊   | Permuting : 676/999 [00:10<00:04,   66.61it/s]
 68%|██████▊   | Permuting : 679/999 [00:11<00:04,   67.10it/s]
 68%|██████▊   | Permuting : 683/999 [00:11<00:04,   69.04it/s]
 69%|██████▊   | Permuting : 686/999 [00:11<00:04,   69.86it/s]
 69%|██████▉   | Permuting : 691/999 [00:11<00:04,   73.12it/s]
 69%|██████▉   | Permuting : 693/999 [00:11<00:04,   72.53it/s]
 70%|██████▉   | Permuting : 695/999 [00:11<00:04,   71.95it/s]
 70%|██████▉   | Permuting : 696/999 [00:11<00:04,   70.13it/s]
 70%|██████▉   | Permuting : 697/999 [00:11<00:04,   67.93it/s]
 70%|██████▉   | Permuting : 698/999 [00:11<00:04,   65.81it/s]
 70%|███████   | Permuting : 703/999 [00:11<00:04,   69.33it/s]
 71%|███████   | Permuting : 706/999 [00:11<00:04,   70.17it/s]
 71%|███████   | Permuting : 708/999 [00:11<00:04,   69.68it/s]
 71%|███████   | Permuting : 710/999 [00:11<00:06,   47.37it/s]
 71%|███████▏  | Permuting : 712/999 [00:11<00:06,   47.73it/s]
 71%|███████▏  | Permuting : 713/999 [00:11<00:06,   46.93it/s]
 72%|███████▏  | Permuting : 715/999 [00:11<00:06,   47.10it/s]
 72%|███████▏  | Permuting : 717/999 [00:11<00:05,   47.26it/s]
 72%|███████▏  | Permuting : 719/999 [00:11<00:05,   47.64it/s]
 72%|███████▏  | Permuting : 724/999 [00:12<00:05,   50.97it/s]
 73%|███████▎  | Permuting : 728/999 [00:12<00:05,   53.23it/s]
 73%|███████▎  | Permuting : 731/999 [00:12<00:04,   54.44it/s]
 74%|███████▎  | Permuting : 736/999 [00:12<00:04,   57.69it/s]
 74%|███████▍  | Permuting : 738/999 [00:12<00:04,   57.73it/s]
 74%|███████▍  | Permuting : 741/999 [00:12<00:04,   58.71it/s]
 74%|███████▍  | Permuting : 742/999 [00:12<00:04,   57.65it/s]
 74%|███████▍  | Permuting : 744/999 [00:12<00:04,   57.71it/s]
 75%|███████▍  | Permuting : 745/999 [00:12<00:04,   56.34it/s]
 75%|███████▍  | Permuting : 747/999 [00:12<00:04,   56.16it/s]
 75%|███████▌  | Permuting : 750/999 [00:12<00:04,   57.08it/s]
 75%|███████▌  | Permuting : 752/999 [00:12<00:04,   57.16it/s]
 75%|███████▌  | Permuting : 754/999 [00:12<00:04,   57.24it/s]
 76%|███████▌  | Permuting : 756/999 [00:12<00:04,   56.97it/s]
 76%|███████▌  | Permuting : 760/999 [00:12<00:04,   59.02it/s]
 76%|███████▋  | Permuting : 764/999 [00:12<00:03,   61.35it/s]
 77%|███████▋  | Permuting : 766/999 [00:12<00:03,   61.27it/s]
 77%|███████▋  | Permuting : 771/999 [00:12<00:03,   64.76it/s]
 78%|███████▊  | Permuting : 776/999 [00:12<00:03,   68.14it/s]
 78%|███████▊  | Permuting : 777/999 [00:12<00:03,   66.15it/s]
 78%|███████▊  | Permuting : 780/999 [00:12<00:03,   66.67it/s]
 78%|███████▊  | Permuting : 784/999 [00:12<00:03,   68.80it/s]
 79%|███████▉  | Permuting : 789/999 [00:12<00:02,   72.10it/s]
 79%|███████▉  | Permuting : 790/999 [00:12<00:02,   70.27it/s]
 79%|███████▉  | Permuting : 793/999 [00:12<00:02,   71.05it/s]
 80%|███████▉  | Permuting : 798/999 [00:12<00:02,   74.33it/s]
 80%|████████  | Permuting : 801/999 [00:12<00:02,   74.47it/s]
 80%|████████  | Permuting : 802/999 [00:13<00:03,   51.13it/s]
 80%|████████  | Permuting : 804/999 [00:13<00:03,   51.38it/s]
 81%|████████  | Permuting : 808/999 [00:13<00:03,   53.23it/s]
 81%|████████  | Permuting : 810/999 [00:13<00:03,   53.16it/s]
 81%|████████▏ | Permuting : 812/999 [00:13<00:03,   53.36it/s]
 81%|████████▏ | Permuting : 814/999 [00:13<00:03,   53.55it/s]
 82%|████████▏ | Permuting : 817/999 [00:13<00:03,   54.45it/s]
 82%|████████▏ | Permuting : 821/999 [00:13<00:03,   56.62it/s]
 82%|████████▏ | Permuting : 822/999 [00:13<00:03,   55.40it/s]
 82%|████████▏ | Permuting : 824/999 [00:13<00:03,   55.24it/s]
 83%|████████▎ | Permuting : 829/999 [00:13<00:02,   58.49it/s]
 83%|████████▎ | Permuting : 830/999 [00:13<00:02,   57.46it/s]
 83%|████████▎ | Permuting : 833/999 [00:13<00:02,   58.33it/s]
 84%|████████▎ | Permuting : 835/999 [00:13<00:02,   58.04it/s]
 84%|████████▍ | Permuting : 837/999 [00:13<00:02,   58.08it/s]
 84%|████████▍ | Permuting : 839/999 [00:13<00:02,   58.11it/s]
 84%|████████▍ | Permuting : 840/999 [00:13<00:02,   57.02it/s]
 84%|████████▍ | Permuting : 843/999 [00:13<00:02,   58.23it/s]
 85%|████████▍ | Permuting : 845/999 [00:13<00:02,   58.27it/s]
 85%|████████▍ | Permuting : 848/999 [00:13<00:02,   59.11it/s]
 85%|████████▌ | Permuting : 853/999 [00:14<00:02,   62.61it/s]
 86%|████████▌ | Permuting : 855/999 [00:14<00:02,   62.08it/s]
 86%|████████▌ | Permuting : 857/999 [00:14<00:02,   61.96it/s]
 86%|████████▌ | Permuting : 858/999 [00:14<00:02,   60.29it/s]
 86%|████████▋ | Permuting : 862/999 [00:14<00:02,   62.63it/s]
 86%|████████▋ | Permuting : 863/999 [00:14<00:02,   60.89it/s]
 87%|████████▋ | Permuting : 868/999 [00:14<00:02,   64.46it/s]
 87%|████████▋ | Permuting : 870/999 [00:14<00:02,   64.22it/s]
 87%|████████▋ | Permuting : 871/999 [00:14<00:02,   62.76it/s]
 87%|████████▋ | Permuting : 872/999 [00:14<00:02,   61.16it/s]
 88%|████████▊ | Permuting : 876/999 [00:14<00:01,   63.56it/s]
 88%|████████▊ | Permuting : 878/999 [00:14<00:02,   41.53it/s]
 88%|████████▊ | Permuting : 883/999 [00:14<00:02,   44.57it/s]
 88%|████████▊ | Permuting : 884/999 [00:14<00:02,   43.94it/s]
 89%|████████▊ | Permuting : 885/999 [00:14<00:02,   43.31it/s]
 89%|████████▉ | Permuting : 888/999 [00:14<00:02,   44.47it/s]
 89%|████████▉ | Permuting : 890/999 [00:15<00:02,   44.92it/s]
 89%|████████▉ | Permuting : 891/999 [00:15<00:02,   44.22it/s]
 90%|████████▉ | Permuting : 895/999 [00:15<00:02,   46.57it/s]
 90%|█████████ | Permuting : 900/999 [00:15<00:01,   49.83it/s]
 91%|█████████ | Permuting : 905/999 [00:15<00:01,   53.05it/s]
 91%|█████████ | Permuting : 906/999 [00:15<00:01,   51.99it/s]
 91%|█████████ | Permuting : 907/999 [00:15<00:01,   50.98it/s]
 91%|█████████ | Permuting : 909/999 [00:15<00:01,   51.25it/s]
 91%|█████████▏| Permuting : 912/999 [00:15<00:01,   52.32it/s]
 91%|█████████▏| Permuting : 913/999 [00:15<00:01,   51.50it/s]
 91%|█████████▏| Permuting : 914/999 [00:15<00:01,   50.68it/s]
 92%|█████████▏| Permuting : 916/999 [00:15<00:01,   50.98it/s]
 92%|█████████▏| Permuting : 917/999 [00:15<00:01,   50.20it/s]
 92%|█████████▏| Permuting : 921/999 [00:15<00:01,   52.44it/s]
 93%|█████████▎| Permuting : 925/999 [00:15<00:01,   54.90it/s]
 93%|█████████▎| Permuting : 926/999 [00:15<00:01,   53.94it/s]
 93%|█████████▎| Permuting : 927/999 [00:15<00:01,   52.71it/s]
 93%|█████████▎| Permuting : 928/999 [00:15<00:01,   51.47it/s]
 93%|█████████▎| Permuting : 930/999 [00:15<00:01,   51.47it/s]
 93%|█████████▎| Permuting : 933/999 [00:15<00:01,   52.91it/s]
 94%|█████████▎| Permuting : 936/999 [00:15<00:01,   54.25it/s]
 94%|█████████▍| Permuting : 937/999 [00:15<00:01,   53.27it/s]
 94%|█████████▍| Permuting : 940/999 [00:15<00:01,   54.36it/s]
 94%|█████████▍| Permuting : 943/999 [00:15<00:01,   55.41it/s]
 95%|█████████▍| Permuting : 948/999 [00:15<00:00,   59.16it/s]
 95%|█████████▌| Permuting : 951/999 [00:15<00:00,   60.01it/s]
 95%|█████████▌| Permuting : 954/999 [00:15<00:00,   60.82it/s]
 96%|█████████▌| Permuting : 956/999 [00:15<00:00,   60.56it/s]
 96%|█████████▌| Permuting : 958/999 [00:16<00:00,   60.51it/s]
 96%|█████████▌| Permuting : 961/999 [00:16<00:00,   61.30it/s]
 96%|█████████▋| Permuting : 964/999 [00:16<00:00,   62.06it/s]
 97%|█████████▋| Permuting : 965/999 [00:16<00:00,   60.31it/s]
 97%|█████████▋| Permuting : 968/999 [00:16<00:00,   61.11it/s]
 97%|█████████▋| Permuting : 972/999 [00:16<00:00,   63.51it/s]
 97%|█████████▋| Permuting : 973/999 [00:16<00:00,   40.23it/s]
 98%|█████████▊| Permuting : 978/999 [00:16<00:00,   41.91it/s]
 98%|█████████▊| Permuting : 981/999 [00:16<00:00,   43.13it/s]
 98%|█████████▊| Permuting : 983/999 [00:16<00:00,   43.58it/s]
 98%|█████████▊| Permuting : 984/999 [00:16<00:00,   43.17it/s]
 99%|█████████▊| Permuting : 986/999 [00:16<00:00,   43.64it/s]
 99%|█████████▉| Permuting : 988/999 [00:16<00:00,   43.91it/s]
 99%|█████████▉| Permuting : 992/999 [00:16<00:00,   46.20it/s]
100%|█████████▉| Permuting : 995/999 [00:17<00:00,   47.31it/s]
100%|█████████▉| Permuting : 997/999 [00:17<00:00,   47.69it/s]
100%|██████████| Permuting : 999/999 [00:17<00:00,   58.60it/s]

Finally, we can plot our results. It is difficult to visualize clusters in time-frequency-sensor space; plotting time-frequency spectrograms and plotting topomaps display time-frequency and sensor space respectively but they are difficult to combine. We will plot topomaps with the clustered sensors colored in white adjacent to spectrograms in order to provide a visualization of the results. This is a dimensionally limited view, however. Each sensor has its own significant time-frequencies, but, in order to display a single spectrogram, all the time-frequencies that are significant for any sensor in the cluster are plotted as significant. This is a difficulty inherent to visualizing high-dimensional data and should be taken into consideration when interpreting results.

F_obs, clusters, p_values, _ = cluster_stats
good_cluster_inds = np.where(p_values < p_accept)[0]

for i_clu, clu_idx in enumerate(good_cluster_inds):
    # unpack cluster information, get unique indices
    freq_inds, time_inds, space_inds = clusters[clu_idx]
    ch_inds = np.unique(space_inds)
    time_inds = np.unique(time_inds)
    freq_inds = np.unique(freq_inds)

    # get topography for F stat
    f_map = F_obs[freq_inds].mean(axis=0)
    f_map = f_map[time_inds].mean(axis=0)

    # get signals at the sensors contributing to the cluster
    sig_times = epochs.times[time_inds]

    # initialize figure
    fig, ax_topo = plt.subplots(1, 1, figsize=(10, 3), layout="constrained")

    # create spatial mask
    mask = np.zeros((f_map.shape[0], 1), dtype=bool)
    mask[ch_inds, :] = True

    # plot average test statistic and mark significant sensors
    f_evoked = mne.EvokedArray(f_map[:, np.newaxis], epochs.info, tmin=0)
    f_evoked.plot_topomap(
        times=0,
        mask=mask,
        axes=ax_topo,
        cmap="Reds",
        vlim=(np.min, np.max),
        show=False,
        colorbar=False,
        mask_params=dict(markersize=10),
    )
    image = ax_topo.images[0]

    # create additional axes (for ERF and colorbar)
    divider = make_axes_locatable(ax_topo)

    # add axes for colorbar
    ax_colorbar = divider.append_axes("right", size="5%", pad=0.05)
    plt.colorbar(image, cax=ax_colorbar)
    ax_topo.set_xlabel(
        "Averaged F-map ({:0.3f} - {:0.3f} s)".format(*sig_times[[0, -1]])
    )

    # remove the title that would otherwise say "0.000 s"
    ax_topo.set_title("")

    # add new axis for spectrogram
    ax_spec = divider.append_axes("right", size="300%", pad=1.2)
    title = f"Cluster #{i_clu + 1}, {len(ch_inds)} spectrogram"
    if len(ch_inds) > 1:
        title += " (max over channels)"
    F_obs_plot = F_obs[..., ch_inds].max(axis=-1)
    F_obs_plot_sig = np.zeros(F_obs_plot.shape) * np.nan
    F_obs_plot_sig[tuple(np.meshgrid(freq_inds, time_inds))] = F_obs_plot[
        tuple(np.meshgrid(freq_inds, time_inds))
    ]

    for f_image, cmap in zip([F_obs_plot, F_obs_plot_sig], ["gray", "autumn"]):
        c = ax_spec.imshow(
            f_image,
            cmap=cmap,
            aspect="auto",
            origin="lower",
            extent=[epochs.times[0], epochs.times[-1], freqs[0], freqs[-1]],
        )
    ax_spec.set_xlabel("Time (ms)")
    ax_spec.set_ylabel("Frequency (Hz)")
    ax_spec.set_title(title)

    # add another colorbar
    ax_colorbar2 = divider.append_axes("right", size="5%", pad=0.05)
    plt.colorbar(c, cax=ax_colorbar2)
    ax_colorbar2.set_ylabel("F-stat")

    # clean up viz
plt.show()
  • Cluster #1, 4 spectrogram (max over channels)
  • Cluster #2, 6 spectrogram (max over channels)

Exercises#

  • What is the smallest p-value you can obtain, given the finite number of permutations? You can find the answers in the references [1][2].

References#

Total running time of the script: (0 minutes 40.975 seconds)

Estimated memory usage: 129 MB

Gallery generated by Sphinx-Gallery