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)")
Sample activity
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$)")
Mean prediction score
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.43s/it]
100%|██████████| Sample : 2/2 [00:02<00:00,    1.19s/it]
100%|██████████| Sample : 2/2 [00:02<00:00,    1.23s/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,   58.80it/s]
100%|██████████| Sample : 2/2 [00:00<00:00,   58.66it/s]
100%|██████████| Sample : 2/2 [00:00<00:00,   58.23it/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,   54.60it/s]
100%|██████████| Sample : 2/2 [00:00<00:00,   55.80it/s]
100%|██████████| Sample : 2/2 [00:00<00:00,   55.23it/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}")
  • Mean Model Coefficients
  • Topomap of model coefficients for delay 0.18

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:15,   62.08it/s]
  2%|▏         | Sample : 134/8384 [00:00<00:01, 4263.69it/s]
  3%|▎         | Sample : 272/8384 [00:00<00:01, 5781.38it/s]
  5%|▍         | Sample : 413/8384 [00:00<00:01, 6585.64it/s]
  7%|▋         | Sample : 555/8384 [00:00<00:01, 7085.37it/s]
  8%|▊         | Sample : 695/8384 [00:00<00:01, 7391.03it/s]
 10%|▉         | Sample : 830/8384 [00:00<00:00, 7562.92it/s]
 12%|█▏        | Sample : 968/8384 [00:00<00:00, 7716.02it/s]
 13%|█▎        | Sample : 1109/8384 [00:00<00:00, 7858.74it/s]
 15%|█▍        | Sample : 1249/8384 [00:00<00:00, 7966.37it/s]
 17%|█▋        | Sample : 1387/8384 [00:00<00:00, 8041.62it/s]
 18%|█▊        | Sample : 1529/8384 [00:00<00:00, 8130.85it/s]
 20%|█▉        | Sample : 1671/8384 [00:00<00:00, 8203.99it/s]
 22%|██▏       | Sample : 1811/8384 [00:00<00:00, 8252.28it/s]
 23%|██▎       | Sample : 1952/8384 [00:00<00:00, 8303.26it/s]
 25%|██▍       | Sample : 2094/8384 [00:00<00:00, 8348.80it/s]
 27%|██▋       | Sample : 2235/8384 [00:00<00:00, 8383.88it/s]
 28%|██▊       | Sample : 2375/8384 [00:00<00:00, 8411.87it/s]
 30%|███       | Sample : 2518/8384 [00:00<00:00, 8450.25it/s]
 32%|███▏      | Sample : 2660/8384 [00:00<00:00, 8479.99it/s]
 33%|███▎      | Sample : 2779/8384 [00:00<00:00, 8400.96it/s]
 34%|███▍      | Sample : 2873/8384 [00:00<00:00, 8214.88it/s]
 36%|███▌      | Sample : 3012/8384 [00:00<00:00, 8247.22it/s]
 38%|███▊      | Sample : 3154/8384 [00:00<00:00, 8291.33it/s]
 39%|███▉      | Sample : 3296/8384 [00:00<00:00, 8330.54it/s]
 41%|████      | Sample : 3436/8384 [00:00<00:00, 8357.18it/s]
 43%|████▎     | Sample : 3578/8384 [00:00<00:00, 8391.30it/s]
 44%|████▍     | Sample : 3721/8384 [00:00<00:00, 8424.60it/s]
 46%|████▌     | Sample : 3863/8384 [00:00<00:00, 8450.96it/s]
 48%|████▊     | Sample : 4003/8384 [00:00<00:00, 8469.56it/s]
 49%|████▉     | Sample : 4147/8384 [00:00<00:00, 8500.79it/s]
 51%|█████     | Sample : 4289/8384 [00:00<00:00, 8523.16it/s]
 53%|█████▎    | Sample : 4432/8384 [00:00<00:00, 8547.34it/s]
 55%|█████▍    | Sample : 4576/8384 [00:00<00:00, 8571.89it/s]
 56%|█████▋    | Sample : 4720/8384 [00:00<00:00, 8597.41it/s]
 58%|█████▊    | Sample : 4865/8384 [00:00<00:00, 8622.79it/s]
 60%|█████▉    | Sample : 5008/8384 [00:00<00:00, 8639.85it/s]
 61%|██████▏   | Sample : 5151/8384 [00:00<00:00, 8656.22it/s]
 63%|██████▎   | Sample : 5295/8384 [00:00<00:00, 8673.17it/s]
 65%|██████▍   | Sample : 5439/8384 [00:00<00:00, 8691.08it/s]
 67%|██████▋   | Sample : 5584/8384 [00:00<00:00, 8709.52it/s]
 68%|██████▊   | Sample : 5728/8384 [00:00<00:00, 8722.86it/s]
 70%|███████   | Sample : 5871/8384 [00:00<00:00, 8734.53it/s]
 72%|███████▏  | Sample : 6015/8384 [00:00<00:00, 8748.87it/s]
 73%|███████▎  | Sample : 6158/8384 [00:00<00:00, 8757.94it/s]
 75%|███████▌  | Sample : 6302/8384 [00:00<00:00, 8770.86it/s]
 77%|███████▋  | Sample : 6445/8384 [00:00<00:00, 8779.51it/s]
 79%|███████▊  | Sample : 6588/8384 [00:00<00:00, 8785.67it/s]
 80%|████████  | Sample : 6730/8384 [00:00<00:00, 8789.80it/s]
 82%|████████▏ | Sample : 6873/8384 [00:00<00:00, 8796.31it/s]
 84%|████████▎ | Sample : 7017/8384 [00:00<00:00, 8805.36it/s]
 85%|████████▌ | Sample : 7160/8384 [00:00<00:00, 8809.49it/s]
 87%|████████▋ | Sample : 7303/8384 [00:00<00:00, 8814.90it/s]
 89%|████████▉ | Sample : 7446/8384 [00:00<00:00, 8820.76it/s]
 91%|█████████ | Sample : 7590/8384 [00:00<00:00, 8827.51it/s]
 92%|█████████▏| Sample : 7734/8384 [00:00<00:00, 8834.08it/s]
 94%|█████████▍| Sample : 7878/8384 [00:00<00:00, 8841.48it/s]
 96%|█████████▌| Sample : 8021/8384 [00:00<00:00, 8844.30it/s]
 97%|█████████▋| Sample : 8166/8384 [00:00<00:00, 8853.77it/s]
 99%|█████████▉| Sample : 8311/8384 [00:00<00:00, 8863.31it/s]
