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()
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:01<00:01,    1.99s/it]
100%|##########| Sample : 2/2 [00:02<00:00,    1.01it/s]
100%|##########| Sample : 2/2 [00:02<00:00,    1.01s/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,   13.80it/s]
100%|##########| Sample : 2/2 [00:00<00:00,   19.81it/s]
100%|##########| Sample : 2/2 [00:00<00:00,   19.52it/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,   20.54it/s]
100%|##########| Sample : 2/2 [00:00<00:00,   23.46it/s]
100%|##########| Sample : 2/2 [00:00<00:00,   23.25it/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<05:53,   23.72it/s]
  1%|          | Sample : 78/8384 [00:00<00:05, 1390.30it/s]
  2%|1         | Sample : 161/8384 [00:00<00:03, 2262.47it/s]
  3%|2         | Sample : 243/8384 [00:00<00:02, 2820.76it/s]
  4%|3         | Sample : 323/8384 [00:00<00:02, 3186.94it/s]
  5%|4         | Sample : 408/8384 [00:00<00:02, 3505.21it/s]
  6%|5         | Sample : 495/8384 [00:00<00:02, 3771.06it/s]
  7%|6         | Sample : 580/8384 [00:00<00:01, 3960.54it/s]
  8%|7         | Sample : 663/8384 [00:00<00:01, 4101.87it/s]
  9%|8         | Sample : 739/8384 [00:00<00:01, 4169.79it/s]
 10%|9         | Sample : 815/8384 [00:00<00:01, 4225.44it/s]
 11%|#         | Sample : 901/8384 [00:00<00:01, 4335.44it/s]
 12%|#1        | Sample : 990/8384 [00:00<00:01, 4448.70it/s]
 13%|#2        | Sample : 1060/8384 [00:00<00:01, 4437.12it/s]
 13%|#3        | Sample : 1107/8384 [00:00<00:01, 4302.80it/s]
 14%|#3        | Sample : 1156/8384 [00:00<00:01, 4193.16it/s]
 14%|#4        | Sample : 1203/8384 [00:00<00:01, 4089.49it/s]
 15%|#4        | Sample : 1253/8384 [00:00<00:01, 4008.19it/s]
 16%|#5        | Sample : 1302/8384 [00:00<00:01, 3931.43it/s]
 16%|#6        | Sample : 1376/8384 [00:00<00:01, 3980.24it/s]
 17%|#7        | Sample : 1458/8384 [00:00<00:01, 4061.10it/s]
 18%|#8        | Sample : 1541/8384 [00:00<00:01, 4139.87it/s]
 19%|#9        | Sample : 1623/8384 [00:00<00:01, 4205.64it/s]
 20%|##        | Sample : 1706/8384 [00:00<00:01, 4271.36it/s]
 21%|##1       | Sample : 1791/8384 [00:00<00:01, 4339.03it/s]
 22%|##2       | Sample : 1876/8384 [00:00<00:01, 4401.62it/s]
 23%|##3       | Sample : 1960/8384 [00:00<00:01, 4454.04it/s]
 24%|##4       | Sample : 2046/8384 [00:00<00:01, 4509.12it/s]
 25%|##5       | Sample : 2132/8384 [00:00<00:01, 4562.88it/s]
 26%|##6       | Sample : 2220/8384 [00:00<00:01, 4617.72it/s]
 28%|##7       | Sample : 2307/8384 [00:00<00:01, 4665.80it/s]
 29%|##8       | Sample : 2394/8384 [00:00<00:01, 4712.02it/s]
 30%|##9       | Sample : 2482/8384 [00:00<00:01, 4757.06it/s]
 31%|###       | Sample : 2568/8384 [00:00<00:01, 4793.34it/s]
 32%|###1      | Sample : 2652/8384 [00:00<00:01, 4818.14it/s]
 33%|###2      | Sample : 2737/8384 [00:00<00:01, 4846.76it/s]
 34%|###3      | Sample : 2824/8384 [00:00<00:01, 4877.89it/s]
 35%|###4      | Sample : 2910/8384 [00:00<00:01, 4904.11it/s]
 36%|###5      | Sample : 2996/8384 [00:00<00:01, 4929.63it/s]
 37%|###6      | Sample : 3080/8384 [00:00<00:01, 4947.07it/s]
 38%|###7      | Sample : 3159/8384 [00:00<00:01, 4944.66it/s]
 39%|###8      | Sample : 3245/8384 [00:00<00:01, 4965.57it/s]
 40%|###9      | Sample : 3331/8384 [00:00<00:01, 4987.59it/s]
 41%|####      | Sample : 3416/8384 [00:00<00:00, 5004.20it/s]
 42%|####1     | Sample : 3504/8384 [00:00<00:00, 5028.86it/s]
 43%|####2     | Sample : 3591/8384 [00:00<00:00, 5050.42it/s]
 44%|####3     | Sample : 3678/8384 [00:00<00:00, 5069.07it/s]
 45%|####4     | Sample : 3764/8384 [00:00<00:00, 5085.29it/s]
 46%|####5     | Sample : 3851/8384 [00:00<00:00, 5102.00it/s]
 47%|####6     | Sample : 3935/8384 [00:00<00:00, 5108.39it/s]
 48%|####7     | Sample : 4022/8384 [00:00<00:00, 5124.34it/s]
 49%|####8     | Sample : 4106/8384 [00:00<00:00, 5129.18it/s]
 50%|#####     | Sample : 4193/8384 [00:00<00:00, 5143.46it/s]
 51%|#####1    | Sample : 4281/8384 [00:00<00:00, 5161.02it/s]
 52%|#####2    | Sample : 4366/8384 [00:00<00:00, 5166.62it/s]
 53%|#####2    | Sample : 4443/8384 [00:00<00:00, 5146.21it/s]
 54%|#####3    | Sample : 4524/8384 [00:00<00:00, 5141.15it/s]
 55%|#####4    | Sample : 4605/8384 [00:00<00:00, 5136.48it/s]
 56%|#####5    | Sample : 4686/8384 [00:00<00:00, 5130.46it/s]
 57%|#####6    | Sample : 4770/8384 [00:00<00:00, 5134.20it/s]
 58%|#####7    | Sample : 4855/8384 [00:01<00:00, 5142.34it/s]
 59%|#####8    | Sample : 4939/8384 [00:01<00:00, 5147.13it/s]
 60%|#####9    | Sample : 5021/8384 [00:01<00:00, 5142.83it/s]
 61%|######    | Sample : 5104/8384 [00:01<00:00, 5145.04it/s]
 62%|######1   | Sample : 5189/8384 [00:01<00:00, 5150.97it/s]
 63%|######2   | Sample : 5273/8384 [00:01<00:00, 5153.43it/s]
 64%|######3   | Sample : 5355/8384 [00:01<00:00, 5149.55it/s]
 65%|######4   | Sample : 5439/8384 [00:01<00:00, 5152.83it/s]
 66%|######5   | Sample : 5523/8384 [00:01<00:00, 5156.00it/s]
 67%|######6   | Sample : 5610/8384 [00:01<00:00, 5167.42it/s]
 68%|######7   | Sample : 5697/8384 [00:01<00:00, 5179.09it/s]
 69%|######8   | Sample : 5783/8384 [00:01<00:00, 5186.81it/s]
 70%|######9   | Sample : 5868/8384 [00:01<00:00, 5192.03it/s]
 71%|#######1  | Sample : 5956/8384 [00:01<00:00, 5206.34it/s]
 72%|#######2  | Sample : 6046/8384 [00:01<00:00, 5227.26it/s]
 73%|#######3  | Sample : 6134/8384 [00:01<00:00, 5238.93it/s]
 74%|#######4  | Sample : 6222/8384 [00:01<00:00, 5251.92it/s]
 75%|#######5  | Sample : 6310/8384 [00:01<00:00, 5262.55it/s]
 76%|#######6  | Sample : 6397/8384 [00:01<00:00, 5269.28it/s]
 77%|#######7  | Sample : 6486/8384 [00:01<00:00, 5283.87it/s]
 78%|#######8  | Sample : 6572/8384 [00:01<00:00, 5288.41it/s]
 79%|#######9  | Sample : 6662/8384 [00:01<00:00, 5304.29it/s]
 81%|########  | Sample : 6753/8384 [00:01<00:00, 5321.45it/s]
 82%|########1 | Sample : 6841/8384 [00:01<00:00, 5329.34it/s]
 83%|########2 | Sample : 6930/8384 [00:01<00:00, 5340.44it/s]
 84%|########3 | Sample : 7021/8384 [00:01<00:00, 5356.07it/s]
 84%|########4 | Sample : 7078/8384 [00:01<00:00, 5262.42it/s]
 85%|########5 | Sample : 7127/8384 [00:01<00:00, 5151.61it/s]
 86%|########5 | Sample : 7177/8384 [00:01<00:00, 5047.52it/s]
 86%|########6 | Sample : 7226/8384 [00:01<00:00, 4944.29it/s]
 87%|########6 | Sample : 7285/8384 [00:01<00:00, 4880.31it/s]
 88%|########7 | Sample : 7376/8384 [00:01<00:00, 4919.78it/s]
 89%|########9 | Sample : 7473/8384 [00:01<00:00, 4975.66it/s]
 90%|######### | Sample : 7569/8384 [00:01<00:00, 5024.31it/s]
 91%|#########1| Sample : 7667/8384 [00:01<00:00, 5078.57it/s]
 93%|#########2| Sample : 7756/8384 [00:01<00:00, 5101.27it/s]
 94%|#########3| Sample : 7842/8384 [00:01<00:00, 5114.93it/s]
 95%|#########4| Sample : 7943/8384 [00:01<00:00, 5174.75it/s]
 96%|#########5| Sample : 8046/8384 [00:01<00:00, 5237.16it/s]
 97%|#########7| Sample : 8150/8384 [00:01<00:00, 5298.31it/s]
 98%|#########8| Sample : 8256/8384 [00:01<00:00, 5362.90it/s]
 99%|#########9| Sample : 8317/8384 [00:01<00:00, 5282.73it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 4980.97it/s]
