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)
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()
Out:
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()
Out:
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.41s/it]
100%|##########| Sample : 2/2 [00:01<00:00, 1.43it/s]
100%|##########| Sample : 2/2 [00:01<00:00, 1.39it/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, 14.14it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 21.36it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 21.01it/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, 21.41it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 28.60it/s]
100%|##########| Sample : 2/2 [00:00<00:00, 28.22it/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()
Out:
split 1 / 3
Fitting 1 epochs, 128 channels
0%| | Sample : 0/8384 [00:00<?, ?it/s]
0%| | Sample : 1/8384 [00:00<04:36, 30.32it/s]
1%|1 | Sample : 100/8384 [00:00<00:03, 2107.97it/s]
2%|2 | Sample : 209/8384 [00:00<00:02, 3334.55it/s]
4%|3 | Sample : 323/8384 [00:00<00:01, 4146.48it/s]
5%|5 | Sample : 426/8384 [00:00<00:01, 4565.55it/s]
6%|6 | Sample : 526/8384 [00:00<00:01, 4837.50it/s]
8%|7 | Sample : 632/8384 [00:00<00:01, 5093.67it/s]
9%|8 | Sample : 741/8384 [00:00<00:01, 5320.99it/s]
10%|# | Sample : 853/8384 [00:00<00:01, 5525.55it/s]
12%|#1 | Sample : 968/8384 [00:00<00:01, 5710.77it/s]
13%|#2 | Sample : 1072/8384 [00:00<00:01, 5794.43it/s]
14%|#4 | Sample : 1174/8384 [00:00<00:01, 5851.27it/s]
15%|#5 | Sample : 1281/8384 [00:00<00:01, 5928.01it/s]
17%|#6 | Sample : 1393/8384 [00:00<00:01, 6022.45it/s]
18%|#7 | Sample : 1508/8384 [00:00<00:01, 6125.49it/s]
19%|#9 | Sample : 1625/8384 [00:00<00:01, 6225.85it/s]
21%|## | Sample : 1733/8384 [00:00<00:01, 6267.95it/s]
22%|##1 | Sample : 1836/8384 [00:00<00:01, 6281.05it/s]
23%|##3 | Sample : 1943/8384 [00:00<00:01, 6309.28it/s]
24%|##4 | Sample : 2054/8384 [00:00<00:00, 6353.41it/s]
26%|##5 | Sample : 2168/8384 [00:00<00:00, 6407.13it/s]
27%|##7 | Sample : 2286/8384 [00:00<00:00, 6472.92it/s]
29%|##8 | Sample : 2397/8384 [00:00<00:00, 6503.41it/s]
30%|##9 | Sample : 2501/8384 [00:00<00:00, 6501.05it/s]
31%|###1 | Sample : 2605/8384 [00:00<00:00, 6500.81it/s]
32%|###2 | Sample : 2714/8384 [00:00<00:00, 6519.28it/s]
34%|###3 | Sample : 2830/8384 [00:00<00:00, 6563.68it/s]
35%|###5 | Sample : 2946/8384 [00:00<00:00, 6604.78it/s]
36%|###6 | Sample : 3060/8384 [00:00<00:00, 6634.70it/s]
38%|###7 | Sample : 3163/8384 [00:00<00:00, 6621.50it/s]
39%|###8 | Sample : 3268/8384 [00:00<00:00, 6617.25it/s]
40%|#### | Sample : 3378/8384 [00:00<00:00, 6631.35it/s]
42%|####1 | Sample : 3491/8384 [00:00<00:00, 6656.81it/s]
43%|####3 | Sample : 3609/8384 [00:00<00:00, 6696.73it/s]
44%|####4 | Sample : 3725/8384 [00:00<00:00, 6728.93it/s]
46%|####5 | Sample : 3828/8384 [00:00<00:00, 6711.09it/s]
47%|####6 | Sample : 3933/8384 [00:00<00:00, 6700.25it/s]
48%|####8 | Sample : 4042/8384 [00:00<00:00, 6705.35it/s]
50%|####9 | Sample : 4156/8384 [00:00<00:00, 6725.95it/s]
51%|##### | Sample : 4270/8384 [00:00<00:00, 6747.19it/s]
52%|#####2 | Sample : 4388/8384 [00:00<00:00, 6780.61it/s]
54%|#####3 | Sample : 4495/8384 [00:00<00:00, 6772.71it/s]
55%|#####4 | Sample : 4599/8384 [00:00<00:00, 6754.96it/s]
56%|#####6 | Sample : 4709/8384 [00:00<00:00, 6758.80it/s]
58%|#####7 | Sample : 4822/8384 [00:00<00:00, 6773.97it/s]
59%|#####8 | Sample : 4937/8384 [00:00<00:00, 6796.36it/s]
60%|###### | Sample : 5054/8384 [00:00<00:00, 6821.63it/s]
62%|######1 | Sample : 5164/8384 [00:00<00:00, 6824.29it/s]
63%|######2 | Sample : 5269/8384 [00:00<00:00, 6808.56it/s]
64%|######4 | Sample : 5378/8384 [00:00<00:00, 6808.74it/s]
65%|######5 | Sample : 5490/8384 [00:00<00:00, 6817.79it/s]
67%|######6 | Sample : 5607/8384 [00:00<00:00, 6842.01it/s]
68%|######8 | Sample : 5724/8384 [00:00<00:00, 6864.84it/s]
70%|######9 | Sample : 5835/8384 [00:00<00:00, 6867.99it/s]
71%|####### | Sample : 5940/8384 [00:00<00:00, 6848.98it/s]
72%|#######2 | Sample : 6048/8384 [00:00<00:00, 6841.59it/s]
73%|#######3 | Sample : 6161/8384 [00:00<00:00, 6850.26it/s]
75%|#######4 | Sample : 6278/8384 [00:00<00:00, 6871.97it/s]
76%|#######6 | Sample : 6394/8384 [00:00<00:00, 6891.62it/s]
78%|#######7 | Sample : 6509/8384 [00:00<00:00, 6905.48it/s]
79%|#######8 | Sample : 6615/8384 [00:00<00:00, 6889.48it/s]
80%|######## | Sample : 6722/8384 [00:01<00:00, 6878.95it/s]
82%|########1 | Sample : 6834/8384 [00:01<00:00, 6883.00it/s]
83%|########2 | Sample : 6951/8384 [00:01<00:00, 6903.70it/s]
84%|########4 | Sample : 7068/8384 [00:01<00:00, 6923.56it/s]
86%|########5 | Sample : 7186/8384 [00:01<00:00, 6945.94it/s]
87%|########6 | Sample : 7294/8384 [00:01<00:00, 6934.26it/s]
88%|########8 | Sample : 7404/8384 [00:01<00:00, 6929.05it/s]
90%|########9 | Sample : 7519/8384 [00:01<00:00, 6939.31it/s]
91%|#########1| Sample : 7636/8384 [00:01<00:00, 6957.41it/s]
92%|#########2| Sample : 7753/8384 [00:01<00:00, 6973.99it/s]
94%|#########3| Sample : 7872/8384 [00:01<00:00, 6996.45it/s]
95%|#########5| Sample : 7982/8384 [00:01<00:00, 6989.44it/s]
97%|#########6| Sample : 8093/8384 [00:01<00:00, 6984.50it/s]
98%|#########7| Sample : 8208/8384 [00:01<00:00, 6993.71it/s]
99%|#########9| Sample : 8322/8384 [00:01<00:00, 6999.57it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 6723.08it/s]
split 2 / 3
Fitting 1 epochs, 128 channels
0%| | Sample : 0/8384 [00:00<?, ?it/s]
0%| | Sample : 1/8384 [00:00<13:36, 10.26it/s]
1%|1 | Sample : 99/8384 [00:00<00:09, 911.15it/s]
2%|2 | Sample : 201/8384 [00:00<00:04, 1644.53it/s]
4%|3 | Sample : 310/8384 [00:00<00:03, 2283.12it/s]
5%|5 | Sample : 421/8384 [00:00<00:02, 2817.81it/s]
6%|6 | Sample : 537/8384 [00:00<00:02, 3295.82it/s]
8%|7 | Sample : 643/8384 [00:00<00:02, 3634.31it/s]
9%|8 | Sample : 745/8384 [00:00<00:01, 3898.01it/s]
10%|# | Sample : 849/8384 [00:00<00:01, 4138.44it/s]
11%|#1 | Sample : 959/8384 [00:00<00:01, 4378.85it/s]
13%|#2 | Sample : 1072/8384 [00:00<00:01, 4607.66it/s]
14%|#4 | Sample : 1189/8384 [00:00<00:01, 4828.51it/s]
16%|#5 | Sample : 1300/8384 [00:00<00:01, 4996.37it/s]
17%|#6 | Sample : 1402/8384 [00:00<00:01, 5101.01it/s]
18%|#7 | Sample : 1506/8384 [00:00<00:01, 5206.39it/s]
19%|#9 | Sample : 1615/8384 [00:00<00:01, 5322.32it/s]
21%|## | Sample : 1729/8384 [00:00<00:01, 5449.71it/s]
22%|##2 | Sample : 1846/8384 [00:00<00:01, 5580.56it/s]
23%|##3 | Sample : 1958/8384 [00:00<00:01, 5676.50it/s]
25%|##4 | Sample : 2060/8384 [00:00<00:01, 5721.86it/s]
26%|##5 | Sample : 2164/8384 [00:00<00:01, 5772.35it/s]
27%|##7 | Sample : 2272/8384 [00:00<00:01, 5835.31it/s]
28%|##8 | Sample : 2387/8384 [00:00<00:01, 5919.76it/s]
30%|##9 | Sample : 2503/8384 [00:00<00:00, 6003.72it/s]
31%|###1 | Sample : 2618/8384 [00:00<00:00, 6075.11it/s]
32%|###2 | Sample : 2721/8384 [00:00<00:00, 6096.98it/s]
34%|###3 | Sample : 2824/8384 [00:00<00:00, 6115.43it/s]
35%|###4 | Sample : 2933/8384 [00:00<00:00, 6155.50it/s]
36%|###6 | Sample : 3046/8384 [00:00<00:00, 6209.38it/s]
38%|###7 | Sample : 3162/8384 [00:00<00:00, 6270.81it/s]
39%|###9 | Sample : 3281/8384 [00:00<00:00, 6338.32it/s]
40%|#### | Sample : 3387/8384 [00:00<00:00, 6352.49it/s]
42%|####1 | Sample : 3490/8384 [00:00<00:00, 6355.03it/s]
43%|####2 | Sample : 3598/8384 [00:00<00:00, 6375.71it/s]
44%|####4 | Sample : 3710/8384 [00:00<00:00, 6410.71it/s]
46%|####5 | Sample : 3825/8384 [00:00<00:00, 6453.87it/s]
47%|####7 | Sample : 3943/8384 [00:00<00:00, 6503.53it/s]
48%|####8 | Sample : 4053/8384 [00:00<00:00, 6522.68it/s]
50%|####9 | Sample : 4156/8384 [00:00<00:00, 6514.84it/s]
51%|##### | Sample : 4263/8384 [00:00<00:00, 6522.77it/s]
52%|#####2 | Sample : 4375/8384 [00:00<00:00, 6547.33it/s]
54%|#####3 | Sample : 4490/8384 [00:00<00:00, 6579.37it/s]
55%|#####4 | Sample : 4607/8384 [00:00<00:00, 6618.10it/s]
56%|#####6 | Sample : 4720/8384 [00:00<00:00, 6640.78it/s]
58%|#####7 | Sample : 4823/8384 [00:00<00:00, 6628.64it/s]
59%|#####8 | Sample : 4929/8384 [00:00<00:00, 6626.69it/s]
60%|###### | Sample : 5040/8384 [00:00<00:00, 6641.62it/s]
61%|######1 | Sample : 5155/8384 [00:00<00:00, 6670.56it/s]
63%|######2 | Sample : 5272/8384 [00:00<00:00, 6703.48it/s]
64%|######4 | Sample : 5383/8384 [00:00<00:00, 6713.09it/s]
65%|######5 | Sample : 5487/8384 [00:00<00:00, 6699.19it/s]
67%|######6 | Sample : 5593/8384 [00:00<00:00, 6693.20it/s]
68%|######8 | Sample : 5704/8384 [00:00<00:00, 6703.02it/s]
69%|######9 | Sample : 5817/8384 [00:00<00:00, 6719.26it/s]
71%|####### | Sample : 5934/8384 [00:00<00:00, 6749.94it/s]
72%|#######2 | Sample : 6052/8384 [00:00<00:00, 6779.62it/s]
73%|#######3 | Sample : 6157/8384 [00:00<00:00, 6768.23it/s]
75%|#######4 | Sample : 6263/8384 [00:01<00:00, 6759.91it/s]
76%|#######6 | Sample : 6374/8384 [00:01<00:00, 6767.34it/s]
77%|#######7 | Sample : 6490/8384 [00:01<00:00, 6791.63it/s]
79%|#######8 | Sample : 6607/8384 [00:01<00:00, 6816.04it/s]
80%|######## | Sample : 6725/8384 [00:01<00:00, 6842.18it/s]
82%|########1 | Sample : 6834/8384 [00:01<00:00, 6840.24it/s]
83%|########2 | Sample : 6940/8384 [00:01<00:00, 6828.18it/s]
84%|########4 | Sample : 7053/8384 [00:01<00:00, 6837.87it/s]
85%|########5 | Sample : 7168/8384 [00:01<00:00, 6855.28it/s]
87%|########6 | Sample : 7285/8384 [00:01<00:00, 6877.70it/s]
88%|########8 | Sample : 7402/8384 [00:01<00:00, 6898.74it/s]
90%|########9 | Sample : 7515/8384 [00:01<00:00, 6906.61it/s]
91%|######### | Sample : 7623/8384 [00:01<00:00, 6895.76it/s]
92%|#########2| Sample : 7734/8384 [00:01<00:00, 6897.25it/s]
94%|#########3| Sample : 7850/8384 [00:01<00:00, 6912.95it/s]
95%|#########5| Sample : 7966/8384 [00:01<00:00, 6928.07it/s]
96%|#########6| Sample : 8084/8384 [00:01<00:00, 6950.54it/s]
98%|#########7| Sample : 8200/8384 [00:01<00:00, 6963.97it/s]
99%|#########9| Sample : 8311/8384 [00:01<00:00, 6961.90it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 6387.48it/s]
split 3 / 3
Fitting 1 epochs, 128 channels
0%| | Sample : 0/8384 [00:00<?, ?it/s]
0%| | Sample : 1/8384 [00:00<11:47, 11.86it/s]
1%|1 | Sample : 99/8384 [00:00<00:08, 1028.69it/s]
2%|2 | Sample : 200/8384 [00:00<00:04, 1814.94it/s]
4%|3 | Sample : 309/8384 [00:00<00:03, 2491.22it/s]
5%|5 | Sample : 423/8384 [00:00<00:02, 3066.85it/s]
6%|6 | Sample : 538/8384 [00:00<00:02, 3544.58it/s]
8%|7 | Sample : 643/8384 [00:00<00:02, 3869.31it/s]
9%|8 | Sample : 745/8384 [00:00<00:01, 4122.52it/s]
10%|# | Sample : 851/8384 [00:00<00:01, 4365.89it/s]
11%|#1 | Sample : 962/8384 [00:00<00:01, 4603.57it/s]
13%|#2 | Sample : 1076/8384 [00:00<00:01, 4828.03it/s]
14%|#4 | Sample : 1193/8384 [00:00<00:01, 5038.45it/s]
16%|#5 | Sample : 1301/8384 [00:00<00:01, 5176.10it/s]
17%|#6 | Sample : 1403/8384 [00:00<00:01, 5270.92it/s]
18%|#7 | Sample : 1508/8384 [00:00<00:01, 5370.33it/s]
19%|#9 | Sample : 1616/8384 [00:00<00:01, 5473.77it/s]
21%|## | Sample : 1731/8384 [00:00<00:01, 5597.04it/s]
22%|##2 | Sample : 1849/8384 [00:00<00:01, 5723.11it/s]
23%|##3 | Sample : 1961/8384 [00:00<00:01, 5811.28it/s]
25%|##4 | Sample : 2064/8384 [00:00<00:01, 5853.65it/s]
26%|##5 | Sample : 2167/8384 [00:00<00:01, 5890.38it/s]
27%|##7 | Sample : 2277/8384 [00:00<00:01, 5953.24it/s]
29%|##8 | Sample : 2392/8384 [00:00<00:00, 6031.23it/s]
30%|##9 | Sample : 2508/8384 [00:00<00:00, 6108.70it/s]
31%|###1 | Sample : 2621/8384 [00:00<00:00, 6166.60it/s]
33%|###2 | Sample : 2725/8384 [00:00<00:00, 6185.55it/s]
34%|###3 | Sample : 2830/8384 [00:00<00:00, 6206.60it/s]
35%|###5 | Sample : 2938/8384 [00:00<00:00, 6238.76it/s]
36%|###6 | Sample : 3052/8384 [00:00<00:00, 6290.24it/s]
38%|###7 | Sample : 3169/8384 [00:00<00:00, 6349.75it/s]
39%|###9 | Sample : 3286/8384 [00:00<00:00, 6403.35it/s]
40%|#### | Sample : 3390/8384 [00:00<00:00, 6408.37it/s]
42%|####1 | Sample : 3494/8384 [00:00<00:00, 6413.49it/s]
43%|####2 | Sample : 3604/8384 [00:00<00:00, 6437.40it/s]
44%|####4 | Sample : 3715/8384 [00:00<00:00, 6463.31it/s]
46%|####5 | Sample : 3830/8384 [00:00<00:00, 6503.75it/s]
47%|####7 | Sample : 3948/8384 [00:00<00:00, 6551.70it/s]
48%|####8 | Sample : 4057/8384 [00:00<00:00, 6562.89it/s]
50%|####9 | Sample : 4160/8384 [00:00<00:00, 6554.31it/s]
51%|##### | Sample : 4269/8384 [00:00<00:00, 6566.32it/s]
52%|#####2 | Sample : 4381/8384 [00:00<00:00, 6589.91it/s]
54%|#####3 | Sample : 4497/8384 [00:00<00:00, 6623.43it/s]
55%|#####5 | Sample : 4615/8384 [00:00<00:00, 6662.05it/s]
56%|#####6 | Sample : 4724/8384 [00:00<00:00, 6667.60it/s]
58%|#####7 | Sample : 4827/8384 [00:00<00:00, 6653.60it/s]
59%|#####8 | Sample : 4934/8384 [00:00<00:00, 6654.30it/s]
60%|###### | Sample : 5045/8384 [00:00<00:00, 6667.74it/s]
62%|######1 | Sample : 5160/8384 [00:00<00:00, 6692.57it/s]
63%|######2 | Sample : 5278/8384 [00:00<00:00, 6725.40it/s]
64%|######4 | Sample : 5392/8384 [00:00<00:00, 6743.56it/s]
66%|######5 | Sample : 5496/8384 [00:00<00:00, 6728.67it/s]
67%|######6 | Sample : 5604/8384 [00:00<00:00, 6729.16it/s]
68%|######8 | Sample : 5716/8384 [00:00<00:00, 6740.69it/s]
70%|######9 | Sample : 5832/8384 [00:00<00:00, 6765.85it/s]
71%|####### | Sample : 5950/8384 [00:00<00:00, 6795.62it/s]
72%|#######2 | Sample : 6064/8384 [00:00<00:00, 6810.44it/s]
74%|#######3 | Sample : 6168/8384 [00:00<00:00, 6792.20it/s]
75%|#######4 | Sample : 6277/8384 [00:01<00:00, 6790.92it/s]
76%|#######6 | Sample : 6388/8384 [00:01<00:00, 6797.62it/s]
78%|#######7 | Sample : 6505/8384 [00:01<00:00, 6823.75it/s]
79%|#######8 | Sample : 6622/8384 [00:01<00:00, 6846.29it/s]
80%|######## | Sample : 6738/8384 [00:01<00:00, 6864.70it/s]
82%|########1 | Sample : 6843/8384 [00:01<00:00, 6845.94it/s]
83%|########2 | Sample : 6952/8384 [00:01<00:00, 6842.22it/s]
84%|########4 | Sample : 7064/8384 [00:01<00:00, 6849.39it/s]
86%|########5 | Sample : 7182/8384 [00:01<00:00, 6874.84it/s]
87%|########7 | Sample : 7299/8384 [00:01<00:00, 6894.70it/s]
88%|########8 | Sample : 7418/8384 [00:01<00:00, 6920.84it/s]
90%|########9 | Sample : 7527/8384 [00:01<00:00, 6914.36it/s]
91%|#########1| Sample : 7634/8384 [00:01<00:00, 6901.44it/s]
92%|#########2| Sample : 7747/8384 [00:01<00:00, 6907.47it/s]
94%|#########3| Sample : 7863/8384 [00:01<00:00, 6923.69it/s]
95%|#########5| Sample : 7981/8384 [00:01<00:00, 6945.29it/s]
97%|#########6| Sample : 8099/8384 [00:01<00:00, 6967.03it/s]
98%|#########7| Sample : 8213/8384 [00:01<00:00, 6973.43it/s]
99%|#########9| Sample : 8326/8384 [00:01<00:00, 6977.07it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 6456.21it/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 18.746 seconds)
Estimated memory usage: 45 MB