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%| | : 1/999 [00:00<00:34, 29.07it/s]
0%| | : 3/999 [00:00<00:22, 44.23it/s]
1%| | : 6/999 [00:00<00:16, 59.63it/s]
1%|1 | : 10/999 [00:00<00:13, 75.36it/s]
1%|1 | : 13/999 [00:00<00:12, 78.29it/s]
2%|1 | : 16/999 [00:00<00:12, 80.22it/s]
2%|1 | : 19/999 [00:00<00:12, 81.60it/s]
2%|2 | : 22/999 [00:00<00:11, 82.65it/s]
3%|2 | : 25/999 [00:00<00:11, 83.48it/s]
3%|2 | : 29/999 [00:00<00:11, 87.79it/s]
3%|3 | : 32/999 [00:00<00:11, 87.86it/s]
4%|3 | : 35/999 [00:00<00:10, 87.96it/s]
4%|3 | : 38/999 [00:00<00:10, 88.04it/s]
4%|4 | : 41/999 [00:00<00:10, 88.12it/s]
5%|4 | : 45/999 [00:00<00:10, 90.93it/s]
5%|4 | : 48/999 [00:00<00:10, 90.74it/s]
5%|5 | : 51/999 [00:00<00:10, 90.58it/s]
5%|5 | : 54/999 [00:00<00:10, 90.41it/s]
6%|5 | : 57/999 [00:00<00:10, 90.28it/s]
6%|6 | : 60/999 [00:00<00:10, 90.16it/s]
6%|6 | : 64/999 [00:00<00:10, 92.30it/s]
7%|6 | : 67/999 [00:00<00:10, 92.04it/s]
7%|7 | : 70/999 [00:00<00:10, 91.81it/s]
7%|7 | : 73/999 [00:00<00:10, 91.59it/s]
8%|7 | : 76/999 [00:00<00:10, 91.37it/s]
8%|8 | : 80/999 [00:00<00:09, 93.19it/s]
8%|8 | : 83/999 [00:00<00:09, 92.88it/s]
9%|8 | : 86/999 [00:00<00:09, 92.59it/s]
9%|8 | : 89/999 [00:00<00:09, 92.24it/s]
9%|9 | : 92/999 [00:01<00:09, 92.01it/s]
10%|9 | : 95/999 [00:01<00:09, 91.79it/s]
10%|9 | : 98/999 [00:01<00:09, 91.59it/s]
10%|# | : 101/999 [00:01<00:09, 91.40it/s]
10%|# | : 104/999 [00:01<00:09, 91.23it/s]
11%|# | : 108/999 [00:01<00:09, 92.85it/s]
11%|#1 | : 111/999 [00:01<00:09, 92.60it/s]
11%|#1 | : 114/999 [00:01<00:09, 92.37it/s]
12%|#1 | : 117/999 [00:01<00:09, 92.16it/s]
12%|#2 | : 120/999 [00:01<00:09, 91.97it/s]
12%|#2 | : 124/999 [00:01<00:09, 93.48it/s]
13%|#2 | : 127/999 [00:01<00:09, 93.21it/s]
13%|#3 | : 130/999 [00:01<00:09, 92.96it/s]
13%|#3 | : 133/999 [00:01<00:09, 92.72it/s]
14%|#3 | : 137/999 [00:01<00:09, 94.13it/s]
14%|#4 | : 140/999 [00:01<00:09, 93.83it/s]
14%|#4 | : 143/999 [00:01<00:09, 93.55it/s]
15%|#4 | : 146/999 [00:01<00:09, 93.28it/s]
15%|#4 | : 149/999 [00:01<00:09, 93.03it/s]
15%|#5 | : 153/999 [00:01<00:08, 94.41it/s]
16%|#5 | : 156/999 [00:01<00:08, 94.11it/s]
16%|#5 | : 159/999 [00:01<00:08, 93.82it/s]
16%|#6 | : 162/999 [00:01<00:08, 93.55it/s]
17%|#6 | : 165/999 [00:01<00:08, 93.29it/s]
17%|#6 | : 169/999 [00:01<00:08, 94.63it/s]
17%|#7 | : 172/999 [00:01<00:08, 94.32it/s]
18%|#7 | : 175/999 [00:01<00:08, 94.02it/s]
18%|#7 | : 178/999 [00:01<00:08, 93.74it/s]
18%|#8 | : 181/999 [00:01<00:08, 93.46it/s]
19%|#8 | : 185/999 [00:01<00:08, 94.75it/s]
19%|#8 | : 188/999 [00:02<00:08, 94.44it/s]
19%|#9 | : 191/999 [00:02<00:08, 94.13it/s]
19%|#9 | : 194/999 [00:02<00:08, 93.85it/s]
20%|#9 | : 197/999 [00:02<00:08, 93.58it/s]
20%|## | : 201/999 [00:02<00:08, 94.88it/s]
20%|## | : 204/999 [00:02<00:08, 94.56it/s]
21%|## | : 207/999 [00:02<00:08, 94.26it/s]
21%|##1 | : 210/999 [00:02<00:08, 93.98it/s]
21%|##1 | : 213/999 [00:02<00:08, 93.71it/s]
22%|##1 | : 216/999 [00:02<00:08, 93.46it/s]
22%|##2 | : 220/999 [00:02<00:08, 94.74it/s]
22%|##2 | : 223/999 [00:02<00:08, 94.41it/s]
23%|##2 | : 226/999 [00:02<00:08, 94.12it/s]
23%|##2 | : 229/999 [00:02<00:08, 93.85it/s]
23%|##3 | : 232/999 [00:02<00:08, 93.59it/s]
24%|##3 | : 236/999 [00:02<00:08, 94.86it/s]
24%|##3 | : 239/999 [00:02<00:08, 94.53it/s]
24%|##4 | : 242/999 [00:02<00:08, 94.24it/s]
25%|##4 | : 245/999 [00:02<00:08, 93.96it/s]
25%|##4 | : 248/999 [00:02<00:08, 93.68it/s]
25%|##5 | : 251/999 [00:02<00:08, 93.41it/s]
26%|##5 | : 255/999 [00:02<00:07, 94.67it/s]
26%|##5 | : 258/999 [00:02<00:07, 94.36it/s]
26%|##6 | : 261/999 [00:02<00:07, 94.07it/s]
26%|##6 | : 264/999 [00:02<00:07, 93.79it/s]
27%|##6 | : 267/999 [00:02<00:07, 93.54it/s]
27%|##7 | : 270/999 [00:02<00:07, 93.30it/s]
27%|##7 | : 274/999 [00:02<00:07, 94.58it/s]
28%|##7 | : 277/999 [00:02<00:07, 94.28it/s]
28%|##8 | : 280/999 [00:03<00:07, 94.00it/s]
28%|##8 | : 283/999 [00:03<00:07, 93.72it/s]
29%|##8 | : 286/999 [00:03<00:07, 93.47it/s]
29%|##8 | : 289/999 [00:03<00:07, 93.23it/s]
29%|##9 | : 293/999 [00:03<00:07, 94.51it/s]
30%|##9 | : 296/999 [00:03<00:07, 94.22it/s]
30%|##9 | : 299/999 [00:03<00:07, 93.94it/s]
30%|### | : 302/999 [00:03<00:07, 93.67it/s]
31%|### | : 305/999 [00:03<00:07, 93.42it/s]
31%|### | : 308/999 [00:03<00:07, 93.19it/s]
31%|###1 | : 311/999 [00:03<00:07, 92.95it/s]
31%|###1 | : 314/999 [00:03<00:07, 92.72it/s]
32%|###1 | : 318/999 [00:03<00:07, 94.00it/s]
32%|###2 | : 321/999 [00:03<00:07, 93.73it/s]
32%|###2 | : 324/999 [00:03<00:07, 93.47it/s]
33%|###2 | : 327/999 [00:03<00:07, 93.22it/s]
33%|###3 | : 330/999 [00:03<00:07, 92.99it/s]
33%|###3 | : 334/999 [00:03<00:07, 94.27it/s]
34%|###3 | : 337/999 [00:03<00:07, 93.99it/s]
34%|###4 | : 340/999 [00:03<00:07, 93.72it/s]
34%|###4 | : 343/999 [00:03<00:07, 93.47it/s]
35%|###4 | : 346/999 [00:03<00:07, 93.24it/s]
35%|###4 | : 349/999 [00:03<00:06, 93.01it/s]
35%|###5 | : 352/999 [00:03<00:06, 92.80it/s]
36%|###5 | : 356/999 [00:03<00:06, 94.08it/s]
36%|###5 | : 359/999 [00:03<00:06, 93.81it/s]
36%|###6 | : 362/999 [00:03<00:06, 93.56it/s]
37%|###6 | : 365/999 [00:03<00:06, 93.31it/s]
37%|###6 | : 368/999 [00:03<00:06, 93.07it/s]
37%|###7 | : 371/999 [00:03<00:06, 92.86it/s]
37%|###7 | : 374/999 [00:04<00:06, 92.66it/s]
38%|###7 | : 377/999 [00:04<00:06, 92.46it/s]
38%|###8 | : 380/999 [00:04<00:06, 92.26it/s]
38%|###8 | : 384/999 [00:04<00:06, 93.56it/s]
39%|###8 | : 387/999 [00:04<00:06, 93.32it/s]
39%|###9 | : 390/999 [00:04<00:06, 93.09it/s]
39%|###9 | : 393/999 [00:04<00:06, 92.86it/s]
40%|###9 | : 396/999 [00:04<00:06, 92.65it/s]
40%|###9 | : 399/999 [00:04<00:06, 92.44it/s]
40%|#### | : 403/999 [00:04<00:06, 93.73it/s]
41%|#### | : 406/999 [00:04<00:06, 93.47it/s]
41%|#### | : 408/999 [00:04<00:06, 91.75it/s]
41%|####1 | : 411/999 [00:04<00:06, 91.59it/s]
41%|####1 | : 414/999 [00:04<00:06, 91.43it/s]
42%|####1 | : 417/999 [00:04<00:06, 91.30it/s]
42%|####2 | : 420/999 [00:04<00:06, 91.17it/s]
42%|####2 | : 423/999 [00:04<00:06, 91.04it/s]
43%|####2 | : 426/999 [00:04<00:06, 90.93it/s]
43%|####2 | : 428/999 [00:04<00:06, 89.34it/s]
43%|####3 | : 432/999 [00:04<00:06, 90.77it/s]
44%|####3 | : 435/999 [00:04<00:06, 90.66it/s]
44%|####3 | : 438/999 [00:04<00:06, 90.55it/s]
44%|####4 | : 441/999 [00:04<00:06, 90.46it/s]
44%|####4 | : 444/999 [00:04<00:06, 90.37it/s]
45%|####4 | : 448/999 [00:04<00:06, 91.77it/s]
45%|####5 | : 451/999 [00:04<00:05, 91.62it/s]
45%|####5 | : 454/999 [00:04<00:05, 91.46it/s]
46%|####5 | : 457/999 [00:04<00:05, 91.33it/s]
46%|####6 | : 461/999 [00:04<00:05, 92.68it/s]
46%|####6 | : 464/999 [00:05<00:05, 92.49it/s]
47%|####6 | : 467/999 [00:05<00:05, 92.29it/s]
47%|####7 | : 470/999 [00:05<00:05, 92.11it/s]
47%|####7 | : 474/999 [00:05<00:05, 93.42it/s]
48%|####7 | : 477/999 [00:05<00:05, 93.18it/s]
48%|####8 | : 480/999 [00:05<00:05, 92.96it/s]
48%|####8 | : 483/999 [00:05<00:05, 92.75it/s]
49%|####8 | : 486/999 [00:05<00:05, 92.55it/s]
49%|####9 | : 490/999 [00:05<00:05, 93.83it/s]
49%|####9 | : 493/999 [00:05<00:05, 93.58it/s]
50%|####9 | : 496/999 [00:05<00:05, 93.33it/s]
50%|##### | : 500/999 [00:05<00:05, 94.58it/s]
50%|##### | : 503/999 [00:05<00:05, 94.29it/s]
51%|##### | : 506/999 [00:05<00:05, 94.00it/s]
51%|##### | : 509/999 [00:05<00:05, 93.74it/s]
51%|#####1 | : 513/999 [00:05<00:05, 94.96it/s]
52%|#####1 | : 516/999 [00:05<00:05, 94.64it/s]
52%|#####1 | : 519/999 [00:05<00:05, 94.35it/s]
52%|#####2 | : 522/999 [00:05<00:05, 94.07it/s]
53%|#####2 | : 526/999 [00:05<00:04, 95.28it/s]
53%|#####2 | : 529/999 [00:05<00:04, 94.94it/s]
53%|#####3 | : 532/999 [00:05<00:04, 94.63it/s]
54%|#####3 | : 535/999 [00:05<00:04, 94.34it/s]
54%|#####3 | : 539/999 [00:05<00:04, 95.54it/s]
54%|#####4 | : 542/999 [00:05<00:04, 95.20it/s]
55%|#####4 | : 545/999 [00:05<00:04, 94.88it/s]
55%|#####4 | : 549/999 [00:05<00:04, 96.05it/s]
55%|#####5 | : 552/999 [00:05<00:04, 95.69it/s]
56%|#####5 | : 555/999 [00:05<00:04, 95.33it/s]
56%|#####5 | : 558/999 [00:05<00:04, 95.00it/s]
56%|#####6 | : 562/999 [00:06<00:04, 96.17it/s]
57%|#####6 | : 565/999 [00:06<00:04, 95.80it/s]
57%|#####6 | : 568/999 [00:06<00:04, 95.45it/s]
57%|#####7 | : 572/999 [00:06<00:04, 96.60it/s]
58%|#####7 | : 575/999 [00:06<00:04, 96.19it/s]
58%|#####7 | : 578/999 [00:06<00:04, 95.81it/s]
58%|#####8 | : 581/999 [00:06<00:04, 95.45it/s]
58%|#####8 | : 584/999 [00:06<00:04, 95.11it/s]
59%|#####8 | : 587/999 [00:06<00:04, 94.76it/s]
59%|#####9 | : 591/999 [00:06<00:04, 95.93it/s]
59%|#####9 | : 594/999 [00:06<00:04, 95.56it/s]
60%|#####9 | : 597/999 [00:06<00:04, 95.22it/s]
60%|###### | : 600/999 [00:06<00:04, 94.90it/s]
60%|###### | : 603/999 [00:06<00:04, 94.60it/s]
61%|###### | : 606/999 [00:06<00:04, 94.30it/s]
61%|###### | : 609/999 [00:06<00:04, 94.03it/s]
61%|######1 | : 612/999 [00:06<00:04, 93.76it/s]
62%|######1 | : 615/999 [00:06<00:04, 93.51it/s]
62%|######1 | : 618/999 [00:06<00:04, 93.28it/s]
62%|######2 | : 621/999 [00:06<00:04, 93.04it/s]
62%|######2 | : 624/999 [00:06<00:04, 92.82it/s]
63%|######2 | : 628/999 [00:06<00:03, 94.08it/s]
63%|######3 | : 631/999 [00:06<00:03, 93.80it/s]
63%|######3 | : 634/999 [00:06<00:03, 93.55it/s]
64%|######3 | : 637/999 [00:06<00:03, 93.31it/s]
64%|######4 | : 640/999 [00:06<00:03, 93.08it/s]
64%|######4 | : 644/999 [00:06<00:03, 94.34it/s]
65%|######4 | : 647/999 [00:06<00:03, 94.06it/s]
65%|######5 | : 650/999 [00:06<00:03, 93.80it/s]
65%|######5 | : 653/999 [00:07<00:03, 93.55it/s]
66%|######5 | : 657/999 [00:07<00:03, 94.79it/s]
66%|######6 | : 660/999 [00:07<00:03, 94.48it/s]
66%|######6 | : 663/999 [00:07<00:03, 94.19it/s]
67%|######6 | : 666/999 [00:07<00:03, 93.91it/s]
67%|######6 | : 669/999 [00:07<00:03, 93.65it/s]
67%|######7 | : 673/999 [00:07<00:03, 94.88it/s]
68%|######7 | : 676/999 [00:07<00:03, 94.56it/s]
68%|######7 | : 679/999 [00:07<00:03, 94.27it/s]
68%|######8 | : 682/999 [00:07<00:03, 94.00it/s]
69%|######8 | : 686/999 [00:07<00:03, 95.21it/s]
69%|######8 | : 689/999 [00:07<00:03, 94.88it/s]
69%|######9 | : 692/999 [00:07<00:03, 94.57it/s]
70%|######9 | : 695/999 [00:07<00:03, 94.29it/s]
70%|######9 | : 699/999 [00:07<00:03, 95.48it/s]
70%|####### | : 702/999 [00:07<00:03, 95.13it/s]
71%|####### | : 705/999 [00:07<00:03, 94.79it/s]
71%|####### | : 709/999 [00:07<00:03, 95.97it/s]
71%|#######1 | : 712/999 [00:07<00:03, 95.61it/s]
72%|#######1 | : 715/999 [00:07<00:02, 95.27it/s]
72%|#######1 | : 719/999 [00:07<00:02, 96.42it/s]
72%|#######2 | : 722/999 [00:07<00:02, 96.03it/s]
73%|#######2 | : 725/999 [00:07<00:02, 95.67it/s]
73%|#######2 | : 728/999 [00:07<00:02, 95.32it/s]
73%|#######3 | : 732/999 [00:07<00:02, 96.47it/s]
74%|#######3 | : 735/999 [00:07<00:02, 96.08it/s]
74%|#######3 | : 738/999 [00:07<00:02, 95.71it/s]
74%|#######4 | : 741/999 [00:07<00:02, 95.36it/s]
75%|#######4 | : 745/999 [00:07<00:02, 96.51it/s]
75%|#######4 | : 748/999 [00:07<00:02, 96.13it/s]
75%|#######5 | : 751/999 [00:08<00:02, 95.76it/s]
75%|#######5 | : 753/999 [00:08<00:02, 93.93it/s]
76%|#######5 | : 757/999 [00:08<00:02, 95.14it/s]
76%|#######6 | : 760/999 [00:08<00:02, 94.82it/s]
76%|#######6 | : 763/999 [00:08<00:02, 94.51it/s]
77%|#######6 | : 766/999 [00:08<00:02, 94.22it/s]
77%|#######6 | : 769/999 [00:08<00:02, 93.94it/s]
77%|#######7 | : 772/999 [00:08<00:02, 93.68it/s]
78%|#######7 | : 775/999 [00:08<00:02, 93.42it/s]
78%|#######7 | : 778/999 [00:08<00:02, 93.19it/s]
78%|#######8 | : 781/999 [00:08<00:02, 92.96it/s]
78%|#######8 | : 784/999 [00:08<00:02, 92.74it/s]
79%|#######8 | : 787/999 [00:08<00:02, 92.53it/s]
79%|#######8 | : 789/999 [00:08<00:02, 90.87it/s]
79%|#######9 | : 792/999 [00:08<00:02, 90.76it/s]
80%|#######9 | : 795/999 [00:08<00:02, 90.65it/s]
80%|#######9 | : 797/999 [00:08<00:02, 89.06it/s]
80%|######## | : 800/999 [00:08<00:02, 89.02it/s]
80%|######## | : 803/999 [00:08<00:02, 88.99it/s]
81%|######## | : 806/999 [00:08<00:02, 88.98it/s]
81%|######## | : 809/999 [00:08<00:02, 88.97it/s]
81%|########1 | : 812/999 [00:08<00:02, 88.96it/s]
82%|########1 | : 815/999 [00:08<00:02, 88.95it/s]
82%|########1 | : 819/999 [00:08<00:01, 90.42it/s]
82%|########2 | : 822/999 [00:08<00:01, 90.34it/s]
83%|########2 | : 825/999 [00:08<00:01, 90.26it/s]
83%|########2 | : 828/999 [00:08<00:01, 90.18it/s]
83%|########3 | : 831/999 [00:08<00:01, 90.11it/s]
83%|########3 | : 834/999 [00:08<00:01, 90.04it/s]
84%|########3 | : 838/999 [00:09<00:01, 91.46it/s]
84%|########4 | : 841/999 [00:09<00:01, 91.33it/s]
84%|########4 | : 844/999 [00:09<00:01, 91.20it/s]
85%|########4 | : 847/999 [00:09<00:01, 91.07it/s]
85%|########5 | : 850/999 [00:09<00:01, 90.95it/s]
85%|########5 | : 853/999 [00:09<00:01, 90.82it/s]
86%|########5 | : 857/999 [00:09<00:01, 92.19it/s]
86%|########6 | : 860/999 [00:09<00:01, 92.00it/s]
86%|########6 | : 863/999 [00:09<00:01, 91.83it/s]
87%|########6 | : 866/999 [00:09<00:01, 91.67it/s]
87%|########6 | : 869/999 [00:09<00:01, 91.52it/s]
87%|########7 | : 873/999 [00:09<00:01, 92.85it/s]
88%|########7 | : 876/999 [00:09<00:01, 92.65it/s]
88%|########7 | : 879/999 [00:09<00:01, 92.45it/s]
88%|########8 | : 882/999 [00:09<00:01, 92.26it/s]
89%|########8 | : 885/999 [00:09<00:01, 92.08it/s]
89%|########8 | : 889/999 [00:09<00:01, 93.39it/s]
89%|########9 | : 892/999 [00:09<00:01, 93.16it/s]
90%|########9 | : 895/999 [00:09<00:01, 92.94it/s]
90%|########9 | : 898/999 [00:09<00:01, 92.73it/s]
90%|######### | : 901/999 [00:09<00:01, 92.53it/s]
91%|######### | : 905/999 [00:09<00:01, 93.82it/s]
91%|######### | : 908/999 [00:09<00:00, 93.55it/s]
91%|#########1| : 911/999 [00:09<00:00, 93.31it/s]
91%|#########1| : 914/999 [00:09<00:00, 93.09it/s]
92%|#########1| : 917/999 [00:09<00:00, 92.87it/s]
92%|#########2| : 921/999 [00:09<00:00, 94.14it/s]
92%|#########2| : 924/999 [00:09<00:00, 93.86it/s]
93%|#########2| : 927/999 [00:09<00:00, 93.61it/s]
93%|#########3| : 930/999 [00:09<00:00, 93.36it/s]
93%|#########3| : 933/999 [00:10<00:00, 93.13it/s]
94%|#########3| : 936/999 [00:10<00:00, 92.91it/s]
94%|#########4| : 940/999 [00:10<00:00, 94.17it/s]
94%|#########4| : 943/999 [00:10<00:00, 93.90it/s]
95%|#########4| : 946/999 [00:10<00:00, 93.65it/s]
95%|#########4| : 949/999 [00:10<00:00, 93.40it/s]
95%|#########5| : 951/999 [00:10<00:00, 91.68it/s]
95%|#########5| : 954/999 [00:10<00:00, 91.52it/s]
96%|#########5| : 957/999 [00:10<00:00, 91.38it/s]
96%|#########6| : 960/999 [00:10<00:00, 91.24it/s]
96%|#########6| : 963/999 [00:10<00:00, 91.12it/s]
97%|#########6| : 966/999 [00:10<00:00, 90.98it/s]
97%|#########6| : 969/999 [00:10<00:00, 90.87it/s]
97%|#########7| : 973/999 [00:10<00:00, 92.24it/s]
98%|#########7| : 976/999 [00:10<00:00, 92.05it/s]
98%|#########7| : 979/999 [00:10<00:00, 91.87it/s]
98%|#########8| : 982/999 [00:10<00:00, 91.70it/s]
99%|#########8| : 985/999 [00:10<00:00, 91.56it/s]
99%|#########8| : 988/999 [00:10<00:00, 91.41it/s]
99%|#########9| : 992/999 [00:10<00:00, 92.76it/s]
100%|#########9| : 995/999 [00:10<00:00, 92.56it/s]
100%|#########9| : 998/999 [00:10<00:00, 92.37it/s]
100%|##########| : 999/999 [00:10<00:00, 93.07it/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 24.756 seconds)

**Estimated memory usage:** 129 MB