split 2 / 3
Fitting 1 epochs, 128 channels

  0%|          | Sample : 0/8384 [00:00<?,       ?it/s]
  0%|          | Sample : 1/8384 [00:00<14:51,    9.40it/s]
  1%|1         | Sample : 85/8384 [00:00<00:11,  724.90it/s]
  2%|1         | Sample : 167/8384 [00:00<00:06, 1276.48it/s]
  3%|3         | Sample : 253/8384 [00:00<00:04, 1752.52it/s]
  4%|3         | Sample : 333/8384 [00:00<00:03, 2103.65it/s]
  5%|4         | Sample : 378/8384 [00:00<00:03, 2172.79it/s]
  5%|5         | Sample : 437/8384 [00:00<00:03, 2320.67it/s]
  6%|6         | Sample : 524/8384 [00:00<00:03, 2609.46it/s]
  7%|7         | Sample : 613/8384 [00:00<00:02, 2872.12it/s]
  8%|8         | Sample : 700/8384 [00:00<00:02, 3091.97it/s]
  9%|9         | Sample : 786/8384 [00:00<00:02, 3279.30it/s]
 10%|9         | Sample : 835/8384 [00:00<00:02, 3256.04it/s]
 10%|#         | Sample : 880/8384 [00:00<00:02, 3220.13it/s]
 11%|#1        | Sample : 933/8384 [00:00<00:02, 3226.41it/s]
 12%|#2        | Sample : 1020/8384 [00:00<00:02, 3388.03it/s]
 13%|#3        | Sample : 1109/8384 [00:00<00:02, 3541.59it/s]
 14%|#4        | Sample : 1197/8384 [00:00<00:01, 3677.13it/s]
 15%|#5        | Sample : 1270/8384 [00:00<00:01, 3738.14it/s]
 16%|#6        | Sample : 1356/8384 [00:00<00:01, 3846.50it/s]
 17%|#7        | Sample : 1442/8384 [00:00<00:01, 3944.89it/s]
 18%|#8        | Sample : 1529/8384 [00:00<00:01, 4039.32it/s]
 19%|#9        | Sample : 1616/8384 [00:00<00:01, 4126.44it/s]
 20%|##        | Sample : 1702/8384 [00:00<00:01, 4204.32it/s]
 21%|##1       | Sample : 1788/8384 [00:00<00:01, 4275.89it/s]
 22%|##2       | Sample : 1874/8384 [00:00<00:01, 4342.42it/s]
 23%|##3       | Sample : 1960/8384 [00:00<00:01, 4404.47it/s]
 24%|##4       | Sample : 2047/8384 [00:00<00:01, 4465.50it/s]
 25%|##5       | Sample : 2133/8384 [00:00<00:01, 4518.39it/s]
 26%|##6       | Sample : 2218/8384 [00:00<00:01, 4564.42it/s]
 27%|##7       | Sample : 2305/8384 [00:00<00:01, 4614.34it/s]
 29%|##8       | Sample : 2393/8384 [00:00<00:01, 4663.32it/s]
 30%|##9       | Sample : 2481/8384 [00:00<00:01, 4710.52it/s]
 31%|###       | Sample : 2567/8384 [00:00<00:01, 4746.53it/s]
 32%|###1      | Sample : 2652/8384 [00:00<00:01, 4778.40it/s]
 33%|###2      | Sample : 2738/8384 [00:00<00:01, 4811.18it/s]
 34%|###3      | Sample : 2825/8384 [00:00<00:01, 4845.06it/s]
 35%|###4      | Sample : 2912/8384 [00:00<00:01, 4876.45it/s]
 36%|###5      | Sample : 2998/8384 [00:00<00:01, 4902.74it/s]
 37%|###6      | Sample : 3084/8384 [00:00<00:01, 4927.79it/s]
 38%|###7      | Sample : 3171/8384 [00:00<00:01, 4953.11it/s]
 39%|###8      | Sample : 3258/8384 [00:00<00:01, 4977.19it/s]
 40%|###9      | Sample : 3345/8384 [00:00<00:01, 5002.07it/s]
 41%|####      | Sample : 3432/8384 [00:00<00:00, 5022.94it/s]
 42%|####1     | Sample : 3518/8384 [00:00<00:00, 5041.22it/s]
 43%|####2     | Sample : 3605/8384 [00:00<00:00, 5059.69it/s]
 44%|####4     | Sample : 3690/8384 [00:00<00:00, 5072.08it/s]
 45%|####5     | Sample : 3775/8384 [00:00<00:00, 5083.33it/s]
 46%|####6     | Sample : 3861/8384 [00:00<00:00, 5097.02it/s]
 47%|####7     | Sample : 3948/8384 [00:00<00:00, 5113.68it/s]
 48%|####8     | Sample : 4035/8384 [00:00<00:00, 5129.49it/s]
 49%|####9     | Sample : 4122/8384 [00:00<00:00, 5145.28it/s]
 50%|#####     | Sample : 4208/8384 [00:00<00:00, 5156.68it/s]
 51%|#####1    | Sample : 4293/8384 [00:00<00:00, 5163.76it/s]
 52%|#####2    | Sample : 4378/8384 [00:00<00:00, 5171.19it/s]
 53%|#####3    | Sample : 4464/8384 [00:00<00:00, 5179.57it/s]
 54%|#####4    | Sample : 4549/8384 [00:00<00:00, 5183.65it/s]
 55%|#####5    | Sample : 4632/8384 [00:01<00:00, 5181.71it/s]
 56%|#####6    | Sample : 4713/8384 [00:01<00:00, 5174.11it/s]
 57%|#####7    | Sample : 4797/8384 [00:01<00:00, 5175.54it/s]
 58%|#####8    | Sample : 4881/8384 [00:01<00:00, 5178.17it/s]
 59%|#####9    | Sample : 4969/8384 [00:01<00:00, 5194.62it/s]
 60%|######    | Sample : 5053/8384 [00:01<00:00, 5196.27it/s]
 61%|######    | Sample : 5097/8384 [00:01<00:00, 5066.74it/s]
 61%|######1   | Sample : 5141/8384 [00:01<00:00, 4946.65it/s]
 62%|######1   | Sample : 5190/8384 [00:01<00:00, 4847.59it/s]
 63%|######2   | Sample : 5272/8384 [00:01<00:00, 4859.92it/s]
 64%|######3   | Sample : 5357/8384 [00:01<00:00, 4881.11it/s]
 65%|######4   | Sample : 5441/8384 [00:01<00:00, 4898.43it/s]
 66%|######5   | Sample : 5527/8384 [00:01<00:00, 4920.81it/s]
 67%|######6   | Sample : 5612/8384 [00:01<00:00, 4937.72it/s]
 68%|######7   | Sample : 5694/8384 [00:01<00:00, 4944.46it/s]
 69%|######8   | Sample : 5778/8384 [00:01<00:00, 4956.85it/s]
 70%|######9   | Sample : 5860/8384 [00:01<00:00, 4962.66it/s]
 71%|#######   | Sample : 5946/8384 [00:01<00:00, 4983.10it/s]
 72%|#######1  | Sample : 6033/8384 [00:01<00:00, 5004.39it/s]
 73%|#######2  | Sample : 6119/8384 [00:01<00:00, 5021.95it/s]
 74%|#######3  | Sample : 6204/8384 [00:01<00:00, 5035.82it/s]
 75%|#######5  | Sample : 6292/8384 [00:01<00:00, 5056.92it/s]
 76%|#######6  | Sample : 6379/8384 [00:01<00:00, 5073.30it/s]
 77%|#######7  | Sample : 6467/8384 [00:01<00:00, 5092.52it/s]
 78%|#######8  | Sample : 6560/8384 [00:01<00:00, 5126.55it/s]
 79%|#######9  | Sample : 6659/8384 [00:01<00:00, 5179.60it/s]
 81%|########  | Sample : 6755/8384 [00:01<00:00, 5219.98it/s]
 82%|########1 | Sample : 6848/8384 [00:01<00:00, 5247.31it/s]
 83%|########2 | Sample : 6939/8384 [00:01<00:00, 5267.02it/s]
 84%|########3 | Sample : 7033/8384 [00:01<00:00, 5296.22it/s]
 85%|########5 | Sample : 7128/8384 [00:01<00:00, 5326.76it/s]
 86%|########6 | Sample : 7223/8384 [00:01<00:00, 5354.94it/s]
 87%|########7 | Sample : 7315/8384 [00:01<00:00, 5372.72it/s]
 88%|########8 | Sample : 7410/8384 [00:01<00:00, 5399.88it/s]
 90%|########9 | Sample : 7507/8384 [00:01<00:00, 5430.61it/s]
 91%|######### | Sample : 7607/8384 [00:01<00:00, 5469.99it/s]
 92%|#########1| Sample : 7703/8384 [00:01<00:00, 5495.39it/s]
 93%|#########3| Sample : 7803/8384 [00:01<00:00, 5532.29it/s]
 94%|#########4| Sample : 7903/8384 [00:01<00:00, 5567.19it/s]
 95%|#########5| Sample : 8005/8384 [00:01<00:00, 5605.88it/s]
 97%|#########6| Sample : 8108/8384 [00:01<00:00, 5644.94it/s]
 98%|#########7| Sample : 8210/8384 [00:01<00:00, 5679.64it/s]
 99%|#########9| Sample : 8312/8384 [00:01<00:00, 5714.31it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 4943.08it/s]
