Receptive Field Estimation and Prediction

This example reproduces figures from Lalor et al.’s mTRF toolbox in MATLAB 1. We will show how the mne.decoding.ReceptiveField class can perform a similar function along with scikit-learn. We will first fit a linear encoding model using the continuously-varying speech envelope to predict activity of a 128 channel EEG system. Then, we will take the reverse approach and try to predict the speech envelope from the EEG (known in the literature as a decoding model, or simply stimulus reconstruction).

# Authors: Chris Holdgraf <choldgraf@gmail.com>
#          Eric Larson <larson.eric.d@gmail.com>
#          Nicolas Barascud <nicolas.barascud@ens.fr>
#
# License: BSD-3-Clause

sphinx_gallery_thumbnail_number = 3

import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
from os.path import join

import mne
from mne.decoding import ReceptiveField
from sklearn.model_selection import KFold
from sklearn.preprocessing import scale

Load the data from the publication

First we will load the data collected in 1. In this experiment subjects listened to natural speech. Raw EEG and the speech stimulus are provided. We will load these below, downsampling the data in order to speed up computation since we know that our features are primarily low-frequency in nature. Then we’ll visualize both the EEG and speech envelope.

path = mne.datasets.mtrf.data_path()
decim = 2
data = loadmat(join(path, 'speech_data.mat'))
raw = data['EEG'].T
speech = data['envelope'].T
sfreq = float(data['Fs'])
sfreq /= decim
speech = mne.filter.resample(speech, down=decim, npad='auto')
raw = mne.filter.resample(raw, down=decim, npad='auto')

# Read in channel positions and create our MNE objects from the raw data
montage = mne.channels.make_standard_montage('biosemi128')
info = mne.create_info(montage.ch_names, sfreq, 'eeg').set_montage(montage)
raw = mne.io.RawArray(raw, info)
n_channels = len(raw.ch_names)

# Plot a sample of brain and stimulus activity
fig, ax = plt.subplots()
lns = ax.plot(scale(raw[:, :800][0].T), color='k', alpha=.1)
ln1 = ax.plot(scale(speech[0, :800]), color='r', lw=2)
ax.legend([lns[0], ln1[0]], ['EEG', 'Speech Envelope'], frameon=False)
ax.set(title="Sample activity", xlabel="Time (s)")
mne.viz.tight_layout()
Sample activity

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()
Mean prediction score

