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.

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

# Authors: Denis Engemann <denis.engemann@gmail.com>
#          Jona Sassenhagen <jona.sassenhagen@gmail.com>
#
# License: BSD-3-Clause
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

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

print(__doc__)

Set parameters

data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
event_fname = data_path + '/MEG/sample/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, 30, fir_design='firwin')
events = mne.read_events(event_fname)

Out:

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 - 30 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: 30.00 Hz
- Upper transition bandwidth: 7.50 Hz (-6 dB cutoff frequency: 33.75 Hz)
- Filter length: 497 samples (3.310 sec)

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,
                    baseline=None, reject=reject, preload=True)

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

X = [epochs[k].get_data() for k in event_id]  # as 3D matrix
X = [np.transpose(x, (0, 2, 1)) for x in X]  # transpose for clustering

Out:

Not setting metadata
Not setting metadata
288 matching events found
No baseline correction applied
Created an SSP operator (subspace dimension = 3)
4 projection items activated
Loading data for 288 events and 106 original time points ...
    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']
    Rejecting  epoch based on EOG : ['EOG 061']
49 bad epochs dropped
Removing projector <Projection | Average EEG reference, active : True, n_channels : 60>
Dropped 19 epochs: 50, 51, 84, 93, 95, 96, 129, 146, 149, 150, 154, 156, 157, 189, 194, 200, 202, 210, 211

Find the FieldTrip neighbor definition to setup sensor adjacency

adjacency, ch_names = find_ch_adjacency(epochs.info, ch_type='mag')

print(type(adjacency))  # it's a sparse matrix!

plt.imshow(adjacency.toarray(), cmap='gray', origin='lower',
           interpolation='nearest')
plt.xlabel('{} Magnetometers'.format(len(ch_names)))
plt.ylabel('{} Magnetometers'.format(len(ch_names)))
plt.title('Between-sensor adjacency')
Between-sensor adjacency

Out:

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. For more background read: Maris/Oostenveld (2007), “Nonparametric statistical testing of EEG- and MEG-data” Journal of Neuroscience Methods, Vol. 164, No. 1., pp. 177-190. doi:10.1016/j.jneumeth.2007.03.024

# set cluster threshold
threshold = 50.0  # very high, but the test is quite sensitive on this data
# set family-wise p-value
p_accept = 0.01

cluster_stats = spatio_temporal_cluster_test(X, n_permutations=1000,
                                             threshold=threshold, tail=1,
                                             n_jobs=1, buffer_size=None,
                                             adjacency=adjacency)

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

Out:

