Note

Click here to download the full example code

# Receptive Field Estimation and Prediction#

This example reproduces figures from Lalor et al.’s mTRF toolbox in
MATLAB 1. We will show how the
`mne.decoding.ReceptiveField`

class
can perform a similar function along with scikit-learn. We will first fit a
linear encoding model using the continuously-varying speech envelope to predict
activity of a 128 channel EEG system. Then, we will take the reverse approach
and try to predict the speech envelope from the EEG (known in the literature
as a decoding model, or simply stimulus reconstruction).

```
# Authors: Chris Holdgraf <choldgraf@gmail.com>
# Eric Larson <larson.eric.d@gmail.com>
# Nicolas Barascud <nicolas.barascud@ens.fr>
#
# License: BSD-3-Clause
```

sphinx_gallery_thumbnail_number = 3

```
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
from os.path import join
import mne
from mne.decoding import ReceptiveField
from sklearn.model_selection import KFold
from sklearn.preprocessing import scale
```

## Load the data from the publication#

First we will load the data collected in 1. In this experiment subjects listened to natural speech. Raw EEG and the speech stimulus are provided. We will load these below, downsampling the data in order to speed up computation since we know that our features are primarily low-frequency in nature. Then we’ll visualize both the EEG and speech envelope.

```
path = mne.datasets.mtrf.data_path()
decim = 2
data = loadmat(join(path, 'speech_data.mat'))
raw = data['EEG'].T
speech = data['envelope'].T
sfreq = float(data['Fs'])
sfreq /= decim
speech = mne.filter.resample(speech, down=decim, npad='auto')
raw = mne.filter.resample(raw, down=decim, npad='auto')
# Read in channel positions and create our MNE objects from the raw data
montage = mne.channels.make_standard_montage('biosemi128')
info = mne.create_info(montage.ch_names, sfreq, 'eeg').set_montage(montage)
raw = mne.io.RawArray(raw, info)
n_channels = len(raw.ch_names)
# Plot a sample of brain and stimulus activity
fig, ax = plt.subplots()
lns = ax.plot(scale(raw[:, :800][0].T), color='k', alpha=.1)
ln1 = ax.plot(scale(speech[0, :800]), color='r', lw=2)
ax.legend([lns[0], ln1[0]], ['EEG', 'Speech Envelope'], frameon=False)
ax.set(title="Sample activity", xlabel="Time (s)")
mne.viz.tight_layout()
```

```
Creating RawArray with float64 data, n_channels=128, n_times=7677
Range : 0 ... 7676 = 0.000 ... 119.938 secs
Ready.
```

## Create and fit a receptive field model#

We will construct an encoding model to find the linear relationship between a time-delayed version of the speech envelope and the EEG signal. This allows us to make predictions about the response to new stimuli.

```
# Define the delays that we will use in the receptive field
tmin, tmax = -.2, .4
# Initialize the model
rf = ReceptiveField(tmin, tmax, sfreq, feature_names=['envelope'],
estimator=1., scoring='corrcoef')
# We'll have (tmax - tmin) * sfreq delays
# and an extra 2 delays since we are inclusive on the beginning / end index
n_delays = int((tmax - tmin) * sfreq) + 2
n_splits = 3
cv = KFold(n_splits)
# Prepare model data (make time the first dimension)
speech = speech.T
Y, _ = raw[:] # Outputs for the model
Y = Y.T
# Iterate through splits, fit the model, and predict/test on held-out data
coefs = np.zeros((n_splits, n_channels, n_delays))
scores = np.zeros((n_splits, n_channels))
for ii, (train, test) in enumerate(cv.split(speech)):
print('split %s / %s' % (ii + 1, n_splits))
rf.fit(speech[train], Y[train])
scores[ii] = rf.score(speech[test], Y[test])
# coef_ is shape (n_outputs, n_features, n_delays). we only have 1 feature
coefs[ii] = rf.coef_[:, 0, :]
times = rf.delays_ / float(rf.sfreq)
# Average scores and coefficients across CV splits
mean_coefs = coefs.mean(axis=0)
mean_scores = scores.mean(axis=0)
# Plot mean prediction scores across all channels
fig, ax = plt.subplots()
ix_chs = np.arange(n_channels)
ax.plot(ix_chs, mean_scores)
ax.axhline(0, ls='--', color='r')
ax.set(title="Mean prediction score", xlabel="Channel", ylabel="Score ($r$)")
mne.viz.tight_layout()
```

```
split 1 / 3
Fitting 1 epochs, 1 channels
0%| | Sample : 0/2 [00:00<?, ?it/s]
50%|##### | Sample : 1/2 [00:02<00:02, 2.25s/it]
100%|##########| Sample : 2/2 [00:02<00:00, 1.11s/it]
100%|##########| Sample : 2/2 [00:02<00:00, 1.14s/it]
split 2 / 3
Fitting 1 epochs, 1 channels
0%| | Sample : 0/2 [00:00<?, ?it/s]
50%|##### | Sample : 1/2 [00:00<00:00, 45.45it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 45.85it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 45.48it/s]
split 3 / 3
Fitting 1 epochs, 1 channels
0%| | Sample : 0/2 [00:00<?, ?it/s]
50%|##### | Sample : 1/2 [00:00<00:00, 47.18it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 47.40it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 46.96it/s]
```

