Note

Click here to download the full example code

# Mass-univariate twoway repeated measures ANOVA on single trial power¶

This script shows how to conduct a mass-univariate repeated measures ANOVA. As the model to be fitted assumes two fully crossed factors, we will study the interplay between perceptual modality (auditory VS visual) and the location of stimulus presentation (left VS right). Here we use single trials as replications (subjects) while iterating over time slices plus frequency bands for to fit our mass-univariate model. For the sake of simplicity we will confine this analysis to one single channel of which we know that it exposes a strong induced response. We will then visualize each effect by creating a corresponding mass-univariate effect image. We conclude with accounting for multiple comparisons by performing a permutation clustering test using the ANOVA as clustering function. The results final will be compared to multiple comparisons using False Discovery Rate correction.

```
# Authors: Denis Engemann <denis.engemann@gmail.com>
# Eric Larson <larson.eric.d@gmail.com>
# Alexandre Gramfort <alexandre.gramfort@inria.fr>
#
# License: BSD (3-clause)
import numpy as np
import matplotlib.pyplot as plt
import mne
from mne.time_frequency import tfr_morlet
from mne.stats import f_threshold_mway_rm, f_mway_rm, fdr_correction
from mne.datasets import sample
print(__doc__)
```

## Set parameters¶

```
data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_raw.fif'
event_fname = data_path + '/MEG/sample/sample_audvis_raw-eve.fif'
tmin, tmax = -0.2, 0.5
# Setup for reading the raw data
raw = mne.io.read_raw_fif(raw_fname)
events = mne.read_events(event_fname)
include = []
raw.info['bads'] += ['MEG 2443'] # bads
# picks MEG gradiometers
picks = mne.pick_types(raw.info, meg='grad', eeg=False, eog=True,
stim=False, include=include, exclude='bads')
ch_name = 'MEG 1332'
# Load conditions
reject = dict(grad=4000e-13, eog=150e-6)
event_id = dict(aud_l=1, aud_r=2, vis_l=3, vis_r=4)
epochs = mne.Epochs(raw, events, event_id, tmin, tmax,
picks=picks, baseline=(None, 0), preload=True,
reject=reject)
epochs.pick_channels([ch_name]) # restrict example to one channel
```

Out:

```
Opening raw data file /home/circleci/mne_data/MNE-sample-data/MEG/sample/sample_audvis_raw.fif...
Read a total of 3 projection items:
PCA-v1 (1 x 102) idle
PCA-v2 (1 x 102) idle
PCA-v3 (1 x 102) idle
Range : 25800 ... 192599 = 42.956 ... 320.670 secs
Ready.
Current compensation grade : 0
Not setting metadata
Not setting metadata
289 matching events found
Applying baseline correction (mode: mean)
3 projection items activated
Loading data for 289 events and 421 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 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']
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']
53 bad epochs dropped
```

We have to make sure all conditions have the same counts, as the ANOVA expects a fully balanced data matrix and does not forgive imbalances that generously (risk of type-I error).

```
epochs.equalize_event_counts(event_id)
# Factor to down-sample the temporal dimension of the TFR computed by
# tfr_morlet.
decim = 2
freqs = np.arange(7, 30, 3) # define frequencies of interest
n_cycles = freqs / freqs[0]
zero_mean = False # don't correct morlet wavelet to be of mean zero
# To have a true wavelet zero_mean should be True but here for illustration
# purposes it helps to spot the evoked response.
```

Out:

```
Dropped 12 epochs: 50, 92, 128, 147, 152, 154, 155, 186, 196, 198, 206, 207
```

## Create TFR representations for all conditions¶

```
epochs_power = list()
for condition in [epochs[k] for k in event_id]:
this_tfr = tfr_morlet(condition, freqs, n_cycles=n_cycles,
decim=decim, average=False, zero_mean=zero_mean,
return_itc=False)
this_tfr.apply_baseline(mode='ratio', baseline=(None, 0))
this_power = this_tfr.data[:, 0, :, :] # we only have one channel.
epochs_power.append(this_power)
```