Out:

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.07s/it]
100%|##########| Sample : 2/2 [00:02<00:00,    1.02s/it]
100%|##########| Sample : 2/2 [00:02<00:00,    1.05s/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,   46.43it/s]
100%|##########| Sample : 2/2 [00:00<00:00,   44.21it/s]
100%|##########| Sample : 2/2 [00:00<00:00,   43.93it/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.55it/s]
100%|##########| Sample : 2/2 [00:00<00:00,   21.39it/s]
100%|##########| Sample : 2/2 [00:00<00:00,   21.63it/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()
  • 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 = -.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<03:14,   43.03it/s]
  1%|1         | Sample : 89/8384 [00:00<00:03, 2328.15it/s]
  2%|2         | Sample : 190/8384 [00:00<00:02, 3539.59it/s]
  3%|3         | Sample : 289/8384 [00:00<00:01, 4175.80it/s]
  5%|4         | Sample : 389/8384 [00:00<00:01, 4595.35it/s]
  6%|5         | Sample : 492/8384 [00:00<00:01, 4918.36it/s]
  7%|7         | Sample : 597/8384 [00:00<00:01, 5174.73it/s]
  8%|8         | Sample : 705/8384 [00:00<00:01, 5390.93it/s]
 10%|9         | Sample : 812/8384 [00:00<00:01, 5552.94it/s]
 11%|#         | Sample : 913/8384 [00:00<00:01, 5643.08it/s]
 12%|#2        | Sample : 1010/8384 [00:00<00:01, 5688.22it/s]
 13%|#3        | Sample : 1114/8384 [00:00<00:01, 5772.86it/s]
 15%|#4        | Sample : 1220/8384 [00:00<00:01, 5855.70it/s]
 16%|#5        | Sample : 1315/8384 [00:00<00:01, 5860.72it/s]
 17%|#6        | Sample : 1418/8384 [00:00<00:01, 5908.61it/s]
 18%|#8        | Sample : 1516/8384 [00:00<00:01, 5925.30it/s]
 19%|#9        | Sample : 1614/8384 [00:00<00:01, 5941.59it/s]
 20%|##        | Sample : 1714/8384 [00:00<00:01, 5963.16it/s]
 22%|##1       | Sample : 1818/8384 [00:00<00:01, 6001.94it/s]
 23%|##2       | Sample : 1907/8384 [00:00<00:01, 5968.24it/s]
 23%|##3       | Sample : 1962/8384 [00:00<00:01, 5779.02it/s]
 24%|##4       | Sample : 2049/8384 [00:00<00:01, 5751.70it/s]
 26%|##5       | Sample : 2147/8384 [00:00<00:01, 5773.96it/s]
 27%|##6       | Sample : 2248/8384 [00:00<00:01, 5808.25it/s]
 28%|##7       | Sample : 2331/8384 [00:00<00:01, 5760.05it/s]
 28%|##8       | Sample : 2384/8384 [00:00<00:01, 5590.94it/s]
 29%|##9       | Sample : 2445/8384 [00:00<00:01, 5471.98it/s]
 30%|###       | Sample : 2542/8384 [00:00<00:01, 5509.30it/s]
 31%|###1      | Sample : 2640/8384 [00:00<00:01, 5545.43it/s]
 32%|###2      | Sample : 2698/8384 [00:00<00:01, 5418.03it/s]
 33%|###2      | Sample : 2760/8384 [00:00<00:01, 5321.39it/s]
 34%|###4      | Sample : 2862/8384 [00:00<00:01, 5383.36it/s]
 35%|###5      | Sample : 2971/8384 [00:00<00:00, 5467.10it/s]
 37%|###6      | Sample : 3078/8384 [00:00<00:00, 5539.21it/s]
 38%|###7      | Sample : 3180/8384 [00:00<00:00, 5587.58it/s]
 39%|###9      | Sample : 3280/8384 [00:00<00:00, 5623.83it/s]
 40%|####      | Sample : 3382/8384 [00:00<00:00, 5666.59it/s]
 42%|####1     | Sample : 3486/8384 [00:00<00:00, 5713.50it/s]
 43%|####2     | Sample : 3591/8384 [00:00<00:00, 5761.76it/s]
 44%|####4     | Sample : 3697/8384 [00:00<00:00, 5809.36it/s]
 45%|####5     | Sample : 3796/8384 [00:00<00:00, 5830.31it/s]
 46%|####6     | Sample : 3886/8384 [00:00<00:00, 5815.86it/s]
 47%|####7     | Sample : 3976/8384 [00:00<00:00, 5805.16it/s]
 49%|####8     | Sample : 4071/8384 [00:00<00:00, 5812.22it/s]
 50%|####9     | Sample : 4167/8384 [00:00<00:00, 5822.39it/s]
 51%|#####     | Sample : 4266/8384 [00:00<00:00, 5839.79it/s]
 52%|#####2    | Sample : 4362/8384 [00:00<00:00, 5847.92it/s]
 53%|#####3    | Sample : 4456/8384 [00:00<00:00, 5846.74it/s]
 54%|#####4    | Sample : 4550/8384 [00:00<00:00, 5846.61it/s]
 55%|#####5    | Sample : 4646/8384 [00:00<00:00, 5852.75it/s]
 57%|#####6    | Sample : 4742/8384 [00:00<00:00, 5858.31it/s]
 58%|#####7    | Sample : 4837/8384 [00:00<00:00, 5861.49it/s]
 59%|#####8    | Sample : 4931/8384 [00:00<00:00, 5861.74it/s]
 60%|#####9    | Sample : 5025/8384 [00:00<00:00, 5861.82it/s]
 61%|######    | Sample : 5111/8384 [00:00<00:00, 5832.65it/s]
 62%|######1   | Sample : 5176/8384 [00:00<00:00, 5736.45it/s]
 63%|######2   | Sample : 5267/8384 [00:00<00:00, 5733.77it/s]
 64%|######4   | Sample : 5373/8384 [00:00<00:00, 5779.34it/s]
 65%|######5   | Sample : 5480/8384 [00:00<00:00, 5825.96it/s]
 67%|######6   | Sample : 5583/8384 [00:00<00:00, 5855.53it/s]
 68%|######7   | Sample : 5684/8384 [00:00<00:00, 5877.74it/s]
 69%|######9   | Sample : 5785/8384 [00:01<00:00, 5899.26it/s]
 70%|#######   | Sample : 5886/8384 [00:01<00:00, 5918.88it/s]
 71%|#######1  | Sample : 5989/8384 [00:01<00:00, 5943.57it/s]
 72%|#######2  | Sample : 6062/8384 [00:01<00:00, 5869.18it/s]
 73%|#######2  | Sample : 6118/8384 [00:01<00:00, 5744.86it/s]
 74%|#######4  | Sample : 6218/8384 [00:01<00:00, 5770.19it/s]
 75%|#######5  | Sample : 6321/8384 [00:01<00:00, 5803.03it/s]
 76%|#######6  | Sample : 6402/8384 [00:01<00:00, 5763.26it/s]
 77%|#######6  | Sample : 6451/8384 [00:01<00:00, 5621.57it/s]
 78%|#######8  | Sample : 6546/8384 [00:01<00:00, 5636.93it/s]
 79%|#######8  | Sample : 6620/8384 [00:01<00:00, 5584.28it/s]
 80%|#######9  | Sample : 6700/8384 [00:01<00:00, 5550.21it/s]
 81%|########  | Sample : 6756/8384 [00:01<00:00, 5445.87it/s]
 81%|########1 | Sample : 6821/8384 [00:01<00:00, 5373.59it/s]
 82%|########1 | Sample : 6873/8384 [00:01<00:00, 5262.18it/s]
 83%|########2 | Sample : 6925/8384 [00:01<00:00, 5156.95it/s]
 84%|########3 | Sample : 7003/8384 [00:01<00:00, 5139.20it/s]
 85%|########4 | Sample : 7097/8384 [00:01<00:00, 5174.73it/s]
 86%|########5 | Sample : 7197/8384 [00:01<00:00, 5228.79it/s]
 87%|########7 | Sample : 7305/8384 [00:01<00:00, 5303.58it/s]
 88%|########8 | Sample : 7415/8384 [00:01<00:00, 5381.54it/s]
 90%|########9 | Sample : 7525/8384 [00:01<00:00, 5454.03it/s]
 91%|#########1| Sample : 7630/8384 [00:01<00:00, 5508.45it/s]
 92%|#########2| Sample : 7739/8384 [00:01<00:00, 5572.17it/s]
 94%|#########3| Sample : 7849/8384 [00:01<00:00, 5635.18it/s]
 95%|#########4| Sample : 7962/8384 [00:01<00:00, 5706.48it/s]
 96%|#########6| Sample : 8075/8384 [00:01<00:00, 5772.41it/s]
 98%|#########7| Sample : 8186/8384 [00:01<00:00, 5828.84it/s]
 99%|#########8| Sample : 8300/8384 [00:01<00:00, 5893.11it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 5713.54it/s]
