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
# Copyright the MNE-Python contributors.
from os.path import join
import matplotlib.pyplot as plt
import numpy as np
from scipy.io import loadmat
from sklearn.model_selection import KFold
from sklearn.preprocessing import scale
import mne
from mne.decoding import ReceptiveField
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"].item())
sfreq /= decim
speech = mne.filter.resample(speech, down=decim, method="polyphase")
raw = mne.filter.resample(raw, down=decim, method="polyphase")
# 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(layout="constrained")
lns = ax.plot(scale(raw[:, :800][0].T), color="k", alpha=0.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)")
Polyphase resampling neighborhood: ±2 input samples
Polyphase resampling neighborhood: ±2 input samples
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 = -0.2, 0.4
# Initialize the model
rf = ReceptiveField(
tmin, tmax, sfreq, feature_names=["envelope"], estimator=1.0, 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(f"split {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(layout="constrained")
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$)")
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.47s/it]
100%|██████████| Sample : 2/2 [00:02<00:00, 1.21s/it]
100%|██████████| Sample : 2/2 [00:02<00:00, 1.25s/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, 56.12it/s]
100%|██████████| Sample : 2/2 [00:00<00:00, 57.08it/s]
100%|██████████| Sample : 2/2 [00:00<00:00, 56.62it/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, 59.20it/s]
100%|██████████| Sample : 2/2 [00:00<00:00, 59.38it/s]
100%|██████████| Sample : 2/2 [00:00<00:00, 58.97it/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), layout="constrained")
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 + 0.2, 0.2),
)
plt.setp(ax.get_xticklabels(), rotation=45)
# 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(layout="constrained")
mne.viz.plot_topomap(
mean_coefs[:, ix_plot], pos=info, axes=ax, show=False, vlim=(-max_coef, max_coef)
)
ax.set(title=f"Topomap of model coefficients\nfor delay {time_plot}")
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 = -0.2, 0.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(f"split {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<02:26, 57.04it/s]
2%|▏ | Sample : 143/8384 [00:00<00:01, 4375.50it/s]
3%|▎ | Sample : 292/8384 [00:00<00:01, 6045.47it/s]
5%|▌ | Sample : 428/8384 [00:00<00:01, 6669.53it/s]
6%|▌ | Sample : 506/8384 [00:00<00:01, 6271.22it/s]
7%|▋ | Sample : 584/8384 [00:00<00:01, 6001.76it/s]
8%|▊ | Sample : 662/8384 [00:00<00:01, 5806.84it/s]
9%|▉ | Sample : 739/8384 [00:00<00:01, 5657.40it/s]
10%|▉ | Sample : 816/8384 [00:00<00:01, 5537.24it/s]
11%|█ | Sample : 894/8384 [00:00<00:01, 5451.57it/s]
12%|█▏ | Sample : 969/8384 [00:00<00:01, 5362.40it/s]
12%|█▏ | Sample : 1045/8384 [00:00<00:01, 5290.83it/s]
13%|█▎ | Sample : 1124/8384 [00:00<00:01, 5249.63it/s]
14%|█▍ | Sample : 1202/8384 [00:00<00:01, 5209.27it/s]
15%|█▌ | Sample : 1279/8384 [00:00<00:01, 5168.78it/s]
16%|█▌ | Sample : 1357/8384 [00:00<00:01, 5139.50it/s]
17%|█▋ | Sample : 1433/8384 [00:00<00:01, 5106.29it/s]
18%|█▊ | Sample : 1512/8384 [00:00<00:01, 5088.33it/s]
19%|█▉ | Sample : 1588/8384 [00:00<00:01, 5060.20it/s]
20%|██ | Sample : 1677/8384 [00:00<00:01, 5096.80it/s]
22%|██▏ | Sample : 1825/8384 [00:00<00:01, 5407.15it/s]
23%|██▎ | Sample : 1968/8384 [00:00<00:01, 5663.58it/s]
25%|██▌ | Sample : 2114/8384 [00:00<00:01, 5911.09it/s]
27%|██▋ | Sample : 2265/8384 [00:00<00:00, 6155.35it/s]
29%|██▉ | Sample : 2416/8384 [00:00<00:00, 6378.72it/s]
31%|███ | Sample : 2565/8384 [00:00<00:00, 6575.28it/s]
32%|███▏ | Sample : 2716/8384 [00:00<00:00, 6762.99it/s]
34%|███▍ | Sample : 2867/8384 [00:00<00:00, 6935.44it/s]
36%|███▌ | Sample : 3018/8384 [00:00<00:00, 7093.85it/s]
38%|███▊ | Sample : 3169/8384 [00:00<00:00, 7239.95it/s]
40%|███▉ | Sample : 3320/8384 [00:00<00:00, 7375.97it/s]
41%|████▏ | Sample : 3470/8384 [00:00<00:00, 7498.80it/s]
43%|████▎ | Sample : 3621/8384 [00:00<00:00, 7615.23it/s]
45%|████▍ | Sample : 3772/8384 [00:00<00:00, 7724.30it/s]
47%|████▋ | Sample : 3923/8384 [00:00<00:00, 7826.16it/s]
49%|████▊ | Sample : 4075/8384 [00:00<00:00, 7922.48it/s]
50%|█████ | Sample : 4225/8384 [00:00<00:00, 8005.45it/s]
52%|█████▏ | Sample : 4375/8384 [00:00<00:00, 8082.00it/s]
54%|█████▍ | Sample : 4526/8384 [00:00<00:00, 8156.97it/s]
56%|█████▌ | Sample : 4673/8384 [00:00<00:00, 8213.41it/s]
58%|█████▊ | Sample : 4823/8384 [00:00<00:00, 8276.49it/s]
59%|█████▉ | Sample : 4967/8384 [00:00<00:00, 8315.25it/s]
61%|██████ | Sample : 5111/8384 [00:00<00:00, 8351.82it/s]
63%|██████▎ | Sample : 5255/8384 [00:00<00:00, 8386.21it/s]
64%|██████▍ | Sample : 5405/8384 [00:00<00:00, 8438.45it/s]
66%|██████▋ | Sample : 5555/8384 [00:00<00:00, 8488.03it/s]
68%|██████▊ | Sample : 5700/8384 [00:00<00:00, 8518.15it/s]
70%|██████▉ | Sample : 5850/8384 [00:00<00:00, 8562.25it/s]
72%|███████▏ | Sample : 5999/8384 [00:00<00:00, 8601.38it/s]
73%|███████▎ | Sample : 6150/8384 [00:00<00:00, 8644.91it/s]
75%|███████▌ | Sample : 6296/8384 [00:00<00:00, 8669.57it/s]
77%|███████▋ | Sample : 6447/8384 [00:00<00:00, 8709.62it/s]
79%|███████▊ | Sample : 6597/8384 [00:00<00:00, 8743.25it/s]
80%|████████ | Sample : 6748/8384 [00:00<00:00, 8779.46it/s]
82%|████████▏ | Sample : 6897/8384 [00:00<00:00, 8807.61it/s]
84%|████████▍ | Sample : 7045/8384 [00:00<00:00, 8827.67it/s]
86%|████████▌ | Sample : 7191/8384 [00:00<00:00, 8842.87it/s]
88%|████████▊ | Sample : 7341/8384 [00:00<00:00, 8868.91it/s]
89%|████████▉ | Sample : 7492/8384 [00:00<00:00, 8896.23it/s]
91%|█████████ | Sample : 7642/8384 [00:00<00:00, 8920.99it/s]
93%|█████████▎| Sample : 7790/8384 [00:00<00:00, 8937.60it/s]
95%|█████████▍| Sample : 7937/8384 [00:00<00:00, 8947.50it/s]
96%|█████████▋| Sample : 8086/8384 [00:01<00:00, 8966.20it/s]
98%|█████████▊| Sample : 8237/8384 [00:01<00:00, 8988.02it/s]
100%|██████████| Sample : 8384/8384 [00:01<00:00, 8016.17it/s]
split 2 / 3
Fitting 1 epochs, 128 channels
0%| | Sample : 0/8384 [00:00<?, ?it/s]
0%| | Sample : 1/8384 [00:00<02:19, 60.21it/s]
2%|▏ | Sample : 135/8384 [00:00<00:01, 4240.34it/s]
3%|▎ | Sample : 277/8384 [00:00<00:01, 5833.61it/s]
5%|▍ | Sample : 412/8384 [00:00<00:01, 6516.04it/s]
7%|▋ | Sample : 547/8384 [00:00<00:01, 6930.10it/s]
8%|▊ | Sample : 690/8384 [00:00<00:01, 7303.77it/s]
10%|▉ | Sample : 837/8384 [00:00<00:00, 7608.37it/s]
12%|█▏ | Sample : 971/8384 [00:00<00:00, 7715.95it/s]
13%|█▎ | Sample : 1115/8384 [00:00<00:00, 7887.06it/s]
15%|█▌ | Sample : 1264/8384 [00:00<00:00, 8060.77it/s]
17%|█▋ | Sample : 1415/8384 [00:00<00:00, 8214.71it/s]
19%|█▊ | Sample : 1565/8384 [00:00<00:00, 8338.82it/s]
20%|██ | Sample : 1717/8384 [00:00<00:00, 8452.39it/s]
22%|██▏ | Sample : 1867/8384 [00:00<00:00, 8539.78it/s]
24%|██▍ | Sample : 2019/8384 [00:00<00:00, 8625.28it/s]
26%|██▌ | Sample : 2171/8384 [00:00<00:00, 8701.14it/s]
28%|██▊ | Sample : 2322/8384 [00:00<00:00, 8761.01it/s]
29%|██▉ | Sample : 2471/8384 [00:00<00:00, 8804.06it/s]
31%|███▏ | Sample : 2622/8384 [00:00<00:00, 8851.76it/s]
33%|███▎ | Sample : 2771/8384 [00:00<00:00, 8886.72it/s]
35%|███▍ | Sample : 2917/8384 [00:00<00:00, 8904.30it/s]
37%|███▋ | Sample : 3064/8384 [00:00<00:00, 8923.23it/s]
38%|███▊ | Sample : 3207/8384 [00:00<00:00, 8923.20it/s]
40%|███▉ | Sample : 3353/8384 [00:00<00:00, 8935.99it/s]
42%|████▏ | Sample : 3503/8384 [00:00<00:00, 8964.74it/s]
44%|████▎ | Sample : 3651/8384 [00:00<00:00, 8981.36it/s]
45%|████▌ | Sample : 3800/8384 [00:00<00:00, 9002.75it/s]
47%|████▋ | Sample : 3950/8384 [00:00<00:00, 9023.45it/s]
49%|████▉ | Sample : 4100/8384 [00:00<00:00, 9043.78it/s]
51%|█████ | Sample : 4251/8384 [00:00<00:00, 9065.53it/s]
52%|█████▏ | Sample : 4398/8384 [00:00<00:00, 9070.62it/s]
54%|█████▍ | Sample : 4546/8384 [00:00<00:00, 9078.37it/s]
56%|█████▌ | Sample : 4690/8384 [00:00<00:00, 9072.66it/s]
58%|█████▊ | Sample : 4828/8384 [00:00<00:00, 9044.24it/s]
59%|█████▉ | Sample : 4969/8384 [00:00<00:00, 9028.27it/s]
61%|██████ | Sample : 5110/8384 [00:00<00:00, 9012.87it/s]
63%|██████▎ | Sample : 5253/8384 [00:00<00:00, 9006.84it/s]
64%|██████▍ | Sample : 5399/8384 [00:00<00:00, 9010.54it/s]
66%|██████▌ | Sample : 5546/8384 [00:00<00:00, 9017.60it/s]
68%|██████▊ | Sample : 5692/8384 [00:00<00:00, 9021.20it/s]
70%|██████▉ | Sample : 5843/8384 [00:00<00:00, 9042.50it/s]
71%|███████▏ | Sample : 5993/8384 [00:00<00:00, 9060.44it/s]
73%|███████▎ | Sample : 6142/8384 [00:00<00:00, 9072.98it/s]
75%|███████▌ | Sample : 6292/8384 [00:00<00:00, 9087.48it/s]
77%|███████▋ | Sample : 6441/8384 [00:00<00:00, 9098.02it/s]
79%|███████▊ | Sample : 6591/8384 [00:00<00:00, 9112.72it/s]
80%|████████ | Sample : 6741/8384 [00:00<00:00, 9124.90it/s]
82%|████████▏ | Sample : 6890/8384 [00:00<00:00, 9134.40it/s]
84%|████████▍ | Sample : 7039/8384 [00:00<00:00, 9142.44it/s]
86%|████████▌ | Sample : 7189/8384 [00:00<00:00, 9152.56it/s]
87%|████████▋ | Sample : 7333/8384 [00:00<00:00, 9143.55it/s]
89%|████████▉ | Sample : 7481/8384 [00:00<00:00, 9149.08it/s]
91%|█████████ | Sample : 7630/8384 [00:00<00:00, 9154.73it/s]
93%|█████████▎| Sample : 7777/8384 [00:00<00:00, 9153.70it/s]
95%|█████████▍| Sample : 7927/8384 [00:00<00:00, 9163.94it/s]
96%|█████████▋| Sample : 8077/8384 [00:00<00:00, 9174.89it/s]
98%|█████████▊| Sample : 8226/8384 [00:00<00:00, 9181.51it/s]
100%|█████████▉| Sample : 8377/8384 [00:00<00:00, 9193.93it/s]
100%|██████████| Sample : 8384/8384 [00:00<00:00, 8988.50it/s]
split 3 / 3
Fitting 1 epochs, 128 channels
0%| | Sample : 0/8384 [00:00<?, ?it/s]
0%| | Sample : 7/8384 [00:00<00:19, 434.91it/s]
2%|▏ | Sample : 143/8384 [00:00<00:01, 4542.60it/s]
3%|▎ | Sample : 287/8384 [00:00<00:01, 6094.60it/s]
5%|▌ | Sample : 433/8384 [00:00<00:01, 6896.78it/s]
7%|▋ | Sample : 571/8384 [00:00<00:01, 7275.54it/s]
8%|▊ | Sample : 707/8384 [00:00<00:01, 7504.16it/s]
10%|█ | Sample : 844/8384 [00:00<00:00, 7673.13it/s]
12%|█▏ | Sample : 982/8384 [00:00<00:00, 7807.04it/s]
13%|█▎ | Sample : 1121/8384 [00:00<00:00, 7920.64it/s]
15%|█▌ | Sample : 1259/8384 [00:00<00:00, 8003.74it/s]
17%|█▋ | Sample : 1405/8384 [00:00<00:00, 8126.96it/s]
19%|█▊ | Sample : 1552/8384 [00:00<00:00, 8238.64it/s]
20%|██ | Sample : 1693/8384 [00:00<00:00, 8294.83it/s]
22%|██▏ | Sample : 1834/8384 [00:00<00:00, 8341.65it/s]
24%|██▎ | Sample : 1977/8384 [00:00<00:00, 8393.35it/s]
25%|██▌ | Sample : 2121/8384 [00:00<00:00, 8446.79it/s]
27%|██▋ | Sample : 2270/8384 [00:00<00:00, 8516.11it/s]
29%|██▉ | Sample : 2416/8384 [00:00<00:00, 8564.93it/s]
31%|███ | Sample : 2565/8384 [00:00<00:00, 8624.49it/s]
32%|███▏ | Sample : 2710/8384 [00:00<00:00, 8656.27it/s]
34%|███▍ | Sample : 2845/8384 [00:00<00:00, 8636.04it/s]
36%|███▌ | Sample : 2988/8384 [00:00<00:00, 8657.29it/s]
37%|███▋ | Sample : 3135/8384 [00:00<00:00, 8692.51it/s]
39%|███▉ | Sample : 3280/8384 [00:00<00:00, 8714.95it/s]
41%|████ | Sample : 3425/8384 [00:00<00:00, 8735.75it/s]
43%|████▎ | Sample : 3571/8384 [00:00<00:00, 8758.10it/s]
44%|████▍ | Sample : 3720/8384 [00:00<00:00, 8793.96it/s]
46%|████▌ | Sample : 3867/8384 [00:00<00:00, 8816.25it/s]
48%|████▊ | Sample : 4014/8384 [00:00<00:00, 8839.89it/s]
50%|████▉ | Sample : 4163/8384 [00:00<00:00, 8867.42it/s]
51%|█████▏ | Sample : 4311/8384 [00:00<00:00, 8890.05it/s]
53%|█████▎ | Sample : 4454/8384 [00:00<00:00, 8891.60it/s]
55%|█████▍ | Sample : 4604/8384 [00:00<00:00, 8917.62it/s]
57%|█████▋ | Sample : 4752/8384 [00:00<00:00, 8934.74it/s]
58%|█████▊ | Sample : 4904/8384 [00:00<00:00, 8966.24it/s]
60%|██████ | Sample : 5055/8384 [00:00<00:00, 8991.37it/s]
62%|██████▏ | Sample : 5201/8384 [00:00<00:00, 8995.89it/s]
64%|██████▎ | Sample : 5344/8384 [00:00<00:00, 8988.25it/s]
65%|██████▌ | Sample : 5487/8384 [00:00<00:00, 8984.90it/s]
67%|██████▋ | Sample : 5631/8384 [00:00<00:00, 8984.50it/s]
69%|██████▉ | Sample : 5778/8384 [00:00<00:00, 8995.64it/s]
71%|███████ | Sample : 5924/8384 [00:00<00:00, 8999.47it/s]
72%|███████▏ | Sample : 6069/8384 [00:00<00:00, 9001.48it/s]
74%|███████▍ | Sample : 6215/8384 [00:00<00:00, 9006.06it/s]
76%|███████▌ | Sample : 6363/8384 [00:00<00:00, 9018.29it/s]
78%|███████▊ | Sample : 6511/8384 [00:00<00:00, 9030.15it/s]
79%|███████▉ | Sample : 6662/8384 [00:00<00:00, 9050.89it/s]
81%|████████▏ | Sample : 6813/8384 [00:00<00:00, 9071.63it/s]
83%|████████▎ | Sample : 6965/8384 [00:00<00:00, 9093.08it/s]
85%|████████▍ | Sample : 7115/8384 [00:00<00:00, 9104.50it/s]
87%|████████▋ | Sample : 7262/8384 [00:00<00:00, 9108.51it/s]
88%|████████▊ | Sample : 7411/8384 [00:00<00:00, 9118.39it/s]
90%|█████████ | Sample : 7560/8384 [00:00<00:00, 9126.39it/s]
92%|█████████▏| Sample : 7711/8384 [00:00<00:00, 9140.39it/s]
94%|█████████▍| Sample : 7861/8384 [00:00<00:00, 9150.40it/s]
96%|█████████▌| Sample : 8012/8384 [00:00<00:00, 9162.31it/s]
97%|█████████▋| Sample : 8162/8384 [00:00<00:00, 9173.23it/s]
99%|█████████▉| Sample : 8312/8384 [00:00<00:00, 9183.35it/s]
100%|██████████| Sample : 8384/8384 [00:00<00:00, 8923.86it/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.0, 5 * int(sfreq))
fig, ax = plt.subplots(figsize=(8, 4), layout="constrained")
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)")
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 = (-0.140, -0.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=f"Model coefficients\nbetween delays {time_plot[0]} and {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=(
f"Inverse-transformed coefficients\nbetween delays {time_plot[0]} and "
f"{time_plot[1]}"
)
)
References#
Total running time of the script: (0 minutes 12.450 seconds)