### Investigate model coefficients#

Finally, we will look at how the linear coefficients (sometimes referred to as beta values) are distributed across time delays as well as across the scalp. We will recreate figure 1 and figure 2 from 1.

```
# Print mean coefficients across all time delays / channels (see Fig 1)
time_plot = 0.180 # For highlighting a specific time.
fig, ax = plt.subplots(figsize=(4, 8))
max_coef = mean_coefs.max()
ax.pcolormesh(times, ix_chs, mean_coefs, cmap='RdBu_r',
vmin=-max_coef, vmax=max_coef, shading='gouraud')
ax.axvline(time_plot, ls='--', color='k', lw=2)
ax.set(xlabel='Delay (s)', ylabel='Channel', title="Mean Model\nCoefficients",
xlim=times[[0, -1]], ylim=[len(ix_chs) - 1, 0],
xticks=np.arange(tmin, tmax + .2, .2))
plt.setp(ax.get_xticklabels(), rotation=45)
mne.viz.tight_layout()
# Make a topographic map of coefficients for a given delay (see Fig 2C)
ix_plot = np.argmin(np.abs(time_plot - times))
fig, ax = plt.subplots()
mne.viz.plot_topomap(mean_coefs[:, ix_plot], pos=info, axes=ax, show=False,
vmin=-max_coef, vmax=max_coef)
ax.set(title="Topomap of model coefficients\nfor delay %s" % time_plot)
mne.viz.tight_layout()
```

## Create and fit a stimulus reconstruction model#

We will now demonstrate another use case for the for the
`mne.decoding.ReceptiveField`

class as we try to predict the stimulus
activity from the EEG data. This is known in the literature as a decoding, or
stimulus reconstruction model 1.
A decoding model aims to find the
relationship between the speech signal and a time-delayed version of the EEG.
This can be useful as we exploit all of the available neural data in a
multivariate context, compared to the encoding case which treats each M/EEG
channel as an independent feature. Therefore, decoding models might provide a
better quality of fit (at the expense of not controlling for stimulus
covariance), especially for low SNR stimuli such as speech.

```
# We use the same lags as in :footcite:`CrosseEtAl2016`. Negative lags now
# index the relationship
# between the neural response and the speech envelope earlier in time, whereas
# positive lags would index how a unit change in the amplitude of the EEG would
# affect later stimulus activity (obviously this should have an amplitude of
# zero).
tmin, tmax = -.2, 0.
# Initialize the model. Here the features are the EEG data. We also specify
# ``patterns=True`` to compute inverse-transformed coefficients during model
# fitting (cf. next section and :footcite:`HaufeEtAl2014`).
# We'll use a ridge regression estimator with an alpha value similar to
# Crosse et al.
sr = ReceptiveField(tmin, tmax, sfreq, feature_names=raw.ch_names,
estimator=1e4, scoring='corrcoef', patterns=True)
# We'll have (tmax - tmin) * sfreq delays
# and an extra 2 delays since we are inclusive on the beginning / end index
n_delays = int((tmax - tmin) * sfreq) + 2
n_splits = 3
cv = KFold(n_splits)
# Iterate through splits, fit the model, and predict/test on held-out data
coefs = np.zeros((n_splits, n_channels, n_delays))
patterns = coefs.copy()
scores = np.zeros((n_splits,))
for ii, (train, test) in enumerate(cv.split(speech)):
print('split %s / %s' % (ii + 1, n_splits))
sr.fit(Y[train], speech[train])
scores[ii] = sr.score(Y[test], speech[test])[0]
# coef_ is shape (n_outputs, n_features, n_delays). We have 128 features
coefs[ii] = sr.coef_[0, :, :]
patterns[ii] = sr.patterns_[0, :, :]
times = sr.delays_ / float(sr.sfreq)
# Average scores and coefficients across CV splits
mean_coefs = coefs.mean(axis=0)
mean_patterns = patterns.mean(axis=0)
mean_scores = scores.mean(axis=0)
max_coef = np.abs(mean_coefs).max()
max_patterns = np.abs(mean_patterns).max()
```

