Note

Click here to download the full example code

# 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')
```

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.16it/s]
0%| | : 4/999 [00:00<00:17, 58.45it/s]
1%| | : 7/999 [00:00<00:14, 68.86it/s]
1%|1 | : 10/999 [00:00<00:13, 74.20it/s]
1%|1 | : 13/999 [00:00<00:12, 77.36it/s]
2%|1 | : 15/999 [00:00<00:13, 73.84it/s]
2%|1 | : 19/999 [00:00<00:12, 81.17it/s]
2%|2 | : 21/999 [00:00<00:12, 77.91it/s]
2%|2 | : 24/999 [00:00<00:12, 79.31it/s]
3%|2 | : 28/999 [00:00<00:11, 84.15it/s]
3%|3 | : 30/999 [00:00<00:11, 81.26it/s]
3%|3 | : 32/999 [00:00<00:12, 78.83it/s]
4%|3 | : 36/999 [00:00<00:11, 82.87it/s]
4%|3 | : 38/999 [00:00<00:11, 80.56it/s]
4%|4 | : 41/999 [00:00<00:11, 81.29it/s]
5%|4 | : 45/999 [00:00<00:11, 84.57it/s]
5%|4 | : 47/999 [00:00<00:11, 82.33it/s]
5%|5 | : 50/999 [00:00<00:11, 82.82it/s]
5%|5 | : 53/999 [00:00<00:11, 83.27it/s]
6%|5 | : 55/999 [00:00<00:11, 81.32it/s]
6%|5 | : 58/999 [00:00<00:11, 81.89it/s]
6%|6 | : 62/999 [00:00<00:11, 84.56it/s]
6%|6 | : 64/999 [00:00<00:11, 82.69it/s]
7%|6 | : 67/999 [00:00<00:11, 83.11it/s]
7%|7 | : 70/999 [00:00<00:11, 83.49it/s]
7%|7 | : 72/999 [00:00<00:11, 81.81it/s]
8%|7 | : 75/999 [00:00<00:11, 82.27it/s]
8%|7 | : 79/999 [00:00<00:10, 84.59it/s]
8%|8 | : 81/999 [00:00<00:11, 82.91it/s]
8%|8 | : 84/999 [00:01<00:10, 83.26it/s]
9%|8 | : 87/999 [00:01<00:10, 83.59it/s]
9%|9 | : 90/999 [00:01<00:10, 83.86it/s]
9%|9 | : 93/999 [00:01<00:10, 84.14it/s]
10%|9 | : 96/999 [00:01<00:10, 84.40it/s]
10%|9 | : 98/999 [00:01<00:10, 82.85it/s]
10%|# | : 102/999 [00:01<00:10, 84.93it/s]
11%|# | : 105/999 [00:01<00:10, 85.16it/s]
11%|# | : 107/999 [00:01<00:10, 83.64it/s]
11%|#1 | : 110/999 [00:01<00:10, 83.94it/s]
11%|#1 | : 113/999 [00:01<00:10, 84.21it/s]
12%|#1 | : 116/999 [00:01<00:10, 84.46it/s]
12%|#1 | : 119/999 [00:01<00:10, 84.70it/s]
12%|#2 | : 122/999 [00:01<00:10, 84.91it/s]
12%|#2 | : 124/999 [00:01<00:10, 83.46it/s]
13%|#2 | : 128/999 [00:01<00:10, 85.39it/s]
13%|#3 | : 130/999 [00:01<00:10, 83.95it/s]
13%|#3 | : 133/999 [00:01<00:10, 84.20it/s]
14%|#3 | : 136/999 [00:01<00:10, 84.45it/s]
14%|#3 | : 139/999 [00:01<00:10, 84.66it/s]
14%|#4 | : 141/999 [00:01<00:10, 83.28it/s]
15%|#4 | : 145/999 [00:01<00:10, 85.16it/s]
15%|#4 | : 148/999 [00:01<00:09, 85.35it/s]
15%|#5 | : 150/999 [00:01<00:10, 83.94it/s]
15%|#5 | : 154/999 [00:01<00:09, 85.76it/s]
16%|#5 | : 156/999 [00:01<00:09, 84.34it/s]
16%|#5 | : 159/999 [00:01<00:09, 84.56it/s]
16%|#6 | : 162/999 [00:01<00:09, 84.76it/s]
17%|#6 | : 165/999 [00:01<00:09, 84.96it/s]
17%|#6 | : 167/999 [00:02<00:09, 83.60it/s]
17%|#7 | : 171/999 [00:02<00:09, 85.41it/s]
17%|#7 | : 173/999 [00:02<00:09, 84.05it/s]
18%|#7 | : 176/999 [00:02<00:09, 84.29it/s]
18%|#8 | : 180/999 [00:02<00:09, 86.06it/s]
18%|#8 | : 182/999 [00:02<00:09, 84.65it/s]
19%|#8 | : 185/999 [00:02<00:09, 84.85it/s]
19%|#8 | : 189/999 [00:02<00:09, 86.58it/s]
19%|#9 | : 191/999 [00:02<00:09, 85.16it/s]
19%|#9 | : 193/999 [00:02<00:09, 83.79it/s]
20%|#9 | : 197/999 [00:02<00:09, 85.57it/s]
20%|#9 | : 199/999 [00:02<00:09, 84.21it/s]
20%|## | : 202/999 [00:02<00:09, 84.44it/s]
21%|## | : 206/999 [00:02<00:09, 86.18it/s]
21%|## | : 208/999 [00:02<00:09, 84.80it/s]
21%|##1 | : 211/999 [00:02<00:09, 84.99it/s]
22%|##1 | : 215/999 [00:02<00:09, 86.69it/s]
22%|##1 | : 217/999 [00:02<00:09, 85.24it/s]
22%|##2 | : 220/999 [00:02<00:09, 85.39it/s]
22%|##2 | : 224/999 [00:02<00:08, 87.06it/s]
23%|##2 | : 226/999 [00:02<00:09, 85.61it/s]
23%|##2 | : 229/999 [00:02<00:08, 85.75it/s]
23%|##3 | : 233/999 [00:02<00:08, 87.38it/s]
24%|##3 | : 235/999 [00:02<00:08, 85.92it/s]
24%|##3 | : 238/999 [00:02<00:08, 86.04it/s]
24%|##4 | : 242/999 [00:02<00:08, 87.66it/s]
24%|##4 | : 244/999 [00:02<00:08, 86.19it/s]
25%|##4 | : 247/999 [00:02<00:08, 86.30it/s]
25%|##5 | : 250/999 [00:02<00:08, 86.42it/s]
25%|##5 | : 253/999 [00:02<00:08, 86.50it/s]
26%|##5 | : 256/999 [00:03<00:08, 86.59it/s]
26%|##5 | : 259/999 [00:03<00:08, 86.69it/s]
26%|##6 | : 261/999 [00:03<00:08, 85.28it/s]
27%|##6 | : 265/999 [00:03<00:08, 86.94it/s]
27%|##6 | : 268/999 [00:03<00:08, 87.02it/s]
27%|##7 | : 270/999 [00:03<00:08, 85.59it/s]
27%|##7 | : 273/999 [00:03<00:08, 85.72it/s]
28%|##7 | : 276/999 [00:03<00:08, 85.87it/s]
28%|##7 | : 279/999 [00:03<00:08, 85.98it/s]
28%|##8 | : 282/999 [00:03<00:08, 86.10it/s]
29%|##8 | : 285/999 [00:03<00:08, 86.23it/s]
29%|##8 | : 287/999 [00:03<00:08, 84.84it/s]
29%|##9 | : 291/999 [00:03<00:08, 86.51it/s]
29%|##9 | : 293/999 [00:03<00:08, 85.13it/s]
30%|##9 | : 296/999 [00:03<00:08, 85.28it/s]
30%|##9 | : 299/999 [00:03<00:08, 85.43it/s]
30%|### | : 302/999 [00:03<00:08, 85.59it/s]
30%|### | : 304/999 [00:03<00:08, 84.26it/s]
31%|### | : 308/999 [00:03<00:08, 85.96it/s]
31%|###1 | : 311/999 [00:03<00:07, 86.10it/s]
31%|###1 | : 313/999 [00:03<00:08, 84.75it/s]
32%|###1 | : 317/999 [00:03<00:07, 86.42it/s]
32%|###1 | : 319/999 [00:03<00:07, 85.05it/s]
32%|###2 | : 322/999 [00:03<00:07, 85.23it/s]
33%|###2 | : 325/999 [00:03<00:07, 85.41it/s]
33%|###2 | : 328/999 [00:03<00:07, 85.57it/s]
33%|###3 | : 330/999 [00:03<00:07, 84.25it/s]
33%|###3 | : 334/999 [00:03<00:07, 85.96it/s]
34%|###3 | : 336/999 [00:03<00:07, 84.61it/s]
34%|###3 | : 339/999 [00:04<00:07, 84.81it/s]
34%|###4 | : 343/999 [00:04<00:07, 86.49it/s]
35%|###4 | : 345/999 [00:04<00:07, 85.12it/s]
35%|###4 | : 347/999 [00:04<00:07, 83.82it/s]
35%|###5 | : 351/999 [00:04<00:07, 85.55it/s]
35%|###5 | : 353/999 [00:04<00:07, 84.23it/s]
36%|###5 | : 356/999 [00:04<00:07, 84.44it/s]
36%|###6 | : 360/999 [00:04<00:07, 86.14it/s]
36%|###6 | : 362/999 [00:04<00:07, 84.79it/s]
37%|###6 | : 365/999 [00:04<00:07, 84.99it/s]
37%|###6 | : 368/999 [00:04<00:07, 85.17it/s]
37%|###7 | : 371/999 [00:04<00:07, 85.34it/s]
37%|###7 | : 373/999 [00:04<00:07, 84.02it/s]
38%|###7 | : 377/999 [00:04<00:07, 85.72it/s]
38%|###7 | : 379/999 [00:04<00:07, 84.39it/s]
38%|###8 | : 382/999 [00:04<00:07, 84.58it/s]
39%|###8 | : 386/999 [00:04<00:07, 86.26it/s]
39%|###8 | : 388/999 [00:04<00:07, 84.87it/s]
39%|###9 | : 391/999 [00:04<00:07, 85.04it/s]
39%|###9 | : 394/999 [00:04<00:07, 85.18it/s]
40%|###9 | : 396/999 [00:04<00:07, 83.85it/s]
40%|###9 | : 399/999 [00:04<00:07, 84.08it/s]
40%|#### | : 403/999 [00:04<00:06, 85.77it/s]
41%|#### | : 405/999 [00:04<00:07, 84.41it/s]
41%|#### | : 408/999 [00:04<00:06, 84.61it/s]
41%|####1 | : 411/999 [00:04<00:06, 84.81it/s]
41%|####1 | : 414/999 [00:04<00:06, 84.97it/s]
42%|####1 | : 417/999 [00:04<00:06, 85.13it/s]
42%|####2 | : 420/999 [00:04<00:06, 85.30it/s]
42%|####2 | : 423/999 [00:04<00:06, 85.44it/s]
43%|####2 | : 426/999 [00:05<00:06, 85.58it/s]
43%|####2 | : 429/999 [00:05<00:06, 85.72it/s]
43%|####3 | : 431/999 [00:05<00:06, 84.36it/s]
44%|####3 | : 435/999 [00:05<00:06, 86.03it/s]
44%|####3 | : 438/999 [00:05<00:06, 86.16it/s]
44%|####4 | : 440/999 [00:05<00:06, 84.79it/s]
44%|####4 | : 443/999 [00:05<00:06, 84.97it/s]
45%|####4 | : 446/999 [00:05<00:06, 85.15it/s]
45%|####4 | : 449/999 [00:05<00:06, 85.31it/s]
45%|####5 | : 452/999 [00:05<00:06, 85.46it/s]
46%|####5 | : 455/999 [00:05<00:06, 85.61it/s]
46%|####5 | : 457/999 [00:05<00:06, 84.27it/s]
46%|####6 | : 461/999 [00:05<00:06, 85.94it/s]
46%|####6 | : 464/999 [00:05<00:06, 86.07it/s]
47%|####6 | : 466/999 [00:05<00:06, 84.71it/s]
47%|####6 | : 469/999 [00:05<00:06, 84.89it/s]
47%|####7 | : 472/999 [00:05<00:06, 85.07it/s]
48%|####7 | : 475/999 [00:05<00:06, 85.23it/s]
48%|####7 | : 478/999 [00:05<00:06, 85.39it/s]
48%|####8 | : 481/999 [00:05<00:06, 85.55it/s]
48%|####8 | : 483/999 [00:05<00:06, 84.21it/s]
49%|####8 | : 487/999 [00:05<00:05, 85.89it/s]
49%|####9 | : 490/999 [00:05<00:05, 86.03it/s]
49%|####9 | : 492/999 [00:05<00:05, 84.67it/s]
50%|####9 | : 496/999 [00:05<00:05, 86.32it/s]
50%|####9 | : 498/999 [00:05<00:05, 84.97it/s]
50%|##### | : 501/999 [00:05<00:05, 85.13it/s]
50%|##### | : 504/999 [00:05<00:05, 85.31it/s]
51%|##### | : 507/999 [00:05<00:05, 85.47it/s]
51%|##### | : 509/999 [00:06<00:05, 84.14it/s]
51%|#####1 | : 513/999 [00:06<00:05, 85.84it/s]
52%|#####1 | : 516/999 [00:06<00:05, 85.98it/s]
52%|#####1 | : 518/999 [00:06<00:05, 84.63it/s]
52%|#####2 | : 522/999 [00:06<00:05, 86.31it/s]
52%|#####2 | : 524/999 [00:06<00:05, 84.95it/s]
53%|#####2 | : 527/999 [00:06<00:05, 85.12it/s]
53%|#####3 | : 531/999 [00:06<00:05, 86.78it/s]
53%|#####3 | : 533/999 [00:06<00:05, 85.39it/s]
54%|#####3 | : 536/999 [00:06<00:05, 85.54it/s]
54%|#####3 | : 539/999 [00:06<00:05, 85.69it/s]
54%|#####4 | : 542/999 [00:06<00:05, 85.81it/s]
55%|#####4 | : 545/999 [00:06<00:05, 85.94it/s]
55%|#####4 | : 548/999 [00:06<00:05, 86.06it/s]
55%|#####5 | : 551/999 [00:06<00:05, 86.16it/s]
55%|#####5 | : 553/999 [00:06<00:05, 84.80it/s]
56%|#####5 | : 557/999 [00:06<00:05, 86.45it/s]
56%|#####5 | : 559/999 [00:06<00:05, 85.06it/s]
56%|#####6 | : 562/999 [00:06<00:05, 85.21it/s]
57%|#####6 | : 565/999 [00:06<00:05, 85.38it/s]
57%|#####6 | : 568/999 [00:06<00:05, 85.51it/s]
57%|#####7 | : 571/999 [00:06<00:04, 85.64it/s]
57%|#####7 | : 574/999 [00:06<00:04, 85.78it/s]
58%|#####7 | : 577/999 [00:06<00:04, 85.90it/s]
58%|#####8 | : 580/999 [00:06<00:04, 86.02it/s]
58%|#####8 | : 583/999 [00:06<00:04, 86.14it/s]
59%|#####8 | : 585/999 [00:06<00:04, 84.77it/s]
59%|#####8 | : 589/999 [00:06<00:04, 86.42it/s]
59%|#####9 | : 592/999 [00:06<00:04, 86.52it/s]
59%|#####9 | : 594/999 [00:06<00:04, 85.13it/s]
60%|#####9 | : 598/999 [00:07<00:04, 86.76it/s]
60%|###### | : 601/999 [00:07<00:04, 86.85it/s]
60%|###### | : 603/999 [00:07<00:04, 85.45it/s]
61%|###### | : 606/999 [00:07<00:04, 85.59it/s]
61%|######1 | : 610/999 [00:07<00:04, 87.21it/s]
61%|######1 | : 612/999 [00:07<00:04, 85.79it/s]
62%|######1 | : 616/999 [00:07<00:04, 87.39it/s]
62%|######1 | : 619/999 [00:07<00:04, 87.46it/s]
62%|######2 | : 621/999 [00:07<00:04, 86.02it/s]
63%|######2 | : 625/999 [00:07<00:04, 87.62it/s]
63%|######2 | : 627/999 [00:07<00:04, 86.20it/s]
63%|######3 | : 630/999 [00:07<00:04, 86.29it/s]
63%|######3 | : 633/999 [00:07<00:04, 86.39it/s]
64%|######3 | : 636/999 [00:07<00:04, 86.50it/s]
64%|######3 | : 639/999 [00:07<00:04, 86.59it/s]
64%|######4 | : 642/999 [00:07<00:04, 86.68it/s]
65%|######4 | : 645/999 [00:07<00:04, 86.77it/s]
65%|######4 | : 648/999 [00:07<00:04, 86.85it/s]
65%|######5 | : 651/999 [00:07<00:04, 86.92it/s]
65%|######5 | : 654/999 [00:07<00:03, 87.01it/s]
66%|######5 | : 657/999 [00:07<00:03, 87.07it/s]
66%|######6 | : 660/999 [00:07<00:03, 87.14it/s]
66%|######6 | : 663/999 [00:07<00:03, 87.21it/s]
67%|######6 | : 666/999 [00:07<00:03, 87.27it/s]
67%|######6 | : 669/999 [00:07<00:03, 87.34it/s]
67%|######7 | : 672/999 [00:07<00:03, 87.41it/s]
67%|######7 | : 674/999 [00:07<00:03, 85.97it/s]
68%|######7 | : 678/999 [00:07<00:03, 87.58it/s]
68%|######8 | : 681/999 [00:07<00:03, 87.64it/s]
68%|######8 | : 683/999 [00:08<00:03, 86.20it/s]
69%|######8 | : 687/999 [00:08<00:03, 87.80it/s]
69%|######8 | : 689/999 [00:08<00:03, 86.37it/s]
69%|######9 | : 692/999 [00:08<00:03, 86.49it/s]
70%|######9 | : 696/999 [00:08<00:03, 88.08it/s]
70%|######9 | : 698/999 [00:08<00:03, 86.64it/s]
70%|####### | : 701/999 [00:08<00:03, 86.74it/s]
71%|####### | : 705/999 [00:08<00:03, 88.31it/s]
71%|####### | : 707/999 [00:08<00:03, 86.83it/s]
71%|#######1 | : 710/999 [00:08<00:03, 86.90it/s]
71%|#######1 | : 714/999 [00:08<00:03, 88.45it/s]
72%|#######1 | : 716/999 [00:08<00:03, 86.96it/s]
72%|#######1 | : 719/999 [00:08<00:03, 87.03it/s]
72%|#######2 | : 722/999 [00:08<00:03, 87.11it/s]
73%|#######2 | : 725/999 [00:08<00:03, 87.15it/s]
73%|#######2 | : 728/999 [00:08<00:03, 87.22it/s]
73%|#######3 | : 731/999 [00:08<00:03, 87.28it/s]
73%|#######3 | : 734/999 [00:08<00:03, 87.32it/s]
74%|#######3 | : 737/999 [00:08<00:02, 87.37it/s]
74%|#######4 | : 740/999 [00:08<00:02, 87.43it/s]
74%|#######4 | : 742/999 [00:08<00:02, 85.98it/s]
75%|#######4 | : 746/999 [00:08<00:02, 87.57it/s]
75%|#######4 | : 749/999 [00:08<00:02, 87.62it/s]
75%|#######5 | : 751/999 [00:08<00:02, 86.17it/s]
76%|#######5 | : 755/999 [00:08<00:02, 87.75it/s]
76%|#######5 | : 758/999 [00:08<00:02, 87.80it/s]
76%|#######6 | : 760/999 [00:08<00:02, 86.34it/s]
76%|#######6 | : 764/999 [00:08<00:02, 87.92it/s]
77%|#######6 | : 767/999 [00:08<00:02, 87.96it/s]
77%|#######6 | : 769/999 [00:08<00:02, 86.52it/s]
77%|#######7 | : 772/999 [00:09<00:02, 86.62it/s]
78%|#######7 | : 776/999 [00:09<00:02, 88.20it/s]
78%|#######7 | : 778/999 [00:09<00:02, 86.75it/s]
78%|#######8 | : 781/999 [00:09<00:02, 86.84it/s]
78%|#######8 | : 784/999 [00:09<00:02, 86.94it/s]
79%|#######8 | : 787/999 [00:09<00:02, 87.02it/s]
79%|#######9 | : 790/999 [00:09<00:02, 87.11it/s]
79%|#######9 | : 793/999 [00:09<00:02, 87.18it/s]
80%|#######9 | : 796/999 [00:09<00:02, 87.25it/s]
80%|#######9 | : 799/999 [00:09<00:02, 87.31it/s]
80%|######## | : 802/999 [00:09<00:02, 87.37it/s]
80%|######## | : 804/999 [00:09<00:02, 85.95it/s]
81%|######## | : 808/999 [00:09<00:02, 87.56it/s]
81%|########1 | : 811/999 [00:09<00:02, 87.61it/s]
81%|########1 | : 813/999 [00:09<00:02, 86.18it/s]
82%|########1 | : 817/999 [00:09<00:02, 87.77it/s]
82%|########2 | : 820/999 [00:09<00:02, 87.81it/s]
82%|########2 | : 822/999 [00:09<00:02, 86.36it/s]
83%|########2 | : 826/999 [00:09<00:01, 87.94it/s]
83%|########2 | : 828/999 [00:09<00:01, 86.50it/s]
83%|########3 | : 831/999 [00:09<00:01, 86.60it/s]
84%|########3 | : 835/999 [00:09<00:01, 88.18it/s]
84%|########3 | : 837/999 [00:09<00:01, 86.72it/s]
84%|########4 | : 840/999 [00:09<00:01, 86.81it/s]
84%|########4 | : 843/999 [00:09<00:01, 86.90it/s]
85%|########4 | : 846/999 [00:09<00:01, 86.99it/s]
85%|########4 | : 849/999 [00:09<00:01, 87.06it/s]
85%|########5 | : 852/999 [00:09<00:01, 87.13it/s]
86%|########5 | : 855/999 [00:09<00:01, 87.20it/s]
86%|########5 | : 857/999 [00:10<00:01, 85.79it/s]
86%|########6 | : 861/999 [00:10<00:01, 87.41it/s]
86%|########6 | : 864/999 [00:10<00:01, 87.47it/s]
87%|########6 | : 866/999 [00:10<00:01, 86.05it/s]
87%|########7 | : 870/999 [00:10<00:01, 87.65it/s]
87%|########7 | : 872/999 [00:10<00:01, 86.21it/s]
88%|########7 | : 875/999 [00:10<00:01, 86.33it/s]
88%|########7 | : 879/999 [00:10<00:01, 87.92it/s]
88%|########8 | : 881/999 [00:10<00:01, 86.44it/s]
88%|########8 | : 884/999 [00:10<00:01, 86.54it/s]
89%|########8 | : 888/999 [00:10<00:01, 88.11it/s]
89%|########9 | : 890/999 [00:10<00:01, 86.62it/s]
89%|########9 | : 893/999 [00:10<00:01, 86.70it/s]
90%|########9 | : 897/999 [00:10<00:01, 88.27it/s]
90%|########9 | : 899/999 [00:10<00:01, 86.79it/s]
90%|######### | : 902/999 [00:10<00:01, 86.87it/s]
91%|######### | : 905/999 [00:10<00:01, 86.95it/s]
91%|######### | : 908/999 [00:10<00:01, 87.00it/s]
91%|#########1| : 911/999 [00:10<00:01, 87.07it/s]
91%|#########1| : 914/999 [00:10<00:00, 87.14it/s]
92%|#########1| : 917/999 [00:10<00:00, 87.19it/s]
92%|#########2| : 920/999 [00:10<00:00, 87.27it/s]
92%|#########2| : 923/999 [00:10<00:00, 87.34it/s]
93%|#########2| : 926/999 [00:10<00:00, 87.38it/s]
93%|#########2| : 929/999 [00:10<00:00, 87.45it/s]
93%|#########3| : 932/999 [00:10<00:00, 87.51it/s]
93%|#########3| : 934/999 [00:10<00:00, 86.09it/s]
94%|#########3| : 937/999 [00:10<00:00, 86.22it/s]
94%|#########4| : 940/999 [00:10<00:00, 86.35it/s]
94%|#########4| : 943/999 [00:10<00:00, 86.45it/s]
95%|#########4| : 946/999 [00:11<00:00, 86.55it/s]
95%|#########4| : 949/999 [00:11<00:00, 86.65it/s]
95%|#########5| : 952/999 [00:11<00:00, 86.74it/s]
96%|#########5| : 955/999 [00:11<00:00, 86.83it/s]
96%|#########5| : 958/999 [00:11<00:00, 86.92it/s]
96%|#########6| : 961/999 [00:11<00:00, 86.99it/s]
96%|#########6| : 964/999 [00:11<00:00, 87.07it/s]
97%|#########6| : 967/999 [00:11<00:00, 87.16it/s]
97%|#########7| : 970/999 [00:11<00:00, 87.24it/s]
97%|#########7| : 973/999 [00:11<00:00, 87.31it/s]
98%|#########7| : 976/999 [00:11<00:00, 87.38it/s]
98%|#########7| : 978/999 [00:11<00:00, 85.96it/s]
98%|#########8| : 982/999 [00:11<00:00, 87.57it/s]
99%|#########8| : 985/999 [00:11<00:00, 87.63it/s]
99%|#########8| : 987/999 [00:11<00:00, 86.20it/s]
99%|#########9| : 991/999 [00:11<00:00, 87.80it/s]
99%|#########9| : 993/999 [00:11<00:00, 86.36it/s]
100%|#########9| : 996/999 [00:11<00:00, 86.47it/s]
100%|##########| : 999/999 [00:11<00:00, 87.86it/s]
100%|##########| : 999/999 [00:11<00:00, 85.94it/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()
```

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 26.491 seconds)

**Estimated memory usage:** 128 MB