stat_fun(H1): min=0.004107 max=196.094418
Running initial clustering
Found 8 clusters
Permuting 999 times...

  0%|          |  : 0/999 [00:00<?,       ?it/s]
  0%|          |  : 2/999 [00:00<00:17,   58.52it/s]
  1%|          |  : 5/999 [00:00<00:13,   73.90it/s]
  1%|          |  : 9/999 [00:00<00:11,   89.39it/s]
  1%|1         |  : 12/999 [00:00<00:11,   89.21it/s]
  2%|1         |  : 16/999 [00:00<00:10,   95.58it/s]
  2%|2         |  : 20/999 [00:00<00:09,   99.84it/s]
  2%|2         |  : 23/999 [00:00<00:09,   98.02it/s]
  3%|2         |  : 26/999 [00:00<00:10,   96.66it/s]
  3%|3         |  : 30/999 [00:00<00:09,   99.56it/s]
  3%|3         |  : 34/999 [00:00<00:09,  101.90it/s]
  4%|3         |  : 37/999 [00:00<00:09,  100.35it/s]
  4%|4         |  : 41/999 [00:00<00:09,  102.31it/s]
  4%|4         |  : 44/999 [00:00<00:09,  100.92it/s]
  5%|4         |  : 48/999 [00:00<00:09,  102.63it/s]
  5%|5         |  : 51/999 [00:00<00:09,  101.35it/s]
  6%|5         |  : 55/999 [00:00<00:09,  102.87it/s]
  6%|5         |  : 58/999 [00:00<00:09,  101.64it/s]
  6%|6         |  : 62/999 [00:00<00:09,  103.02it/s]
  7%|6         |  : 65/999 [00:00<00:09,  101.87it/s]
  7%|6         |  : 68/999 [00:00<00:09,  100.85it/s]
  7%|7         |  : 72/999 [00:00<00:09,  102.18it/s]
  8%|7         |  : 75/999 [00:00<00:09,  101.18it/s]
  8%|7         |  : 78/999 [00:00<00:09,  100.28it/s]
  8%|8         |  : 82/999 [00:00<00:09,  101.54it/s]
  9%|8         |  : 85/999 [00:00<00:09,  100.66it/s]
  9%|8         |  : 89/999 [00:00<00:08,  101.86it/s]
  9%|9         |  : 92/999 [00:00<00:08,  100.98it/s]
 10%|9         |  : 95/999 [00:00<00:09,  100.16it/s]
 10%|9         |  : 99/999 [00:00<00:08,  101.33it/s]
 10%|#         |  : 102/999 [00:01<00:08,  100.53it/s]
 11%|#         |  : 105/999 [00:01<00:08,   99.79it/s]
 11%|#         |  : 109/999 [00:01<00:08,  100.94it/s]
 11%|#1        |  : 112/999 [00:01<00:08,  100.18it/s]
 12%|#1        |  : 115/999 [00:01<00:08,   99.49it/s]
 12%|#1        |  : 119/999 [00:01<00:08,  100.60it/s]
 12%|#2        |  : 122/999 [00:01<00:08,   99.90it/s]
 13%|#2        |  : 126/999 [00:01<00:08,  100.97it/s]
 13%|#2        |  : 129/999 [00:01<00:08,  100.25it/s]
 13%|#3        |  : 132/999 [00:01<00:08,   99.59it/s]
 14%|#3        |  : 136/999 [00:01<00:08,  100.65it/s]
 14%|#3        |  : 139/999 [00:01<00:08,   99.96it/s]
 14%|#4        |  : 142/999 [00:01<00:08,   99.32it/s]
 15%|#4        |  : 146/999 [00:01<00:08,  100.37it/s]
 15%|#4        |  : 149/999 [00:01<00:08,   99.72it/s]
 15%|#5        |  : 153/999 [00:01<00:08,  100.75it/s]
 16%|#5        |  : 156/999 [00:01<00:08,  100.08it/s]
 16%|#6        |  : 160/999 [00:01<00:08,  101.08it/s]
 16%|#6        |  : 164/999 [00:01<00:08,  102.01it/s]
 17%|#6        |  : 167/999 [00:01<00:08,  101.29it/s]
 17%|#7        |  : 171/999 [00:01<00:08,  102.21it/s]
 17%|#7        |  : 174/999 [00:01<00:08,  101.49it/s]
 18%|#7        |  : 178/999 [00:01<00:08,  102.40it/s]
 18%|#8        |  : 181/999 [00:01<00:08,  101.67it/s]
 19%|#8        |  : 185/999 [00:01<00:07,  102.54it/s]
 19%|#8        |  : 188/999 [00:01<00:07,  101.81it/s]
 19%|#9        |  : 192/999 [00:01<00:07,  102.69it/s]
 20%|#9        |  : 195/999 [00:01<00:07,  101.96it/s]
 20%|#9        |  : 199/999 [00:01<00:07,  102.82it/s]
 20%|##        |  : 202/999 [00:01<00:07,  102.08it/s]
 21%|##        |  : 206/999 [00:02<00:07,  102.92it/s]
 21%|##        |  : 209/999 [00:02<00:07,  102.19it/s]
 21%|##1       |  : 213/999 [00:02<00:07,  103.03it/s]
 22%|##1       |  : 216/999 [00:02<00:07,  102.26it/s]
 22%|##1       |  : 219/999 [00:02<00:07,  101.56it/s]
 22%|##2       |  : 223/999 [00:02<00:07,  102.40it/s]
 23%|##2       |  : 226/999 [00:02<00:07,  101.69it/s]
 23%|##3       |  : 230/999 [00:02<00:07,  102.54it/s]
 23%|##3       |  : 233/999 [00:02<00:07,  101.82it/s]
 24%|##3       |  : 236/999 [00:02<00:07,  101.14it/s]
 24%|##4       |  : 240/999 [00:02<00:07,  102.01it/s]
 24%|##4       |  : 243/999 [00:02<00:07,  101.33it/s]
 25%|##4       |  : 247/999 [00:02<00:07,  102.20it/s]
 25%|##5       |  : 250/999 [00:02<00:07,  101.50it/s]
 25%|##5       |  : 254/999 [00:02<00:07,  102.36it/s]
 26%|##5       |  : 257/999 [00:02<00:07,  101.66it/s]
 26%|##6       |  : 261/999 [00:02<00:07,  102.50it/s]
 26%|##6       |  : 264/999 [00:02<00:07,  101.81it/s]
 27%|##6       |  : 268/999 [00:02<00:07,  102.65it/s]
 27%|##7       |  : 271/999 [00:02<00:07,  101.94it/s]
 28%|##7       |  : 275/999 [00:02<00:07,  102.77it/s]
 28%|##7       |  : 278/999 [00:02<00:07,  102.04it/s]
 28%|##8       |  : 282/999 [00:02<00:06,  102.87it/s]
 29%|##8       |  : 285/999 [00:02<00:06,  102.15it/s]
 29%|##8       |  : 289/999 [00:02<00:06,  102.97it/s]
 29%|##9       |  : 292/999 [00:02<00:06,  102.25it/s]
 30%|##9       |  : 296/999 [00:02<00:06,  103.06it/s]
 30%|##9       |  : 299/999 [00:02<00:06,  102.32it/s]
 30%|###       |  : 303/999 [00:02<00:06,  103.11it/s]
 31%|###       |  : 306/999 [00:03<00:06,  102.37it/s]
 31%|###1      |  : 310/999 [00:03<00:06,  103.15it/s]
 31%|###1      |  : 313/999 [00:03<00:06,  102.41it/s]
 32%|###1      |  : 317/999 [00:03<00:06,  103.19it/s]
 32%|###2      |  : 320/999 [00:03<00:06,  102.45it/s]
 32%|###2      |  : 324/999 [00:03<00:06,  103.25it/s]
 33%|###2      |  : 327/999 [00:03<00:06,  102.51it/s]
 33%|###3      |  : 330/999 [00:03<00:06,  101.81it/s]
 33%|###3      |  : 333/999 [00:03<00:06,  101.15it/s]
 34%|###3      |  : 337/999 [00:03<00:06,  102.01it/s]
 34%|###4      |  : 340/999 [00:03<00:06,  101.34it/s]
 34%|###4      |  : 344/999 [00:03<00:06,  102.18it/s]
 35%|###4      |  : 347/999 [00:03<00:06,  101.50it/s]
 35%|###5      |  : 350/999 [00:03<00:06,  100.86it/s]
 35%|###5      |  : 354/999 [00:03<00:06,  101.72it/s]
 36%|###5      |  : 357/999 [00:03<00:06,  101.06it/s]
 36%|###6      |  : 361/999 [00:03<00:06,  101.93it/s]
 36%|###6      |  : 364/999 [00:03<00:06,  101.26it/s]
 37%|###6      |  : 367/999 [00:03<00:06,  100.62it/s]
 37%|###7      |  : 371/999 [00:03<00:06,  101.50it/s]
 37%|###7      |  : 374/999 [00:03<00:06,  100.85it/s]
 38%|###7      |  : 378/999 [00:03<00:06,  101.73it/s]
 38%|###8      |  : 381/999 [00:03<00:06,  101.07it/s]
 39%|###8      |  : 385/999 [00:03<00:06,  101.94it/s]
 39%|###8      |  : 388/999 [00:03<00:06,  101.27it/s]
 39%|###9      |  : 392/999 [00:03<00:05,  102.11it/s]
 40%|###9      |  : 395/999 [00:03<00:05,  101.43it/s]
 40%|###9      |  : 399/999 [00:03<00:05,  102.28it/s]
 40%|####      |  : 402/999 [00:03<00:05,  101.60it/s]
 41%|####      |  : 405/999 [00:03<00:05,  100.95it/s]
 41%|####      |  : 409/999 [00:04<00:05,  101.80it/s]
 41%|####1     |  : 412/999 [00:04<00:05,  101.14it/s]
 42%|####1     |  : 416/999 [00:04<00:05,  101.99it/s]
 42%|####1     |  : 419/999 [00:04<00:05,  101.32it/s]
 42%|####2     |  : 423/999 [00:04<00:05,  102.17it/s]
 43%|####2     |  : 426/999 [00:04<00:05,  101.50it/s]
 43%|####3     |  : 430/999 [00:04<00:05,  102.34it/s]
 43%|####3     |  : 433/999 [00:04<00:05,  101.64it/s]
 44%|####3     |  : 436/999 [00:04<00:05,  100.99it/s]
 44%|####4     |  : 440/999 [00:04<00:05,  101.85it/s]
 44%|####4     |  : 443/999 [00:04<00:05,  101.18it/s]
 45%|####4     |  : 447/999 [00:04<00:05,  102.04it/s]
 45%|####5     |  : 450/999 [00:04<00:05,  101.36it/s]
 45%|####5     |  : 454/999 [00:04<00:05,  102.19it/s]
 46%|####5     |  : 457/999 [00:04<00:05,  101.52it/s]
 46%|####6     |  : 461/999 [00:04<00:05,  102.35it/s]
 46%|####6     |  : 464/999 [00:04<00:05,  101.67it/s]
 47%|####6     |  : 468/999 [00:04<00:05,  102.50it/s]
 47%|####7     |  : 471/999 [00:04<00:05,  101.81it/s]
 48%|####7     |  : 475/999 [00:04<00:05,  102.63it/s]
 48%|####7     |  : 478/999 [00:04<00:05,  101.93it/s]
 48%|####8     |  : 482/999 [00:04<00:05,  102.74it/s]
 49%|####8     |  : 485/999 [00:04<00:05,  102.04it/s]
 49%|####8     |  : 489/999 [00:04<00:04,  102.85it/s]
 49%|####9     |  : 492/999 [00:04<00:04,  102.14it/s]
 50%|####9     |  : 495/999 [00:04<00:04,  101.46it/s]
 50%|####9     |  : 499/999 [00:04<00:04,  102.29it/s]
 50%|#####     |  : 502/999 [00:04<00:04,  101.62it/s]
 51%|#####     |  : 506/999 [00:04<00:04,  102.45it/s]
 51%|#####     |  : 509/999 [00:05<00:04,  101.76it/s]
 51%|#####1    |  : 513/999 [00:05<00:04,  102.59it/s]
 52%|#####1    |  : 516/999 [00:05<00:04,  101.89it/s]
 52%|#####2    |  : 520/999 [00:05<00:04,  102.71it/s]
 52%|#####2    |  : 523/999 [00:05<00:04,  102.01it/s]
 53%|#####2    |  : 527/999 [00:05<00:04,  102.83it/s]
 53%|#####3    |  : 531/999 [00:05<00:04,  103.60it/s]
 53%|#####3    |  : 534/999 [00:05<00:04,  102.86it/s]
 54%|#####3    |  : 538/999 [00:05<00:04,  103.62it/s]
 54%|#####4    |  : 541/999 [00:05<00:04,  102.88it/s]
 55%|#####4    |  : 545/999 [00:05<00:04,  103.64it/s]
 55%|#####4    |  : 548/999 [00:05<00:04,  102.90it/s]
 55%|#####5    |  : 551/999 [00:05<00:04,  102.19it/s]
 56%|#####5    |  : 555/999 [00:05<00:04,  102.99it/s]
 56%|#####5    |  : 558/999 [00:05<00:04,  102.28it/s]
 56%|#####6    |  : 561/999 [00:05<00:04,  101.60it/s]
 57%|#####6    |  : 565/999 [00:05<00:04,  102.43it/s]
 57%|#####6    |  : 569/999 [00:05<00:04,  103.22it/s]
 57%|#####7    |  : 572/999 [00:05<00:04,  102.50it/s]
 58%|#####7    |  : 576/999 [00:05<00:04,  103.30it/s]
 58%|#####8    |  : 580/999 [00:05<00:04,  104.04it/s]
 58%|#####8    |  : 583/999 [00:05<00:04,  103.27it/s]
 59%|#####8    |  : 587/999 [00:05<00:03,  104.03it/s]
 59%|#####9    |  : 591/999 [00:05<00:03,  104.75it/s]
 59%|#####9    |  : 594/999 [00:05<00:03,  103.94it/s]
 60%|#####9    |  : 598/999 [00:05<00:03,  104.66it/s]
 60%|######    |  : 601/999 [00:05<00:03,  103.85it/s]
 61%|######    |  : 605/999 [00:05<00:03,  104.58it/s]
 61%|######    |  : 609/999 [00:05<00:03,  105.26it/s]
 61%|######1   |  : 613/999 [00:05<00:03,  105.90it/s]
 62%|######1   |  : 616/999 [00:06<00:03,  105.05it/s]
 62%|######2   |  : 620/999 [00:06<00:03,  105.72it/s]
 62%|######2   |  : 624/999 [00:06<00:03,  106.34it/s]
 63%|######2   |  : 627/999 [00:06<00:03,  105.45it/s]
 63%|######3   |  : 631/999 [00:06<00:03,  106.09it/s]
 64%|######3   |  : 635/999 [00:06<00:03,  106.71it/s]
 64%|######3   |  : 638/999 [00:06<00:03,  105.80it/s]
 64%|######4   |  : 642/999 [00:06<00:03,  106.42it/s]
 65%|######4   |  : 646/999 [00:06<00:03,  107.01it/s]
 65%|######4   |  : 649/999 [00:06<00:03,  106.09it/s]
 65%|######5   |  : 653/999 [00:06<00:03,  106.70it/s]
 66%|######5   |  : 657/999 [00:06<00:03,  107.27it/s]
 66%|######6   |  : 660/999 [00:06<00:03,  106.34it/s]
 66%|######6   |  : 664/999 [00:06<00:03,  106.94it/s]
 67%|######6   |  : 668/999 [00:06<00:03,  107.51it/s]
 67%|######7   |  : 671/999 [00:06<00:03,  106.57it/s]
 68%|######7   |  : 675/999 [00:06<00:03,  107.15it/s]
 68%|######7   |  : 678/999 [00:06<00:03,  106.24it/s]
 68%|######8   |  : 682/999 [00:06<00:02,  106.84it/s]
 69%|######8   |  : 686/999 [00:06<00:02,  107.43it/s]
 69%|######8   |  : 689/999 [00:06<00:02,  106.50it/s]
 69%|######9   |  : 693/999 [00:06<00:02,  107.09it/s]
 70%|######9   |  : 697/999 [00:06<00:02,  107.66it/s]
 70%|#######   |  : 700/999 [00:06<00:02,  106.72it/s]
 70%|#######   |  : 704/999 [00:06<00:02,  107.31it/s]
 71%|#######   |  : 707/999 [00:06<00:02,  106.38it/s]
 71%|#######1  |  : 711/999 [00:06<00:02,  106.98it/s]
 72%|#######1  |  : 715/999 [00:06<00:02,  107.55it/s]
 72%|#######1  |  : 718/999 [00:06<00:02,  106.61it/s]
 72%|#######2  |  : 722/999 [00:07<00:02,  107.18it/s]
 73%|#######2  |  : 725/999 [00:07<00:02,  106.26it/s]
 73%|#######2  |  : 729/999 [00:07<00:02,  106.85it/s]
 73%|#######3  |  : 733/999 [00:07<00:02,  107.42it/s]
 74%|#######3  |  : 736/999 [00:07<00:02,  106.49it/s]
 74%|#######4  |  : 740/999 [00:07<00:02,  107.08it/s]
 74%|#######4  |  : 743/999 [00:07<00:02,  106.15it/s]
 75%|#######4  |  : 747/999 [00:07<00:02,  106.76it/s]
 75%|#######5  |  : 751/999 [00:07<00:02,  107.34it/s]
 75%|#######5  |  : 754/999 [00:07<00:02,  106.41it/s]
 76%|#######5  |  : 758/999 [00:07<00:02,  107.01it/s]
 76%|#######6  |  : 761/999 [00:07<00:02,  106.10it/s]
 77%|#######6  |  : 765/999 [00:07<00:02,  106.72it/s]
 77%|#######6  |  : 769/999 [00:07<00:02,  107.29it/s]
 77%|#######7  |  : 772/999 [00:07<00:02,  106.36it/s]
 78%|#######7  |  : 776/999 [00:07<00:02,  106.96it/s]
 78%|#######7  |  : 779/999 [00:07<00:02,  106.04it/s]
 78%|#######8  |  : 783/999 [00:07<00:02,  106.65it/s]
 79%|#######8  |  : 786/999 [00:07<00:02,  105.75it/s]
 79%|#######9  |  : 790/999 [00:07<00:01,  106.36it/s]
 79%|#######9  |  : 794/999 [00:07<00:01,  106.95it/s]
 80%|#######9  |  : 797/999 [00:07<00:01,  106.05it/s]
 80%|########  |  : 801/999 [00:07<00:01,  106.66it/s]
 81%|########  |  : 805/999 [00:07<00:01,  107.25it/s]
 81%|########  |  : 808/999 [00:07<00:01,  106.31it/s]
 81%|########1 |  : 812/999 [00:07<00:01,  106.91it/s]
 82%|########1 |  : 816/999 [00:07<00:01,  107.48it/s]
 82%|########1 |  : 819/999 [00:07<00:01,  106.55it/s]
 82%|########2 |  : 823/999 [00:07<00:01,  107.13it/s]
 83%|########2 |  : 827/999 [00:07<00:01,  107.69it/s]
 83%|########3 |  : 830/999 [00:08<00:01,  106.75it/s]
 83%|########3 |  : 834/999 [00:08<00:01,  107.33it/s]
 84%|########3 |  : 838/999 [00:08<00:01,  107.88it/s]
 84%|########4 |  : 841/999 [00:08<00:01,  106.93it/s]
 85%|########4 |  : 845/999 [00:08<00:01,  107.50it/s]
 85%|########4 |  : 848/999 [00:08<00:01,  106.56it/s]
 85%|########5 |  : 852/999 [00:08<00:01,  107.14it/s]
 86%|########5 |  : 856/999 [00:08<00:01,  107.69it/s]
 86%|########5 |  : 859/999 [00:08<00:01,  106.74it/s]
 86%|########6 |  : 863/999 [00:08<00:01,  107.31it/s]
 87%|########6 |  : 867/999 [00:08<00:01,  107.86it/s]
 87%|########7 |  : 871/999 [00:08<00:01,  108.38it/s]
 87%|########7 |  : 874/999 [00:08<00:01,  107.40it/s]
 88%|########7 |  : 878/999 [00:08<00:01,  107.95it/s]
 88%|########8 |  : 882/999 [00:08<00:01,  108.47it/s]
 89%|########8 |  : 885/999 [00:08<00:01,  107.49it/s]
 89%|########8 |  : 889/999 [00:08<00:01,  108.04it/s]
 89%|########9 |  : 892/999 [00:08<00:00,  107.08it/s]
 90%|########9 |  : 896/999 [00:08<00:00,  107.64it/s]
 90%|########9 |  : 899/999 [00:08<00:00,  106.70it/s]
 90%|######### |  : 903/999 [00:08<00:00,  107.29it/s]
 91%|######### |  : 907/999 [00:08<00:00,  107.83it/s]
 91%|#########1|  : 910/999 [00:08<00:00,  106.88it/s]
 91%|#########1|  : 914/999 [00:08<00:00,  107.46it/s]
 92%|#########1|  : 917/999 [00:08<00:00,  106.53it/s]
 92%|#########2|  : 921/999 [00:08<00:00,  107.12it/s]
 93%|#########2|  : 925/999 [00:08<00:00,  107.67it/s]
 93%|#########2|  : 928/999 [00:08<00:00,  106.73it/s]
 93%|#########3|  : 932/999 [00:08<00:00,  107.31it/s]
 94%|#########3|  : 936/999 [00:08<00:00,  107.86it/s]
 94%|#########3|  : 939/999 [00:09<00:00,  106.91it/s]
 94%|#########4|  : 943/999 [00:09<00:00,  107.49it/s]
 95%|#########4|  : 946/999 [00:09<00:00,  106.55it/s]
 95%|#########5|  : 950/999 [00:09<00:00,  107.14it/s]
 95%|#########5|  : 954/999 [00:09<00:00,  107.69it/s]
 96%|#########5|  : 957/999 [00:09<00:00,  106.75it/s]
 96%|#########6|  : 961/999 [00:09<00:00,  107.32it/s]
 97%|#########6|  : 965/999 [00:09<00:00,  107.87it/s]
 97%|#########6|  : 968/999 [00:09<00:00,  106.90it/s]
 97%|#########7|  : 972/999 [00:09<00:00,  107.47it/s]
 98%|#########7|  : 976/999 [00:09<00:00,  108.00it/s]
 98%|#########8|  : 980/999 [00:09<00:00,  108.52it/s]
 98%|#########8|  : 983/999 [00:09<00:00,  107.51it/s]
 99%|#########8|  : 987/999 [00:09<00:00,  108.05it/s]
 99%|#########9|  : 991/999 [00:09<00:00,  108.57it/s]
 99%|#########9|  : 994/999 [00:09<00:00,  107.58it/s]
