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:02<00:02, 2.01s/it]
100%|##########| Sample : 2/2 [00:02<00:00, 1.01it/s]
100%|##########| Sample : 2/2 [00:02<00:00, 1.02s/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, 43.37it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 45.43it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 45.02it/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, 43.01it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 45.40it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 44.98it/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:43, 24.39it/s]
1%| | Sample : 62/8384 [00:00<00:07, 1126.94it/s]
1%|1 | Sample : 117/8384 [00:00<00:04, 1662.64it/s]
2%|2 | Sample : 178/8384 [00:00<00:03, 2083.92it/s]
3%|2 | Sample : 226/8384 [00:00<00:03, 2240.25it/s]
3%|3 | Sample : 268/8384 [00:00<00:03, 2292.16it/s]
4%|3 | Sample : 312/8384 [00:00<00:03, 2352.31it/s]
4%|4 | Sample : 358/8384 [00:00<00:03, 2413.93it/s]
5%|4 | Sample : 404/8384 [00:00<00:03, 2462.71it/s]
5%|5 | Sample : 448/8384 [00:00<00:03, 2490.10it/s]
6%|6 | Sample : 505/8384 [00:00<00:03, 2596.70it/s]
7%|6 | Sample : 558/8384 [00:00<00:02, 2666.81it/s]
7%|7 | Sample : 613/8384 [00:00<00:02, 2737.13it/s]
8%|8 | Sample : 672/8384 [00:00<00:02, 2822.35it/s]
9%|8 | Sample : 724/8384 [00:00<00:02, 2857.14it/s]
9%|9 | Sample : 782/8384 [00:00<00:02, 2918.39it/s]
10%|# | Sample : 857/8384 [00:00<00:02, 3059.43it/s]
11%|# | Sample : 914/8384 [00:00<00:02, 3096.88it/s]
12%|#1 | Sample : 969/8384 [00:00<00:02, 3120.69it/s]
12%|#2 | Sample : 1020/8384 [00:00<00:02, 3124.21it/s]
13%|#2 | Sample : 1078/8384 [00:00<00:02, 3159.19it/s]
14%|#3 | Sample : 1134/8384 [00:00<00:02, 3183.04it/s]
14%|#4 | Sample : 1193/8384 [00:00<00:02, 3217.35it/s]
15%|#5 | Sample : 1264/8384 [00:00<00:02, 3299.10it/s]
16%|#5 | Sample : 1339/8384 [00:00<00:02, 3391.00it/s]
17%|#6 | Sample : 1410/8384 [00:00<00:02, 3457.65it/s]
18%|#7 | Sample : 1478/8384 [00:00<00:01, 3507.06it/s]
18%|#8 | Sample : 1535/8384 [00:00<00:01, 3508.66it/s]
19%|#8 | Sample : 1588/8384 [00:00<00:01, 3495.89it/s]
20%|#9 | Sample : 1661/8384 [00:00<00:01, 3560.66it/s]
21%|## | Sample : 1727/8384 [00:00<00:01, 3592.30it/s]
22%|##1 | Sample : 1805/8384 [00:00<00:01, 3668.40it/s]
22%|##2 | Sample : 1850/8384 [00:00<00:01, 3615.66it/s]
23%|##2 | Sample : 1898/8384 [00:00<00:01, 3577.25it/s]
23%|##3 | Sample : 1967/8384 [00:00<00:01, 3617.72it/s]
24%|##4 | Sample : 2035/8384 [00:00<00:01, 3652.05it/s]
25%|##4 | Sample : 2095/8384 [00:00<00:01, 3657.42it/s]
26%|##5 | Sample : 2149/8384 [00:00<00:01, 3640.00it/s]
26%|##6 | Sample : 2203/8384 [00:00<00:01, 3621.57it/s]
27%|##7 | Sample : 2269/8384 [00:00<00:01, 3647.80it/s]
28%|##7 | Sample : 2339/8384 [00:00<00:01, 3685.40it/s]
29%|##8 | Sample : 2410/8384 [00:00<00:01, 3725.64it/s]
30%|##9 | Sample : 2480/8384 [00:00<00:01, 3759.02it/s]
30%|### | Sample : 2551/8384 [00:00<00:01, 3793.70it/s]
31%|###1 | Sample : 2615/8384 [00:00<00:01, 3802.28it/s]
32%|###2 | Sample : 2684/8384 [00:00<00:01, 3827.73it/s]
33%|###2 | Sample : 2752/8384 [00:00<00:01, 3849.16it/s]
34%|###3 | Sample : 2823/8384 [00:00<00:01, 3880.73it/s]
35%|###4 | Sample : 2899/8384 [00:00<00:01, 3926.90it/s]
35%|###5 | Sample : 2970/8384 [00:00<00:01, 3952.67it/s]
36%|###6 | Sample : 3041/8384 [00:00<00:01, 3977.00it/s]
37%|###7 | Sample : 3116/8384 [00:00<00:01, 4014.10it/s]
38%|###7 | Sample : 3180/8384 [00:00<00:01, 4010.27it/s]
39%|###8 | Sample : 3234/8384 [00:00<00:01, 3973.99it/s]
39%|###9 | Sample : 3284/8384 [00:00<00:01, 3926.98it/s]
40%|###9 | Sample : 3335/8384 [00:00<00:01, 3886.44it/s]
41%|#### | Sample : 3401/8384 [00:00<00:01, 3896.44it/s]
41%|####1 | Sample : 3467/8384 [00:00<00:01, 3904.32it/s]
42%|####1 | Sample : 3517/8384 [00:00<00:01, 3861.04it/s]
43%|####2 | Sample : 3571/8384 [00:00<00:01, 3832.58it/s]
43%|####3 | Sample : 3634/8384 [00:01<00:01, 3836.38it/s]
44%|####4 | Sample : 3692/8384 [00:01<00:01, 3824.13it/s]
45%|####4 | Sample : 3759/8384 [00:01<00:01, 3842.41it/s]
46%|####5 | Sample : 3831/8384 [00:01<00:01, 3875.01it/s]
47%|####6 | Sample : 3907/8384 [00:01<00:01, 3916.88it/s]
47%|####7 | Sample : 3979/8384 [00:01<00:01, 3946.42it/s]
48%|####8 | Sample : 4026/8384 [00:01<00:01, 3893.36it/s]
49%|####8 | Sample : 4077/8384 [00:01<00:01, 3856.01it/s]
49%|####9 | Sample : 4139/8384 [00:01<00:01, 3854.42it/s]
50%|##### | Sample : 4197/8384 [00:01<00:01, 3841.13it/s]
51%|##### | Sample : 4251/8384 [00:01<00:01, 3815.71it/s]
52%|#####1 | Sample : 4318/8384 [00:01<00:01, 3832.09it/s]
52%|#####2 | Sample : 4372/8384 [00:01<00:01, 3807.35it/s]
53%|#####2 | Sample : 4436/8384 [00:01<00:01, 3816.22it/s]
54%|#####3 | Sample : 4496/8384 [00:01<00:01, 3810.01it/s]
54%|#####4 | Sample : 4549/8384 [00:01<00:01, 3782.18it/s]
55%|#####5 | Sample : 4612/8384 [00:01<00:00, 3789.50it/s]
56%|#####5 | Sample : 4665/8384 [00:01<00:00, 3762.58it/s]
56%|#####6 | Sample : 4730/8384 [00:01<00:00, 3777.13it/s]
57%|#####7 | Sample : 4808/8384 [00:01<00:00, 3830.47it/s]
58%|#####8 | Sample : 4874/8384 [00:01<00:00, 3843.37it/s]
59%|#####8 | Sample : 4927/8384 [00:01<00:00, 3813.09it/s]
59%|#####9 | Sample : 4982/8384 [00:01<00:00, 3790.49it/s]
60%|###### | Sample : 5034/8384 [00:01<00:00, 3761.30it/s]
61%|###### | Sample : 5096/8384 [00:01<00:00, 3765.30it/s]
62%|######1 | Sample : 5164/8384 [00:01<00:00, 3788.20it/s]
62%|######2 | Sample : 5230/8384 [00:01<00:00, 3803.67it/s]
63%|######3 | Sample : 5302/8384 [00:01<00:00, 3838.47it/s]
64%|######3 | Sample : 5364/8384 [00:01<00:00, 3839.99it/s]
65%|######4 | Sample : 5433/8384 [00:01<00:00, 3861.29it/s]
66%|######5 | Sample : 5501/8384 [00:01<00:00, 3879.58it/s]
66%|######6 | Sample : 5571/8384 [00:01<00:00, 3902.56it/s]
67%|######7 | Sample : 5639/8384 [00:01<00:00, 3918.86it/s]
68%|######8 | Sample : 5707/8384 [00:01<00:00, 3933.98it/s]
69%|######8 | Sample : 5773/8384 [00:01<00:00, 3943.16it/s]
70%|######9 | Sample : 5838/8384 [00:01<00:00, 3947.46it/s]
70%|####### | Sample : 5907/8384 [00:01<00:00, 3964.25it/s]
71%|#######1 | Sample : 5966/8384 [00:01<00:00, 3947.08it/s]
72%|#######1 | Sample : 6026/8384 [00:01<00:00, 3934.85it/s]
73%|#######2 | Sample : 6081/8384 [00:01<00:00, 3906.43it/s]
73%|#######3 | Sample : 6139/8384 [00:01<00:00, 3891.38it/s]
74%|#######3 | Sample : 6195/8384 [00:01<00:00, 3869.68it/s]
74%|#######4 | Sample : 6243/8384 [00:01<00:00, 3825.31it/s]
75%|#######5 | Sample : 6296/8384 [00:01<00:00, 3798.96it/s]
76%|#######5 | Sample : 6349/8384 [00:01<00:00, 3772.36it/s]
76%|#######6 | Sample : 6401/8384 [00:01<00:00, 3742.83it/s]
77%|#######6 | Sample : 6452/8384 [00:01<00:00, 3713.93it/s]
78%|#######7 | Sample : 6500/8384 [00:01<00:00, 3678.01it/s]
78%|#######8 | Sample : 6550/8384 [00:01<00:00, 3648.32it/s]
79%|#######8 | Sample : 6600/8384 [00:01<00:00, 3621.63it/s]
79%|#######9 | Sample : 6650/8384 [00:01<00:00, 3595.02it/s]
80%|#######9 | Sample : 6699/8384 [00:01<00:00, 3567.66it/s]
80%|######## | Sample : 6748/8384 [00:01<00:00, 3540.90it/s]
81%|########1 | Sample : 6795/8384 [00:01<00:00, 3508.94it/s]
82%|########1 | Sample : 6865/8384 [00:01<00:00, 3549.72it/s]
83%|########2 | Sample : 6939/8384 [00:01<00:00, 3602.48it/s]
84%|########3 | Sample : 7013/8384 [00:01<00:00, 3652.18it/s]
85%|########4 | Sample : 7088/8384 [00:01<00:00, 3701.96it/s]
85%|########5 | Sample : 7161/8384 [00:01<00:00, 3743.23it/s]
86%|########6 | Sample : 7235/8384 [00:01<00:00, 3786.64it/s]
87%|########7 | Sample : 7297/8384 [00:01<00:00, 3789.95it/s]
88%|########7 | Sample : 7361/8384 [00:01<00:00, 3797.75it/s]
88%|########8 | Sample : 7416/8384 [00:02<00:00, 3778.79it/s]
89%|########9 | Sample : 7479/8384 [00:02<00:00, 3785.40it/s]
90%|########9 | Sample : 7540/8384 [00:02<00:00, 3785.03it/s]
91%|######### | Sample : 7590/8384 [00:02<00:00, 3749.68it/s]
91%|#########1| Sample : 7640/8384 [00:02<00:00, 3716.55it/s]
92%|#########1| Sample : 7702/8384 [00:02<00:00, 3722.75it/s]
93%|#########2| Sample : 7772/8384 [00:02<00:00, 3754.21it/s]
93%|#########3| Sample : 7828/8384 [00:02<00:00, 3740.66it/s]
94%|#########4| Sample : 7901/8384 [00:02<00:00, 3779.30it/s]
95%|#########5| Sample : 7977/8384 [00:02<00:00, 3826.74it/s]
96%|#########5| Sample : 8047/8384 [00:02<00:00, 3851.96it/s]
97%|#########6| Sample : 8116/8384 [00:02<00:00, 3874.21it/s]
98%|#########7| Sample : 8184/8384 [00:02<00:00, 3892.08it/s]
98%|#########8| Sample : 8254/8384 [00:02<00:00, 3914.38it/s]
99%|#########9| Sample : 8323/8384 [00:02<00:00, 3932.52it/s]
100%|##########| Sample : 8384/8384 [00:02<00:00, 3726.57it/s]
split 2 / 3
Fitting 1 epochs, 128 channels
0%| | Sample : 0/8384 [00:00<?, ?it/s]
0%| | Sample : 1/8384 [00:00<03:41, 37.93it/s]
1%| | Sample : 71/8384 [00:00<00:04, 1722.07it/s]
2%|1 | Sample : 143/8384 [00:00<00:03, 2524.88it/s]
3%|2 | Sample : 218/8384 [00:00<00:02, 3023.75it/s]
4%|3 | Sample : 294/8384 [00:00<00:02, 3358.32it/s]
4%|4 | Sample : 367/8384 [00:00<00:02, 3561.27it/s]
5%|5 | Sample : 440/8384 [00:00<00:02, 3708.97it/s]
6%|6 | Sample : 513/8384 [00:00<00:02, 3821.03it/s]
7%|7 | Sample : 587/8384 [00:00<00:01, 3920.87it/s]
8%|7 | Sample : 658/8384 [00:00<00:01, 3980.66it/s]
9%|8 | Sample : 734/8384 [00:00<00:01, 4060.04it/s]
10%|9 | Sample : 806/8384 [00:00<00:01, 4102.80it/s]
10%|# | Sample : 878/8384 [00:00<00:01, 4141.16it/s]
11%|#1 | Sample : 952/8384 [00:00<00:01, 4182.28it/s]
12%|#2 | Sample : 1025/8384 [00:00<00:01, 4214.02it/s]
13%|#3 | Sample : 1097/8384 [00:00<00:01, 4238.58it/s]
14%|#3 | Sample : 1171/8384 [00:00<00:01, 4267.18it/s]
15%|#4 | Sample : 1245/8384 [00:00<00:01, 4294.28it/s]
16%|#5 | Sample : 1319/8384 [00:00<00:01, 4317.84it/s]
17%|#6 | Sample : 1392/8384 [00:00<00:01, 4333.93it/s]
17%|#7 | Sample : 1467/8384 [00:00<00:01, 4357.48it/s]
18%|#8 | Sample : 1540/8384 [00:00<00:01, 4368.93it/s]
19%|#9 | Sample : 1612/8384 [00:00<00:01, 4374.11it/s]
20%|## | Sample : 1685/8384 [00:00<00:01, 4384.64it/s]
21%|## | Sample : 1739/8384 [00:00<00:01, 4314.56it/s]
21%|##1 | Sample : 1792/8384 [00:00<00:01, 4246.74it/s]
22%|##2 | Sample : 1848/8384 [00:00<00:01, 4192.79it/s]
23%|##2 | Sample : 1909/8384 [00:00<00:01, 4165.47it/s]
23%|##3 | Sample : 1961/8384 [00:00<00:01, 4106.51it/s]
24%|##4 | Sample : 2015/8384 [00:00<00:01, 4059.55it/s]
25%|##4 | Sample : 2068/8384 [00:00<00:01, 4010.52it/s]
25%|##5 | Sample : 2134/8384 [00:00<00:01, 4013.56it/s]
26%|##6 | Sample : 2186/8384 [00:00<00:01, 3966.78it/s]
27%|##6 | Sample : 2243/8384 [00:00<00:01, 3939.41it/s]
27%|##7 | Sample : 2302/8384 [00:00<00:01, 3923.81it/s]
28%|##8 | Sample : 2364/8384 [00:00<00:01, 3919.23it/s]
29%|##9 | Sample : 2438/8384 [00:00<00:01, 3958.66it/s]
30%|##9 | Sample : 2511/8384 [00:00<00:01, 3992.65it/s]
31%|### | Sample : 2585/8384 [00:00<00:01, 4025.85it/s]
32%|###1 | Sample : 2657/8384 [00:00<00:01, 4052.28it/s]
33%|###2 | Sample : 2729/8384 [00:00<00:01, 4076.73it/s]
33%|###3 | Sample : 2801/8384 [00:00<00:01, 4097.38it/s]
34%|###4 | Sample : 2858/8384 [00:00<00:01, 4064.42it/s]
35%|###4 | Sample : 2915/8384 [00:00<00:01, 4036.55it/s]
36%|###5 | Sample : 2985/8384 [00:00<00:01, 4053.54it/s]
36%|###6 | Sample : 3059/8384 [00:00<00:01, 4083.30it/s]
37%|###7 | Sample : 3132/8384 [00:00<00:01, 4108.14it/s]
38%|###8 | Sample : 3207/8384 [00:00<00:01, 4137.63it/s]
39%|###9 | Sample : 3280/8384 [00:00<00:01, 4157.63it/s]
40%|###9 | Sample : 3352/8384 [00:00<00:01, 4173.33it/s]
41%|#### | Sample : 3405/8384 [00:00<00:01, 4122.99it/s]
41%|####1 | Sample : 3455/8384 [00:00<00:01, 4065.80it/s]
42%|####1 | Sample : 3506/8384 [00:00<00:01, 4017.75it/s]
42%|####2 | Sample : 3555/8384 [00:00<00:01, 3964.30it/s]
43%|####2 | Sample : 3603/8384 [00:00<00:01, 3910.12it/s]
44%|####3 | Sample : 3651/8384 [00:00<00:01, 3860.02it/s]
44%|####4 | Sample : 3713/8384 [00:00<00:01, 3860.15it/s]
45%|####4 | Sample : 3766/8384 [00:00<00:01, 3828.11it/s]
46%|####5 | Sample : 3815/8384 [00:00<00:01, 3785.49it/s]
46%|####6 | Sample : 3864/8384 [00:00<00:01, 3744.62it/s]
47%|####6 | Sample : 3912/8384 [00:00<00:01, 3702.77it/s]
47%|####7 | Sample : 3961/8384 [00:01<00:01, 3669.06it/s]
48%|####7 | Sample : 4012/8384 [00:01<00:01, 3641.46it/s]
48%|####8 | Sample : 4061/8384 [00:01<00:01, 3608.69it/s]
49%|####9 | Sample : 4109/8384 [00:01<00:01, 3575.83it/s]
50%|####9 | Sample : 4158/8384 [00:01<00:01, 3547.33it/s]
50%|##### | Sample : 4213/8384 [00:01<00:01, 3537.69it/s]
51%|##### | Sample : 4263/8384 [00:01<00:01, 3516.04it/s]
51%|#####1 | Sample : 4312/8384 [00:01<00:01, 3490.23it/s]
52%|#####2 | Sample : 4360/8384 [00:01<00:01, 3464.31it/s]
53%|#####2 | Sample : 4408/8384 [00:01<00:01, 3439.39it/s]
53%|#####3 | Sample : 4456/8384 [00:01<00:01, 3415.17it/s]
54%|#####3 | Sample : 4504/8384 [00:01<00:01, 3393.28it/s]
54%|#####4 | Sample : 4553/8384 [00:01<00:01, 3374.81it/s]
55%|#####4 | Sample : 4602/8384 [00:01<00:01, 3358.01it/s]
55%|#####5 | Sample : 4651/8384 [00:01<00:01, 3342.14it/s]
56%|#####6 | Sample : 4700/8384 [00:01<00:01, 3325.04it/s]
57%|#####6 | Sample : 4750/8384 [00:01<00:01, 3312.87it/s]
57%|#####7 | Sample : 4798/8384 [00:01<00:01, 3295.57it/s]
58%|#####7 | Sample : 4846/8384 [00:01<00:01, 3278.74it/s]
58%|#####8 | Sample : 4895/8384 [00:01<00:01, 3265.47it/s]
59%|#####8 | Sample : 4945/8384 [00:01<00:01, 3258.29it/s]
60%|#####9 | Sample : 5000/8384 [00:01<00:01, 3265.37it/s]
60%|###### | Sample : 5052/8384 [00:01<00:01, 3263.12it/s]
61%|###### | Sample : 5101/8384 [00:01<00:01, 3249.88it/s]
61%|######1 | Sample : 5152/8384 [00:01<00:00, 3244.22it/s]
62%|######2 | Sample : 5202/8384 [00:01<00:00, 3235.21it/s]
63%|######2 | Sample : 5251/8384 [00:01<00:00, 3224.28it/s]
63%|######3 | Sample : 5300/8384 [00:01<00:00, 3214.85it/s]
64%|######3 | Sample : 5349/8384 [00:01<00:00, 3204.37it/s]
64%|######4 | Sample : 5399/8384 [00:01<00:00, 3200.11it/s]
65%|######5 | Sample : 5461/8384 [00:01<00:00, 3233.30it/s]
66%|######5 | Sample : 5531/8384 [00:01<00:00, 3288.23it/s]
67%|######6 | Sample : 5599/8384 [00:01<00:00, 3335.43it/s]
68%|######7 | Sample : 5673/8384 [00:01<00:00, 3397.86it/s]
68%|######8 | Sample : 5742/8384 [00:01<00:00, 3442.72it/s]
69%|######9 | Sample : 5809/8384 [00:01<00:00, 3479.45it/s]
70%|####### | Sample : 5876/8384 [00:01<00:00, 3514.01it/s]
71%|####### | Sample : 5941/8384 [00:01<00:00, 3540.62it/s]
72%|#######1 | Sample : 6009/8384 [00:01<00:00, 3573.21it/s]
72%|#######2 | Sample : 6076/8384 [00:01<00:00, 3601.65it/s]
73%|#######3 | Sample : 6141/8384 [00:01<00:00, 3623.16it/s]
74%|#######3 | Sample : 6203/8384 [00:01<00:00, 3635.20it/s]
75%|#######4 | Sample : 6274/8384 [00:01<00:00, 3674.77it/s]
76%|#######5 | Sample : 6344/8384 [00:01<00:00, 3709.24it/s]
76%|#######6 | Sample : 6413/8384 [00:01<00:00, 3737.28it/s]
77%|#######7 | Sample : 6483/8384 [00:01<00:00, 3767.44it/s]
78%|#######8 | Sample : 6553/8384 [00:01<00:00, 3794.77it/s]
79%|#######8 | Sample : 6602/8384 [00:01<00:00, 3756.74it/s]
79%|#######9 | Sample : 6648/8384 [00:01<00:00, 3710.94it/s]
80%|#######9 | Sample : 6694/8384 [00:01<00:00, 3665.90it/s]
80%|######## | Sample : 6739/8384 [00:01<00:00, 3623.31it/s]
81%|########1 | Sample : 6802/8384 [00:01<00:00, 3636.99it/s]
82%|########2 | Sample : 6875/8384 [00:01<00:00, 3682.95it/s]
83%|########2 | Sample : 6948/8384 [00:01<00:00, 3726.14it/s]
84%|########3 | Sample : 7021/8384 [00:01<00:00, 3765.24it/s]
85%|########4 | Sample : 7094/8384 [00:01<00:00, 3804.26it/s]
85%|########5 | Sample : 7167/8384 [00:01<00:00, 3841.45it/s]
86%|########6 | Sample : 7238/8384 [00:01<00:00, 3869.98it/s]
87%|########7 | Sample : 7310/8384 [00:01<00:00, 3899.86it/s]
88%|########8 | Sample : 7383/8384 [00:01<00:00, 3932.76it/s]
89%|########8 | Sample : 7452/8384 [00:01<00:00, 3950.01it/s]
90%|########9 | Sample : 7519/8384 [00:01<00:00, 3961.34it/s]
90%|######### | Sample : 7586/8384 [00:02<00:00, 3969.27it/s]
91%|#########1| Sample : 7652/8384 [00:02<00:00, 3976.80it/s]
92%|#########2| Sample : 7719/8384 [00:02<00:00, 3985.43it/s]
93%|#########2| Sample : 7787/8384 [00:02<00:00, 3998.50it/s]
94%|#########3| Sample : 7854/8384 [00:02<00:00, 4005.94it/s]
94%|#########4| Sample : 7922/8384 [00:02<00:00, 4015.76it/s]
95%|#########5| Sample : 7990/8384 [00:02<00:00, 4027.05it/s]
96%|#########6| Sample : 8056/8384 [00:02<00:00, 4030.74it/s]
97%|#########6| Sample : 8125/8384 [00:02<00:00, 4042.16it/s]
98%|#########7| Sample : 8194/8384 [00:02<00:00, 4054.09it/s]
99%|#########8| Sample : 8263/8384 [00:02<00:00, 4065.08it/s]
99%|#########9| Sample : 8331/8384 [00:02<00:00, 4071.90it/s]
100%|##########| Sample : 8384/8384 [00:02<00:00, 3808.79it/s]
split 3 / 3
Fitting 1 epochs, 128 channels
0%| | Sample : 0/8384 [00:00<?, ?it/s]
0%| | Sample : 1/8384 [00:00<03:49, 36.53it/s]
1%| | Sample : 48/8384 [00:00<00:07, 1138.86it/s]
1%|1 | Sample : 94/8384 [00:00<00:05, 1631.01it/s]
2%|1 | Sample : 141/8384 [00:00<00:04, 1928.29it/s]
2%|2 | Sample : 189/8384 [00:00<00:03, 2130.75it/s]
3%|2 | Sample : 236/8384 [00:00<00:03, 2262.89it/s]
3%|3 | Sample : 283/8384 [00:00<00:03, 2360.21it/s]
4%|3 | Sample : 334/8384 [00:00<00:03, 2471.03it/s]
5%|4 | Sample : 399/8384 [00:00<00:02, 2671.41it/s]
5%|5 | Sample : 447/8384 [00:00<00:02, 2706.42it/s]
6%|5 | Sample : 495/8384 [00:00<00:02, 2733.54it/s]
6%|6 | Sample : 543/8384 [00:00<00:02, 2756.35it/s]
7%|7 | Sample : 591/8384 [00:00<00:02, 2776.71it/s]
8%|7 | Sample : 640/8384 [00:00<00:02, 2797.21it/s]
8%|8 | Sample : 698/8384 [00:00<00:02, 2871.13it/s]
9%|9 | Sample : 760/8384 [00:00<00:02, 2956.39it/s]
10%|9 | Sample : 828/8384 [00:00<00:02, 3061.45it/s]
11%|# | Sample : 899/8384 [00:00<00:02, 3171.30it/s]
12%|#1 | Sample : 974/8384 [00:00<00:02, 3288.08it/s]
12%|#2 | Sample : 1046/8384 [00:00<00:02, 3379.19it/s]
13%|#3 | Sample : 1120/8384 [00:00<00:02, 3470.51it/s]
14%|#4 | Sample : 1185/8384 [00:00<00:02, 3509.46it/s]
15%|#4 | Sample : 1257/8384 [00:00<00:01, 3576.37it/s]
16%|#5 | Sample : 1332/8384 [00:00<00:01, 3652.81it/s]
17%|#6 | Sample : 1405/8384 [00:00<00:01, 3714.39it/s]
17%|#7 | Sample : 1467/8384 [00:00<00:01, 3720.77it/s]
18%|#8 | Sample : 1526/8384 [00:00<00:01, 3718.13it/s]
19%|#8 | Sample : 1584/8384 [00:00<00:01, 3710.94it/s]
20%|#9 | Sample : 1647/8384 [00:00<00:01, 3722.44it/s]
20%|## | Sample : 1705/8384 [00:00<00:01, 3715.90it/s]
21%|##1 | Sample : 1764/8384 [00:00<00:01, 3711.99it/s]
22%|##1 | Sample : 1831/8384 [00:00<00:01, 3739.62it/s]
22%|##2 | Sample : 1886/8384 [00:00<00:01, 3720.46it/s]
23%|##3 | Sample : 1954/8384 [00:00<00:01, 3748.93it/s]
24%|##4 | Sample : 2024/8384 [00:00<00:01, 3783.08it/s]
25%|##4 | Sample : 2081/8384 [00:00<00:01, 3767.36it/s]
26%|##5 | Sample : 2140/8384 [00:00<00:01, 3759.65it/s]
26%|##6 | Sample : 2195/8384 [00:00<00:01, 3738.35it/s]
27%|##6 | Sample : 2259/8384 [00:00<00:01, 3750.33it/s]
28%|##7 | Sample : 2321/8384 [00:00<00:01, 3754.67it/s]
28%|##8 | Sample : 2384/8384 [00:00<00:01, 3764.85it/s]
29%|##9 | Sample : 2458/8384 [00:00<00:01, 3811.78it/s]
30%|### | Sample : 2517/8384 [00:00<00:01, 3802.72it/s]
31%|### | Sample : 2578/8384 [00:00<00:01, 3799.85it/s]
32%|###1 | Sample : 2647/8384 [00:00<00:01, 3825.57it/s]
32%|###2 | Sample : 2720/8384 [00:00<00:01, 3864.64it/s]
33%|###3 | Sample : 2785/8384 [00:00<00:01, 3873.67it/s]
34%|###4 | Sample : 2854/8384 [00:00<00:01, 3897.23it/s]
35%|###4 | Sample : 2929/8384 [00:00<00:01, 3937.78it/s]
36%|###5 | Sample : 3005/8384 [00:00<00:01, 3979.15it/s]
37%|###6 | Sample : 3079/8384 [00:00<00:01, 4012.91it/s]
38%|###7 | Sample : 3154/8384 [00:00<00:01, 4046.17it/s]
39%|###8 | Sample : 3228/8384 [00:00<00:01, 4074.67it/s]
39%|###9 | Sample : 3305/8384 [00:00<00:01, 4113.55it/s]
40%|#### | Sample : 3380/8384 [00:00<00:01, 4141.40it/s]
41%|####1 | Sample : 3454/8384 [00:00<00:01, 4163.94it/s]
42%|####2 | Sample : 3527/8384 [00:00<00:01, 4182.81it/s]
43%|####2 | Sample : 3602/8384 [00:00<00:01, 4208.46it/s]
44%|####3 | Sample : 3678/8384 [00:00<00:01, 4235.40it/s]
45%|####4 | Sample : 3753/8384 [00:00<00:01, 4256.71it/s]
46%|####5 | Sample : 3825/8384 [00:00<00:01, 4267.16it/s]
46%|####6 | Sample : 3887/8384 [00:01<00:01, 4246.63it/s]
47%|####7 | Sample : 3941/8384 [00:01<00:01, 4201.53it/s]
48%|####7 | Sample : 3997/8384 [00:01<00:01, 4163.55it/s]
48%|####8 | Sample : 4058/8384 [00:01<00:01, 4144.15it/s]
49%|####9 | Sample : 4112/8384 [00:01<00:01, 4104.47it/s]
50%|####9 | Sample : 4169/8384 [00:01<00:01, 4073.62it/s]
50%|##### | Sample : 4221/8384 [00:01<00:01, 4028.45it/s]
51%|#####1 | Sample : 4280/8384 [00:01<00:01, 4010.59it/s]
52%|#####1 | Sample : 4346/8384 [00:01<00:01, 4014.77it/s]
53%|#####2 | Sample : 4410/8384 [00:01<00:00, 4013.23it/s]
53%|#####3 | Sample : 4474/8384 [00:01<00:00, 4011.65it/s]
54%|#####4 | Sample : 4549/8384 [00:01<00:00, 4043.88it/s]
55%|#####5 | Sample : 4623/8384 [00:01<00:00, 4073.07it/s]
56%|#####6 | Sample : 4698/8384 [00:01<00:00, 4103.29it/s]
57%|#####6 | Sample : 4773/8384 [00:01<00:00, 4132.14it/s]
58%|#####7 | Sample : 4849/8384 [00:01<00:00, 4161.06it/s]
59%|#####8 | Sample : 4922/8384 [00:01<00:00, 4180.73it/s]
60%|#####9 | Sample : 4999/8384 [00:01<00:00, 4212.36it/s]
60%|###### | Sample : 5071/8384 [00:01<00:00, 4225.50it/s]
61%|######1 | Sample : 5147/8384 [00:01<00:00, 4251.85it/s]
62%|######2 | Sample : 5225/8384 [00:01<00:00, 4280.25it/s]
63%|######3 | Sample : 5300/8384 [00:01<00:00, 4298.37it/s]
64%|######4 | Sample : 5374/8384 [00:01<00:00, 4311.82it/s]
65%|######5 | Sample : 5450/8384 [00:01<00:00, 4333.35it/s]
66%|######5 | Sample : 5526/8384 [00:01<00:00, 4352.29it/s]
67%|######6 | Sample : 5603/8384 [00:01<00:00, 4372.72it/s]
68%|######7 | Sample : 5674/8384 [00:01<00:00, 4374.66it/s]
68%|######8 | Sample : 5726/8384 [00:01<00:00, 4314.66it/s]
69%|######9 | Sample : 5801/8384 [00:01<00:00, 4330.91it/s]
70%|####### | Sample : 5878/8384 [00:01<00:00, 4354.43it/s]
71%|####### | Sample : 5952/8384 [00:01<00:00, 4365.79it/s]
72%|#######1 | Sample : 6027/8384 [00:01<00:00, 4379.97it/s]
73%|#######2 | Sample : 6100/8384 [00:01<00:00, 4386.72it/s]
74%|#######3 | Sample : 6174/8384 [00:01<00:00, 4396.58it/s]
75%|#######4 | Sample : 6250/8384 [00:01<00:00, 4413.89it/s]
75%|#######5 | Sample : 6327/8384 [00:01<00:00, 4431.04it/s]
76%|#######6 | Sample : 6383/8384 [00:01<00:00, 4382.99it/s]
77%|#######6 | Sample : 6438/8384 [00:01<00:00, 4333.19it/s]
77%|#######7 | Sample : 6495/8384 [00:01<00:00, 4294.08it/s]
78%|#######8 | Sample : 6557/8384 [00:01<00:00, 4272.69it/s]
79%|#######8 | Sample : 6610/8384 [00:01<00:00, 4222.08it/s]
79%|#######9 | Sample : 6663/8384 [00:01<00:00, 4175.85it/s]
80%|######## | Sample : 6726/8384 [00:01<00:00, 4163.49it/s]
81%|######## | Sample : 6783/8384 [00:01<00:00, 4133.03it/s]
82%|########1 | Sample : 6837/8384 [00:01<00:00, 4094.38it/s]
82%|########2 | Sample : 6896/8384 [00:01<00:00, 4072.61it/s]
83%|########3 | Sample : 6961/8384 [00:01<00:00, 4071.45it/s]
84%|########3 | Sample : 7022/8384 [00:01<00:00, 4056.18it/s]
84%|########4 | Sample : 7082/8384 [00:01<00:00, 4038.17it/s]
85%|########5 | Sample : 7142/8384 [00:01<00:00, 4021.73it/s]
86%|########5 | Sample : 7204/8384 [00:01<00:00, 4013.94it/s]
87%|########6 | Sample : 7262/8384 [00:01<00:00, 3991.49it/s]
87%|########7 | Sample : 7317/8384 [00:01<00:00, 3961.02it/s]
88%|########7 | Sample : 7374/8384 [00:01<00:00, 3939.36it/s]
89%|########8 | Sample : 7432/8384 [00:01<00:00, 3923.43it/s]
89%|########9 | Sample : 7485/8384 [00:01<00:00, 3890.93it/s]
90%|######### | Sample : 7550/8384 [00:01<00:00, 3899.06it/s]
91%|######### | Sample : 7606/8384 [00:01<00:00, 3878.90it/s]
91%|#########1| Sample : 7665/8384 [00:01<00:00, 3868.88it/s]
92%|#########2| Sample : 7738/8384 [00:01<00:00, 3902.38it/s]
93%|#########2| Sample : 7792/8384 [00:01<00:00, 3873.17it/s]
94%|#########3| Sample : 7856/8384 [00:01<00:00, 3878.83it/s]
95%|#########4| Sample : 7928/8384 [00:02<00:00, 3907.89it/s]
95%|#########5| Sample : 8003/8384 [00:02<00:00, 3944.63it/s]
96%|#########6| Sample : 8074/8384 [00:02<00:00, 3968.96it/s]
97%|#########7| Sample : 8148/8384 [00:02<00:00, 4001.17it/s]
98%|#########8| Sample : 8223/8384 [00:02<00:00, 4034.82it/s]
99%|#########8| Sample : 8284/8384 [00:02<00:00, 4022.18it/s]
99%|#########9| Sample : 8340/8384 [00:02<00:00, 3993.35it/s]
100%|##########| Sample : 8384/8384 [00:02<00:00, 3959.73it/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 21.335 seconds)

**Estimated memory usage:** 18 MB