```
split 1 / 3
Fitting 1 epochs, 128 channels
0%| | Sample : 0/8384 [00:00<?, ?it/s]
0%| | Sample : 1/8384 [00:00<05:21, 26.04it/s]
1%| | Sample : 65/8384 [00:00<00:06, 1234.07it/s]
2%|1 | Sample : 139/8384 [00:00<00:04, 2054.12it/s]
3%|2 | Sample : 213/8384 [00:00<00:03, 2575.84it/s]
3%|3 | Sample : 290/8384 [00:00<00:02, 2960.59it/s]
4%|4 | Sample : 356/8384 [00:00<00:02, 3139.68it/s]
5%|5 | Sample : 431/8384 [00:00<00:02, 3352.70it/s]
6%|6 | Sample : 508/8384 [00:00<00:02, 3538.41it/s]
7%|6 | Sample : 583/8384 [00:00<00:02, 3669.50it/s]
8%|7 | Sample : 657/8384 [00:00<00:02, 3772.75it/s]
9%|8 | Sample : 733/8384 [00:00<00:01, 3872.26it/s]
10%|9 | Sample : 807/8384 [00:00<00:01, 3944.11it/s]
11%|# | Sample : 884/8384 [00:00<00:01, 4025.86it/s]
11%|#1 | Sample : 961/8384 [00:00<00:01, 4092.89it/s]
12%|#2 | Sample : 1037/8384 [00:00<00:01, 4146.64it/s]
13%|#3 | Sample : 1113/8384 [00:00<00:01, 4193.58it/s]
14%|#4 | Sample : 1190/8384 [00:00<00:01, 4240.65it/s]
15%|#5 | Sample : 1265/8384 [00:00<00:01, 4274.69it/s]
16%|#5 | Sample : 1338/8384 [00:00<00:01, 4293.43it/s]
17%|#6 | Sample : 1414/8384 [00:00<00:01, 4324.77it/s]
18%|#7 | Sample : 1489/8384 [00:00<00:01, 4349.70it/s]
19%|#8 | Sample : 1566/8384 [00:00<00:01, 4378.42it/s]
20%|#9 | Sample : 1641/8384 [00:00<00:01, 4398.78it/s]
20%|## | Sample : 1717/8384 [00:00<00:01, 4421.19it/s]
21%|##1 | Sample : 1792/8384 [00:00<00:01, 4437.94it/s]
22%|##2 | Sample : 1867/8384 [00:00<00:01, 4451.21it/s]
23%|##3 | Sample : 1944/8384 [00:00<00:01, 4472.83it/s]
24%|##4 | Sample : 2017/8384 [00:00<00:01, 4478.25it/s]
25%|##4 | Sample : 2092/8384 [00:00<00:01, 4489.48it/s]
26%|##5 | Sample : 2167/8384 [00:00<00:01, 4500.74it/s]
27%|##6 | Sample : 2239/8384 [00:00<00:01, 4499.43it/s]
28%|##7 | Sample : 2317/8384 [00:00<00:01, 4519.43it/s]
29%|##8 | Sample : 2393/8384 [00:00<00:01, 4530.55it/s]
29%|##9 | Sample : 2466/8384 [00:00<00:01, 4529.97it/s]
30%|### | Sample : 2537/8384 [00:00<00:01, 4523.15it/s]
31%|###1 | Sample : 2610/8384 [00:00<00:01, 4522.74it/s]
32%|###2 | Sample : 2688/8384 [00:00<00:01, 4539.97it/s]
33%|###2 | Sample : 2762/8384 [00:00<00:01, 4543.29it/s]
34%|###3 | Sample : 2839/8384 [00:00<00:01, 4556.22it/s]
35%|###4 | Sample : 2915/8384 [00:00<00:01, 4565.21it/s]
36%|###5 | Sample : 2988/8384 [00:00<00:01, 4563.47it/s]
37%|###6 | Sample : 3065/8384 [00:00<00:01, 4574.45it/s]
37%|###7 | Sample : 3137/8384 [00:00<00:01, 4568.83it/s]
38%|###8 | Sample : 3212/8384 [00:00<00:01, 4575.22it/s]
39%|###9 | Sample : 3292/8384 [00:00<00:01, 4597.11it/s]
40%|#### | Sample : 3372/8384 [00:00<00:01, 4614.23it/s]
41%|####1 | Sample : 3450/8384 [00:00<00:01, 4627.92it/s]
42%|####2 | Sample : 3526/8384 [00:00<00:01, 4632.50it/s]
43%|####2 | Sample : 3602/8384 [00:00<00:01, 4636.71it/s]
44%|####3 | Sample : 3672/8384 [00:00<00:01, 4620.84it/s]
45%|####4 | Sample : 3748/8384 [00:00<00:01, 4626.65it/s]
46%|####5 | Sample : 3825/8384 [00:00<00:00, 4633.72it/s]
47%|####6 | Sample : 3899/8384 [00:00<00:00, 4630.42it/s]
47%|####7 | Sample : 3975/8384 [00:00<00:00, 4634.92it/s]
48%|####8 | Sample : 4046/8384 [00:00<00:00, 4622.72it/s]
49%|####9 | Sample : 4121/8384 [00:00<00:00, 4623.68it/s]
50%|##### | Sample : 4197/8384 [00:00<00:00, 4627.75it/s]
51%|##### | Sample : 4272/8384 [00:00<00:00, 4627.74it/s]
52%|#####1 | Sample : 4346/8384 [00:00<00:00, 4625.63it/s]
53%|#####2 | Sample : 4423/8384 [00:00<00:00, 4633.62it/s]
54%|#####3 | Sample : 4501/8384 [00:01<00:00, 4643.88it/s]
55%|#####4 | Sample : 4575/8384 [00:01<00:00, 4642.20it/s]
56%|#####5 | Sample : 4655/8384 [00:01<00:00, 4658.07it/s]
56%|#####6 | Sample : 4730/8384 [00:01<00:00, 4659.36it/s]
57%|#####7 | Sample : 4805/8384 [00:01<00:00, 4657.86it/s]
58%|#####8 | Sample : 4876/8384 [00:01<00:00, 4645.94it/s]
59%|#####9 | Sample : 4953/8384 [00:01<00:00, 4653.75it/s]
60%|#####9 | Sample : 5027/8384 [00:01<00:00, 4650.42it/s]
61%|###### | Sample : 5104/8384 [00:01<00:00, 4656.66it/s]
62%|######1 | Sample : 5181/8384 [00:01<00:00, 4662.67it/s]
63%|######2 | Sample : 5255/8384 [00:01<00:00, 4659.01it/s]
64%|######3 | Sample : 5331/8384 [00:01<00:00, 4661.74it/s]
64%|######4 | Sample : 5407/8384 [00:01<00:00, 4665.76it/s]
65%|######5 | Sample : 5482/8384 [00:01<00:00, 4665.68it/s]
66%|######6 | Sample : 5558/8384 [00:01<00:00, 4667.48it/s]
67%|######7 | Sample : 5634/8384 [00:01<00:00, 4670.04it/s]
68%|######8 | Sample : 5716/8384 [00:01<00:00, 4690.73it/s]
69%|######9 | Sample : 5794/8384 [00:01<00:00, 4697.64it/s]
70%|####### | Sample : 5869/8384 [00:01<00:00, 4695.40it/s]
71%|####### | Sample : 5947/8384 [00:01<00:00, 4702.13it/s]
72%|#######1 | Sample : 6022/8384 [00:01<00:00, 4700.75it/s]
73%|#######2 | Sample : 6098/8384 [00:01<00:00, 4700.76it/s]
74%|#######3 | Sample : 6177/8384 [00:01<00:00, 4710.37it/s]
75%|#######4 | Sample : 6255/8384 [00:01<00:00, 4718.26it/s]
76%|#######5 | Sample : 6330/8384 [00:01<00:00, 4716.28it/s]
76%|#######6 | Sample : 6404/8384 [00:01<00:00, 4711.03it/s]
77%|#######7 | Sample : 6483/8384 [00:01<00:00, 4721.06it/s]
78%|#######8 | Sample : 6561/8384 [00:01<00:00, 4727.87it/s]
79%|#######9 | Sample : 6637/8384 [00:01<00:00, 4727.86it/s]
80%|######## | Sample : 6713/8384 [00:01<00:00, 4728.72it/s]
81%|######## | Sample : 6790/8384 [00:01<00:00, 4732.49it/s]
82%|########1 | Sample : 6867/8384 [00:01<00:00, 4736.19it/s]
83%|########2 | Sample : 6946/8384 [00:01<00:00, 4745.35it/s]
84%|########3 | Sample : 7027/8384 [00:01<00:00, 4759.57it/s]
85%|########4 | Sample : 7106/8384 [00:01<00:00, 4765.08it/s]
86%|########5 | Sample : 7184/8384 [00:01<00:00, 4768.23it/s]
87%|########6 | Sample : 7264/8384 [00:01<00:00, 4779.70it/s]
88%|########7 | Sample : 7342/8384 [00:01<00:00, 4783.55it/s]
89%|########8 | Sample : 7421/8384 [00:01<00:00, 4790.15it/s]
89%|########9 | Sample : 7501/8384 [00:01<00:00, 4800.63it/s]
90%|######### | Sample : 7582/8384 [00:01<00:00, 4812.67it/s]
91%|#########1| Sample : 7662/8384 [00:01<00:00, 4820.14it/s]
92%|#########2| Sample : 7745/8384 [00:01<00:00, 4836.08it/s]
93%|#########3| Sample : 7826/8384 [00:01<00:00, 4845.36it/s]
94%|#########4| Sample : 7907/8384 [00:01<00:00, 4855.91it/s]
95%|#########5| Sample : 7990/8384 [00:01<00:00, 4870.33it/s]
96%|#########6| Sample : 8074/8384 [00:01<00:00, 4888.99it/s]
97%|#########7| Sample : 8159/8384 [00:01<00:00, 4908.23it/s]
98%|#########8| Sample : 8245/8384 [00:01<00:00, 4930.95it/s]
99%|#########9| Sample : 8332/8384 [00:01<00:00, 4956.00it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 4647.47it/s]
split 2 / 3
Fitting 1 epochs, 128 channels
0%| | Sample : 0/8384 [00:00<?, ?it/s]
0%| | Sample : 1/8384 [00:00<03:11, 43.83it/s]
1%| | Sample : 76/8384 [00:00<00:04, 2005.09it/s]
2%|1 | Sample : 152/8384 [00:00<00:02, 2844.59it/s]
3%|2 | Sample : 231/8384 [00:00<00:02, 3354.98it/s]
4%|3 | Sample : 308/8384 [00:00<00:02, 3650.36it/s]
5%|4 | Sample : 380/8384 [00:00<00:02, 3795.18it/s]
5%|5 | Sample : 452/8384 [00:00<00:02, 3904.78it/s]
6%|6 | Sample : 530/8384 [00:00<00:01, 4037.87it/s]
7%|7 | Sample : 608/8384 [00:00<00:01, 4144.72it/s]
8%|8 | Sample : 686/8384 [00:00<00:01, 4228.08it/s]
9%|9 | Sample : 765/8384 [00:00<00:01, 4303.31it/s]
10%|# | Sample : 845/8384 [00:00<00:01, 4373.52it/s]
11%|#1 | Sample : 923/8384 [00:00<00:01, 4417.30it/s]
12%|#1 | Sample : 997/8384 [00:00<00:01, 4434.73it/s]
13%|#2 | Sample : 1073/8384 [00:00<00:01, 4461.85it/s]
14%|#3 | Sample : 1149/8384 [00:00<00:01, 4484.89it/s]
15%|#4 | Sample : 1227/8384 [00:00<00:01, 4517.65it/s]
16%|#5 | Sample : 1305/8384 [00:00<00:01, 4543.32it/s]
16%|#6 | Sample : 1382/8384 [00:00<00:01, 4562.44it/s]
17%|#7 | Sample : 1462/8384 [00:00<00:01, 4591.62it/s]
18%|#8 | Sample : 1540/8384 [00:00<00:01, 4610.01it/s]
19%|#9 | Sample : 1617/8384 [00:00<00:01, 4624.39it/s]
20%|## | Sample : 1695/8384 [00:00<00:01, 4641.75it/s]
21%|##1 | Sample : 1771/8384 [00:00<00:01, 4649.22it/s]
22%|##1 | Sample : 1842/8384 [00:00<00:01, 4630.46it/s]
23%|##2 | Sample : 1918/8384 [00:00<00:01, 4636.74it/s]
24%|##3 | Sample : 1994/8384 [00:00<00:01, 4640.25it/s]
25%|##4 | Sample : 2071/8384 [00:00<00:01, 4647.78it/s]
26%|##5 | Sample : 2147/8384 [00:00<00:01, 4654.07it/s]
27%|##6 | Sample : 2224/8384 [00:00<00:01, 4663.27it/s]
27%|##7 | Sample : 2300/8384 [00:00<00:01, 4667.89it/s]
28%|##8 | Sample : 2375/8384 [00:00<00:01, 4667.96it/s]
29%|##9 | Sample : 2446/8384 [00:00<00:01, 4653.95it/s]
30%|### | Sample : 2520/8384 [00:00<00:01, 4650.87it/s]
31%|### | Sample : 2595/8384 [00:00<00:01, 4650.46it/s]
32%|###1 | Sample : 2669/8384 [00:00<00:01, 4646.05it/s]
33%|###2 | Sample : 2745/8384 [00:00<00:01, 4649.35it/s]
34%|###3 | Sample : 2821/8384 [00:00<00:01, 4654.44it/s]
35%|###4 | Sample : 2893/8384 [00:00<00:01, 4644.56it/s]
35%|###5 | Sample : 2965/8384 [00:00<00:01, 4636.26it/s]
36%|###6 | Sample : 3034/8384 [00:00<00:01, 4617.14it/s]
37%|###7 | Sample : 3109/8384 [00:00<00:01, 4618.43it/s]
38%|###7 | Sample : 3184/8384 [00:00<00:01, 4619.58it/s]
39%|###8 | Sample : 3259/8384 [00:00<00:01, 4621.25it/s]
40%|###9 | Sample : 3335/8384 [00:00<00:01, 4626.31it/s]
41%|#### | Sample : 3412/8384 [00:00<00:01, 4634.26it/s]
42%|####1 | Sample : 3486/8384 [00:00<00:01, 4631.71it/s]
42%|####2 | Sample : 3561/8384 [00:00<00:01, 4631.93it/s]
43%|####3 | Sample : 3634/8384 [00:00<00:01, 4627.62it/s]
44%|####4 | Sample : 3704/8384 [00:00<00:01, 4611.01it/s]
45%|####5 | Sample : 3778/8384 [00:00<00:00, 4610.37it/s]
46%|####5 | Sample : 3852/8384 [00:00<00:00, 4610.83it/s]
47%|####6 | Sample : 3929/8384 [00:00<00:00, 4618.24it/s]
48%|####7 | Sample : 4004/8384 [00:00<00:00, 4621.83it/s]
49%|####8 | Sample : 4076/8384 [00:00<00:00, 4614.01it/s]
49%|####9 | Sample : 4145/8384 [00:00<00:00, 4597.68it/s]
50%|##### | Sample : 4222/8384 [00:00<00:00, 4607.25it/s]
51%|#####1 | Sample : 4301/8384 [00:00<00:00, 4622.32it/s]
52%|#####2 | Sample : 4378/8384 [00:00<00:00, 4632.22it/s]
53%|#####3 | Sample : 4453/8384 [00:00<00:00, 4633.89it/s]
54%|#####4 | Sample : 4528/8384 [00:00<00:00, 4636.07it/s]
55%|#####4 | Sample : 4606/8384 [00:01<00:00, 4647.17it/s]
56%|#####5 | Sample : 4683/8384 [00:01<00:00, 4655.25it/s]
57%|#####6 | Sample : 4760/8384 [00:01<00:00, 4663.15it/s]
58%|#####7 | Sample : 4838/8384 [00:01<00:00, 4672.42it/s]
59%|#####8 | Sample : 4916/8384 [00:01<00:00, 4680.50it/s]
60%|#####9 | Sample : 4993/8384 [00:01<00:00, 4685.56it/s]
60%|###### | Sample : 5071/8384 [00:01<00:00, 4693.09it/s]
61%|######1 | Sample : 5147/8384 [00:01<00:00, 4695.68it/s]
62%|######2 | Sample : 5222/8384 [00:01<00:00, 4694.08it/s]
63%|######3 | Sample : 5297/8384 [00:01<00:00, 4693.01it/s]
64%|######4 | Sample : 5373/8384 [00:01<00:00, 4695.49it/s]
65%|######5 | Sample : 5450/8384 [00:01<00:00, 4700.19it/s]
66%|######5 | Sample : 5526/8384 [00:01<00:00, 4700.42it/s]
67%|######6 | Sample : 5604/8384 [00:01<00:00, 4709.03it/s]
68%|######7 | Sample : 5679/8384 [00:01<00:00, 4706.88it/s]
69%|######8 | Sample : 5755/8384 [00:01<00:00, 4706.53it/s]
70%|######9 | Sample : 5830/8384 [00:01<00:00, 4705.02it/s]
70%|####### | Sample : 5904/8384 [00:01<00:00, 4700.18it/s]
71%|#######1 | Sample : 5980/8384 [00:01<00:00, 4702.11it/s]
72%|#######2 | Sample : 6056/8384 [00:01<00:00, 4701.94it/s]
73%|#######3 | Sample : 6130/8384 [00:01<00:00, 4695.74it/s]
74%|#######4 | Sample : 6208/8384 [00:01<00:00, 4703.27it/s]
75%|#######5 | Sample : 6288/8384 [00:01<00:00, 4717.21it/s]
76%|#######5 | Sample : 6370/8384 [00:01<00:00, 4735.22it/s]
77%|#######6 | Sample : 6453/8384 [00:01<00:00, 4757.91it/s]
78%|#######7 | Sample : 6539/8384 [00:01<00:00, 4788.06it/s]
79%|#######9 | Sample : 6625/8384 [00:01<00:00, 4816.11it/s]
80%|######## | Sample : 6708/8384 [00:01<00:00, 4833.72it/s]
81%|########1 | Sample : 6796/8384 [00:01<00:00, 4865.54it/s]
82%|########2 | Sample : 6883/8384 [00:01<00:00, 4893.25it/s]
83%|########3 | Sample : 6970/8384 [00:01<00:00, 4917.67it/s]
84%|########4 | Sample : 7059/8384 [00:01<00:00, 4948.79it/s]
85%|########5 | Sample : 7146/8384 [00:01<00:00, 4972.09it/s]
86%|########6 | Sample : 7230/8384 [00:01<00:00, 4985.99it/s]
87%|########7 | Sample : 7316/8384 [00:01<00:00, 5004.94it/s]
88%|########8 | Sample : 7405/8384 [00:01<00:00, 5030.79it/s]
89%|########9 | Sample : 7494/8384 [00:01<00:00, 5054.77it/s]
90%|######### | Sample : 7580/8384 [00:01<00:00, 5068.80it/s]
91%|#########1| Sample : 7670/8384 [00:01<00:00, 5095.09it/s]
93%|#########2| Sample : 7756/8384 [00:01<00:00, 5107.77it/s]
94%|#########3| Sample : 7843/8384 [00:01<00:00, 5122.15it/s]
95%|#########4| Sample : 7933/8384 [00:01<00:00, 5146.85it/s]
96%|#########5| Sample : 8023/8384 [00:01<00:00, 5169.23it/s]
97%|#########6| Sample : 8112/8384 [00:01<00:00, 5186.04it/s]
98%|#########7| Sample : 8203/8384 [00:01<00:00, 5209.36it/s]
99%|#########8| Sample : 8294/8384 [00:01<00:00, 5231.33it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 4805.90it/s]
split 3 / 3
Fitting 1 epochs, 128 channels
0%| | Sample : 0/8384 [00:00<?, ?it/s]
0%| | Sample : 1/8384 [00:00<02:37, 53.37it/s]
1%|1 | Sample : 84/8384 [00:00<00:03, 2483.45it/s]
2%|1 | Sample : 164/8384 [00:00<00:02, 3319.41it/s]
3%|2 | Sample : 244/8384 [00:00<00:02, 3744.25it/s]
3%|2 | Sample : 248/8384 [00:00<00:03, 2560.05it/s]
4%|3 | Sample : 324/8384 [00:00<00:02, 2910.03it/s]
5%|4 | Sample : 402/8384 [00:00<00:02, 3194.35it/s]
6%|5 | Sample : 479/8384 [00:00<00:02, 3405.49it/s]
7%|6 | Sample : 559/8384 [00:00<00:02, 3595.48it/s]
8%|7 | Sample : 640/8384 [00:00<00:02, 3758.82it/s]
9%|8 | Sample : 718/8384 [00:00<00:01, 3874.26it/s]
9%|9 | Sample : 783/8384 [00:00<00:01, 3890.81it/s]
10%|9 | Sample : 838/8384 [00:00<00:01, 3844.52it/s]
11%|# | Sample : 910/8384 [00:00<00:01, 3901.75it/s]
12%|#1 | Sample : 980/8384 [00:00<00:01, 3941.09it/s]
13%|#2 | Sample : 1056/8384 [00:00<00:01, 4007.77it/s]
14%|#3 | Sample : 1133/8384 [00:00<00:01, 4073.48it/s]
14%|#4 | Sample : 1210/8384 [00:00<00:01, 4130.48it/s]
15%|#5 | Sample : 1288/8384 [00:00<00:01, 4187.07it/s]
16%|#6 | Sample : 1365/8384 [00:00<00:01, 4230.77it/s]
17%|#7 | Sample : 1441/8384 [00:00<00:01, 4266.04it/s]
18%|#8 | Sample : 1520/8384 [00:00<00:01, 4310.86it/s]
19%|#9 | Sample : 1599/8384 [00:00<00:01, 4352.86it/s]
20%|## | Sample : 1677/8384 [00:00<00:01, 4388.11it/s]
21%|## | Sample : 1754/8384 [00:00<00:01, 4415.93it/s]
22%|##1 | Sample : 1834/8384 [00:00<00:01, 4452.23it/s]
23%|##2 | Sample : 1913/8384 [00:00<00:01, 4481.99it/s]
24%|##3 | Sample : 1996/8384 [00:00<00:01, 4524.24it/s]
25%|##4 | Sample : 2080/8384 [00:00<00:01, 4569.61it/s]
26%|##5 | Sample : 2162/8384 [00:00<00:01, 4603.07it/s]
27%|##6 | Sample : 2244/8384 [00:00<00:01, 4633.50it/s]
28%|##7 | Sample : 2326/8384 [00:00<00:01, 4662.54it/s]
29%|##8 | Sample : 2408/8384 [00:00<00:01, 4687.59it/s]
30%|##9 | Sample : 2490/8384 [00:00<00:01, 4711.42it/s]
31%|### | Sample : 2571/8384 [00:00<00:01, 4731.86it/s]
32%|###1 | Sample : 2653/8384 [00:00<00:01, 4754.05it/s]
33%|###2 | Sample : 2732/8384 [00:00<00:01, 4763.42it/s]
34%|###3 | Sample : 2810/8384 [00:00<00:01, 4769.39it/s]
34%|###4 | Sample : 2891/8384 [00:00<00:01, 4785.98it/s]
35%|###5 | Sample : 2973/8384 [00:00<00:01, 4804.58it/s]
36%|###6 | Sample : 3056/8384 [00:00<00:01, 4825.51it/s]
37%|###7 | Sample : 3141/8384 [00:00<00:01, 4852.52it/s]
38%|###8 | Sample : 3224/8384 [00:00<00:01, 4870.84it/s]
39%|###9 | Sample : 3308/8384 [00:00<00:01, 4891.03it/s]
40%|#### | Sample : 3390/8384 [00:00<00:01, 4902.58it/s]
41%|####1 | Sample : 3471/8384 [00:00<00:01, 4908.98it/s]
42%|####2 | Sample : 3554/8384 [00:00<00:00, 4921.40it/s]
43%|####3 | Sample : 3635/8384 [00:00<00:00, 4926.62it/s]
44%|####4 | Sample : 3715/8384 [00:00<00:00, 4929.45it/s]
45%|####5 | Sample : 3796/8384 [00:00<00:00, 4936.39it/s]
46%|####6 | Sample : 3880/8384 [00:00<00:00, 4951.11it/s]
47%|####7 | Sample : 3962/8384 [00:00<00:00, 4959.23it/s]
48%|####8 | Sample : 4042/8384 [00:00<00:00, 4959.80it/s]
49%|####9 | Sample : 4124/8384 [00:00<00:00, 4965.95it/s]
50%|##### | Sample : 4208/8384 [00:00<00:00, 4980.28it/s]
51%|#####1 | Sample : 4292/8384 [00:00<00:00, 4992.59it/s]
52%|#####2 | Sample : 4376/8384 [00:00<00:00, 5004.61it/s]
53%|#####3 | Sample : 4460/8384 [00:00<00:00, 5014.63it/s]
54%|#####4 | Sample : 4547/8384 [00:00<00:00, 5033.95it/s]
55%|#####5 | Sample : 4629/8384 [00:00<00:00, 5038.13it/s]
56%|#####6 | Sample : 4712/8384 [00:00<00:00, 5043.67it/s]
57%|#####7 | Sample : 4797/8384 [00:01<00:00, 5056.29it/s]
58%|#####8 | Sample : 4882/8384 [00:01<00:00, 5068.04it/s]
59%|#####9 | Sample : 4968/8384 [00:01<00:00, 5082.69it/s]
60%|###### | Sample : 5048/8384 [00:01<00:00, 5077.65it/s]
61%|######1 | Sample : 5133/8384 [00:01<00:00, 5087.92it/s]
62%|######2 | Sample : 5218/8384 [00:01<00:00, 5098.46it/s]
63%|######3 | Sample : 5304/8384 [00:01<00:00, 5112.57it/s]
64%|######4 | Sample : 5388/8384 [00:01<00:00, 5116.89it/s]
65%|######5 | Sample : 5471/8384 [00:01<00:00, 5120.23it/s]
66%|######6 | Sample : 5555/8384 [00:01<00:00, 5125.49it/s]
67%|######7 | Sample : 5636/8384 [00:01<00:00, 5119.74it/s]
68%|######8 | Sample : 5718/8384 [00:01<00:00, 5119.99it/s]
69%|######9 | Sample : 5804/8384 [00:01<00:00, 5131.10it/s]
70%|####### | Sample : 5883/8384 [00:01<00:00, 5119.64it/s]
71%|#######1 | Sample : 5966/8384 [00:01<00:00, 5122.23it/s]
72%|#######2 | Sample : 6045/8384 [00:01<00:00, 5109.50it/s]
73%|#######2 | Sample : 6120/8384 [00:01<00:00, 5088.14it/s]
74%|#######3 | Sample : 6196/8384 [00:01<00:00, 5070.56it/s]
75%|#######4 | Sample : 6272/8384 [00:01<00:00, 5053.03it/s]
76%|#######5 | Sample : 6348/8384 [00:01<00:00, 5036.68it/s]
77%|#######6 | Sample : 6424/8384 [00:01<00:00, 5019.75it/s]
78%|#######7 | Sample : 6499/8384 [00:01<00:00, 5001.19it/s]
78%|#######8 | Sample : 6577/8384 [00:01<00:00, 4992.55it/s]
79%|#######9 | Sample : 6657/8384 [00:01<00:00, 4992.48it/s]
80%|######## | Sample : 6736/8384 [00:01<00:00, 4988.48it/s]
81%|########1 | Sample : 6818/8384 [00:01<00:00, 4992.99it/s]
82%|########2 | Sample : 6903/8384 [00:01<00:00, 5008.88it/s]
83%|########3 | Sample : 6988/8384 [00:01<00:00, 5021.60it/s]
84%|########4 | Sample : 7073/8384 [00:01<00:00, 5035.09it/s]
85%|########5 | Sample : 7158/8384 [00:01<00:00, 5047.46it/s]
86%|########6 | Sample : 7237/8384 [00:01<00:00, 5040.29it/s]
87%|########7 | Sample : 7316/8384 [00:01<00:00, 5031.96it/s]
88%|########8 | Sample : 7398/8384 [00:01<00:00, 5036.10it/s]
89%|########9 | Sample : 7479/8384 [00:01<00:00, 5036.49it/s]
90%|######### | Sample : 7557/8384 [00:01<00:00, 5028.10it/s]
91%|#########1| Sample : 7638/8384 [00:01<00:00, 5027.26it/s]
92%|#########2| Sample : 7718/8384 [00:01<00:00, 5022.70it/s]
93%|#########3| Sample : 7799/8384 [00:01<00:00, 5021.97it/s]
94%|#########3| Sample : 7877/8384 [00:01<00:00, 5013.48it/s]
95%|#########4| Sample : 7957/8384 [00:01<00:00, 5011.39it/s]
96%|#########5| Sample : 8038/8384 [00:01<00:00, 5010.99it/s]
97%|#########6| Sample : 8120/8384 [00:01<00:00, 5015.43it/s]
98%|#########7| Sample : 8206/8384 [00:01<00:00, 5031.90it/s]
99%|#########8| Sample : 8293/8384 [00:01<00:00, 5051.57it/s]
100%|#########9| Sample : 8382/8384 [00:01<00:00, 5075.02it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 4869.33it/s]
```

