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:00<00:00,   35.02it/s]
100%|##########| Sample : 2/2 [00:00<00:00,   35.27it/s]
100%|##########| Sample : 2/2 [00:00<00:00,   37.45it/s]
split 2 / 3
Fitting 1 epochs, 1 channels

  0%|          | Sample : 0/2 [00:00<?,       ?it/s]
 50%|#####     | Sample : 1/2 [00:00<00:00,   40.88it/s]
100%|##########| Sample : 2/2 [00:00<00:00,   41.01it/s]
100%|##########| Sample : 2/2 [00:00<00:00,   41.97it/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,   46.50it/s]
100%|##########| Sample : 2/2 [00:00<00:00,   46.29it/s]
100%|##########| Sample : 2/2 [00:00<00:00,   44.15it/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:47,   36.86it/s]
  1%|          | Sample : 65/8384 [00:00<03:34,   38.78it/s]
  2%|1         | Sample : 139/8384 [00:00<03:22,   40.80it/s]
  3%|2         | Sample : 214/8384 [00:00<03:10,   42.93it/s]
  3%|3         | Sample : 289/8384 [00:00<02:59,   45.16it/s]
  4%|4         | Sample : 363/8384 [00:00<02:48,   47.52it/s]
  5%|5         | Sample : 437/8384 [00:00<02:38,   49.99it/s]
  6%|6         | Sample : 515/8384 [00:00<02:29,   52.59it/s]
  7%|7         | Sample : 590/8384 [00:00<02:20,   55.33it/s]
  8%|7         | Sample : 666/8384 [00:00<02:12,   58.20it/s]
  9%|8         | Sample : 739/8384 [00:00<02:04,   61.23it/s]
 10%|9         | Sample : 814/8384 [00:00<01:57,   64.40it/s]
 11%|#         | Sample : 892/8384 [00:00<01:50,   67.75it/s]
 12%|#1        | Sample : 968/8384 [00:00<01:44,   71.26it/s]
 12%|#2        | Sample : 1044/8384 [00:00<01:37,   74.95it/s]
 13%|#3        | Sample : 1120/8384 [00:00<01:32,   78.83it/s]
 14%|#4        | Sample : 1197/8384 [00:00<01:26,   82.91it/s]
 15%|#5        | Sample : 1274/8384 [00:00<01:21,   87.19it/s]
 16%|#6        | Sample : 1349/8384 [00:00<01:16,   91.69it/s]
 17%|#6        | Sample : 1424/8384 [00:00<01:12,   96.41it/s]
 18%|#7        | Sample : 1501/8384 [00:00<01:07,  101.38it/s]
 19%|#8        | Sample : 1579/8384 [00:00<01:03,  106.60it/s]
 20%|#9        | Sample : 1655/8384 [00:00<01:00,  112.08it/s]
 21%|##        | Sample : 1729/8384 [00:00<00:56,  117.82it/s]
 22%|##1       | Sample : 1803/8384 [00:00<00:53,  123.86it/s]
 22%|##2       | Sample : 1878/8384 [00:00<00:49,  130.19it/s]
 23%|##3       | Sample : 1956/8384 [00:00<00:46,  136.85it/s]
 24%|##4       | Sample : 2032/8384 [00:00<00:44,  143.84it/s]
 25%|##5       | Sample : 2107/8384 [00:00<00:41,  151.16it/s]
 26%|##6       | Sample : 2182/8384 [00:00<00:39,  158.85it/s]
 27%|##6       | Sample : 2259/8384 [00:00<00:36,  166.91it/s]
 28%|##7       | Sample : 2335/8384 [00:00<00:34,  175.37it/s]
 29%|##8       | Sample : 2412/8384 [00:00<00:32,  184.25it/s]
 30%|##9       | Sample : 2488/8384 [00:00<00:30,  193.55it/s]
 31%|###       | Sample : 2563/8384 [00:00<00:28,  203.30it/s]
 32%|###1      | Sample : 2641/8384 [00:00<00:26,  213.52it/s]
 32%|###2      | Sample : 2718/8384 [00:00<00:25,  224.23it/s]
 33%|###3      | Sample : 2793/8384 [00:00<00:23,  235.44it/s]
 34%|###4      | Sample : 2867/8384 [00:00<00:22,  247.16it/s]
 35%|###5      | Sample : 2941/8384 [00:00<00:20,  259.44it/s]
 36%|###5      | Sample : 3017/8384 [00:00<00:19,  272.31it/s]
 37%|###6      | Sample : 3094/8384 [00:00<00:18,  285.78it/s]
 38%|###7      | Sample : 3170/8384 [00:00<00:17,  299.86it/s]
 39%|###8      | Sample : 3246/8384 [00:00<00:16,  314.59it/s]
 40%|###9      | Sample : 3324/8384 [00:00<00:15,  330.02it/s]
 41%|####      | Sample : 3404/8384 [00:00<00:14,  346.18it/s]
 42%|####1     | Sample : 3482/8384 [00:00<00:13,  363.02it/s]
 42%|####2     | Sample : 3559/8384 [00:00<00:12,  380.60it/s]
 43%|####3     | Sample : 3636/8384 [00:00<00:11,  398.97it/s]
 44%|####4     | Sample : 3715/8384 [00:00<00:11,  418.17it/s]
 45%|####5     | Sample : 3793/8384 [00:00<00:10,  438.20it/s]
 46%|####6     | Sample : 3870/8384 [00:00<00:09,  459.04it/s]
 47%|####7     | Sample : 3946/8384 [00:00<00:09,  480.76it/s]
 48%|####8     | Sample : 4025/8384 [00:00<00:08,  503.45it/s]
 49%|####8     | Sample : 4104/8384 [00:00<00:08,  527.11it/s]
 50%|####9     | Sample : 4178/8384 [00:00<00:07,  551.54it/s]
 51%|#####     | Sample : 4252/8384 [00:00<00:07,  576.92it/s]
 52%|#####1    | Sample : 4327/8384 [00:00<00:06,  603.36it/s]
 53%|#####2    | Sample : 4404/8384 [00:00<00:06,  630.95it/s]
 53%|#####3    | Sample : 4481/8384 [00:00<00:05,  659.59it/s]
 54%|#####4    | Sample : 4559/8384 [00:00<00:05,  689.34it/s]
 55%|#####5    | Sample : 4635/8384 [00:01<00:05,  720.12it/s]
 56%|#####6    | Sample : 4712/8384 [00:01<00:04,  752.05it/s]
 57%|#####7    | Sample : 4793/8384 [00:01<00:04,  785.41it/s]
 58%|#####8    | Sample : 4872/8384 [00:01<00:04,  819.83it/s]
 59%|#####9    | Sample : 4949/8384 [00:01<00:04,  855.31it/s]
 60%|#####9    | Sample : 5027/8384 [00:01<00:03,  892.01it/s]
 61%|######    | Sample : 5106/8384 [00:01<00:03,  930.08it/s]
 62%|######1   | Sample : 5184/8384 [00:01<00:03,  969.25it/s]
 63%|######2   | Sample : 5263/8384 [00:01<00:03, 1009.74it/s]
 64%|######3   | Sample : 5340/8384 [00:01<00:02, 1051.25it/s]
 65%|######4   | Sample : 5419/8384 [00:01<00:02, 1094.18it/s]
 66%|######5   | Sample : 5499/8384 [00:01<00:02, 1138.58it/s]
 67%|######6   | Sample : 5576/8384 [00:01<00:02, 1183.72it/s]
 67%|######7   | Sample : 5654/8384 [00:01<00:02, 1230.12it/s]
 68%|######8   | Sample : 5733/8384 [00:01<00:02, 1277.99it/s]
 69%|######9   | Sample : 5813/8384 [00:01<00:01, 1327.32it/s]
 70%|#######   | Sample : 5893/8384 [00:01<00:01, 1377.93it/s]
 71%|#######1  | Sample : 5972/8384 [00:01<00:01, 1429.24it/s]
 72%|#######2  | Sample : 6050/8384 [00:01<00:01, 1481.50it/s]
 73%|#######3  | Sample : 6130/8384 [00:01<00:01, 1535.41it/s]
 74%|#######4  | Sample : 6212/8384 [00:01<00:01, 1590.84it/s]
 75%|#######5  | Sample : 6291/8384 [00:01<00:01, 1646.62it/s]
 76%|#######5  | Sample : 6368/8384 [00:01<00:01, 1702.46it/s]
 77%|#######6  | Sample : 6447/8384 [00:01<00:01, 1760.03it/s]
 78%|#######7  | Sample : 6527/8384 [00:01<00:01, 1818.93it/s]
 79%|#######8  | Sample : 6608/8384 [00:01<00:00, 1878.86it/s]
 80%|#######9  | Sample : 6686/8384 [00:01<00:00, 1938.21it/s]
 81%|########  | Sample : 6764/8384 [00:01<00:00, 1998.18it/s]
 82%|########1 | Sample : 6842/8384 [00:01<00:00, 2058.91it/s]
 83%|########2 | Sample : 6923/8384 [00:01<00:00, 2121.64it/s]
 84%|########3 | Sample : 7004/8384 [00:01<00:00, 2184.80it/s]
 84%|########4 | Sample : 7083/8384 [00:01<00:00, 2247.31it/s]
 85%|########5 | Sample : 7163/8384 [00:01<00:00, 2310.44it/s]
 86%|########6 | Sample : 7244/8384 [00:01<00:00, 2374.93it/s]
 87%|########7 | Sample : 7326/8384 [00:01<00:00, 2440.38it/s]
 88%|########8 | Sample : 7407/8384 [00:01<00:00, 2505.02it/s]
 89%|########9 | Sample : 7488/8384 [00:01<00:00, 2569.75it/s]
 90%|######### | Sample : 7569/8384 [00:01<00:00, 2633.94it/s]
 91%|#########1| Sample : 7650/8384 [00:01<00:00, 2698.66it/s]
 92%|#########2| Sample : 7732/8384 [00:01<00:00, 2763.36it/s]
 93%|#########3| Sample : 7813/8384 [00:01<00:00, 2826.96it/s]
 94%|#########4| Sample : 7895/8384 [00:01<00:00, 2891.11it/s]
 95%|#########5| Sample : 7978/8384 [00:01<00:00, 2955.61it/s]
 96%|#########6| Sample : 8060/8384 [00:01<00:00, 3018.89it/s]
 97%|#########7| Sample : 8142/8384 [00:01<00:00, 3081.12it/s]
 98%|#########8| Sample : 8224/8384 [00:01<00:00, 3143.78it/s]
 99%|#########9| Sample : 8308/8384 [00:01<00:00, 3207.40it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 4753.04it/s]