100%|██████████| Sample : 8384/8384 [00:00<00:00, 8628.39it/s]
split 2 / 3
Fitting 1 epochs, 128 channels

  0%|          | Sample : 0/8384 [00:00<?,       ?it/s]
  0%|          | Sample : 12/8384 [00:00<00:11,  749.35it/s]
  2%|▏         | Sample : 146/8384 [00:00<00:01, 4649.34it/s]
  3%|▎         | Sample : 283/8384 [00:00<00:01, 6016.44it/s]
  5%|▌         | Sample : 421/8384 [00:00<00:01, 6708.78it/s]
  7%|▋         | Sample : 559/8384 [00:00<00:01, 7130.06it/s]
  8%|▊         | Sample : 699/8384 [00:00<00:01, 7429.02it/s]
 10%|▉         | Sample : 834/8384 [00:00<00:00, 7593.53it/s]
 12%|█▏        | Sample : 966/8384 [00:00<00:00, 7689.68it/s]
 13%|█▎        | Sample : 1102/8384 [00:00<00:00, 7797.42it/s]
 15%|█▍        | Sample : 1242/8384 [00:00<00:00, 7913.76it/s]
 16%|█▋        | Sample : 1383/8384 [00:00<00:00, 8014.35it/s]
 18%|█▊        | Sample : 1525/8384 [00:00<00:00, 8105.77it/s]
 20%|█▉        | Sample : 1662/8384 [00:00<00:00, 8152.00it/s]
 21%|██▏       | Sample : 1800/8384 [00:00<00:00, 8194.60it/s]
 23%|██▎       | Sample : 1942/8384 [00:00<00:00, 8253.47it/s]
 25%|██▍       | Sample : 2084/8384 [00:00<00:00, 8307.00it/s]
 27%|██▋       | Sample : 2225/8384 [00:00<00:00, 8347.17it/s]
 28%|██▊       | Sample : 2366/8384 [00:00<00:00, 8383.53it/s]
 30%|██▉       | Sample : 2502/8384 [00:00<00:00, 8390.38it/s]
 32%|███▏      | Sample : 2641/8384 [00:00<00:00, 8409.93it/s]
 33%|███▎      | Sample : 2781/8384 [00:00<00:00, 8430.84it/s]
 35%|███▍      | Sample : 2918/8384 [00:00<00:00, 8437.01it/s]
 36%|███▋      | Sample : 3060/8384 [00:00<00:00, 8468.35it/s]
 38%|███▊      | Sample : 3200/8384 [00:00<00:00, 8487.62it/s]
 40%|███▉      | Sample : 3339/8384 [00:00<00:00, 8499.78it/s]
 42%|████▏     | Sample : 3481/8384 [00:00<00:00, 8523.98it/s]
 43%|████▎     | Sample : 3625/8384 [00:00<00:00, 8552.81it/s]
 45%|████▍     | Sample : 3769/8384 [00:00<00:00, 8579.25it/s]
 47%|████▋     | Sample : 3913/8384 [00:00<00:00, 8606.17it/s]
 48%|████▊     | Sample : 4057/8384 [00:00<00:00, 8627.59it/s]
 50%|█████     | Sample : 4199/8384 [00:00<00:00, 8641.42it/s]
 52%|█████▏    | Sample : 4340/8384 [00:00<00:00, 8651.47it/s]
 53%|█████▎    | Sample : 4484/8384 [00:00<00:00, 8672.21it/s]
 55%|█████▌    | Sample : 4628/8384 [00:00<00:00, 8689.29it/s]
 57%|█████▋    | Sample : 4761/8384 [00:00<00:00, 8666.32it/s]
 58%|█████▊    | Sample : 4892/8384 [00:00<00:00, 8636.31it/s]
 60%|█████▉    | Sample : 5023/8384 [00:00<00:00, 8607.61it/s]
 61%|██████▏   | Sample : 5153/8384 [00:00<00:00, 8575.94it/s]
 63%|██████▎   | Sample : 5287/8384 [00:00<00:00, 8562.51it/s]
 65%|██████▍   | Sample : 5425/8384 [00:00<00:00, 8564.77it/s]
 66%|██████▋   | Sample : 5562/8384 [00:00<00:00, 8561.47it/s]
 68%|██████▊   | Sample : 5699/8384 [00:00<00:00, 8557.88it/s]
 70%|██████▉   | Sample : 5836/8384 [00:00<00:00, 8557.10it/s]
 71%|███████   | Sample : 5969/8384 [00:00<00:00, 8541.60it/s]
 73%|███████▎  | Sample : 6108/8384 [00:00<00:00, 8549.37it/s]
 75%|███████▍  | Sample : 6250/8384 [00:00<00:00, 8566.21it/s]
 76%|███████▋  | Sample : 6393/8384 [00:00<00:00, 8585.66it/s]
 78%|███████▊  | Sample : 6532/8384 [00:00<00:00, 8590.15it/s]
 80%|███████▉  | Sample : 6674/8384 [00:00<00:00, 8605.10it/s]
 81%|████████▏ | Sample : 6819/8384 [00:00<00:00, 8629.06it/s]
 83%|████████▎ | Sample : 6964/8384 [00:00<00:00, 8649.73it/s]
 85%|████████▍ | Sample : 7107/8384 [00:00<00:00, 8663.92it/s]
 86%|████████▋ | Sample : 7252/8384 [00:00<00:00, 8685.13it/s]
 88%|████████▊ | Sample : 7398/8384 [00:00<00:00, 8706.48it/s]
 90%|████████▉ | Sample : 7542/8384 [00:00<00:00, 8721.31it/s]
 92%|█████████▏| Sample : 7687/8384 [00:00<00:00, 8738.63it/s]
 93%|█████████▎| Sample : 7832/8384 [00:00<00:00, 8754.87it/s]
 95%|█████████▌| Sample : 7978/8384 [00:00<00:00, 8772.10it/s]
 97%|█████████▋| Sample : 8122/8384 [00:00<00:00, 8782.85it/s]
 99%|█████████▊| Sample : 8268/8384 [00:00<00:00, 8797.25it/s]