Out:

```
Not setting metadata
Applying baseline correction (mode: ratio)
Not setting metadata
Applying baseline correction (mode: ratio)
Not setting metadata
Applying baseline correction (mode: ratio)
Not setting metadata
Applying baseline correction (mode: ratio)
```

## Setup repeated measures ANOVA¶

We will tell the ANOVA how to interpret the data matrix in terms of factors. This is done via the factor levels argument which is a list of the number factor levels for each factor.

```
n_conditions = len(epochs.event_id)
n_replications = epochs.events.shape[0] // n_conditions
factor_levels = [2, 2] # number of levels in each factor
effects = 'A*B' # this is the default signature for computing all effects
# Other possible options are 'A' or 'B' for the corresponding main effects
# or 'A:B' for the interaction effect only (this notation is borrowed from the
# R formula language)
n_freqs = len(freqs)
times = 1e3 * epochs.times[::decim]
n_times = len(times)
```

Now we’ll assemble the data matrix and swap axes so the trial replications are the first dimension and the conditions are the second dimension.

```
data = np.swapaxes(np.asarray(epochs_power), 1, 0)
# reshape last two dimensions in one mass-univariate observation-vector
data = data.reshape(n_replications, n_conditions, n_freqs * n_times)
# so we have replications * conditions * observations:
print(data.shape)
```

Out:

```
(56, 4, 1688)
```

While the iteration scheme used above for assembling the data matrix makes sure the first two dimensions are organized as expected (with A = modality and B = location):

trial |
A1B1 |
A1B2 |
A2B1 |
B2B2 |
---|---|---|---|---|

1 |
1.34 |
2.53 |
0.97 |
1.74 |

… |
… |
… |
… |
… |

56 |
2.45 |
7.90 |
3.09 |
4.76 |

Now we’re ready to run our repeated measures ANOVA.

Note. As we treat trials as subjects, the test only accounts for time locked responses despite the ‘induced’ approach. For analysis for induced power at the group level averaged TRFs are required.

```
fvals, pvals = f_mway_rm(data, factor_levels, effects=effects)
effect_labels = ['modality', 'location', 'modality by location']
# let's visualize our effects by computing f-images
for effect, sig, effect_label in zip(fvals, pvals, effect_labels):
plt.figure()
# show naive F-values in gray
plt.imshow(effect.reshape(8, 211), cmap=plt.cm.gray, extent=[times[0],
times[-1], freqs[0], freqs[-1]], aspect='auto',
origin='lower')
# create mask for significant Time-frequency locations
effect = np.ma.masked_array(effect, [sig > .05])
plt.imshow(effect.reshape(8, 211), cmap='RdBu_r', extent=[times[0],
times[-1], freqs[0], freqs[-1]], aspect='auto',
origin='lower')
plt.colorbar()
plt.xlabel('Time (ms)')
plt.ylabel('Frequency (Hz)')
plt.title(r"Time-locked response for '%s' (%s)" % (effect_label, ch_name))
plt.show()
```

## Account for multiple comparisons using FDR versus permutation clustering test¶

First we need to slightly modify the ANOVA function to be suitable for the clustering procedure. Also want to set some defaults. Let’s first override effects to confine the analysis to the interaction

```
effects = 'A:B'
```

A stat_fun must deal with a variable number of input arguments. Inside the clustering function each condition will be passed as flattened array, necessitated by the clustering procedure. The ANOVA however expects an input array of dimensions: subjects X conditions X observations (optional). The following function catches the list input and swaps the first and the second dimension and finally calls the ANOVA function.