split 2 / 3
Fitting 1 epochs, 128 channels

  0%|          | Sample : 0/8384 [00:00<?,       ?it/s]
  0%|          | Sample : 1/8384 [00:00<03:18,   42.33it/s]
  1%|          | Sample : 75/8384 [00:00<03:06,   44.54it/s]
  2%|1         | Sample : 148/8384 [00:00<02:55,   46.86it/s]
  3%|2         | Sample : 220/8384 [00:00<02:45,   49.29it/s]
  4%|3         | Sample : 295/8384 [00:00<02:35,   51.86it/s]
  4%|4         | Sample : 370/8384 [00:00<02:26,   54.56it/s]
  5%|5         | Sample : 444/8384 [00:00<02:18,   57.39it/s]
  6%|6         | Sample : 520/8384 [00:00<02:10,   60.37it/s]
  7%|7         | Sample : 595/8384 [00:00<02:02,   63.51it/s]
  8%|8         | Sample : 672/8384 [00:00<01:55,   66.80it/s]
  9%|8         | Sample : 747/8384 [00:00<01:48,   70.27it/s]
 10%|9         | Sample : 819/8384 [00:00<01:42,   73.90it/s]
 11%|#         | Sample : 896/8384 [00:00<01:36,   77.73it/s]
 12%|#1        | Sample : 972/8384 [00:00<01:30,   81.75it/s]
 13%|#2        | Sample : 1049/8384 [00:00<01:25,   85.98it/s]
 13%|#3        | Sample : 1124/8384 [00:00<01:20,   90.41it/s]
 14%|#4        | Sample : 1199/8384 [00:00<01:15,   95.07it/s]
 15%|#5        | Sample : 1275/8384 [00:00<01:11,   99.97it/s]
 16%|#6        | Sample : 1351/8384 [00:00<01:06,  105.12it/s]
 17%|#7        | Sample : 1427/8384 [00:00<01:02,  110.52it/s]
 18%|#7        | Sample : 1502/8384 [00:00<00:59,  116.19it/s]
 19%|#8        | Sample : 1577/8384 [00:00<00:55,  122.15it/s]
 20%|#9        | Sample : 1651/8384 [00:00<00:52,  128.40it/s]
 21%|##        | Sample : 1726/8384 [00:00<00:49,  134.96it/s]
 21%|##1       | Sample : 1799/8384 [00:00<00:46,  141.84it/s]
 22%|##2       | Sample : 1872/8384 [00:00<00:43,  149.06it/s]
 23%|##3       | Sample : 1947/8384 [00:00<00:41,  156.64it/s]
 24%|##4       | Sample : 2020/8384 [00:00<00:38,  164.58it/s]
 25%|##4       | Sample : 2093/8384 [00:00<00:36,  172.92it/s]
 26%|##5       | Sample : 2166/8384 [00:00<00:34,  181.66it/s]
 27%|##6       | Sample : 2239/8384 [00:00<00:32,  190.82it/s]
 28%|##7       | Sample : 2314/8384 [00:00<00:30,  200.43it/s]
 29%|##8       | Sample : 2393/8384 [00:00<00:28,  210.52it/s]
 29%|##9       | Sample : 2468/8384 [00:00<00:26,  221.07it/s]
 30%|###       | Sample : 2543/8384 [00:00<00:25,  232.12it/s]
 31%|###1      | Sample : 2618/8384 [00:00<00:23,  243.70it/s]
 32%|###2      | Sample : 2694/8384 [00:00<00:22,  255.83it/s]
 33%|###3      | Sample : 2769/8384 [00:00<00:20,  268.52it/s]
 34%|###3      | Sample : 2843/8384 [00:00<00:19,  281.79it/s]
 35%|###4      | Sample : 2919/8384 [00:00<00:18,  295.69it/s]
 36%|###5      | Sample : 2996/8384 [00:00<00:17,  310.25it/s]
 37%|###6      | Sample : 3073/8384 [00:00<00:16,  325.46it/s]
 38%|###7      | Sample : 3149/8384 [00:00<00:15,  341.36it/s]
 38%|###8      | Sample : 3224/8384 [00:00<00:14,  357.95it/s]
 39%|###9      | Sample : 3301/8384 [00:00<00:13,  375.31it/s]
 40%|####      | Sample : 3380/8384 [00:00<00:12,  393.48it/s]
 41%|####1     | Sample : 3458/8384 [00:00<00:11,  412.42it/s]
 42%|####2     | Sample : 3535/8384 [00:00<00:11,  432.15it/s]
 43%|####3     | Sample : 3611/8384 [00:00<00:10,  452.71it/s]
 44%|####4     | Sample : 3689/8384 [00:00<00:09,  474.22it/s]
 45%|####4     | Sample : 3768/8384 [00:00<00:09,  496.65it/s]
 46%|####5     | Sample : 3845/8384 [00:00<00:08,  519.95it/s]
 47%|####6     | Sample : 3922/8384 [00:00<00:08,  544.21it/s]
 48%|####7     | Sample : 4001/8384 [00:00<00:07,  569.55it/s]
 49%|####8     | Sample : 4080/8384 [00:00<00:07,  595.88it/s]
 50%|####9     | Sample : 4158/8384 [00:00<00:06,  623.19it/s]
 51%|#####     | Sample : 4234/8384 [00:00<00:06,  651.48it/s]
 51%|#####1    | Sample : 4311/8384 [00:00<00:05,  680.88it/s]
 52%|#####2    | Sample : 4389/8384 [00:00<00:05,  711.47it/s]
 53%|#####3    | Sample : 4468/8384 [00:00<00:05,  743.25it/s]
 54%|#####4    | Sample : 4545/8384 [00:00<00:04,  776.03it/s]
 55%|#####5    | Sample : 4622/8384 [00:01<00:04,  809.94it/s]
 56%|#####6    | Sample : 4701/8384 [00:01<00:04,  845.25it/s]
 57%|#####7    | Sample : 4781/8384 [00:01<00:04,  881.83it/s]
 58%|#####7    | Sample : 4858/8384 [00:01<00:03,  919.32it/s]
 59%|#####8    | Sample : 4935/8384 [00:01<00:03,  957.93it/s]
 60%|#####9    | Sample : 5013/8384 [00:01<00:03,  997.96it/s]
 61%|######    | Sample : 5093/8384 [00:01<00:03, 1039.47it/s]
 62%|######1   | Sample : 5171/8384 [00:01<00:02, 1081.90it/s]
 63%|######2   | Sample : 5242/8384 [00:01<00:02, 1124.32it/s]
 63%|######3   | Sample : 5313/8384 [00:01<00:02, 1167.89it/s]
 64%|######4   | Sample : 5391/8384 [00:01<00:02, 1214.00it/s]
 65%|######5   | Sample : 5470/8384 [00:01<00:02, 1261.48it/s]
 66%|######6   | Sample : 5549/8384 [00:01<00:02, 1310.07it/s]
 67%|######7   | Sample : 5626/8384 [00:01<00:02, 1359.38it/s]
 68%|######7   | Sample : 5701/8384 [00:01<00:01, 1409.16it/s]
 69%|######8   | Sample : 5778/8384 [00:01<00:01, 1460.69it/s]
 70%|######9   | Sample : 5857/8384 [00:01<00:01, 1513.95it/s]
 71%|#######   | Sample : 5933/8384 [00:01<00:01, 1567.06it/s]
 72%|#######1  | Sample : 6008/8384 [00:01<00:01, 1620.93it/s]
 73%|#######2  | Sample : 6085/8384 [00:01<00:01, 1676.51it/s]
 74%|#######3  | Sample : 6166/8384 [00:01<00:01, 1734.22it/s]
 74%|#######4  | Sample : 6246/8384 [00:01<00:01, 1792.54it/s]
 75%|#######5  | Sample : 6325/8384 [00:01<00:01, 1851.44it/s]
 76%|#######6  | Sample : 6404/8384 [00:01<00:01, 1911.05it/s]
 77%|#######7  | Sample : 6485/8384 [00:01<00:00, 1972.23it/s]
 78%|#######8  | Sample : 6566/8384 [00:01<00:00, 2033.90it/s]
 79%|#######9  | Sample : 6645/8384 [00:01<00:00, 2095.03it/s]
 80%|########  | Sample : 6724/8384 [00:01<00:00, 2156.62it/s]
 81%|########1 | Sample : 6804/8384 [00:01<00:00, 2219.35it/s]
 82%|########2 | Sample : 6886/8384 [00:01<00:00, 2283.68it/s]
 83%|########3 | Sample : 6967/8384 [00:01<00:00, 2347.77it/s]
 84%|########4 | Sample : 7046/8384 [00:01<00:00, 2410.65it/s]
 85%|########4 | Sample : 7123/8384 [00:01<00:00, 2472.33it/s]
 86%|########5 | Sample : 7203/8384 [00:01<00:00, 2536.43it/s]
 87%|########6 | Sample : 7284/8384 [00:01<00:00, 2600.97it/s]
 88%|########7 | Sample : 7364/8384 [00:01<00:00, 2664.19it/s]
 89%|########8 | Sample : 7444/8384 [00:01<00:00, 2727.84it/s]
 90%|########9 | Sample : 7524/8384 [00:01<00:00, 2791.21it/s]
 91%|######### | Sample : 7606/8384 [00:01<00:00, 2855.46it/s]
 92%|#########1| Sample : 7687/8384 [00:01<00:00, 2918.84it/s]
 93%|#########2| Sample : 7768/8384 [00:01<00:00, 2981.66it/s]
 94%|#########3| Sample : 7850/8384 [00:01<00:00, 3044.84it/s]
 95%|#########4| Sample : 7932/8384 [00:01<00:00, 3107.83it/s]
 96%|#########5| Sample : 8014/8384 [00:01<00:00, 3169.19it/s]
 97%|#########6| Sample : 8095/8384 [00:01<00:00, 3228.40it/s]
 98%|#########7| Sample : 8177/8384 [00:01<00:00, 3289.05it/s]
 99%|#########8| Sample : 8259/8384 [00:01<00:00, 3348.22it/s]
