Note

Go to the end 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:01<00:01, 1.93s/it]
100%|##########| Sample : 2/2 [00:01<00:00, 1.05it/s]
100%|##########| Sample : 2/2 [00:01<00:00, 1.02it/s]
split 2 / 3
Fitting 1 epochs, 1 channels
0%| | Sample : 0/2 [00:00<?, ?it/s]
50%|##### | Sample : 1/2 [00:00<00:00, 49.12it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 49.26it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 48.85it/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, 48.38it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 49.10it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 48.56it/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,
vlim=(-max_coef, 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:14, 26.69it/s]
1%| | Sample : 69/8384 [00:00<00:06, 1333.37it/s]
2%|1 | Sample : 135/8384 [00:00<00:04, 2016.91it/s]
2%|2 | Sample : 200/8384 [00:00<00:03, 2433.00it/s]
3%|3 | Sample : 272/8384 [00:00<00:02, 2798.88it/s]
4%|4 | Sample : 349/8384 [00:00<00:02, 3114.69it/s]
5%|5 | Sample : 426/8384 [00:00<00:02, 3354.04it/s]
6%|5 | Sample : 503/8384 [00:00<00:02, 3540.40it/s]
7%|6 | Sample : 581/8384 [00:00<00:02, 3695.74it/s]
8%|7 | Sample : 658/8384 [00:00<00:02, 3818.50it/s]
9%|8 | Sample : 739/8384 [00:00<00:01, 3949.49it/s]
10%|9 | Sample : 816/8384 [00:00<00:01, 4033.62it/s]
11%|# | Sample : 894/8384 [00:00<00:01, 4110.47it/s]
12%|#1 | Sample : 972/8384 [00:00<00:01, 4179.19it/s]
12%|#2 | Sample : 1047/8384 [00:00<00:01, 4219.92it/s]
13%|#3 | Sample : 1126/8384 [00:00<00:01, 4276.36it/s]
14%|#4 | Sample : 1206/8384 [00:00<00:01, 4331.24it/s]
15%|#5 | Sample : 1285/8384 [00:00<00:01, 4375.99it/s]
16%|#6 | Sample : 1363/8384 [00:00<00:01, 4413.47it/s]
17%|#7 | Sample : 1440/8384 [00:00<00:01, 4441.79it/s]
18%|#8 | Sample : 1516/8384 [00:00<00:01, 4462.60it/s]
19%|#9 | Sample : 1594/8384 [00:00<00:01, 4489.82it/s]
20%|#9 | Sample : 1672/8384 [00:00<00:01, 4516.56it/s]
21%|## | Sample : 1752/8384 [00:00<00:01, 4546.82it/s]
22%|##1 | Sample : 1831/8384 [00:00<00:01, 4572.82it/s]
23%|##2 | Sample : 1912/8384 [00:00<00:01, 4602.55it/s]
24%|##3 | Sample : 1990/8384 [00:00<00:01, 4619.86it/s]
25%|##4 | Sample : 2068/8384 [00:00<00:01, 4633.79it/s]
26%|##5 | Sample : 2147/8384 [00:00<00:01, 4651.73it/s]
27%|##6 | Sample : 2226/8384 [00:00<00:01, 4665.75it/s]
27%|##7 | Sample : 2304/8384 [00:00<00:01, 4676.16it/s]
28%|##8 | Sample : 2380/8384 [00:00<00:01, 4679.85it/s]
29%|##9 | Sample : 2459/8384 [00:00<00:01, 4692.97it/s]
30%|### | Sample : 2536/8384 [00:00<00:01, 4697.79it/s]
31%|###1 | Sample : 2614/8384 [00:00<00:01, 4706.14it/s]
32%|###2 | Sample : 2690/8384 [00:00<00:01, 4705.96it/s]
33%|###3 | Sample : 2769/8384 [00:00<00:01, 4718.48it/s]
34%|###3 | Sample : 2845/8384 [00:00<00:01, 4719.69it/s]
35%|###4 | Sample : 2924/8384 [00:00<00:01, 4730.69it/s]
36%|###5 | Sample : 3006/8384 [00:00<00:01, 4751.10it/s]
37%|###6 | Sample : 3088/8384 [00:00<00:01, 4770.88it/s]
38%|###7 | Sample : 3170/8384 [00:00<00:01, 4789.62it/s]
39%|###8 | Sample : 3252/8384 [00:00<00:01, 4805.71it/s]
40%|###9 | Sample : 3336/8384 [00:00<00:01, 4828.47it/s]
41%|#### | Sample : 3419/8384 [00:00<00:01, 4846.85it/s]
42%|####1 | Sample : 3498/8384 [00:00<00:01, 4849.49it/s]
43%|####2 | Sample : 3577/8384 [00:00<00:00, 4853.71it/s]
44%|####3 | Sample : 3658/8384 [00:00<00:00, 4864.62it/s]
45%|####4 | Sample : 3738/8384 [00:00<00:00, 4871.21it/s]
46%|####5 | Sample : 3822/8384 [00:00<00:00, 4889.33it/s]
47%|####6 | Sample : 3904/8384 [00:00<00:00, 4901.23it/s]
48%|####7 | Sample : 3984/8384 [00:00<00:00, 4905.82it/s]
48%|####8 | Sample : 4064/8384 [00:00<00:00, 4908.29it/s]
49%|####9 | Sample : 4147/8384 [00:00<00:00, 4920.45it/s]
50%|##### | Sample : 4229/8384 [00:00<00:00, 4929.07it/s]
51%|#####1 | Sample : 4313/8384 [00:00<00:00, 4943.30it/s]
52%|#####2 | Sample : 4396/8384 [00:00<00:00, 4956.08it/s]
53%|#####3 | Sample : 4479/8384 [00:00<00:00, 4967.50it/s]
54%|#####4 | Sample : 4562/8384 [00:00<00:00, 4976.83it/s]
55%|#####5 | Sample : 4643/8384 [00:00<00:00, 4981.25it/s]
56%|#####6 | Sample : 4724/8384 [00:01<00:00, 4984.90it/s]
57%|#####7 | Sample : 4802/8384 [00:01<00:00, 4978.12it/s]
58%|#####8 | Sample : 4878/8384 [00:01<00:00, 4964.96it/s]
59%|#####9 | Sample : 4955/8384 [00:01<00:00, 4956.64it/s]
60%|###### | Sample : 5031/8384 [00:01<00:00, 4945.87it/s]
61%|###### | Sample : 5108/8384 [00:01<00:00, 4938.48it/s]
62%|######1 | Sample : 5186/8384 [00:01<00:00, 4933.15it/s]
63%|######2 | Sample : 5265/8384 [00:01<00:00, 4930.11it/s]
64%|######3 | Sample : 5343/8384 [00:01<00:00, 4926.51it/s]
65%|######4 | Sample : 5424/8384 [00:01<00:00, 4931.14it/s]
66%|######5 | Sample : 5503/8384 [00:01<00:00, 4929.94it/s]
67%|######6 | Sample : 5581/8384 [00:01<00:00, 4926.30it/s]
67%|######7 | Sample : 5659/8384 [00:01<00:00, 4922.30it/s]
68%|######8 | Sample : 5738/8384 [00:01<00:00, 4919.91it/s]
69%|######9 | Sample : 5817/8384 [00:01<00:00, 4919.55it/s]
70%|####### | Sample : 5897/8384 [00:01<00:00, 4923.18it/s]
71%|#######1 | Sample : 5976/8384 [00:01<00:00, 4923.23it/s]
72%|#######2 | Sample : 6058/8384 [00:01<00:00, 4931.47it/s]
73%|#######3 | Sample : 6144/8384 [00:01<00:00, 4951.38it/s]
74%|#######4 | Sample : 6227/8384 [00:01<00:00, 4963.07it/s]
75%|#######5 | Sample : 6310/8384 [00:01<00:00, 4973.80it/s]
76%|#######6 | Sample : 6396/8384 [00:01<00:00, 4993.18it/s]
77%|#######7 | Sample : 6479/8384 [00:01<00:00, 5002.85it/s]
78%|#######8 | Sample : 6564/8384 [00:01<00:00, 5017.52it/s]
79%|#######9 | Sample : 6651/8384 [00:01<00:00, 5038.08it/s]
80%|######## | Sample : 6736/8384 [00:01<00:00, 5049.67it/s]
81%|########1 | Sample : 6821/8384 [00:01<00:00, 5062.63it/s]
82%|########2 | Sample : 6906/8384 [00:01<00:00, 5072.51it/s]
83%|########3 | Sample : 6992/8384 [00:01<00:00, 5086.98it/s]
84%|########4 | Sample : 7076/8384 [00:01<00:00, 5094.02it/s]
85%|########5 | Sample : 7161/8384 [00:01<00:00, 5102.85it/s]
86%|########6 | Sample : 7244/8384 [00:01<00:00, 5105.48it/s]
87%|########7 | Sample : 7322/8384 [00:01<00:00, 5091.36it/s]
88%|########8 | Sample : 7407/8384 [00:01<00:00, 5099.87it/s]
89%|########9 | Sample : 7491/8384 [00:01<00:00, 5107.17it/s]
90%|######### | Sample : 7576/8384 [00:01<00:00, 5117.24it/s]
91%|#########1| Sample : 7662/8384 [00:01<00:00, 5127.59it/s]
92%|#########2| Sample : 7749/8384 [00:01<00:00, 5140.61it/s]
93%|#########3| Sample : 7837/8384 [00:01<00:00, 5155.82it/s]
95%|#########4| Sample : 7924/8384 [00:01<00:00, 5169.04it/s]
96%|#########5| Sample : 8013/8384 [00:01<00:00, 5188.25it/s]
97%|#########6| Sample : 8102/8384 [00:01<00:00, 5204.49it/s]
98%|#########7| Sample : 8190/8384 [00:01<00:00, 5218.65it/s]
99%|#########8| Sample : 8278/8384 [00:01<00:00, 5230.14it/s]
100%|#########9| Sample : 8367/8384 [00:01<00:00, 5243.78it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 4889.99it/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.74it/s]
1%| | Sample : 77/8384 [00:00<00:04, 2030.50it/s]
2%|1 | Sample : 155/8384 [00:00<00:02, 2899.98it/s]
3%|2 | Sample : 235/8384 [00:00<00:02, 3406.11it/s]
4%|3 | Sample : 312/8384 [00:00<00:02, 3691.50it/s]
5%|4 | Sample : 392/8384 [00:00<00:02, 3922.22it/s]
6%|5 | Sample : 470/8384 [00:00<00:01, 4065.26it/s]
7%|6 | Sample : 549/8384 [00:00<00:01, 4183.22it/s]
8%|7 | Sample : 630/8384 [00:00<00:01, 4296.04it/s]
8%|8 | Sample : 711/8384 [00:00<00:01, 4382.88it/s]
9%|9 | Sample : 793/8384 [00:00<00:01, 4461.14it/s]
10%|# | Sample : 874/8384 [00:00<00:01, 4522.22it/s]
11%|#1 | Sample : 953/8384 [00:00<00:01, 4562.38it/s]
12%|#2 | Sample : 1032/8384 [00:00<00:01, 4597.77it/s]
13%|#3 | Sample : 1112/8384 [00:00<00:01, 4633.04it/s]
14%|#4 | Sample : 1193/8384 [00:00<00:01, 4667.79it/s]
15%|#5 | Sample : 1273/8384 [00:00<00:01, 4694.60it/s]
16%|#6 | Sample : 1353/8384 [00:00<00:01, 4715.18it/s]
17%|#7 | Sample : 1433/8384 [00:00<00:01, 4736.60it/s]
18%|#8 | Sample : 1512/8384 [00:00<00:01, 4750.33it/s]
19%|#9 | Sample : 1593/8384 [00:00<00:01, 4770.49it/s]
20%|#9 | Sample : 1675/8384 [00:00<00:01, 4794.82it/s]
21%|## | Sample : 1757/8384 [00:00<00:01, 4815.77it/s]
22%|##1 | Sample : 1838/8384 [00:00<00:01, 4831.73it/s]
23%|##2 | Sample : 1918/8384 [00:00<00:01, 4840.57it/s]
24%|##3 | Sample : 1996/8384 [00:00<00:01, 4842.67it/s]
25%|##4 | Sample : 2077/8384 [00:00<00:01, 4855.64it/s]
26%|##5 | Sample : 2159/8384 [00:00<00:01, 4869.63it/s]
27%|##6 | Sample : 2238/8384 [00:00<00:01, 4873.56it/s]
28%|##7 | Sample : 2318/8384 [00:00<00:01, 4878.59it/s]
29%|##8 | Sample : 2397/8384 [00:00<00:01, 4879.33it/s]
29%|##9 | Sample : 2473/8384 [00:00<00:01, 4869.31it/s]
30%|### | Sample : 2554/8384 [00:00<00:01, 4878.10it/s]
31%|###1 | Sample : 2634/8384 [00:00<00:01, 4883.77it/s]
32%|###2 | Sample : 2711/8384 [00:00<00:01, 4878.81it/s]
33%|###3 | Sample : 2790/8384 [00:00<00:01, 4881.97it/s]
34%|###4 | Sample : 2870/8384 [00:00<00:01, 4887.28it/s]
35%|###5 | Sample : 2949/8384 [00:00<00:01, 4887.10it/s]
36%|###6 | Sample : 3028/8384 [00:00<00:01, 4887.31it/s]
37%|###6 | Sample : 3101/8384 [00:00<00:01, 4866.94it/s]
38%|###7 | Sample : 3177/8384 [00:00<00:01, 4860.08it/s]
39%|###8 | Sample : 3255/8384 [00:00<00:01, 4858.22it/s]
40%|###9 | Sample : 3334/8384 [00:00<00:01, 4862.01it/s]
41%|#### | Sample : 3414/8384 [00:00<00:01, 4867.35it/s]
42%|####1 | Sample : 3493/8384 [00:00<00:01, 4869.52it/s]
43%|####2 | Sample : 3569/8384 [00:00<00:00, 4858.77it/s]
43%|####3 | Sample : 3647/8384 [00:00<00:00, 4856.82it/s]
44%|####4 | Sample : 3726/8384 [00:00<00:00, 4859.94it/s]
45%|####5 | Sample : 3801/8384 [00:00<00:00, 4849.98it/s]
46%|####6 | Sample : 3882/8384 [00:00<00:00, 4859.39it/s]
47%|####7 | Sample : 3961/8384 [00:00<00:00, 4861.42it/s]
48%|####8 | Sample : 4040/8384 [00:00<00:00, 4863.44it/s]
49%|####9 | Sample : 4119/8384 [00:00<00:00, 4865.30it/s]
50%|##### | Sample : 4201/8384 [00:00<00:00, 4877.11it/s]
51%|#####1 | Sample : 4280/8384 [00:00<00:00, 4879.81it/s]
52%|#####2 | Sample : 4360/8384 [00:00<00:00, 4883.12it/s]
53%|#####2 | Sample : 4442/8384 [00:00<00:00, 4893.53it/s]
54%|#####3 | Sample : 4526/8384 [00:00<00:00, 4909.43it/s]
55%|#####4 | Sample : 4605/8384 [00:00<00:00, 4908.07it/s]
56%|#####5 | Sample : 4683/8384 [00:00<00:00, 4904.99it/s]
57%|#####6 | Sample : 4766/8384 [00:00<00:00, 4918.46it/s]
58%|#####7 | Sample : 4851/8384 [00:01<00:00, 4937.13it/s]
59%|#####8 | Sample : 4937/8384 [00:01<00:00, 4957.88it/s]
60%|#####9 | Sample : 5021/8384 [00:01<00:00, 4972.32it/s]
61%|###### | Sample : 5106/8384 [00:01<00:00, 4988.55it/s]
62%|######1 | Sample : 5192/8384 [00:01<00:00, 5007.28it/s]
63%|######2 | Sample : 5279/8384 [00:01<00:00, 5026.52it/s]
64%|######3 | Sample : 5363/8384 [00:01<00:00, 5036.94it/s]
65%|######4 | Sample : 5447/8384 [00:01<00:00, 5047.17it/s]
66%|######5 | Sample : 5529/8384 [00:01<00:00, 5051.05it/s]
67%|######6 | Sample : 5612/8384 [00:01<00:00, 5057.79it/s]
68%|######7 | Sample : 5697/8384 [00:01<00:00, 5068.74it/s]
69%|######8 | Sample : 5782/8384 [00:01<00:00, 5080.49it/s]
70%|######9 | Sample : 5867/8384 [00:01<00:00, 5091.90it/s]
71%|####### | Sample : 5949/8384 [00:01<00:00, 5092.61it/s]
72%|#######1 | Sample : 6029/8384 [00:01<00:00, 5085.20it/s]
73%|#######2 | Sample : 6113/8384 [00:01<00:00, 5090.97it/s]
74%|#######3 | Sample : 6199/8384 [00:01<00:00, 5102.76it/s]
75%|#######4 | Sample : 6285/8384 [00:01<00:00, 5115.70it/s]
76%|#######5 | Sample : 6370/8384 [00:01<00:00, 5122.93it/s]
77%|#######7 | Sample : 6456/8384 [00:01<00:00, 5133.84it/s]
78%|#######8 | Sample : 6540/8384 [00:01<00:00, 5137.64it/s]
79%|#######8 | Sample : 6622/8384 [00:01<00:00, 5135.50it/s]
80%|#######9 | Sample : 6703/8384 [00:01<00:00, 5128.90it/s]
81%|######## | Sample : 6783/8384 [00:01<00:00, 5121.98it/s]
82%|########1 | Sample : 6865/8384 [00:01<00:00, 5121.92it/s]
83%|########2 | Sample : 6947/8384 [00:01<00:00, 5119.42it/s]
84%|########3 | Sample : 7028/8384 [00:01<00:00, 5115.59it/s]
85%|########4 | Sample : 7110/8384 [00:01<00:00, 5114.12it/s]
86%|########5 | Sample : 7192/8384 [00:01<00:00, 5114.45it/s]
87%|########6 | Sample : 7274/8384 [00:01<00:00, 5114.49it/s]
88%|########7 | Sample : 7354/8384 [00:01<00:00, 5107.00it/s]
89%|########8 | Sample : 7436/8384 [00:01<00:00, 5104.85it/s]
90%|########9 | Sample : 7516/8384 [00:01<00:00, 5097.63it/s]
91%|######### | Sample : 7597/8384 [00:01<00:00, 5093.26it/s]
92%|#########1| Sample : 7677/8384 [00:01<00:00, 5084.89it/s]
93%|#########2| Sample : 7756/8384 [00:01<00:00, 5074.65it/s]
93%|#########3| Sample : 7836/8384 [00:01<00:00, 5070.13it/s]
94%|#########4| Sample : 7916/8384 [00:01<00:00, 5066.39it/s]
95%|#########5| Sample : 8000/8384 [00:01<00:00, 5073.53it/s]
96%|#########6| Sample : 8080/8384 [00:01<00:00, 5068.89it/s]
97%|#########7| Sample : 8164/8384 [00:01<00:00, 5075.64it/s]
98%|#########8| Sample : 8248/8384 [00:01<00:00, 5083.18it/s]
99%|#########9| Sample : 8336/8384 [00:01<00:00, 5102.27it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 4959.87it/s]
split 3 / 3
Fitting 1 epochs, 128 channels
0%| | Sample : 0/8384 [00:00<?, ?it/s]
0%| | Sample : 1/8384 [00:00<03:08, 44.56it/s]
1%| | Sample : 79/8384 [00:00<00:03, 2115.00it/s]
2%|1 | Sample : 158/8384 [00:00<00:02, 2989.90it/s]
3%|2 | Sample : 237/8384 [00:00<00:02, 3465.67it/s]
4%|3 | Sample : 314/8384 [00:00<00:02, 3738.59it/s]
5%|4 | Sample : 391/8384 [00:00<00:02, 3928.30it/s]
6%|5 | Sample : 470/8384 [00:00<00:01, 4082.46it/s]
7%|6 | Sample : 548/8384 [00:00<00:01, 4191.43it/s]
7%|7 | Sample : 627/8384 [00:00<00:01, 4286.61it/s]
8%|8 | Sample : 709/8384 [00:00<00:01, 4382.95it/s]
9%|9 | Sample : 791/8384 [00:00<00:01, 4463.30it/s]
10%|# | Sample : 870/8384 [00:00<00:01, 4510.63it/s]
11%|#1 | Sample : 949/8384 [00:00<00:01, 4548.65it/s]
12%|#2 | Sample : 1030/8384 [00:00<00:01, 4595.93it/s]
13%|#3 | Sample : 1112/8384 [00:00<00:01, 4641.24it/s]
14%|#4 | Sample : 1193/8384 [00:00<00:01, 4673.99it/s]
15%|#5 | Sample : 1275/8384 [00:00<00:01, 4707.23it/s]
16%|#6 | Sample : 1354/8384 [00:00<00:01, 4724.48it/s]
17%|#7 | Sample : 1434/8384 [00:00<00:01, 4741.89it/s]
18%|#8 | Sample : 1511/8384 [00:00<00:01, 4743.47it/s]
19%|#8 | Sample : 1589/8384 [00:00<00:01, 4752.20it/s]
20%|#9 | Sample : 1669/8384 [00:00<00:01, 4766.37it/s]
21%|## | Sample : 1747/8384 [00:00<00:01, 4770.66it/s]
22%|##1 | Sample : 1824/8384 [00:00<00:01, 4771.98it/s]
23%|##2 | Sample : 1901/8384 [00:00<00:01, 4773.40it/s]
24%|##3 | Sample : 1977/8384 [00:00<00:01, 4769.97it/s]
25%|##4 | Sample : 2055/8384 [00:00<00:01, 4775.42it/s]
25%|##5 | Sample : 2135/8384 [00:00<00:01, 4789.78it/s]
26%|##6 | Sample : 2214/8384 [00:00<00:01, 4796.26it/s]
27%|##7 | Sample : 2292/8384 [00:00<00:01, 4799.23it/s]
28%|##8 | Sample : 2371/8384 [00:00<00:01, 4803.55it/s]
29%|##9 | Sample : 2451/8384 [00:00<00:01, 4813.88it/s]
30%|### | Sample : 2531/8384 [00:00<00:01, 4824.45it/s]
31%|###1 | Sample : 2611/8384 [00:00<00:01, 4833.67it/s]
32%|###2 | Sample : 2691/8384 [00:00<00:01, 4841.81it/s]
33%|###3 | Sample : 2770/8384 [00:00<00:01, 4845.90it/s]
34%|###3 | Sample : 2850/8384 [00:00<00:01, 4851.44it/s]
35%|###4 | Sample : 2929/8384 [00:00<00:01, 4856.29it/s]
36%|###5 | Sample : 3011/8384 [00:00<00:01, 4870.28it/s]
37%|###6 | Sample : 3090/8384 [00:00<00:01, 4872.15it/s]
38%|###7 | Sample : 3168/8384 [00:00<00:01, 4869.47it/s]
39%|###8 | Sample : 3245/8384 [00:00<00:01, 4863.15it/s]
40%|###9 | Sample : 3321/8384 [00:00<00:01, 4854.93it/s]
40%|#### | Sample : 3395/8384 [00:00<00:01, 4840.88it/s]
41%|####1 | Sample : 3472/8384 [00:00<00:01, 4838.60it/s]
42%|####2 | Sample : 3551/8384 [00:00<00:00, 4843.00it/s]
43%|####3 | Sample : 3632/8384 [00:00<00:00, 4852.59it/s]
44%|####4 | Sample : 3710/8384 [00:00<00:00, 4853.49it/s]
45%|####5 | Sample : 3790/8384 [00:00<00:00, 4861.25it/s]
46%|####6 | Sample : 3868/8384 [00:00<00:00, 4860.50it/s]
47%|####7 | Sample : 3946/8384 [00:00<00:00, 4858.82it/s]
48%|####8 | Sample : 4027/8384 [00:00<00:00, 4868.33it/s]
49%|####9 | Sample : 4109/8384 [00:00<00:00, 4879.63it/s]
50%|##### | Sample : 4192/8384 [00:00<00:00, 4892.80it/s]
51%|##### | Sample : 4273/8384 [00:00<00:00, 4898.84it/s]
52%|#####1 | Sample : 4355/8384 [00:00<00:00, 4908.16it/s]
53%|#####2 | Sample : 4437/8384 [00:00<00:00, 4919.10it/s]
54%|#####3 | Sample : 4522/8384 [00:00<00:00, 4938.89it/s]
55%|#####4 | Sample : 4606/8384 [00:00<00:00, 4954.58it/s]
56%|#####5 | Sample : 4689/8384 [00:00<00:00, 4965.52it/s]
57%|#####6 | Sample : 4768/8384 [00:00<00:00, 4961.78it/s]
58%|#####7 | Sample : 4851/8384 [00:01<00:00, 4971.72it/s]
59%|#####8 | Sample : 4932/8384 [00:01<00:00, 4974.11it/s]
60%|#####9 | Sample : 5013/8384 [00:01<00:00, 4976.97it/s]
61%|###### | Sample : 5095/8384 [00:01<00:00, 4982.90it/s]
62%|######1 | Sample : 5180/8384 [00:01<00:00, 4999.75it/s]
63%|######2 | Sample : 5264/8384 [00:01<00:00, 5009.97it/s]
64%|######3 | Sample : 5349/8384 [00:01<00:00, 5023.59it/s]
65%|######4 | Sample : 5434/8384 [00:01<00:00, 5037.88it/s]
66%|######5 | Sample : 5516/8384 [00:01<00:00, 5039.98it/s]
67%|######6 | Sample : 5598/8384 [00:01<00:00, 5042.47it/s]
68%|######7 | Sample : 5680/8384 [00:01<00:00, 5045.72it/s]
69%|######8 | Sample : 5761/8384 [00:01<00:00, 5045.39it/s]
70%|######9 | Sample : 5841/8384 [00:01<00:00, 5042.32it/s]
71%|####### | Sample : 5922/8384 [00:01<00:00, 5042.39it/s]
72%|#######1 | Sample : 6004/8384 [00:01<00:00, 5045.30it/s]
73%|#######2 | Sample : 6090/8384 [00:01<00:00, 5060.75it/s]
74%|#######3 | Sample : 6172/8384 [00:01<00:00, 5062.42it/s]
75%|#######4 | Sample : 6253/8384 [00:01<00:00, 5060.50it/s]
76%|#######5 | Sample : 6334/8384 [00:01<00:00, 5059.12it/s]
77%|#######6 | Sample : 6414/8384 [00:01<00:00, 5054.90it/s]
77%|#######7 | Sample : 6496/8384 [00:01<00:00, 5056.63it/s]
78%|#######8 | Sample : 6579/8384 [00:01<00:00, 5061.06it/s]
79%|#######9 | Sample : 6662/8384 [00:01<00:00, 5067.15it/s]
80%|######## | Sample : 6745/8384 [00:01<00:00, 5071.14it/s]
81%|########1 | Sample : 6829/8384 [00:01<00:00, 5077.23it/s]
82%|########2 | Sample : 6907/8384 [00:01<00:00, 5066.28it/s]
83%|########3 | Sample : 6986/8384 [00:01<00:00, 5057.29it/s]
84%|########4 | Sample : 7067/8384 [00:01<00:00, 5055.92it/s]
85%|########5 | Sample : 7147/8384 [00:01<00:00, 5050.41it/s]
86%|########6 | Sample : 7227/8384 [00:01<00:00, 5044.79it/s]
87%|########7 | Sample : 7306/8384 [00:01<00:00, 5037.49it/s]
88%|########8 | Sample : 7387/8384 [00:01<00:00, 5036.32it/s]
89%|########9 | Sample : 7469/8384 [00:01<00:00, 5039.36it/s]
90%|######### | Sample : 7548/8384 [00:01<00:00, 5033.58it/s]
91%|######### | Sample : 7628/8384 [00:01<00:00, 5030.61it/s]
92%|#########1| Sample : 7708/8384 [00:01<00:00, 5026.60it/s]
93%|#########2| Sample : 7790/8384 [00:01<00:00, 5031.01it/s]
94%|#########3| Sample : 7874/8384 [00:01<00:00, 5041.41it/s]
95%|#########4| Sample : 7958/8384 [00:01<00:00, 5049.33it/s]
96%|#########5| Sample : 8042/8384 [00:01<00:00, 5058.35it/s]
97%|#########6| Sample : 8127/8384 [00:01<00:00, 5070.14it/s]
98%|#########7| Sample : 8211/8384 [00:01<00:00, 5078.86it/s]
99%|#########8| Sample : 8298/8384 [00:01<00:00, 5094.43it/s]
100%|#########9| Sample : 8382/8384 [00:01<00:00, 5100.47it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 4938.09it/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,
vlim=(-max_coef, 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, vlim=(-max_patterns, 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#

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

**Estimated memory usage:** 19 MB