```
def stat_fun(*args):
return f_mway_rm(np.swapaxes(args, 1, 0), factor_levels=factor_levels,
effects=effects, return_pvals=False)[0]
# The ANOVA returns a tuple f-values and p-values, we will pick the former.
pthresh = 0.001 # set threshold rather high to save some time
f_thresh = f_threshold_mway_rm(n_replications, factor_levels, effects,
pthresh)
tail = 1 # f-test, so tail > 0
n_permutations = 256 # Save some time (the test won't be too sensitive ...)
T_obs, clusters, cluster_p_values, h0 = mne.stats.permutation_cluster_test(
epochs_power, stat_fun=stat_fun, threshold=f_thresh, tail=tail, n_jobs=1,
n_permutations=n_permutations, buffer_size=None, out_type='mask')
```

Out:

```
stat_fun(H1): min=0.000001 max=14.419588
Running initial clustering
Found 1 clusters
Permuting 255 times...
0%| | : 0/255 [00:00<?, ?it/s]
0%| | : 1/255 [00:00<00:17, 14.68it/s]
1%|1 | : 3/255 [00:00<00:16, 15.25it/s]
2%|1 | : 4/255 [00:00<00:16, 15.62it/s]
2%|1 | : 5/255 [00:00<00:15, 16.00it/s]
2%|2 | : 6/255 [00:00<00:15, 16.37it/s]
3%|3 | : 8/255 [00:00<00:14, 16.99it/s]
4%|3 | : 9/255 [00:00<00:14, 17.35it/s]
4%|3 | : 10/255 [00:00<00:13, 17.72it/s]
4%|4 | : 11/255 [00:00<00:13, 18.08it/s]
5%|4 | : 12/255 [00:00<00:13, 18.43it/s]
5%|5 | : 13/255 [00:00<00:12, 18.78it/s]
5%|5 | : 14/255 [00:00<00:12, 19.13it/s]
6%|6 | : 16/255 [00:00<00:12, 19.80it/s]
7%|6 | : 17/255 [00:00<00:11, 20.12it/s]
7%|7 | : 18/255 [00:00<00:11, 20.45it/s]
7%|7 | : 19/255 [00:00<00:11, 20.76it/s]
8%|7 | : 20/255 [00:00<00:11, 21.07it/s]
9%|8 | : 22/255 [00:00<00:10, 21.77it/s]
9%|9 | : 23/255 [00:00<00:10, 22.06it/s]
9%|9 | : 24/255 [00:00<00:10, 22.34it/s]
10%|9 | : 25/255 [00:00<00:10, 22.61it/s]
10%|# | : 26/255 [00:00<00:10, 22.88it/s]
11%|# | : 28/255 [00:00<00:09, 23.59it/s]
11%|#1 | : 29/255 [00:00<00:09, 23.83it/s]
12%|#1 | : 30/255 [00:00<00:09, 24.06it/s]
12%|#2 | : 31/255 [00:00<00:09, 24.28it/s]
13%|#2 | : 32/255 [00:00<00:09, 24.50it/s]
13%|#3 | : 34/255 [00:00<00:08, 25.23it/s]
14%|#3 | : 35/255 [00:01<00:08, 25.41it/s]
14%|#4 | : 36/255 [00:01<00:08, 25.59it/s]
15%|#4 | : 37/255 [00:01<00:08, 25.76it/s]
15%|#4 | : 38/255 [00:01<00:08, 25.92it/s]
15%|#5 | : 39/255 [00:01<00:08, 26.07it/s]
16%|#5 | : 40/255 [00:01<00:08, 26.22it/s]
16%|#6 | : 41/255 [00:01<00:08, 26.37it/s]
17%|#6 | : 43/255 [00:01<00:07, 27.12it/s]
17%|#7 | : 44/255 [00:01<00:07, 27.22it/s]
18%|#7 | : 45/255 [00:01<00:07, 27.32it/s]
18%|#8 | : 46/255 [00:01<00:07, 27.42it/s]
18%|#8 | : 47/255 [00:01<00:07, 27.52it/s]
19%|#8 | : 48/255 [00:01<00:07, 27.61it/s]
19%|#9 | : 49/255 [00:01<00:07, 27.69it/s]
20%|## | : 51/255 [00:01<00:07, 28.45it/s]
20%|## | : 52/255 [00:01<00:07, 28.50it/s]
21%|## | : 53/255 [00:01<00:07, 28.54it/s]
21%|##1 | : 54/255 [00:01<00:07, 28.59it/s]
22%|##1 | : 55/255 [00:01<00:06, 28.63it/s]
22%|##1 | : 56/255 [00:01<00:06, 28.67it/s]
22%|##2 | : 57/255 [00:01<00:06, 28.70it/s]
23%|##2 | : 58/255 [00:01<00:06, 28.74it/s]
23%|##3 | : 59/255 [00:01<00:06, 28.78it/s]
24%|##3 | : 61/255 [00:01<00:06, 29.53it/s]
24%|##4 | : 62/255 [00:01<00:06, 29.53it/s]
25%|##4 | : 63/255 [00:01<00:06, 29.52it/s]
25%|##5 | : 64/255 [00:01<00:06, 29.52it/s]
25%|##5 | : 65/255 [00:01<00:06, 29.52it/s]
26%|##5 | : 66/255 [00:01<00:06, 29.52it/s]
27%|##6 | : 68/255 [00:02<00:06, 30.27it/s]
27%|##7 | : 69/255 [00:02<00:06, 30.23it/s]
27%|##7 | : 70/255 [00:02<00:06, 30.19it/s]
28%|##7 | : 71/255 [00:02<00:06, 30.15it/s]
28%|##8 | : 72/255 [00:02<00:06, 30.12it/s]
29%|##8 | : 73/255 [00:02<00:06, 30.07it/s]
29%|##9 | : 74/255 [00:02<00:06, 30.04it/s]
30%|##9 | : 76/255 [00:02<00:05, 30.80it/s]
30%|### | : 77/255 [00:02<00:05, 30.73it/s]
31%|### | : 78/255 [00:02<00:05, 30.66it/s]
31%|### | : 79/255 [00:02<00:05, 30.60it/s]
31%|###1 | : 80/255 [00:02<00:05, 30.54it/s]
32%|###1 | : 81/255 [00:02<00:05, 30.48it/s]
32%|###2 | : 82/255 [00:02<00:05, 30.43it/s]
33%|###2 | : 83/255 [00:02<00:05, 30.38it/s]
33%|###2 | : 84/255 [00:02<00:05, 30.33it/s]
33%|###3 | : 85/255 [00:02<00:05, 30.29it/s]
34%|###4 | : 87/255 [00:02<00:05, 31.03it/s]
35%|###4 | : 88/255 [00:02<00:05, 30.95it/s]
35%|###4 | : 89/255 [00:02<00:05, 30.87it/s]
35%|###5 | : 90/255 [00:02<00:05, 30.79it/s]
36%|###5 | : 91/255 [00:02<00:05, 30.72it/s]
36%|###6 | : 92/255 [00:02<00:05, 30.66it/s]
36%|###6 | : 93/255 [00:02<00:05, 30.60it/s]
37%|###7 | : 95/255 [00:02<00:05, 31.35it/s]
38%|###7 | : 96/255 [00:02<00:05, 31.24it/s]
38%|###8 | : 97/255 [00:02<00:05, 31.15it/s]
38%|###8 | : 98/255 [00:02<00:05, 31.06it/s]
39%|###8 | : 99/255 [00:02<00:05, 30.98it/s]
39%|###9 | : 100/255 [00:02<00:05, 30.90it/s]
40%|###9 | : 101/255 [00:03<00:04, 30.82it/s]
40%|#### | : 102/255 [00:03<00:04, 30.75it/s]
41%|#### | : 104/255 [00:03<00:04, 31.50it/s]
41%|####1 | : 105/255 [00:03<00:04, 31.39it/s]
42%|####1 | : 106/255 [00:03<00:04, 31.28it/s]
42%|####1 | : 107/255 [00:03<00:04, 31.19it/s]
42%|####2 | : 108/255 [00:03<00:04, 31.10it/s]
43%|####2 | : 109/255 [00:03<00:04, 31.01it/s]
43%|####3 | : 110/255 [00:03<00:04, 30.93it/s]
44%|####3 | : 112/255 [00:03<00:04, 31.67it/s]
44%|####4 | : 113/255 [00:03<00:04, 31.55it/s]
45%|####4 | : 114/255 [00:03<00:04, 31.44it/s]
45%|####5 | : 115/255 [00:03<00:04, 31.33it/s]
45%|####5 | : 116/255 [00:03<00:04, 31.23it/s]
46%|####5 | : 117/255 [00:03<00:04, 31.14it/s]
46%|####6 | : 118/255 [00:03<00:04, 31.05it/s]
47%|####6 | : 119/255 [00:03<00:04, 30.96it/s]
47%|####7 | : 121/255 [00:03<00:04, 31.70it/s]
48%|####7 | : 122/255 [00:03<00:04, 31.58it/s]
48%|####8 | : 123/255 [00:03<00:04, 31.47it/s]
49%|####8 | : 124/255 [00:03<00:04, 31.36it/s]
49%|####9 | : 125/255 [00:03<00:04, 31.26it/s]
49%|####9 | : 126/255 [00:03<00:04, 31.16it/s]
50%|####9 | : 127/255 [00:03<00:04, 31.07it/s]
50%|##### | : 128/255 [00:03<00:04, 30.99it/s]
51%|##### | : 130/255 [00:03<00:03, 31.72it/s]
51%|#####1 | : 131/255 [00:03<00:03, 31.60it/s]
52%|#####1 | : 132/255 [00:03<00:03, 31.48it/s]
52%|#####2 | : 133/255 [00:03<00:03, 31.37it/s]
53%|#####2 | : 134/255 [00:04<00:03, 31.27it/s]
53%|#####2 | : 135/255 [00:04<00:03, 31.17it/s]
53%|#####3 | : 136/255 [00:04<00:03, 31.08it/s]
54%|#####3 | : 137/255 [00:04<00:03, 30.99it/s]
55%|#####4 | : 139/255 [00:04<00:03, 31.73it/s]
55%|#####4 | : 140/255 [00:04<00:03, 31.61it/s]
55%|#####5 | : 141/255 [00:04<00:03, 31.49it/s]
56%|#####5 | : 142/255 [00:04<00:03, 31.38it/s]
56%|#####6 | : 143/255 [00:04<00:03, 31.28it/s]
56%|#####6 | : 144/255 [00:04<00:03, 31.18it/s]
57%|#####6 | : 145/255 [00:04<00:03, 31.09it/s]
57%|#####7 | : 146/255 [00:04<00:03, 31.00it/s]
58%|#####8 | : 148/255 [00:04<00:03, 31.75it/s]
58%|#####8 | : 149/255 [00:04<00:03, 31.63it/s]
59%|#####8 | : 150/255 [00:04<00:03, 31.51it/s]
59%|#####9 | : 151/255 [00:04<00:03, 31.40it/s]
60%|#####9 | : 152/255 [00:04<00:03, 31.29it/s]
60%|###### | : 153/255 [00:04<00:03, 31.20it/s]
60%|###### | : 154/255 [00:04<00:03, 31.11it/s]
61%|###### | : 155/255 [00:04<00:03, 31.02it/s]
61%|######1 | : 156/255 [00:04<00:03, 30.93it/s]
62%|######1 | : 158/255 [00:04<00:03, 31.67it/s]
62%|######2 | : 159/255 [00:04<00:03, 31.55it/s]
63%|######2 | : 160/255 [00:04<00:03, 31.44it/s]
63%|######3 | : 161/255 [00:04<00:02, 31.33it/s]
64%|######3 | : 162/255 [00:04<00:02, 31.23it/s]
64%|######3 | : 163/255 [00:04<00:02, 31.14it/s]
64%|######4 | : 164/255 [00:04<00:02, 31.05it/s]
65%|######5 | : 166/255 [00:04<00:02, 31.79it/s]
65%|######5 | : 167/255 [00:04<00:02, 31.66it/s]
66%|######5 | : 168/255 [00:05<00:02, 31.54it/s]
66%|######6 | : 169/255 [00:05<00:02, 31.43it/s]
67%|######6 | : 170/255 [00:05<00:02, 31.32it/s]
67%|######7 | : 171/255 [00:05<00:02, 31.23it/s]
67%|######7 | : 172/255 [00:05<00:02, 31.13it/s]
68%|######7 | : 173/255 [00:05<00:02, 31.04it/s]
69%|######8 | : 175/255 [00:05<00:02, 31.79it/s]
69%|######9 | : 176/255 [00:05<00:02, 31.67it/s]
69%|######9 | : 177/255 [00:05<00:02, 31.55it/s]
70%|######9 | : 178/255 [00:05<00:02, 31.44it/s]
70%|####### | : 179/255 [00:05<00:02, 31.33it/s]
71%|####### | : 180/255 [00:05<00:02, 31.23it/s]
71%|####### | : 181/255 [00:05<00:02, 31.14it/s]
71%|#######1 | : 182/255 [00:05<00:02, 31.05it/s]
72%|#######2 | : 184/255 [00:05<00:02, 31.78it/s]
73%|#######2 | : 185/255 [00:05<00:02, 31.66it/s]
73%|#######2 | : 186/255 [00:05<00:02, 31.54it/s]
73%|#######3 | : 187/255 [00:05<00:02, 31.43it/s]
74%|#######3 | : 188/255 [00:05<00:02, 31.32it/s]
74%|#######4 | : 189/255 [00:05<00:02, 31.22it/s]
75%|#######4 | : 190/255 [00:05<00:02, 31.13it/s]
75%|#######4 | : 191/255 [00:05<00:02, 31.04it/s]
75%|#######5 | : 192/255 [00:05<00:02, 30.96it/s]
76%|#######6 | : 194/255 [00:05<00:01, 31.69it/s]
76%|#######6 | : 195/255 [00:05<00:01, 31.57it/s]
77%|#######6 | : 196/255 [00:05<00:01, 31.46it/s]
77%|#######7 | : 197/255 [00:05<00:01, 31.35it/s]
78%|#######8 | : 199/255 [00:05<00:01, 32.10it/s]
78%|#######8 | : 200/255 [00:05<00:01, 31.96it/s]
79%|#######8 | : 201/255 [00:06<00:01, 31.82it/s]
79%|#######9 | : 202/255 [00:06<00:01, 31.70it/s]
80%|#######9 | : 203/255 [00:06<00:01, 31.58it/s]
80%|######## | : 204/255 [00:06<00:01, 31.46it/s]
81%|######## | : 206/255 [00:06<00:01, 32.21it/s]
81%|########1 | : 207/255 [00:06<00:01, 32.06it/s]
82%|########1 | : 208/255 [00:06<00:01, 31.91it/s]
82%|########1 | : 209/255 [00:06<00:01, 31.78it/s]
82%|########2 | : 210/255 [00:06<00:01, 31.65it/s]
83%|########2 | : 211/255 [00:06<00:01, 31.54it/s]
83%|########3 | : 212/255 [00:06<00:01, 31.42it/s]
84%|########3 | : 213/255 [00:06<00:01, 31.32it/s]
84%|########3 | : 214/255 [00:06<00:01, 31.22it/s]
84%|########4 | : 215/255 [00:06<00:01, 31.13it/s]
85%|########5 | : 217/255 [00:06<00:01, 31.88it/s]
85%|########5 | : 218/255 [00:06<00:01, 31.75it/s]
86%|########5 | : 219/255 [00:06<00:01, 31.63it/s]
86%|########6 | : 220/255 [00:06<00:01, 31.51it/s]
87%|########6 | : 221/255 [00:06<00:01, 31.40it/s]
87%|########7 | : 222/255 [00:06<00:01, 31.30it/s]
87%|########7 | : 223/255 [00:06<00:01, 31.20it/s]
88%|########8 | : 225/255 [00:06<00:00, 31.94it/s]
89%|########8 | : 226/255 [00:06<00:00, 31.81it/s]
89%|########9 | : 227/255 [00:06<00:00, 31.68it/s]
89%|########9 | : 228/255 [00:06<00:00, 31.56it/s]
90%|########9 | : 229/255 [00:06<00:00, 31.45it/s]
90%|######### | : 230/255 [00:06<00:00, 31.34it/s]
91%|######### | : 232/255 [00:06<00:00, 32.09it/s]
91%|#########1| : 233/255 [00:06<00:00, 31.95it/s]
92%|#########1| : 234/255 [00:07<00:00, 31.81it/s]
92%|#########2| : 235/255 [00:07<00:00, 31.68it/s]
93%|#########2| : 236/255 [00:07<00:00, 31.56it/s]
93%|#########2| : 237/255 [00:07<00:00, 31.45it/s]
93%|#########3| : 238/255 [00:07<00:00, 31.35it/s]
94%|#########4| : 240/255 [00:07<00:00, 32.10it/s]
95%|#########4| : 241/255 [00:07<00:00, 31.95it/s]
95%|#########4| : 242/255 [00:07<00:00, 31.82it/s]
95%|#########5| : 243/255 [00:07<00:00, 31.69it/s]
96%|#########5| : 244/255 [00:07<00:00, 31.57it/s]
96%|#########6| : 245/255 [00:07<00:00, 31.46it/s]
96%|#########6| : 246/255 [00:07<00:00, 31.35it/s]
97%|#########6| : 247/255 [00:07<00:00, 31.25it/s]
98%|#########7| : 249/255 [00:07<00:00, 32.00it/s]
98%|#########8| : 250/255 [00:07<00:00, 31.86it/s]
98%|#########8| : 251/255 [00:07<00:00, 31.73it/s]
99%|#########8| : 252/255 [00:07<00:00, 31.60it/s]
99%|#########9| : 253/255 [00:07<00:00, 31.49it/s]
100%|#########9| : 254/255 [00:07<00:00, 31.38it/s]
100%|##########| : 255/255 [00:07<00:00, 33.42it/s]
Computing cluster p-values
Done.
```