split 3 / 3
Fitting 1 epochs, 128 channels

  0%|          | Sample : 0/8384 [00:00<?,       ?it/s]
  0%|          | Sample : 1/8384 [00:00<04:23,   31.77it/s]
  1%|1         | Sample : 92/8384 [00:00<00:04, 2000.47it/s]
  2%|2         | Sample : 173/8384 [00:00<00:02, 2815.16it/s]
  3%|3         | Sample : 259/8384 [00:00<00:02, 3375.95it/s]
  4%|4         | Sample : 344/8384 [00:00<00:02, 3733.02it/s]
  5%|4         | Sample : 402/8384 [00:00<00:02, 3712.63it/s]
  6%|5         | Sample : 489/8384 [00:00<00:01, 3964.60it/s]
  7%|6         | Sample : 574/8384 [00:00<00:01, 4143.69it/s]
  8%|7         | Sample : 659/8384 [00:00<00:01, 4286.73it/s]
  9%|8         | Sample : 745/8384 [00:00<00:01, 4412.18it/s]
 10%|9         | Sample : 831/8384 [00:00<00:01, 4514.57it/s]
 11%|#         | Sample : 898/8384 [00:00<00:01, 4478.66it/s]
 12%|#1        | Sample : 984/8384 [00:00<00:01, 4565.56it/s]
 13%|#2        | Sample : 1069/8384 [00:00<00:01, 4630.67it/s]
 14%|#3        | Sample : 1152/8384 [00:00<00:01, 4679.84it/s]
 15%|#4        | Sample : 1238/8384 [00:00<00:01, 4737.26it/s]
 16%|#5        | Sample : 1327/8384 [00:00<00:01, 4804.66it/s]
 17%|#6        | Sample : 1397/8384 [00:00<00:01, 4764.93it/s]
 18%|#7        | Sample : 1480/8384 [00:00<00:01, 4797.55it/s]
 19%|#8        | Sample : 1561/8384 [00:00<00:01, 4815.74it/s]
 20%|#9        | Sample : 1644/8384 [00:00<00:01, 4842.83it/s]
 21%|##        | Sample : 1728/8384 [00:00<00:01, 4870.78it/s]
 22%|##1       | Sample : 1812/8384 [00:00<00:01, 4894.83it/s]
 23%|##2       | Sample : 1889/8384 [00:00<00:01, 4881.87it/s]
 23%|##3       | Sample : 1956/8384 [00:00<00:01, 4833.05it/s]
 24%|##4       | Sample : 2041/8384 [00:00<00:01, 4863.56it/s]
 25%|##5       | Sample : 2126/8384 [00:00<00:01, 4890.64it/s]
 26%|##6       | Sample : 2210/8384 [00:00<00:01, 4912.10it/s]
 27%|##7       | Sample : 2295/8384 [00:00<00:01, 4934.49it/s]
 28%|##8       | Sample : 2383/8384 [00:00<00:01, 4967.32it/s]
 29%|##9       | Sample : 2453/8384 [00:00<00:01, 4930.73it/s]
 30%|###       | Sample : 2538/8384 [00:00<00:01, 4951.61it/s]
 31%|###1      | Sample : 2625/8384 [00:00<00:01, 4980.74it/s]
 32%|###2      | Sample : 2714/8384 [00:00<00:01, 5012.27it/s]
 33%|###3      | Sample : 2803/8384 [00:00<00:01, 5041.97it/s]
 34%|###4      | Sample : 2889/8384 [00:00<00:01, 5059.04it/s]
 35%|###5      | Sample : 2956/8384 [00:00<00:01, 5007.94it/s]
 36%|###6      | Sample : 3043/8384 [00:00<00:01, 5032.59it/s]
 37%|###7      | Sample : 3133/8384 [00:00<00:01, 5066.39it/s]
 38%|###8      | Sample : 3222/8384 [00:00<00:01, 5093.09it/s]
 40%|###9      | Sample : 3313/8384 [00:00<00:00, 5126.11it/s]
 41%|####      | Sample : 3402/8384 [00:00<00:00, 5149.40it/s]
 41%|####1     | Sample : 3479/8384 [00:00<00:00, 5129.38it/s]
 43%|####2     | Sample : 3568/8384 [00:00<00:00, 5150.00it/s]
 44%|####3     | Sample : 3656/8384 [00:00<00:00, 5167.79it/s]
 45%|####4     | Sample : 3744/8384 [00:00<00:00, 5185.24it/s]
 46%|####5     | Sample : 3833/8384 [00:00<00:00, 5205.24it/s]
 47%|####6     | Sample : 3924/8384 [00:00<00:00, 5229.52it/s]
 48%|####7     | Sample : 4000/8384 [00:00<00:00, 5201.81it/s]
 49%|####8     | Sample : 4079/8384 [00:00<00:00, 5186.04it/s]
 50%|####9     | Sample : 4164/8384 [00:00<00:00, 5191.86it/s]
 50%|#####     | Sample : 4233/8384 [00:00<00:00, 5143.85it/s]
 51%|#####1    | Sample : 4317/8384 [00:00<00:00, 5149.07it/s]
 53%|#####2    | Sample : 4407/8384 [00:00<00:00, 5172.43it/s]
 54%|#####3    | Sample : 4486/8384 [00:00<00:00, 5156.52it/s]
 54%|#####4    | Sample : 4567/8384 [00:00<00:00, 5151.01it/s]
 56%|#####5    | Sample : 4655/8384 [00:00<00:00, 5166.51it/s]
 56%|#####6    | Sample : 4728/8384 [00:00<00:00, 5134.89it/s]
 57%|#####7    | Sample : 4809/8384 [00:00<00:00, 5128.53it/s]
 58%|#####8    | Sample : 4892/8384 [00:00<00:00, 5130.49it/s]
 59%|#####9    | Sample : 4975/8384 [00:00<00:00, 5130.93it/s]
 60%|######    | Sample : 5048/8384 [00:01<00:00, 5100.70it/s]
 61%|######1   | Sample : 5130/8384 [00:01<00:00, 5101.50it/s]
 62%|######1   | Sample : 5194/8384 [00:01<00:00, 5044.59it/s]
 63%|######2   | Sample : 5250/8384 [00:01<00:00, 4962.56it/s]
 63%|######3   | Sample : 5320/8384 [00:01<00:00, 4929.15it/s]
 64%|######4   | Sample : 5404/8384 [00:01<00:00, 4945.19it/s]
 65%|######5   | Sample : 5474/8384 [00:01<00:00, 4914.96it/s]
 66%|######6   | Sample : 5554/8384 [00:01<00:00, 4918.59it/s]
 67%|######7   | Sample : 5630/8384 [00:01<00:00, 4908.65it/s]
 68%|######8   | Sample : 5713/8384 [00:01<00:00, 4920.38it/s]
 69%|######9   | Sample : 5797/8384 [00:01<00:00, 4936.18it/s]
 70%|#######   | Sample : 5876/8384 [00:01<00:00, 4935.28it/s]
 71%|#######   | Sample : 5945/8384 [00:01<00:00, 4900.99it/s]
 72%|#######1  | Sample : 6019/8384 [00:01<00:00, 4886.49it/s]
 73%|#######2  | Sample : 6088/8384 [00:01<00:00, 4854.57it/s]
 74%|#######3  | Sample : 6171/8384 [00:01<00:00, 4868.46it/s]
 75%|#######4  | Sample : 6251/8384 [00:01<00:00, 4874.10it/s]
 76%|#######5  | Sample : 6337/8384 [00:01<00:00, 4897.80it/s]
 76%|#######6  | Sample : 6403/8384 [00:01<00:00, 4858.16it/s]
 77%|#######7  | Sample : 6482/8384 [00:01<00:00, 4859.90it/s]
 78%|#######8  | Sample : 6566/8384 [00:01<00:00, 4879.36it/s]
 79%|#######9  | Sample : 6655/8384 [00:01<00:00, 4911.08it/s]
 80%|########  | Sample : 6742/8384 [00:01<00:00, 4936.02it/s]
 81%|########1 | Sample : 6828/8384 [00:01<00:00, 4957.97it/s]
 82%|########2 | Sample : 6913/8384 [00:01<00:00, 4975.03it/s]
 83%|########3 | Sample : 6984/8384 [00:01<00:00, 4947.66it/s]
 84%|########4 | Sample : 7076/8384 [00:01<00:00, 4986.40it/s]
 85%|########5 | Sample : 7168/8384 [00:01<00:00, 5024.30it/s]
 87%|########6 | Sample : 7261/8384 [00:01<00:00, 5062.89it/s]
 88%|########7 | Sample : 7351/8384 [00:01<00:00, 5090.46it/s]
 89%|########8 | Sample : 7445/8384 [00:01<00:00, 5128.70it/s]
 90%|########9 | Sample : 7515/8384 [00:01<00:00, 5089.64it/s]
 91%|######### | Sample : 7607/8384 [00:01<00:00, 5122.52it/s]
 92%|#########1| Sample : 7701/8384 [00:01<00:00, 5157.84it/s]
 93%|#########3| Sample : 7799/8384 [00:01<00:00, 5205.95it/s]
 94%|#########4| Sample : 7896/8384 [00:01<00:00, 5246.16it/s]
 95%|#########5| Sample : 7991/8384 [00:01<00:00, 5279.05it/s]
 96%|#########6| Sample : 8062/8384 [00:01<00:00, 5235.71it/s]
 97%|#########7| Sample : 8159/8384 [00:01<00:00, 5276.73it/s]
 98%|#########8| Sample : 8257/8384 [00:01<00:00, 5318.89it/s]
100%|#########9| Sample : 8358/8384 [00:01<00:00, 5368.17it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 5049.74it/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.912 seconds)

Estimated memory usage: 45 MB

Gallery generated by Sphinx-Gallery