split 2 / 3
Fitting 1 epochs, 128 channels

  0%|          | Sample : 0/8384 [00:00<?,       ?it/s]
  0%|          | Sample : 1/8384 [00:00<11:27,   12.19it/s]
  1%|1         | Sample : 90/8384 [00:00<00:08,  955.79it/s]
  2%|2         | Sample : 177/8384 [00:00<00:05, 1633.91it/s]
  3%|3         | Sample : 267/8384 [00:00<00:03, 2180.40it/s]
  4%|4         | Sample : 359/8384 [00:00<00:03, 2627.49it/s]
  5%|5         | Sample : 448/8384 [00:00<00:02, 2968.75it/s]
  6%|6         | Sample : 540/8384 [00:00<00:02, 3270.45it/s]
  7%|7         | Sample : 627/8384 [00:00<00:02, 3491.70it/s]
  9%|8         | Sample : 716/8384 [00:00<00:02, 3694.58it/s]
 10%|9         | Sample : 806/8384 [00:00<00:01, 3871.28it/s]
 11%|#         | Sample : 895/8384 [00:00<00:01, 4019.79it/s]
 12%|#1        | Sample : 981/8384 [00:00<00:01, 4132.77it/s]
 13%|#2        | Sample : 1068/8384 [00:00<00:01, 4238.67it/s]
 14%|#3        | Sample : 1157/8384 [00:00<00:01, 4344.57it/s]
 15%|#4        | Sample : 1247/8384 [00:00<00:01, 4439.74it/s]
 16%|#5        | Sample : 1340/8384 [00:00<00:01, 4540.17it/s]
 17%|#7        | Sample : 1427/8384 [00:00<00:01, 4604.23it/s]
 18%|#8        | Sample : 1515/8384 [00:00<00:01, 4668.50it/s]
 19%|#9        | Sample : 1604/8384 [00:00<00:01, 4728.09it/s]
 20%|#9        | Sample : 1653/8384 [00:00<00:01, 4609.03it/s]
 21%|##        | Sample : 1743/8384 [00:00<00:01, 4677.59it/s]
 22%|##1       | Sample : 1844/8384 [00:00<00:01, 4783.98it/s]
 23%|##3       | Sample : 1944/8384 [00:00<00:01, 4877.38it/s]
 24%|##4       | Sample : 2044/8384 [00:00<00:01, 4965.09it/s]
 26%|##5       | Sample : 2146/8384 [00:00<00:01, 5051.86it/s]
 27%|##6       | Sample : 2246/8384 [00:00<00:01, 5125.69it/s]
 28%|##7       | Sample : 2347/8384 [00:00<00:01, 5197.31it/s]
 29%|##9       | Sample : 2447/8384 [00:00<00:01, 5261.59it/s]
 30%|###       | Sample : 2549/8384 [00:00<00:01, 5327.39it/s]
 32%|###1      | Sample : 2651/8384 [00:00<00:01, 5387.64it/s]
 33%|###2      | Sample : 2751/8384 [00:00<00:01, 5438.42it/s]
 34%|###4      | Sample : 2851/8384 [00:00<00:01, 5483.36it/s]
 35%|###5      | Sample : 2952/8384 [00:00<00:00, 5529.18it/s]
 36%|###6      | Sample : 3054/8384 [00:00<00:00, 5576.20it/s]
 38%|###7      | Sample : 3155/8384 [00:00<00:00, 5617.87it/s]
 39%|###8      | Sample : 3254/8384 [00:00<00:00, 5650.14it/s]
 40%|###9      | Sample : 3353/8384 [00:00<00:00, 5679.23it/s]
 41%|####1     | Sample : 3455/8384 [00:00<00:00, 5716.09it/s]
 42%|####2     | Sample : 3556/8384 [00:00<00:00, 5748.63it/s]
 44%|####3     | Sample : 3656/8384 [00:00<00:00, 5776.00it/s]
 45%|####4     | Sample : 3758/8384 [00:00<00:00, 5806.64it/s]
 46%|####6     | Sample : 3860/8384 [00:00<00:00, 5835.72it/s]
 47%|####7     | Sample : 3960/8384 [00:00<00:00, 5857.61it/s]
 48%|####8     | Sample : 4061/8384 [00:00<00:00, 5880.73it/s]
 50%|####9     | Sample : 4163/8384 [00:00<00:00, 5905.14it/s]
 51%|#####     | Sample : 4264/8384 [00:00<00:00, 5923.44it/s]
 52%|#####2    | Sample : 4365/8384 [00:00<00:00, 5942.32it/s]
 53%|#####3    | Sample : 4466/8384 [00:00<00:00, 5961.44it/s]
 54%|#####4    | Sample : 4569/8384 [00:00<00:00, 5985.22it/s]
 56%|#####5    | Sample : 4670/8384 [00:00<00:00, 6002.52it/s]
 56%|#####6    | Sample : 4722/8384 [00:00<00:00, 5856.05it/s]
 57%|#####7    | Sample : 4804/8384 [00:00<00:00, 5815.52it/s]
 58%|#####8    | Sample : 4902/8384 [00:00<00:00, 5830.97it/s]
 60%|#####9    | Sample : 5002/8384 [00:00<00:00, 5851.07it/s]
 61%|######    | Sample : 5104/8384 [00:00<00:00, 5877.48it/s]
 62%|######2   | Sample : 5205/8384 [00:00<00:00, 5898.38it/s]
 63%|######3   | Sample : 5306/8384 [00:00<00:00, 5918.24it/s]
 64%|######4   | Sample : 5407/8384 [00:00<00:00, 5938.10it/s]
 66%|######5   | Sample : 5509/8384 [00:01<00:00, 5958.26it/s]
 67%|######6   | Sample : 5610/8384 [00:01<00:00, 5973.70it/s]
 68%|######8   | Sample : 5711/8384 [00:01<00:00, 5988.66it/s]
 69%|######9   | Sample : 5812/8384 [00:01<00:00, 6005.22it/s]
 71%|#######   | Sample : 5914/8384 [00:01<00:00, 6022.99it/s]
 72%|#######1  | Sample : 6016/8384 [00:01<00:00, 6039.03it/s]
 73%|#######2  | Sample : 6120/8384 [00:01<00:00, 6060.26it/s]
 74%|#######4  | Sample : 6223/8384 [00:01<00:00, 6077.95it/s]
 75%|#######5  | Sample : 6325/8384 [00:01<00:00, 6092.21it/s]
 77%|#######6  | Sample : 6429/8384 [00:01<00:00, 6110.97it/s]
 78%|#######7  | Sample : 6534/8384 [00:01<00:00, 6132.68it/s]
 79%|#######9  | Sample : 6638/8384 [00:01<00:00, 6150.73it/s]
 80%|########  | Sample : 6743/8384 [00:01<00:00, 6169.32it/s]
 82%|########1 | Sample : 6849/8384 [00:01<00:00, 6189.64it/s]
 83%|########2 | Sample : 6954/8384 [00:01<00:00, 6205.64it/s]
 84%|########4 | Sample : 7055/8384 [00:01<00:00, 6210.89it/s]
 85%|########5 | Sample : 7156/8384 [00:01<00:00, 6215.20it/s]
 87%|########6 | Sample : 7261/8384 [00:01<00:00, 6232.02it/s]
 88%|########7 | Sample : 7367/8384 [00:01<00:00, 6251.61it/s]
 89%|########9 | Sample : 7473/8384 [00:01<00:00, 6270.27it/s]
 90%|######### | Sample : 7578/8384 [00:01<00:00, 6284.81it/s]
 92%|#########1| Sample : 7684/8384 [00:01<00:00, 6299.87it/s]
 93%|#########2| Sample : 7791/8384 [00:01<00:00, 6319.37it/s]
 94%|#########3| Sample : 7849/8384 [00:01<00:00, 6178.88it/s]
 95%|#########4| Sample : 7931/8384 [00:01<00:00, 6124.40it/s]
 96%|#########5| Sample : 8034/8384 [00:01<00:00, 6139.66it/s]
 97%|#########7| Sample : 8140/8384 [00:01<00:00, 6162.90it/s]
 98%|#########8| Sample : 8249/8384 [00:01<00:00, 6194.21it/s]