100%|██████████| Sample : 8384/8384 [00:00<00:00, 8590.08it/s]
split 3 / 3
Fitting 1 epochs, 128 channels

  0%|          | Sample : 0/8384 [00:00<?,       ?it/s]
  0%|          | Sample : 13/8384 [00:00<00:10,  810.12it/s]
  2%|▏         | Sample : 146/8384 [00:00<00:01, 4648.52it/s]
  3%|▎         | Sample : 284/8384 [00:00<00:01, 6038.68it/s]
  5%|▌         | Sample : 422/8384 [00:00<00:01, 6730.35it/s]
  7%|▋         | Sample : 560/8384 [00:00<00:01, 7137.87it/s]
  8%|▊         | Sample : 696/8384 [00:00<00:01, 7385.01it/s]
 10%|▉         | Sample : 832/8384 [00:00<00:00, 7561.54it/s]
 12%|█▏        | Sample : 970/8384 [00:00<00:00, 7717.93it/s]
 13%|█▎        | Sample : 1108/8384 [00:00<00:00, 7837.15it/s]
 15%|█▍        | Sample : 1247/8384 [00:00<00:00, 7937.81it/s]
 17%|█▋        | Sample : 1385/8384 [00:00<00:00, 8016.13it/s]
 18%|█▊        | Sample : 1523/8384 [00:00<00:00, 8075.88it/s]
 20%|█▉        | Sample : 1663/8384 [00:00<00:00, 8139.93it/s]
 22%|██▏       | Sample : 1803/8384 [00:00<00:00, 8196.50it/s]
 23%|██▎       | Sample : 1943/8384 [00:00<00:00, 8247.41it/s]
 25%|██▍       | Sample : 2086/8384 [00:00<00:00, 8306.31it/s]
 27%|██▋       | Sample : 2227/8384 [00:00<00:00, 8344.73it/s]
 28%|██▊       | Sample : 2368/8384 [00:00<00:00, 8381.60it/s]
 30%|██▉       | Sample : 2513/8384 [00:00<00:00, 8431.35it/s]
 32%|███▏      | Sample : 2656/8384 [00:00<00:00, 8469.51it/s]
 33%|███▎      | Sample : 2801/8384 [00:00<00:00, 8510.29it/s]
 35%|███▌      | Sample : 2946/8384 [00:00<00:00, 8550.20it/s]
 37%|███▋      | Sample : 3089/8384 [00:00<00:00, 8577.78it/s]
 39%|███▊      | Sample : 3234/8384 [00:00<00:00, 8609.58it/s]
 40%|████      | Sample : 3378/8384 [00:00<00:00, 8634.12it/s]
 42%|████▏     | Sample : 3520/8384 [00:00<00:00, 8649.04it/s]
 44%|████▎     | Sample : 3659/8384 [00:00<00:00, 8650.46it/s]
 45%|████▌     | Sample : 3800/8384 [00:00<00:00, 8656.49it/s]
 47%|████▋     | Sample : 3937/8384 [00:00<00:00, 8646.79it/s]
 49%|████▊     | Sample : 4076/8384 [00:00<00:00, 8646.58it/s]
 50%|█████     | Sample : 4214/8384 [00:00<00:00, 8645.06it/s]
 52%|█████▏    | Sample : 4351/8384 [00:00<00:00, 8639.80it/s]
 54%|█████▎    | Sample : 4487/8384 [00:00<00:00, 8630.13it/s]
 55%|█████▌    | Sample : 4626/8384 [00:00<00:00, 8632.70it/s]
 57%|█████▋    | Sample : 4769/8384 [00:00<00:00, 8647.28it/s]
 59%|█████▊    | Sample : 4906/8384 [00:00<00:00, 8640.09it/s]
 60%|██████    | Sample : 5040/8384 [00:00<00:00, 8623.03it/s]
 62%|██████▏   | Sample : 5176/8384 [00:00<00:00, 8613.23it/s]
 63%|██████▎   | Sample : 5313/8384 [00:00<00:00, 8607.76it/s]
 65%|██████▍   | Sample : 5449/8384 [00:00<00:00, 8599.25it/s]
 67%|██████▋   | Sample : 5590/8384 [00:00<00:00, 8610.30it/s]
 68%|██████▊   | Sample : 5730/8384 [00:00<00:00, 8617.43it/s]
 70%|███████   | Sample : 5871/8384 [00:00<00:00, 8628.04it/s]
 72%|███████▏  | Sample : 6010/8384 [00:00<00:00, 8630.21it/s]
 73%|███████▎  | Sample : 6152/8384 [00:00<00:00, 8642.08it/s]
 75%|███████▌  | Sample : 6291/8384 [00:00<00:00, 8644.58it/s]
 77%|███████▋  | Sample : 6429/8384 [00:00<00:00, 8640.15it/s]
 78%|███████▊  | Sample : 6570/8384 [00:00<00:00, 8649.26it/s]
 80%|████████  | Sample : 6711/8384 [00:00<00:00, 8656.91it/s]
 82%|████████▏ | Sample : 6854/8384 [00:00<00:00, 8669.78it/s]
 83%|████████▎ | Sample : 6996/8384 [00:00<00:00, 8679.32it/s]
 85%|████████▌ | Sample : 7137/8384 [00:00<00:00, 8683.64it/s]
 87%|████████▋ | Sample : 7280/8384 [00:00<00:00, 8695.11it/s]
 89%|████████▊ | Sample : 7425/8384 [00:00<00:00, 8712.03it/s]
 90%|█████████ | Sample : 7568/8384 [00:00<00:00, 8722.70it/s]
 92%|█████████▏| Sample : 7712/8384 [00:00<00:00, 8737.27it/s]
 94%|█████████▎| Sample : 7857/8384 [00:00<00:00, 8753.02it/s]
 95%|█████████▌| Sample : 8001/8384 [00:00<00:00, 8763.18it/s]
 97%|█████████▋| Sample : 8147/8384 [00:00<00:00, 8779.87it/s]
 99%|█████████▉| Sample : 8290/8384 [00:00<00:00, 8787.03it/s]
100%|██████████| Sample : 8384/8384 [00:00<00:00, 8607.00it/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)")
Stimulus reconstruction

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]}"
    )
)
Model coefficients between delays -0.14 and -0.125, Inverse-transformed coefficients between delays -0.14 and -0.125

References#

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

Gallery generated by Sphinx-Gallery