Note
Click here to download the full example code
Spectro-temporal receptive field (STRF) estimation on continuous data¶
This demonstrates how an encoding model can be fit with multiple continuous inputs. In this case, we simulate the model behind a spectro-temporal receptive field (or STRF). First, we create a linear filter that maps patterns in spectro-temporal space onto an output, representing neural activity. We fit a receptive field model that attempts to recover the original linear filter that was used to create this data.
# Authors: Chris Holdgraf <choldgraf@gmail.com>
# Eric Larson <larson.eric.d@gmail.com>
#
# License: BSD-3-Clause
import numpy as np
import matplotlib.pyplot as plt
import mne
from mne.decoding import ReceptiveField, TimeDelayingRidge
from scipy.stats import multivariate_normal
from scipy.io import loadmat
from sklearn.preprocessing import scale
rng = np.random.RandomState(1337) # To make this example reproducible
Load audio data¶
We’ll read in the audio data from 1 in order to simulate a response.
In addition, we’ll downsample the data along the time dimension in order to speed up computation. Note that depending on the input values, this may not be desired. For example if your input stimulus varies more quickly than 1/2 the sampling rate to which we are downsampling.
# Read in audio that's been recorded in epochs.
path_audio = mne.datasets.mtrf.data_path()
data = loadmat(path_audio + '/speech_data.mat')
audio = data['spectrogram'].T
sfreq = float(data['Fs'][0, 0])
n_decim = 2
audio = mne.filter.resample(audio, down=n_decim, npad='auto')
sfreq /= n_decim
Create a receptive field¶
We’ll simulate a linear receptive field for a theoretical neural signal. This defines how the signal will respond to power in this receptive field space.
n_freqs = 20
tmin, tmax = -0.1, 0.4
# To simulate the data we'll create explicit delays here
delays_samp = np.arange(np.round(tmin * sfreq),
np.round(tmax * sfreq) + 1).astype(int)
delays_sec = delays_samp / sfreq
freqs = np.linspace(50, 5000, n_freqs)
grid = np.array(np.meshgrid(delays_sec, freqs))
# We need data to be shaped as n_epochs, n_features, n_times, so swap axes here
grid = grid.swapaxes(0, -1).swapaxes(0, 1)
# Simulate a temporal receptive field with a Gabor filter
means_high = [.1, 500]
means_low = [.2, 2500]
cov = [[.001, 0], [0, 500000]]
gauss_high = multivariate_normal.pdf(grid, means_high, cov)
gauss_low = -1 * multivariate_normal.pdf(grid, means_low, cov)
weights = gauss_high + gauss_low # Combine to create the "true" STRF
kwargs = dict(vmax=np.abs(weights).max(), vmin=-np.abs(weights).max(),
cmap='RdBu_r', shading='gouraud')
fig, ax = plt.subplots()
ax.pcolormesh(delays_sec, freqs, weights, **kwargs)
ax.set(title='Simulated STRF', xlabel='Time Lags (s)', ylabel='Frequency (Hz)')
plt.setp(ax.get_xticklabels(), rotation=45)
plt.autoscale(tight=True)
mne.viz.tight_layout()
Simulate a neural response¶
Using this receptive field, we’ll create an artificial neural response to a stimulus.
To do this, we’ll create a time-delayed version of the receptive field, and then calculate the dot product between this and the stimulus. Note that this is effectively doing a convolution between the stimulus and the receptive field. See here for more information.
# Reshape audio to split into epochs, then make epochs the first dimension.
n_epochs, n_seconds = 16, 5
audio = audio[:, :int(n_seconds * sfreq * n_epochs)]
X = audio.reshape([n_freqs, n_epochs, -1]).swapaxes(0, 1)
n_times = X.shape[-1]
# Delay the spectrogram according to delays so it can be combined w/ the STRF
# Lags will now be in axis 1, then we reshape to vectorize
delays = np.arange(np.round(tmin * sfreq),
np.round(tmax * sfreq) + 1).astype(int)
# Iterate through indices and append
X_del = np.zeros((len(delays),) + X.shape)
for ii, ix_delay in enumerate(delays):
# These arrays will take/put particular indices in the data
take = [slice(None)] * X.ndim
put = [slice(None)] * X.ndim
if ix_delay > 0:
take[-1] = slice(None, -ix_delay)
put[-1] = slice(ix_delay, None)
elif ix_delay < 0:
take[-1] = slice(-ix_delay, None)
put[-1] = slice(None, ix_delay)
X_del[ii][tuple(put)] = X[tuple(take)]
# Now set the delayed axis to the 2nd dimension
X_del = np.rollaxis(X_del, 0, 3)
X_del = X_del.reshape([n_epochs, -1, n_times])
n_features = X_del.shape[1]
weights_sim = weights.ravel()
# Simulate a neural response to the sound, given this STRF
y = np.zeros((n_epochs, n_times))
for ii, iep in enumerate(X_del):
# Simulate this epoch and add random noise
noise_amp = .002
y[ii] = np.dot(weights_sim, iep) + noise_amp * rng.randn(n_times)
# Plot the first 2 trials of audio and the simulated electrode activity
X_plt = scale(np.hstack(X[:2]).T).T
y_plt = scale(np.hstack(y[:2]))
time = np.arange(X_plt.shape[-1]) / sfreq
_, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6), sharex=True)
ax1.pcolormesh(time, freqs, X_plt, vmin=0, vmax=4, cmap='Reds',
shading='gouraud')
ax1.set_title('Input auditory features')
ax1.set(ylim=[freqs.min(), freqs.max()], ylabel='Frequency (Hz)')
ax2.plot(time, y_plt)
ax2.set(xlim=[time.min(), time.max()], title='Simulated response',
xlabel='Time (s)', ylabel='Activity (a.u.)')
mne.viz.tight_layout()
Fit a model to recover this receptive field¶
Finally, we’ll use the mne.decoding.ReceptiveField
class to recover
the linear receptive field of this signal. Note that properties of the
receptive field (e.g. smoothness) will depend on the autocorrelation in the
inputs and outputs.
# Create training and testing data
train, test = np.arange(n_epochs - 1), n_epochs - 1
X_train, X_test, y_train, y_test = X[train], X[test], y[train], y[test]
X_train, X_test, y_train, y_test = [np.rollaxis(ii, -1, 0) for ii in
(X_train, X_test, y_train, y_test)]
# Model the simulated data as a function of the spectrogram input
alphas = np.logspace(-3, 3, 7)
scores = np.zeros_like(alphas)
models = []
for ii, alpha in enumerate(alphas):
rf = ReceptiveField(tmin, tmax, sfreq, freqs, estimator=alpha)
rf.fit(X_train, y_train)
# Now make predictions about the model output, given input stimuli.
scores[ii] = rf.score(X_test, y_test)
models.append(rf)
times = rf.delays_ / float(rf.sfreq)
# Choose the model that performed best on the held out data
ix_best_alpha = np.argmax(scores)
best_mod = models[ix_best_alpha]
coefs = best_mod.coef_[0]
best_pred = best_mod.predict(X_test)[:, 0]
# Plot the original STRF, and the one that we recovered with modeling.
_, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3), sharey=True, sharex=True)
ax1.pcolormesh(delays_sec, freqs, weights, **kwargs)
ax2.pcolormesh(times, rf.feature_names, coefs, **kwargs)
ax1.set_title('Original STRF')
ax2.set_title('Best Reconstructed STRF')
plt.setp([iax.get_xticklabels() for iax in [ax1, ax2]], rotation=45)
plt.autoscale(tight=True)
mne.viz.tight_layout()
# Plot the actual response and the predicted response on a held out stimulus
time_pred = np.arange(best_pred.shape[0]) / sfreq
fig, ax = plt.subplots()
ax.plot(time_pred, y_test, color='k', alpha=.2, lw=4)
ax.plot(time_pred, best_pred, color='r', lw=1)
ax.set(title='Original and predicted activity', xlabel='Time (s)')
ax.legend(['Original', 'Predicted'])
plt.autoscale(tight=True)
mne.viz.tight_layout()
Out:
Fitting 15 epochs, 20 channels
0%| | Sample : 0/3450 [00:00<?, ?it/s]
0%| | Sample : 1/3450 [00:05<5:33:08, 5.80s/it]
12%|#2 | Sample : 417/3450 [00:05<00:40, 75.51it/s]
24%|##4 | Sample : 835/3450 [00:05<00:16, 154.73it/s]
36%|###6 | Sample : 1254/3450 [00:05<00:09, 237.79it/s]
49%|####8 | Sample : 1674/3450 [00:05<00:05, 324.87it/s]
61%|###### | Sample : 2093/3450 [00:05<00:03, 415.67it/s]
73%|#######2 | Sample : 2513/3450 [00:05<00:01, 510.81it/s]
85%|########4 | Sample : 2931/3450 [00:05<00:00, 609.71it/s]
97%|#########7| Sample : 3351/3450 [00:05<00:00, 713.50it/s]
100%|##########| Sample : 3450/3450 [00:05<00:00, 582.05it/s]
Fitting 15 epochs, 20 channels
0%| | Sample : 0/3450 [00:00<?, ?it/s]
12%|#2 | Sample : 420/3450 [00:00<00:00, 26233.53it/s]
24%|##4 | Sample : 844/3450 [00:00<00:00, 26364.67it/s]
35%|###5 | Sample : 1208/3450 [00:00<00:00, 25085.18it/s]
47%|####6 | Sample : 1621/3450 [00:00<00:00, 25280.48it/s]
59%|#####9 | Sample : 2051/3450 [00:00<00:00, 25632.47it/s]
61%|###### | Sample : 2099/3450 [00:00<00:00, 16270.88it/s]
67%|######7 | Sample : 2322/3450 [00:00<00:00, 15958.33it/s]
74%|#######3 | Sample : 2549/3450 [00:00<00:00, 15738.57it/s]
80%|######## | Sample : 2761/3450 [00:00<00:00, 15438.86it/s]
86%|########6 | Sample : 2972/3450 [00:00<00:00, 15192.93it/s]
92%|#########2| Sample : 3181/3450 [00:00<00:00, 14976.35it/s]
98%|#########8| Sample : 3397/3450 [00:00<00:00, 14832.42it/s]
100%|##########| Sample : 3450/3450 [00:00<00:00, 15457.13it/s]
Fitting 15 epochs, 20 channels
0%| | Sample : 0/3450 [00:00<?, ?it/s]
12%|#2 | Sample : 426/3450 [00:00<00:00, 26609.48it/s]
25%|##4 | Sample : 853/3450 [00:00<00:00, 26646.40it/s]
32%|###2 | Sample : 1111/3450 [00:00<00:00, 12528.04it/s]
44%|####4 | Sample : 1530/3450 [00:00<00:00, 14813.57it/s]
57%|#####6 | Sample : 1956/3450 [00:00<00:00, 16580.08it/s]
69%|######9 | Sample : 2383/3450 [00:00<00:00, 17956.01it/s]
82%|########1 | Sample : 2812/3450 [00:00<00:00, 19064.41it/s]
94%|#########3| Sample : 3237/3450 [00:00<00:00, 19937.66it/s]
100%|##########| Sample : 3450/3450 [00:00<00:00, 19835.00it/s]
Fitting 15 epochs, 20 channels
0%| | Sample : 0/3450 [00:00<?, ?it/s]
12%|#2 | Sample : 425/3450 [00:00<00:00, 26539.11it/s]
25%|##4 | Sample : 850/3450 [00:00<00:00, 26520.69it/s]
35%|###5 | Sample : 1217/3450 [00:00<00:00, 12079.08it/s]
47%|####7 | Sample : 1632/3450 [00:00<00:00, 14152.41it/s]
60%|#####9 | Sample : 2058/3450 [00:00<00:00, 15847.08it/s]
72%|#######2 | Sample : 2486/3450 [00:00<00:00, 17212.99it/s]
84%|########4 | Sample : 2907/3450 [00:00<00:00, 18273.13it/s]
96%|#########6| Sample : 3324/3450 [00:00<00:00, 19123.79it/s]
100%|##########| Sample : 3450/3450 [00:00<00:00, 18856.50it/s]
Fitting 15 epochs, 20 channels
0%| | Sample : 0/3450 [00:00<?, ?it/s]
12%|#1 | Sample : 411/3450 [00:00<00:00, 25642.74it/s]
24%|##3 | Sample : 822/3450 [00:00<00:00, 25650.76it/s]
36%|###5 | Sample : 1240/3450 [00:00<00:00, 25804.86it/s]
37%|###6 | Sample : 1268/3450 [00:00<00:00, 11841.65it/s]
49%|####8 | Sample : 1678/3450 [00:00<00:00, 13879.61it/s]
61%|###### | Sample : 2101/3450 [00:00<00:00, 15567.19it/s]
73%|#######3 | Sample : 2531/3450 [00:00<00:00, 16953.73it/s]
86%|########5 | Sample : 2952/3450 [00:00<00:00, 18029.80it/s]
98%|#########7| Sample : 3370/3450 [00:00<00:00, 18903.12it/s]
100%|##########| Sample : 3450/3450 [00:00<00:00, 18644.38it/s]
Fitting 15 epochs, 20 channels
0%| | Sample : 0/3450 [00:00<?, ?it/s]
12%|#2 | Sample : 420/3450 [00:00<00:00, 26230.40it/s]
24%|##4 | Sample : 839/3450 [00:00<00:00, 26204.78it/s]
36%|###6 | Sample : 1254/3450 [00:00<00:00, 26096.10it/s]
42%|####2 | Sample : 1458/3450 [00:00<00:00, 13436.12it/s]
54%|#####4 | Sample : 1874/3450 [00:00<00:00, 15252.84it/s]
67%|######6 | Sample : 2301/3450 [00:00<00:00, 16749.58it/s]
79%|#######9 | Sample : 2728/3450 [00:00<00:00, 17960.13it/s]
91%|#########1| Sample : 3156/3450 [00:00<00:00, 18956.99it/s]
100%|##########| Sample : 3450/3450 [00:00<00:00, 19234.63it/s]
Fitting 15 epochs, 20 channels
0%| | Sample : 0/3450 [00:00<?, ?it/s]
12%|#2 | Sample : 425/3450 [00:00<00:00, 26547.81it/s]
24%|##4 | Sample : 842/3450 [00:00<00:00, 26297.77it/s]
37%|###6 | Sample : 1265/3450 [00:00<00:00, 26338.20it/s]
49%|####8 | Sample : 1679/3450 [00:00<00:00, 14529.53it/s]
61%|###### | Sample : 2091/3450 [00:00<00:00, 16051.77it/s]
73%|#######2 | Sample : 2512/3450 [00:00<00:00, 17330.53it/s]
85%|########5 | Sample : 2938/3450 [00:00<00:00, 18409.33it/s]
98%|#########7| Sample : 3370/3450 [00:00<00:00, 19341.50it/s]
100%|##########| Sample : 3450/3450 [00:00<00:00, 19265.54it/s]
Visualize the effects of regularization¶
Above we fit a mne.decoding.ReceptiveField
model for one of many
values for the ridge regularization parameter. Here we will plot the model
score as well as the model coefficients for each value, in order to
visualize how coefficients change with different levels of regularization.
These issues as well as the STRF pipeline are described in detail
in 234.
# Plot model score for each ridge parameter
fig = plt.figure(figsize=(10, 4))
ax = plt.subplot2grid([2, len(alphas)], [1, 0], 1, len(alphas))
ax.plot(np.arange(len(alphas)), scores, marker='o', color='r')
ax.annotate('Best parameter', (ix_best_alpha, scores[ix_best_alpha]),
(ix_best_alpha, scores[ix_best_alpha] - .1),
arrowprops={'arrowstyle': '->'})
plt.xticks(np.arange(len(alphas)), ["%.0e" % ii for ii in alphas])
ax.set(xlabel="Ridge regularization value", ylabel="Score ($R^2$)",
xlim=[-.4, len(alphas) - .6])
mne.viz.tight_layout()
# Plot the STRF of each ridge parameter
for ii, (rf, i_alpha) in enumerate(zip(models, alphas)):
ax = plt.subplot2grid([2, len(alphas)], [0, ii], 1, 1)
ax.pcolormesh(times, rf.feature_names, rf.coef_[0], **kwargs)
plt.xticks([], [])
plt.yticks([], [])
plt.autoscale(tight=True)
fig.suptitle('Model coefficients / scores for many ridge parameters', y=1)
mne.viz.tight_layout()
Using different regularization types¶
In addition to the standard ridge regularization, the
mne.decoding.TimeDelayingRidge
class also exposes
Laplacian regularization
term as:
This imposes a smoothness constraint of nearby time samples and/or features. Quoting 1 :
Tikhonov [identity] regularization (Equation 5) reduces overfitting by smoothing the TRF estimate in a way that is insensitive to the amplitude of the signal of interest. However, the Laplacian approach (Equation 6) reduces off-sample error whilst preserving signal amplitude (Lalor et al., 2006). As a result, this approach usually leads to an improved estimate of the system’s response (as indexed by MSE) compared to Tikhonov regularization.
scores_lap = np.zeros_like(alphas)
models_lap = []
for ii, alpha in enumerate(alphas):
estimator = TimeDelayingRidge(tmin, tmax, sfreq, reg_type='laplacian',
alpha=alpha)
rf = ReceptiveField(tmin, tmax, sfreq, freqs, estimator=estimator)
rf.fit(X_train, y_train)
# Now make predictions about the model output, given input stimuli.
scores_lap[ii] = rf.score(X_test, y_test)
models_lap.append(rf)
ix_best_alpha_lap = np.argmax(scores_lap)
Out:
Fitting 15 epochs, 20 channels
0%| | Sample : 0/3450 [00:00<?, ?it/s]
11%|#1 | Sample : 395/3450 [00:00<00:00, 24677.52it/s]
24%|##3 | Sample : 823/3450 [00:00<00:00, 25690.83it/s]
36%|###6 | Sample : 1257/3450 [00:00<00:00, 26180.74it/s]
49%|####9 | Sample : 1699/3450 [00:00<00:00, 26563.33it/s]
62%|######2 | Sample : 2147/3450 [00:00<00:00, 26876.66it/s]
75%|#######5 | Sample : 2596/3450 [00:00<00:00, 27095.50it/s]
88%|########8 | Sample : 3038/3450 [00:00<00:00, 27177.73it/s]
100%|##########| Sample : 3450/3450 [00:00<00:00, 27206.40it/s]
Fitting 15 epochs, 20 channels
0%| | Sample : 0/3450 [00:00<?, ?it/s]
13%|#2 | Sample : 441/3450 [00:00<00:00, 27508.75it/s]
24%|##3 | Sample : 822/3450 [00:00<00:00, 19266.56it/s]
36%|###6 | Sample : 1242/3450 [00:00<00:00, 21274.69it/s]
49%|####8 | Sample : 1681/3450 [00:00<00:00, 22701.52it/s]
62%|######1 | Sample : 2129/3450 [00:00<00:00, 23740.27it/s]
75%|#######4 | Sample : 2579/3450 [00:00<00:00, 24484.31it/s]
84%|########4 | Sample : 2906/3450 [00:00<00:00, 20051.90it/s]
97%|#########7| Sample : 3350/3450 [00:00<00:00, 20966.93it/s]
100%|##########| Sample : 3450/3450 [00:00<00:00, 21305.15it/s]
Fitting 15 epochs, 20 channels
0%| | Sample : 0/3450 [00:00<?, ?it/s]
13%|#2 | Sample : 435/3450 [00:00<00:00, 27157.09it/s]
25%|##5 | Sample : 879/3450 [00:00<00:00, 27442.93it/s]
31%|### | Sample : 1062/3450 [00:00<00:00, 15443.44it/s]
43%|####3 | Sample : 1492/3450 [00:00<00:00, 17802.62it/s]
56%|#####6 | Sample : 1934/3450 [00:00<00:00, 19553.67it/s]
69%|######8 | Sample : 2373/3450 [00:00<00:00, 20800.79it/s]
82%|########1 | Sample : 2821/3450 [00:00<00:00, 21827.75it/s]
95%|#########4| Sample : 3269/3450 [00:00<00:00, 22629.38it/s]
100%|##########| Sample : 3450/3450 [00:00<00:00, 22491.91it/s]
Fitting 15 epochs, 20 channels
0%| | Sample : 0/3450 [00:00<?, ?it/s]
13%|#2 | Sample : 443/3450 [00:00<00:00, 27629.80it/s]
26%|##5 | Sample : 891/3450 [00:00<00:00, 27798.63it/s]
39%|###8 | Sample : 1343/3450 [00:00<00:00, 27937.57it/s]
46%|####5 | Sample : 1580/3450 [00:00<00:00, 15093.39it/s]
59%|#####8 | Sample : 2020/3450 [00:00<00:00, 16938.48it/s]
72%|#######1 | Sample : 2469/3450 [00:00<00:00, 18443.63it/s]
85%|########4 | Sample : 2918/3450 [00:00<00:00, 19640.08it/s]
98%|#########7| Sample : 3368/3450 [00:00<00:00, 20622.33it/s]
100%|##########| Sample : 3450/3450 [00:00<00:00, 20557.89it/s]
Fitting 15 epochs, 20 channels
0%| | Sample : 0/3450 [00:00<?, ?it/s]
13%|#2 | Sample : 438/3450 [00:00<00:00, 27327.71it/s]
26%|##5 | Sample : 884/3450 [00:00<00:00, 27600.52it/s]
39%|###8 | Sample : 1338/3450 [00:00<00:00, 27870.94it/s]
51%|#####1 | Sample : 1770/3450 [00:00<00:00, 27624.20it/s]
58%|#####8 | Sample : 2001/3450 [00:00<00:00, 15852.44it/s]
70%|####### | Sample : 2429/3450 [00:00<00:00, 17264.30it/s]
83%|########3 | Sample : 2869/3450 [00:00<00:00, 18493.81it/s]
96%|#########6| Sample : 3319/3450 [00:00<00:00, 19572.56it/s]
100%|##########| Sample : 3450/3450 [00:00<00:00, 19912.00it/s]
Fitting 15 epochs, 20 channels
0%| | Sample : 0/3450 [00:00<?, ?it/s]
13%|#2 | Sample : 438/3450 [00:00<00:00, 27357.01it/s]
24%|##4 | Sample : 839/3450 [00:00<00:00, 24126.08it/s]
37%|###7 | Sample : 1279/3450 [00:00<00:00, 25243.75it/s]
50%|##### | Sample : 1726/3450 [00:00<00:00, 25937.47it/s]
62%|######2 | Sample : 2150/3450 [00:00<00:00, 26057.13it/s]
65%|######5 | Sample : 2250/3450 [00:00<00:00, 15844.59it/s]
77%|#######7 | Sample : 2661/3450 [00:00<00:00, 17028.78it/s]
90%|########9 | Sample : 3097/3450 [00:00<00:00, 18176.80it/s]
100%|##########| Sample : 3450/3450 [00:00<00:00, 19216.06it/s]
Fitting 15 epochs, 20 channels
0%| | Sample : 0/3450 [00:00<?, ?it/s]
12%|#2 | Sample : 430/3450 [00:00<00:00, 26836.56it/s]
24%|##4 | Sample : 833/3450 [00:00<00:00, 19354.83it/s]
37%|###6 | Sample : 1268/3450 [00:00<00:00, 21599.12it/s]
50%|####9 | Sample : 1715/3450 [00:00<00:00, 23059.42it/s]
63%|######2 | Sample : 2168/3450 [00:00<00:00, 24083.70it/s]
75%|#######5 | Sample : 2596/3450 [00:00<00:00, 24537.40it/s]
78%|#######8 | Sample : 2698/3450 [00:00<00:00, 18287.94it/s]
90%|######### | Sample : 3112/3450 [00:00<00:00, 19185.06it/s]
100%|##########| Sample : 3450/3450 [00:00<00:00, 20134.09it/s]
Compare model performance¶
Below we visualize the model performance of each regularization method (ridge vs. Laplacian) for different levels of alpha. As you can see, the Laplacian method performs better in general, because it imposes a smoothness constraint along the time and feature dimensions of the coefficients. This matches the “true” receptive field structure and results in a better model fit.
fig = plt.figure(figsize=(10, 6))
ax = plt.subplot2grid([3, len(alphas)], [2, 0], 1, len(alphas))
ax.plot(np.arange(len(alphas)), scores_lap, marker='o', color='r')
ax.plot(np.arange(len(alphas)), scores, marker='o', color='0.5', ls=':')
ax.annotate('Best Laplacian', (ix_best_alpha_lap,
scores_lap[ix_best_alpha_lap]),
(ix_best_alpha_lap, scores_lap[ix_best_alpha_lap] - .1),
arrowprops={'arrowstyle': '->'})
ax.annotate('Best Ridge', (ix_best_alpha, scores[ix_best_alpha]),
(ix_best_alpha, scores[ix_best_alpha] - .1),
arrowprops={'arrowstyle': '->'})
plt.xticks(np.arange(len(alphas)), ["%.0e" % ii for ii in alphas])
ax.set(xlabel="Laplacian regularization value", ylabel="Score ($R^2$)",
xlim=[-.4, len(alphas) - .6])
mne.viz.tight_layout()
# Plot the STRF of each ridge parameter
xlim = times[[0, -1]]
for ii, (rf_lap, rf, i_alpha) in enumerate(zip(models_lap, models, alphas)):
ax = plt.subplot2grid([3, len(alphas)], [0, ii], 1, 1)
ax.pcolormesh(times, rf_lap.feature_names, rf_lap.coef_[0], **kwargs)
ax.set(xticks=[], yticks=[], xlim=xlim)
if ii == 0:
ax.set(ylabel='Laplacian')
ax = plt.subplot2grid([3, len(alphas)], [1, ii], 1, 1)
ax.pcolormesh(times, rf.feature_names, rf.coef_[0], **kwargs)
ax.set(xticks=[], yticks=[], xlim=xlim)
if ii == 0:
ax.set(ylabel='Ridge')
fig.suptitle('Model coefficients / scores for laplacian regularization', y=1)
mne.viz.tight_layout()
Plot the original STRF, and the one that we recovered with modeling.
rf = models[ix_best_alpha]
rf_lap = models_lap[ix_best_alpha_lap]
_, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(9, 3),
sharey=True, sharex=True)
ax1.pcolormesh(delays_sec, freqs, weights, **kwargs)
ax2.pcolormesh(times, rf.feature_names, rf.coef_[0], **kwargs)
ax3.pcolormesh(times, rf_lap.feature_names, rf_lap.coef_[0], **kwargs)
ax1.set_title('Original STRF')
ax2.set_title('Best Ridge STRF')
ax3.set_title('Best Laplacian STRF')
plt.setp([iax.get_xticklabels() for iax in [ax1, ax2, ax3]], rotation=45)
plt.autoscale(tight=True)
mne.viz.tight_layout()
References¶
- 1(1,2)
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
Frédéric E. Theunissen, Stephen V. David, Nandini C. Singh, Ann Hsu, William E. Vinje, and Jack L. Gallant. Estimating spatio-temporal receptive fields of auditory and visual neurons from their responses to natural stimuli. Network: Computation in Neural Systems, 12(3):289–316, 2001. doi:10.1080/net.12.3.289.316.
- 3
Ben Willmore and Darragh Smyth. Methods for first-order kernel estimation: simple-cell receptive fields from responses to natural scenes. Network: Computation in Neural Systems, 14(3):553–577, 2003. doi:10.1088/0954-898X_14_3_309.
- 4
Christopher R. Holdgraf, Wendy de Heer, Brian Pasley, Jochem Rieger, Nathan Crone, Jack J. Lin, Robert T. Knight, and Frédéric E. Theunissen. Rapid tuning shifts in human auditory cortex enhance speech intelligibility. Nature Communications, 2016. doi:10.1038/ncomms13654.
Total running time of the script: ( 0 minutes 27.144 seconds)
Estimated memory usage: 9 MB