100%|#########9| Sample : 8361/8384 [00:01<00:00, 6232.77it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 5701.72it/s]
split 3 / 3
Fitting 1 epochs, 128 channels

  0%|          | Sample : 0/8384 [00:00<?,       ?it/s]
  0%|          | Sample : 1/8384 [00:00<02:32,   55.14it/s]
  1%|1         | Sample : 103/8384 [00:00<00:02, 3095.26it/s]
  2%|2         | Sample : 205/8384 [00:00<00:01, 4196.40it/s]
  4%|3         | Sample : 309/8384 [00:00<00:01, 4789.75it/s]
  5%|4         | Sample : 410/8384 [00:00<00:01, 5113.46it/s]
  6%|6         | Sample : 512/8384 [00:00<00:01, 5335.11it/s]
  7%|7         | Sample : 616/8384 [00:00<00:01, 5515.04it/s]
  9%|8         | Sample : 721/8384 [00:00<00:01, 5664.89it/s]
 10%|9         | Sample : 825/8384 [00:00<00:01, 5768.89it/s]
 11%|#1        | Sample : 929/8384 [00:00<00:01, 5853.19it/s]
 12%|#2        | Sample : 1031/8384 [00:00<00:01, 5910.70it/s]
 14%|#3        | Sample : 1133/8384 [00:00<00:01, 5957.36it/s]
 15%|#4        | Sample : 1235/8384 [00:00<00:01, 5998.78it/s]
 16%|#5        | Sample : 1337/8384 [00:00<00:01, 6032.12it/s]
 17%|#7        | Sample : 1440/8384 [00:00<00:01, 6069.14it/s]
 18%|#8        | Sample : 1542/8384 [00:00<00:01, 6095.57it/s]
 20%|#9        | Sample : 1645/8384 [00:00<00:01, 6124.06it/s]
 21%|##        | Sample : 1747/8384 [00:00<00:01, 6141.55it/s]
 22%|##2       | Sample : 1849/8384 [00:00<00:01, 6156.76it/s]
 23%|##3       | Sample : 1950/8384 [00:00<00:01, 6165.74it/s]
 24%|##4       | Sample : 2052/8384 [00:00<00:01, 6180.96it/s]
 26%|##5       | Sample : 2154/8384 [00:00<00:01, 6192.23it/s]
 27%|##6       | Sample : 2256/8384 [00:00<00:00, 6204.92it/s]
 28%|##8       | Sample : 2358/8384 [00:00<00:00, 6216.39it/s]
 29%|##9       | Sample : 2458/8384 [00:00<00:00, 6216.07it/s]
 31%|###       | Sample : 2559/8384 [00:00<00:00, 6218.76it/s]
 32%|###1      | Sample : 2654/8384 [00:00<00:00, 6199.79it/s]
 33%|###2      | Sample : 2755/8384 [00:00<00:00, 6206.08it/s]
 34%|###4      | Sample : 2857/8384 [00:00<00:00, 6216.15it/s]
 35%|###5      | Sample : 2962/8384 [00:00<00:00, 6235.05it/s]
 37%|###6      | Sample : 3066/8384 [00:00<00:00, 6250.62it/s]
 38%|###7      | Sample : 3171/8384 [00:00<00:00, 6268.74it/s]
 39%|###9      | Sample : 3276/8384 [00:00<00:00, 6283.48it/s]
 40%|####      | Sample : 3381/8384 [00:00<00:00, 6299.05it/s]
 42%|####1     | Sample : 3487/8384 [00:00<00:00, 6317.21it/s]
 43%|####2     | Sample : 3592/8384 [00:00<00:00, 6330.48it/s]
 44%|####4     | Sample : 3696/8384 [00:00<00:00, 6339.62it/s]
 45%|####5     | Sample : 3798/8384 [00:00<00:00, 6341.34it/s]
 47%|####6     | Sample : 3902/8384 [00:00<00:00, 6348.72it/s]
 48%|####7     | Sample : 4005/8384 [00:00<00:00, 6353.68it/s]
 49%|####8     | Sample : 4108/8384 [00:00<00:00, 6355.11it/s]
 50%|#####     | Sample : 4210/8384 [00:00<00:00, 6355.80it/s]
 51%|#####1    | Sample : 4313/8384 [00:00<00:00, 6358.35it/s]
 53%|#####2    | Sample : 4417/8384 [00:00<00:00, 6363.45it/s]
 54%|#####3    | Sample : 4520/8384 [00:00<00:00, 6367.25it/s]
 55%|#####5    | Sample : 4624/8384 [00:00<00:00, 6373.73it/s]
 56%|#####6    | Sample : 4728/8384 [00:00<00:00, 6378.88it/s]
 58%|#####7    | Sample : 4830/8384 [00:00<00:00, 6378.02it/s]
 59%|#####8    | Sample : 4932/8384 [00:00<00:00, 6377.16it/s]
 60%|######    | Sample : 5035/8384 [00:00<00:00, 6378.39it/s]
 61%|######1   | Sample : 5135/8384 [00:00<00:00, 6371.29it/s]
 62%|######2   | Sample : 5238/8384 [00:00<00:00, 6373.33it/s]
 64%|######3   | Sample : 5345/8384 [00:00<00:00, 6388.38it/s]
 65%|######5   | Sample : 5450/8384 [00:00<00:00, 6395.78it/s]
 66%|######6   | Sample : 5556/8384 [00:00<00:00, 6405.73it/s]
 68%|######7   | Sample : 5661/8384 [00:00<00:00, 6412.22it/s]
 69%|######8   | Sample : 5764/8384 [00:00<00:00, 6411.42it/s]
 70%|######9   | Sample : 5867/8384 [00:00<00:00, 6411.77it/s]
 71%|#######1  | Sample : 5969/8384 [00:00<00:00, 6409.08it/s]
 72%|#######2  | Sample : 6069/8384 [00:00<00:00, 6398.21it/s]
 74%|#######3  | Sample : 6170/8384 [00:00<00:00, 6393.11it/s]
 75%|#######4  | Sample : 6271/8384 [00:00<00:00, 6388.58it/s]
 76%|#######5  | Sample : 6371/8384 [00:01<00:00, 6380.18it/s]
 77%|#######7  | Sample : 6473/8384 [00:01<00:00, 6379.14it/s]
 78%|#######8  | Sample : 6575/8384 [00:01<00:00, 6376.42it/s]
 80%|#######9  | Sample : 6681/8384 [00:01<00:00, 6388.29it/s]
 81%|########  | Sample : 6786/8384 [00:01<00:00, 6395.93it/s]
 82%|########2 | Sample : 6893/8384 [00:01<00:00, 6410.22it/s]
 83%|########3 | Sample : 7000/8384 [00:01<00:00, 6423.29it/s]
 85%|########4 | Sample : 7106/8384 [00:01<00:00, 6431.34it/s]
 86%|########5 | Sample : 7209/8384 [00:01<00:00, 6430.70it/s]
 87%|########7 | Sample : 7314/8384 [00:01<00:00, 6434.66it/s]
 89%|########8 | Sample : 7423/8384 [00:01<00:00, 6451.90it/s]
 90%|########9 | Sample : 7529/8384 [00:01<00:00, 6459.42it/s]
 91%|#########1| Sample : 7635/8384 [00:01<00:00, 6466.03it/s]
 92%|#########1| Sample : 7712/8384 [00:01<00:00, 6381.58it/s]
 93%|#########2| Sample : 7764/8384 [00:01<00:00, 6221.48it/s]
 93%|#########3| Sample : 7816/8384 [00:01<00:00, 6067.05it/s]
 94%|#########4| Sample : 7884/8384 [00:01<00:00, 5973.74it/s]
 95%|#########5| Sample : 7997/8384 [00:01<00:00, 6028.30it/s]
 96%|#########6| Sample : 8062/8384 [00:01<00:00, 5928.03it/s]
 97%|#########6| Sample : 8116/8384 [00:01<00:00, 5795.97it/s]
 97%|#########7| Sample : 8168/8384 [00:01<00:00, 5665.25it/s]
 98%|#########8| Sample : 8257/8384 [00:01<00:00, 5660.02it/s]
100%|#########9| Sample : 8376/8384 [00:01<00:00, 5748.91it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 6122.06it/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()
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 = (-.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()
Model coefficients between delays -0.14 and -0.125, Inverse-transformed coefficients between delays -0.14 and -0.125

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 21.567 seconds)

Estimated memory usage: 45 MB

Gallery generated by Sphinx-Gallery