100%|#########9| Sample : 8343/8384 [00:01<00:00, 3409.59it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 4735.82it/s]
split 3 / 3
Fitting 1 epochs, 128 channels

  0%|          | Sample : 0/8384 [00:00<?,       ?it/s]
  0%|          | Sample : 1/8384 [00:00<03:16,   42.66it/s]
  1%|          | Sample : 76/8384 [00:00<03:05,   44.89it/s]
  2%|1         | Sample : 151/8384 [00:00<02:54,   47.23it/s]
  3%|2         | Sample : 225/8384 [00:00<02:44,   49.69it/s]
  4%|3         | Sample : 301/8384 [00:00<02:34,   52.27it/s]
  5%|4         | Sample : 378/8384 [00:00<02:25,   54.99it/s]
  5%|5         | Sample : 452/8384 [00:00<02:17,   57.85it/s]
  6%|6         | Sample : 525/8384 [00:00<02:09,   60.85it/s]
  7%|7         | Sample : 600/8384 [00:00<02:01,   64.01it/s]
  8%|8         | Sample : 678/8384 [00:00<01:54,   67.33it/s]
  9%|9         | Sample : 755/8384 [00:00<01:47,   70.83it/s]
 10%|9         | Sample : 830/8384 [00:00<01:41,   74.49it/s]
 11%|#         | Sample : 904/8384 [00:00<01:35,   78.35it/s]
 12%|#1        | Sample : 979/8384 [00:00<01:29,   82.40it/s]
 13%|#2        | Sample : 1055/8384 [00:00<01:24,   86.65it/s]
 13%|#3        | Sample : 1131/8384 [00:00<01:19,   91.13it/s]
 14%|#4        | Sample : 1206/8384 [00:00<01:14,   95.82it/s]
 15%|#5        | Sample : 1282/8384 [00:00<01:10,  100.76it/s]
 16%|#6        | Sample : 1360/8384 [00:00<01:06,  105.95it/s]
 17%|#7        | Sample : 1436/8384 [00:00<01:02,  111.39it/s]
 18%|#8        | Sample : 1511/8384 [00:00<00:58,  117.11it/s]
 19%|#8        | Sample : 1587/8384 [00:00<00:55,  123.11it/s]
 20%|#9        | Sample : 1663/8384 [00:00<00:51,  129.41it/s]
 21%|##        | Sample : 1739/8384 [00:00<00:48,  136.02it/s]
 22%|##1       | Sample : 1814/8384 [00:00<00:45,  142.96it/s]
 23%|##2       | Sample : 1889/8384 [00:00<00:43,  150.24it/s]
 23%|##3       | Sample : 1965/8384 [00:00<00:40,  157.89it/s]
 24%|##4       | Sample : 2043/8384 [00:00<00:38,  165.91it/s]
 25%|##5       | Sample : 2120/8384 [00:00<00:35,  174.32it/s]
 26%|##6       | Sample : 2195/8384 [00:00<00:33,  183.14it/s]
 27%|##7       | Sample : 2268/8384 [00:00<00:31,  192.37it/s]
 28%|##7       | Sample : 2342/8384 [00:00<00:29,  202.05it/s]
 29%|##8       | Sample : 2418/8384 [00:00<00:28,  212.21it/s]
 30%|##9       | Sample : 2494/8384 [00:00<00:26,  222.85it/s]
 31%|###       | Sample : 2570/8384 [00:00<00:24,  233.99it/s]
 32%|###1      | Sample : 2647/8384 [00:00<00:23,  245.68it/s]
 33%|###2      | Sample : 2726/8384 [00:00<00:21,  257.93it/s]
 33%|###3      | Sample : 2803/8384 [00:00<00:20,  270.73it/s]
 34%|###4      | Sample : 2879/8384 [00:00<00:19,  284.13it/s]
 35%|###5      | Sample : 2956/8384 [00:00<00:18,  298.15it/s]
 36%|###6      | Sample : 3035/8384 [00:00<00:17,  312.84it/s]
 37%|###7      | Sample : 3112/8384 [00:00<00:16,  328.17it/s]
 38%|###8      | Sample : 3187/8384 [00:00<00:15,  344.17it/s]
 39%|###8      | Sample : 3263/8384 [00:00<00:14,  360.90it/s]
 40%|###9      | Sample : 3340/8384 [00:00<00:13,  378.39it/s]
 41%|####      | Sample : 3418/8384 [00:00<00:12,  396.68it/s]
 42%|####1     | Sample : 3495/8384 [00:00<00:11,  415.75it/s]
 43%|####2     | Sample : 3572/8384 [00:00<00:11,  435.62it/s]
 44%|####3     | Sample : 3648/8384 [00:00<00:10,  456.34it/s]
 44%|####4     | Sample : 3726/8384 [00:00<00:09,  477.99it/s]
 45%|####5     | Sample : 3803/8384 [00:00<00:09,  500.51it/s]
 46%|####6     | Sample : 3879/8384 [00:00<00:08,  523.93it/s]
 47%|####7     | Sample : 3955/8384 [00:00<00:08,  548.30it/s]
 48%|####8     | Sample : 4033/8384 [00:00<00:07,  573.74it/s]
 49%|####9     | Sample : 4112/8384 [00:00<00:07,  600.24it/s]
 50%|####9     | Sample : 4188/8384 [00:00<00:06,  627.65it/s]
 51%|#####     | Sample : 4264/8384 [00:00<00:06,  656.11it/s]
 52%|#####1    | Sample : 4342/8384 [00:00<00:05,  685.77it/s]
 53%|#####2    | Sample : 4422/8384 [00:00<00:05,  716.63it/s]
 54%|#####3    | Sample : 4499/8384 [00:00<00:05,  748.42it/s]
 55%|#####4    | Sample : 4577/8384 [00:00<00:04,  781.42it/s]
 56%|#####5    | Sample : 4655/8384 [00:01<00:04,  815.63it/s]
 56%|#####6    | Sample : 4734/8384 [00:01<00:04,  851.12it/s]
 57%|#####7    | Sample : 4813/8384 [00:01<00:04,  887.84it/s]
 58%|#####8    | Sample : 4890/8384 [00:01<00:03,  925.56it/s]
 59%|#####9    | Sample : 4968/8384 [00:01<00:03,  964.53it/s]
 60%|######    | Sample : 5047/8384 [00:01<00:03, 1004.96it/s]
 61%|######1   | Sample : 5128/8384 [00:01<00:03, 1046.81it/s]
 62%|######2   | Sample : 5205/8384 [00:01<00:02, 1089.30it/s]
 63%|######2   | Sample : 5281/8384 [00:01<00:02, 1132.84it/s]
 64%|######3   | Sample : 5359/8384 [00:01<00:02, 1177.91it/s]
 65%|######4   | Sample : 5439/8384 [00:01<00:02, 1224.56it/s]
 66%|######5   | Sample : 5518/8384 [00:01<00:02, 1272.37it/s]
 67%|######6   | Sample : 5595/8384 [00:01<00:02, 1320.77it/s]
 68%|######7   | Sample : 5672/8384 [00:01<00:01, 1370.35it/s]
 69%|######8   | Sample : 5750/8384 [00:01<00:01, 1421.38it/s]
 70%|######9   | Sample : 5831/8384 [00:01<00:01, 1474.18it/s]
 70%|#######   | Sample : 5910/8384 [00:01<00:01, 1527.49it/s]
 71%|#######1  | Sample : 5987/8384 [00:01<00:01, 1581.30it/s]
 72%|#######2  | Sample : 6066/8384 [00:01<00:01, 1636.77it/s]
 73%|#######3  | Sample : 6147/8384 [00:01<00:01, 1694.01it/s]
 74%|#######4  | Sample : 6226/8384 [00:01<00:01, 1751.32it/s]
 75%|#######5  | Sample : 6304/8384 [00:01<00:01, 1808.98it/s]
 76%|#######6  | Sample : 6383/8384 [00:01<00:01, 1867.89it/s]
 77%|#######7  | Sample : 6464/8384 [00:01<00:00, 1928.52it/s]
 78%|#######8  | Sample : 6545/8384 [00:01<00:00, 1989.72it/s]
 79%|#######8  | Sample : 6623/8384 [00:01<00:00, 2050.36it/s]
 80%|#######9  | Sample : 6701/8384 [00:01<00:00, 2111.43it/s]
 81%|########  | Sample : 6782/8384 [00:01<00:00, 2174.27it/s]
 82%|########1 | Sample : 6864/8384 [00:01<00:00, 2238.29it/s]
 83%|########2 | Sample : 6943/8384 [00:01<00:00, 2300.85it/s]
 84%|########3 | Sample : 7023/8384 [00:01<00:00, 2364.56it/s]
 85%|########4 | Sample : 7101/8384 [00:01<00:00, 2426.97it/s]
 86%|########5 | Sample : 7182/8384 [00:01<00:00, 2491.43it/s]
 87%|########6 | Sample : 7263/8384 [00:01<00:00, 2556.18it/s]
 88%|########7 | Sample : 7344/8384 [00:01<00:00, 2620.24it/s]
 89%|########8 | Sample : 7425/8384 [00:01<00:00, 2684.38it/s]
 90%|########9 | Sample : 7507/8384 [00:01<00:00, 2749.42it/s]
 91%|######### | Sample : 7589/8384 [00:01<00:00, 2814.48it/s]
 91%|#########1| Sample : 7671/8384 [00:01<00:00, 2878.89it/s]
 92%|#########2| Sample : 7751/8384 [00:01<00:00, 2941.14it/s]
 93%|#########3| Sample : 7832/8384 [00:01<00:00, 3003.06it/s]
 94%|#########4| Sample : 7914/8384 [00:01<00:00, 3066.38it/s]
 95%|#########5| Sample : 7996/8384 [00:01<00:00, 3128.91it/s]
 96%|#########6| Sample : 8078/8384 [00:01<00:00, 3190.87it/s]
 97%|#########7| Sample : 8160/8384 [00:01<00:00, 3251.79it/s]
 98%|#########8| Sample : 8242/8384 [00:01<00:00, 3311.82it/s]
 99%|#########9| Sample : 8324/8384 [00:01<00:00, 3371.01it/s]
100%|##########| Sample : 8384/8384 [00:01<00:00, 4768.12it/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()

plt.show()
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 15.415 seconds)

Estimated memory usage: 9 MB

Gallery generated by Sphinx-Gallery