### Visualize stimulus reconstruction#

To get a sense of our model performance, we can plot the actual and predicted stimulus envelopes side by side.

```
y_pred = sr.predict(Y[test])
time = np.linspace(0, 2., 5 * int(sfreq))
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(time, speech[test][sr.valid_samples_][:int(5 * sfreq)],
color='grey', lw=2, ls='--')
ax.plot(time, y_pred[sr.valid_samples_][:int(5 * sfreq)], color='r', lw=2)
ax.legend([lns[0], ln1[0]], ['Envelope', 'Reconstruction'], frameon=False)
ax.set(title="Stimulus reconstruction")
ax.set_xlabel('Time (s)')
mne.viz.tight_layout()
```

### Investigate model coefficients#

Finally, we will look at how the decoding model coefficients are distributed across the scalp. We will attempt to recreate figure 5 from 1. The decoding model weights reflect the channels that contribute most toward reconstructing the stimulus signal, but are not directly interpretable in a neurophysiological sense. Here we also look at the coefficients obtained via an inversion procedure 2, which have a more straightforward interpretation as their value (and sign) directly relates to the stimulus signal’s strength (and effect direction).

```
time_plot = (-.140, -.125) # To average between two timepoints.
ix_plot = np.arange(np.argmin(np.abs(time_plot[0] - times)),
np.argmin(np.abs(time_plot[1] - times)))
fig, ax = plt.subplots(1, 2)
mne.viz.plot_topomap(np.mean(mean_coefs[:, ix_plot], axis=1),
pos=info, axes=ax[0], show=False,
vmin=-max_coef, vmax=max_coef)
ax[0].set(title="Model coefficients\nbetween delays %s and %s"
% (time_plot[0], time_plot[1]))
mne.viz.plot_topomap(np.mean(mean_patterns[:, ix_plot], axis=1),
pos=info, axes=ax[1],
show=False, vmin=-max_patterns, vmax=max_patterns)
ax[1].set(title="Inverse-transformed coefficients\nbetween delays %s and %s"
% (time_plot[0], time_plot[1]))
mne.viz.tight_layout()
```

## References#

- 1(1,2,3,4,5)
Michael J. Crosse, Giovanni M. Di Liberto, Adam Bednar, and Edmund C. Lalor. The multivariate temporal response function (mTRF) toolbox: a MATLAB toolbox for relating neural signals to continuous stimuli.

*Frontiers in Human Neuroscience*, 2016. doi:10.3389/fnhum.2016.00604.- 2
Stefan Haufe, Frank Meinecke, Kai Görgen, Sven Dähne, John-Dylan Haynes, Benjamin Blankertz, and Felix Bießmann. On the interpretation of weight vectors of linear models in multivariate neuroimaging.

*NeuroImage*, 87:96–110, 2014. doi:10.1016/j.neuroimage.2013.10.067.

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

**Estimated memory usage:** 50 MB