Create new stats image with only significant clusters:

```
good_clusters = np.where(cluster_p_values < .05)[0]
T_obs_plot = np.ma.masked_array(T_obs,
np.invert(clusters[np.squeeze(good_clusters)]))
plt.figure()
for f_image, cmap in zip([T_obs, T_obs_plot], [plt.cm.gray, 'RdBu_r']):
plt.imshow(f_image, cmap=cmap, extent=[times[0], times[-1],
freqs[0], freqs[-1]], aspect='auto',
origin='lower')
plt.xlabel('Time (ms)')
plt.ylabel('Frequency (Hz)')
plt.title("Time-locked response for 'modality by location' (%s)\n"
" cluster-level corrected (p <= 0.05)" % ch_name)
plt.show()
```

Now using FDR:

```
mask, _ = fdr_correction(pvals[2])
T_obs_plot2 = np.ma.masked_array(T_obs, np.invert(mask))
plt.figure()
for f_image, cmap in zip([T_obs, T_obs_plot2], [plt.cm.gray, 'RdBu_r']):
plt.imshow(f_image, cmap=cmap, extent=[times[0], times[-1],
freqs[0], freqs[-1]], aspect='auto',
origin='lower')
plt.xlabel('Time (ms)')
plt.ylabel('Frequency (Hz)')
plt.title("Time-locked response for 'modality by location' (%s)\n"
" FDR corrected (p <= 0.05)" % ch_name)
plt.show()
```

Both cluster level and FDR correction help get rid of potential spots we saw in the naive f-images.

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

**Estimated memory usage:** 163 MB