Note
Click here to download the full example code
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
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.
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.91s/it]
100%|##########| Sample : 2/2 [00:01<00:00, 1.06it/s]
100%|##########| Sample : 2/2 [00:01<00:00, 1.03it/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, 53.81it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 52.41it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 51.95it/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, 51.31it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 51.51it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 51.07it/s]
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()
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<04:35, 30.42it/s]
1%| | Sample : 78/8384 [00:00<00:05, 1647.12it/s]
2%|1 | Sample : 165/8384 [00:00<00:03, 2638.66it/s]
3%|3 | Sample : 257/8384 [00:00<00:02, 3309.13it/s]
4%|4 | Sample : 350/8384 [00:00<00:02, 3772.37it/s]
5%|5 | Sample : 443/8384 [00:00<00:01, 4102.57it/s]
6%|6 | Sample : 536/8384 [00:00<00:01, 4352.18it/s]
8%|7 | Sample : 631/8384 [00:00<00:01, 4562.66it/s]
9%|8 | Sample : 725/8384 [00:00<00:01, 4721.26it/s]
10%|9 | Sample : 819/8384 [00:00<00:01, 4849.50it/s]
11%|# | Sample : 914/8384 [00:00<00:01, 4965.66it/s]
12%|#2 | Sample : 1009/8384 [00:00<00:01, 5064.46it/s]
13%|#3 | Sample : 1105/8384 [00:00<00:01, 5153.59it/s]
14%|#4 | Sample : 1201/8384 [00:00<00:01, 5229.33it/s]
15%|#5 | Sample : 1297/8384 [00:00<00:01, 5296.12it/s]
17%|#6 | Sample : 1391/8384 [00:00<00:01, 5344.84it/s]
18%|#7 | Sample : 1484/8384 [00:00<00:01, 5381.45it/s]
19%|#8 | Sample : 1577/8384 [00:00<00:01, 5414.33it/s]
20%|#9 | Sample : 1667/8384 [00:00<00:01, 5428.93it/s]
21%|## | Sample : 1760/8384 [00:00<00:01, 5455.61it/s]
22%|##2 | Sample : 1854/8384 [00:00<00:01, 5484.41it/s]
23%|##3 | Sample : 1947/8384 [00:00<00:01, 5504.01it/s]
24%|##4 | Sample : 2040/8384 [00:00<00:01, 5524.40it/s]
25%|##5 | Sample : 2130/8384 [00:00<00:01, 5531.27it/s]
26%|##6 | Sample : 2221/8384 [00:00<00:01, 5539.54it/s]
28%|##7 | Sample : 2313/8384 [00:00<00:01, 5549.85it/s]
29%|##8 | Sample : 2404/8384 [00:00<00:01, 5555.97it/s]
30%|##9 | Sample : 2498/8384 [00:00<00:01, 5575.74it/s]
31%|### | Sample : 2593/8384 [00:00<00:01, 5596.14it/s]
32%|###2 | Sample : 2686/8384 [00:00<00:01, 5607.86it/s]
33%|###3 | Sample : 2781/8384 [00:00<00:00, 5627.18it/s]
34%|###4 | Sample : 2877/8384 [00:00<00:00, 5646.65it/s]
35%|###5 | Sample : 2971/8384 [00:00<00:00, 5659.80it/s]
37%|###6 | Sample : 3067/8384 [00:00<00:00, 5677.66it/s]
38%|###7 | Sample : 3163/8384 [00:00<00:00, 5695.95it/s]
39%|###8 | Sample : 3256/8384 [00:00<00:00, 5701.76it/s]
40%|###9 | Sample : 3352/8384 [00:00<00:00, 5716.43it/s]
41%|####1 | Sample : 3449/8384 [00:00<00:00, 5733.56it/s]
42%|####2 | Sample : 3542/8384 [00:00<00:00, 5736.02it/s]
43%|####3 | Sample : 3636/8384 [00:00<00:00, 5741.29it/s]
45%|####4 | Sample : 3733/8384 [00:00<00:00, 5757.49it/s]
46%|####5 | Sample : 3829/8384 [00:00<00:00, 5769.59it/s]
47%|####6 | Sample : 3926/8384 [00:00<00:00, 5785.37it/s]
48%|####7 | Sample : 4022/8384 [00:00<00:00, 5796.05it/s]
49%|####9 | Sample : 4117/8384 [00:00<00:00, 5802.97it/s]
50%|##### | Sample : 4211/8384 [00:00<00:00, 5806.13it/s]
51%|#####1 | Sample : 4304/8384 [00:00<00:00, 5804.01it/s]
52%|#####2 | Sample : 4399/8384 [00:00<00:00, 5808.94it/s]
54%|#####3 | Sample : 4495/8384 [00:00<00:00, 5817.09it/s]
55%|#####4 | Sample : 4592/8384 [00:00<00:00, 5827.75it/s]
56%|#####5 | Sample : 4686/8384 [00:00<00:00, 5829.65it/s]
57%|#####7 | Sample : 4782/8384 [00:00<00:00, 5836.40it/s]
58%|#####8 | Sample : 4877/8384 [00:00<00:00, 5840.38it/s]
59%|#####9 | Sample : 4972/8384 [00:00<00:00, 5843.08it/s]
60%|###### | Sample : 5065/8384 [00:00<00:00, 5840.33it/s]
62%|######1 | Sample : 5159/8384 [00:00<00:00, 5840.63it/s]
63%|######2 | Sample : 5253/8384 [00:00<00:00, 5840.40it/s]
64%|######3 | Sample : 5346/8384 [00:00<00:00, 5837.72it/s]
65%|######4 | Sample : 5441/8384 [00:00<00:00, 5840.05it/s]
66%|######5 | Sample : 5532/8384 [00:00<00:00, 5829.23it/s]
67%|######7 | Sample : 5625/8384 [00:00<00:00, 5828.00it/s]
68%|######8 | Sample : 5722/8384 [00:01<00:00, 5838.04it/s]
69%|######9 | Sample : 5818/8384 [00:01<00:00, 5844.27it/s]
71%|####### | Sample : 5912/8384 [00:01<00:00, 5843.45it/s]
72%|#######1 | Sample : 6007/8384 [00:01<00:00, 5846.65it/s]
73%|#######2 | Sample : 6104/8384 [00:01<00:00, 5857.39it/s]
74%|#######3 | Sample : 6203/8384 [00:01<00:00, 5872.67it/s]
75%|#######5 | Sample : 6298/8384 [00:01<00:00, 5875.16it/s]
76%|#######6 | Sample : 6393/8384 [00:01<00:00, 5877.42it/s]
77%|#######7 | Sample : 6491/8384 [00:01<00:00, 5888.37it/s]
79%|#######8 | Sample : 6586/8384 [00:01<00:00, 5888.12it/s]
80%|#######9 | Sample : 6684/8384 [00:01<00:00, 5898.41it/s]
81%|######## | Sample : 6782/8384 [00:01<00:00, 5909.48it/s]
82%|########2 | Sample : 6882/8384 [00:01<00:00, 5924.87it/s]
83%|########3 | Sample : 6978/8384 [00:01<00:00, 5925.78it/s]
84%|########4 | Sample : 7075/8384 [00:01<00:00, 5930.99it/s]
86%|########5 | Sample : 7173/8384 [00:01<00:00, 5938.22it/s]
87%|########6 | Sample : 7270/8384 [00:01<00:00, 5942.83it/s]
88%|########7 | Sample : 7367/8384 [00:01<00:00, 5948.05it/s]
89%|########9 | Sample : 7466/8384 [00:01<00:00, 5959.16it/s]
90%|######### | Sample : 7565/8384 [00:01<00:00, 5970.53it/s]
91%|#########1| Sample : 7664/8384 [00:01<00:00, 5980.50it/s]
93%|#########2| Sample : 7760/8384 [00:01<00:00, 5981.27it/s]
94%|#########3| Sample : 7859/8384 [00:01<00:00, 5987.70it/s]
95%|#########4| Sample : 7956/8384 [00:01<00:00, 5991.26it/s]
96%|#########6| Sample : 8054/8384 [00:01<00:00, 5996.51it/s]
97%|#########7| Sample : 8152/8384 [00:01<00:00, 6002.78it/s]
98%|#########8| Sample : 8253/8384 [00:01<00:00, 6016.64it/s]
100%|#########9| Sample : 8350/8384 [00:01<00:00, 6018.13it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 5767.49it/s]
split 2 / 3
Fitting 1 epochs, 128 channels
0%| | Sample : 0/8384 [00:00<?, ?it/s]
0%| | Sample : 1/8384 [00:00<06:29, 21.53it/s]
1%|1 | Sample : 87/8384 [00:00<00:05, 1442.36it/s]
2%|2 | Sample : 173/8384 [00:00<00:03, 2297.53it/s]
3%|3 | Sample : 261/8384 [00:00<00:02, 2893.59it/s]
4%|4 | Sample : 351/8384 [00:00<00:02, 3337.25it/s]
5%|5 | Sample : 441/8384 [00:00<00:02, 3668.70it/s]
6%|6 | Sample : 531/8384 [00:00<00:02, 3925.68it/s]
7%|7 | Sample : 619/8384 [00:00<00:01, 4119.27it/s]
8%|8 | Sample : 709/8384 [00:00<00:01, 4288.23it/s]
10%|9 | Sample : 799/8384 [00:00<00:01, 4430.88it/s]
11%|# | Sample : 889/8384 [00:00<00:01, 4550.96it/s]
12%|#1 | Sample : 981/8384 [00:00<00:01, 4666.04it/s]
13%|#2 | Sample : 1072/8384 [00:00<00:01, 4755.62it/s]
14%|#3 | Sample : 1164/8384 [00:00<00:01, 4843.23it/s]
15%|#4 | Sample : 1254/8384 [00:00<00:01, 4909.58it/s]
16%|#6 | Sample : 1344/8384 [00:00<00:01, 4964.37it/s]
17%|#7 | Sample : 1434/8384 [00:00<00:01, 5016.36it/s]
18%|#8 | Sample : 1521/8384 [00:00<00:01, 5048.47it/s]
19%|#9 | Sample : 1610/8384 [00:00<00:01, 5087.10it/s]
20%|## | Sample : 1702/8384 [00:00<00:01, 5132.95it/s]
21%|##1 | Sample : 1795/8384 [00:00<00:01, 5179.36it/s]
22%|##2 | Sample : 1886/8384 [00:00<00:01, 5211.89it/s]
24%|##3 | Sample : 1976/8384 [00:00<00:01, 5239.88it/s]
25%|##4 | Sample : 2066/8384 [00:00<00:01, 5262.72it/s]
26%|##5 | Sample : 2156/8384 [00:00<00:01, 5285.75it/s]
27%|##6 | Sample : 2248/8384 [00:00<00:01, 5314.93it/s]
28%|##7 | Sample : 2339/8384 [00:00<00:01, 5336.31it/s]
29%|##8 | Sample : 2430/8384 [00:00<00:01, 5357.77it/s]
30%|### | Sample : 2521/8384 [00:00<00:01, 5376.96it/s]
31%|###1 | Sample : 2611/8384 [00:00<00:01, 5388.80it/s]
32%|###2 | Sample : 2703/8384 [00:00<00:01, 5410.52it/s]
33%|###3 | Sample : 2795/8384 [00:00<00:01, 5430.72it/s]
34%|###4 | Sample : 2886/8384 [00:00<00:01, 5443.90it/s]
36%|###5 | Sample : 2978/8384 [00:00<00:00, 5461.67it/s]
37%|###6 | Sample : 3072/8384 [00:00<00:00, 5484.36it/s]
38%|###7 | Sample : 3169/8384 [00:00<00:00, 5515.42it/s]
39%|###8 | Sample : 3268/8384 [00:00<00:00, 5551.63it/s]
40%|#### | Sample : 3363/8384 [00:00<00:00, 5572.74it/s]
41%|####1 | Sample : 3451/8384 [00:00<00:00, 5566.46it/s]
42%|####2 | Sample : 3544/8384 [00:00<00:00, 5579.87it/s]
43%|####3 | Sample : 3639/8384 [00:00<00:00, 5597.76it/s]
45%|####4 | Sample : 3734/8384 [00:00<00:00, 5615.79it/s]
46%|####5 | Sample : 3829/8384 [00:00<00:00, 5630.59it/s]
47%|####6 | Sample : 3921/8384 [00:00<00:00, 5635.15it/s]
48%|####7 | Sample : 4016/8384 [00:00<00:00, 5649.29it/s]
49%|####9 | Sample : 4112/8384 [00:00<00:00, 5666.03it/s]
50%|##### | Sample : 4208/8384 [00:00<00:00, 5682.12it/s]
51%|#####1 | Sample : 4303/8384 [00:00<00:00, 5693.26it/s]
52%|#####2 | Sample : 4396/8384 [00:00<00:00, 5697.80it/s]
54%|#####3 | Sample : 4489/8384 [00:00<00:00, 5701.33it/s]
55%|#####4 | Sample : 4586/8384 [00:00<00:00, 5718.03it/s]
56%|#####5 | Sample : 4679/8384 [00:00<00:00, 5720.81it/s]
57%|#####6 | Sample : 4774/8384 [00:00<00:00, 5731.41it/s]
58%|#####8 | Sample : 4870/8384 [00:00<00:00, 5743.41it/s]
59%|#####9 | Sample : 4967/8384 [00:00<00:00, 5758.78it/s]
60%|###### | Sample : 5064/8384 [00:00<00:00, 5772.58it/s]
62%|######1 | Sample : 5159/8384 [00:00<00:00, 5781.08it/s]
63%|######2 | Sample : 5256/8384 [00:00<00:00, 5792.88it/s]
64%|######3 | Sample : 5352/8384 [00:00<00:00, 5801.07it/s]
65%|######5 | Sample : 5451/8384 [00:00<00:00, 5820.05it/s]
66%|######6 | Sample : 5550/8384 [00:01<00:00, 5837.54it/s]
67%|######7 | Sample : 5650/8384 [00:01<00:00, 5857.04it/s]
69%|######8 | Sample : 5748/8384 [00:01<00:00, 5870.54it/s]
70%|######9 | Sample : 5845/8384 [00:01<00:00, 5878.43it/s]
71%|####### | Sample : 5941/8384 [00:01<00:00, 5882.66it/s]
72%|#######1 | Sample : 6036/8384 [00:01<00:00, 5885.47it/s]
73%|#######3 | Sample : 6136/8384 [00:01<00:00, 5901.22it/s]
74%|#######4 | Sample : 6235/8384 [00:01<00:00, 5913.06it/s]
76%|#######5 | Sample : 6331/8384 [00:01<00:00, 5916.35it/s]
77%|#######6 | Sample : 6427/8384 [00:01<00:00, 5919.15it/s]
78%|#######7 | Sample : 6524/8384 [00:01<00:00, 5923.63it/s]
79%|#######8 | Sample : 6612/8384 [00:01<00:00, 5900.92it/s]
80%|#######9 | Sample : 6704/8384 [00:01<00:00, 5890.10it/s]
81%|########1 | Sample : 6795/8384 [00:01<00:00, 5878.60it/s]
82%|########2 | Sample : 6890/8384 [00:01<00:00, 5880.12it/s]
83%|########3 | Sample : 6989/8384 [00:01<00:00, 5893.24it/s]
84%|########4 | Sample : 7084/8384 [00:01<00:00, 5894.29it/s]
86%|########5 | Sample : 7179/8384 [00:01<00:00, 5894.89it/s]
87%|########6 | Sample : 7276/8384 [00:01<00:00, 5902.27it/s]
88%|########7 | Sample : 7370/8384 [00:01<00:00, 5898.86it/s]
89%|########9 | Sample : 7467/8384 [00:01<00:00, 5906.75it/s]
90%|######### | Sample : 7563/8384 [00:01<00:00, 5908.71it/s]
91%|#########1| Sample : 7658/8384 [00:01<00:00, 5908.14it/s]
92%|#########2| Sample : 7755/8384 [00:01<00:00, 5915.74it/s]
94%|#########3| Sample : 7854/8384 [00:01<00:00, 5929.36it/s]
95%|#########4| Sample : 7955/8384 [00:01<00:00, 5947.83it/s]
96%|#########6| Sample : 8057/8384 [00:01<00:00, 5966.20it/s]
97%|#########7| Sample : 8155/8384 [00:01<00:00, 5972.69it/s]
98%|#########8| Sample : 8255/8384 [00:01<00:00, 5986.51it/s]
100%|#########9| Sample : 8356/8384 [00:01<00:00, 6000.83it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 5650.91it/s]
split 3 / 3
Fitting 1 epochs, 128 channels
0%| | Sample : 0/8384 [00:00<?, ?it/s]
0%| | Sample : 1/8384 [00:00<02:44, 50.83it/s]
1%|1 | Sample : 91/8384 [00:00<00:03, 2617.23it/s]
2%|2 | Sample : 182/8384 [00:00<00:02, 3610.23it/s]
3%|3 | Sample : 276/8384 [00:00<00:01, 4181.00it/s]
4%|4 | Sample : 369/8384 [00:00<00:01, 4521.83it/s]
5%|5 | Sample : 461/8384 [00:00<00:01, 4740.57it/s]
7%|6 | Sample : 550/8384 [00:00<00:01, 4870.82it/s]
8%|7 | Sample : 640/8384 [00:00<00:01, 4978.73it/s]
9%|8 | Sample : 730/8384 [00:00<00:01, 5057.40it/s]
10%|9 | Sample : 820/8384 [00:00<00:01, 5122.23it/s]
11%|# | Sample : 909/8384 [00:00<00:01, 5169.44it/s]
12%|#1 | Sample : 999/8384 [00:00<00:01, 5210.53it/s]
13%|#2 | Sample : 1087/8384 [00:00<00:01, 5238.68it/s]
14%|#4 | Sample : 1177/8384 [00:00<00:01, 5271.75it/s]
15%|#5 | Sample : 1267/8384 [00:00<00:01, 5299.14it/s]
16%|#6 | Sample : 1358/8384 [00:00<00:01, 5329.06it/s]
17%|#7 | Sample : 1449/8384 [00:00<00:01, 5354.92it/s]
18%|#8 | Sample : 1540/8384 [00:00<00:01, 5381.81it/s]
19%|#9 | Sample : 1631/8384 [00:00<00:01, 5401.38it/s]
21%|## | Sample : 1720/8384 [00:00<00:01, 5410.85it/s]
22%|##1 | Sample : 1811/8384 [00:00<00:01, 5428.80it/s]
23%|##2 | Sample : 1899/8384 [00:00<00:01, 5433.13it/s]
24%|##3 | Sample : 1988/8384 [00:00<00:01, 5440.42it/s]
25%|##4 | Sample : 2080/8384 [00:00<00:01, 5458.14it/s]
26%|##5 | Sample : 2170/8384 [00:00<00:01, 5466.66it/s]
27%|##6 | Sample : 2259/8384 [00:00<00:01, 5470.62it/s]
28%|##8 | Sample : 2348/8384 [00:00<00:01, 5476.62it/s]
29%|##9 | Sample : 2437/8384 [00:00<00:01, 5479.01it/s]
30%|### | Sample : 2527/8384 [00:00<00:01, 5488.09it/s]
31%|###1 | Sample : 2617/8384 [00:00<00:01, 5494.31it/s]
32%|###2 | Sample : 2707/8384 [00:00<00:01, 5499.27it/s]
33%|###3 | Sample : 2797/8384 [00:00<00:01, 5505.71it/s]
34%|###4 | Sample : 2886/8384 [00:00<00:00, 5508.28it/s]
36%|###5 | Sample : 2979/8384 [00:00<00:00, 5524.02it/s]
37%|###6 | Sample : 3072/8384 [00:00<00:00, 5538.01it/s]
38%|###7 | Sample : 3162/8384 [00:00<00:00, 5540.50it/s]
39%|###8 | Sample : 3252/8384 [00:00<00:00, 5542.01it/s]
40%|###9 | Sample : 3340/8384 [00:00<00:00, 5537.38it/s]
41%|#### | Sample : 3430/8384 [00:00<00:00, 5540.70it/s]
42%|####1 | Sample : 3520/8384 [00:00<00:00, 5543.65it/s]
43%|####3 | Sample : 3607/8384 [00:00<00:00, 5535.21it/s]
44%|####4 | Sample : 3697/8384 [00:00<00:00, 5539.39it/s]
45%|####5 | Sample : 3785/8384 [00:00<00:00, 5535.80it/s]
46%|####6 | Sample : 3874/8384 [00:00<00:00, 5535.26it/s]
47%|####7 | Sample : 3965/8384 [00:00<00:00, 5541.84it/s]
48%|####8 | Sample : 4054/8384 [00:00<00:00, 5542.80it/s]
49%|####9 | Sample : 4143/8384 [00:00<00:00, 5543.24it/s]
50%|##### | Sample : 4231/8384 [00:00<00:00, 5539.79it/s]
52%|#####1 | Sample : 4320/8384 [00:00<00:00, 5539.52it/s]
53%|#####2 | Sample : 4410/8384 [00:00<00:00, 5543.59it/s]
54%|#####3 | Sample : 4500/8384 [00:00<00:00, 5544.93it/s]
55%|#####4 | Sample : 4590/8384 [00:00<00:00, 5546.19it/s]
56%|#####5 | Sample : 4681/8384 [00:00<00:00, 5550.48it/s]
57%|#####6 | Sample : 4774/8384 [00:00<00:00, 5561.63it/s]
58%|#####8 | Sample : 4864/8384 [00:00<00:00, 5563.19it/s]
59%|#####9 | Sample : 4954/8384 [00:00<00:00, 5564.83it/s]
60%|###### | Sample : 5043/8384 [00:00<00:00, 5564.09it/s]
61%|######1 | Sample : 5134/8384 [00:00<00:00, 5568.67it/s]
62%|######2 | Sample : 5222/8384 [00:00<00:00, 5564.17it/s]
63%|######3 | Sample : 5308/8384 [00:00<00:00, 5552.42it/s]
64%|######4 | Sample : 5397/8384 [00:00<00:00, 5552.15it/s]
65%|######5 | Sample : 5485/8384 [00:01<00:00, 5548.17it/s]
66%|######6 | Sample : 5575/8384 [00:01<00:00, 5551.57it/s]
68%|######7 | Sample : 5664/8384 [00:01<00:00, 5550.82it/s]
69%|######8 | Sample : 5752/8384 [00:01<00:00, 5545.36it/s]
70%|######9 | Sample : 5839/8384 [00:01<00:00, 5539.50it/s]
71%|####### | Sample : 5928/8384 [00:01<00:00, 5537.90it/s]
72%|#######1 | Sample : 6016/8384 [00:01<00:00, 5535.60it/s]
73%|#######2 | Sample : 6103/8384 [00:01<00:00, 5528.88it/s]
74%|#######3 | Sample : 6192/8384 [00:01<00:00, 5529.76it/s]
75%|#######4 | Sample : 6281/8384 [00:01<00:00, 5529.53it/s]
76%|#######5 | Sample : 6368/8384 [00:01<00:00, 5523.63it/s]
77%|#######7 | Sample : 6458/8384 [00:01<00:00, 5527.76it/s]
78%|#######8 | Sample : 6547/8384 [00:01<00:00, 5528.12it/s]
79%|#######9 | Sample : 6637/8384 [00:01<00:00, 5530.09it/s]
80%|######## | Sample : 6728/8384 [00:01<00:00, 5535.10it/s]
81%|########1 | Sample : 6820/8384 [00:01<00:00, 5543.42it/s]
82%|########2 | Sample : 6913/8384 [00:01<00:00, 5555.29it/s]
84%|########3 | Sample : 7008/8384 [00:01<00:00, 5572.86it/s]
85%|########4 | Sample : 7102/8384 [00:01<00:00, 5586.84it/s]
86%|########5 | Sample : 7195/8384 [00:01<00:00, 5596.35it/s]
87%|########6 | Sample : 7290/8384 [00:01<00:00, 5613.40it/s]
88%|########8 | Sample : 7385/8384 [00:01<00:00, 5626.82it/s]
89%|########9 | Sample : 7479/8384 [00:01<00:00, 5638.44it/s]
90%|######### | Sample : 7576/8384 [00:01<00:00, 5659.14it/s]
92%|#########1| Sample : 7673/8384 [00:01<00:00, 5677.86it/s]
93%|#########2| Sample : 7770/8384 [00:01<00:00, 5694.24it/s]
94%|#########3| Sample : 7865/8384 [00:01<00:00, 5705.54it/s]
95%|#########5| Sample : 7965/8384 [00:01<00:00, 5730.11it/s]
96%|#########6| Sample : 8063/8384 [00:01<00:00, 5747.27it/s]
97%|#########7| Sample : 8162/8384 [00:01<00:00, 5766.71it/s]
99%|#########8| Sample : 8260/8384 [00:01<00:00, 5782.85it/s]
100%|#########9| Sample : 8358/8384 [00:01<00:00, 5799.76it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 5570.27it/s]
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()
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()
Total running time of the script: ( 0 minutes 19.326 seconds)
Estimated memory usage: 51 MB