100%|#########9|  : 998/999 [00:09<00:00,  108.11it/s]
100%|##########|  : 999/999 [00:09<00:00,  104.28it/s]
Computing cluster p-values
Done.

Note. The same functions work with source estimate. 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

# 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 = T_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))

    # 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',
                          vmin=np.min, vmax=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]]))

    # add new axis for time courses and plot time courses
    ax_signals = divider.append_axes('right', size='300%', pad=1.2)
    title = 'Cluster #{0}, {1} sensor'.format(i_clu + 1, len(ch_inds))
    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)

    # clean up viz
    mne.viz.tight_layout(fig=fig)
    fig.subplots_adjust(bottom=.05)
    plt.show()
  • 0.000 s, Cluster #1, 3 sensors (mean) (GFP)
  • 0.000 s, Cluster #2, 15 sensors (mean) (GFP)
  • 0.000 s, Cluster #3, 1 sensor
  • 0.000 s, Cluster #4, 3 sensors (mean) (GFP)
  • 0.000 s, Cluster #5, 1 sensor
  • 0.000 s, Cluster #6, 2 sensors (mean) (GFP)
  • 0.000 s, Cluster #7, 1 sensor
  • 0.000 s, Cluster #8, 1 sensor

Out:

combining channels using "gfp"
combining channels using "gfp"
combining channels using "gfp"
combining channels using "gfp"
combining channels using "gfp"
combining channels using "gfp"
combining channels using "gfp"
combining channels using "gfp"
combining channels using "gfp"
combining channels using "gfp"
combining channels using "gfp"
combining channels using "gfp"
combining channels using "gfp"
combining channels using "gfp"
combining channels using "gfp"
combining channels using "gfp"

Exercises

  • What is the smallest p-value you can obtain, given the finite number of permutations?

  • use an F distribution to compute the threshold by traditional significance levels. Hint: take a look at scipy.stats.f

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

Estimated memory usage: 128 MB

Gallery generated by Sphinx-Gallery