Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haribharadwaj/11232865 to your computer and use it in GitHub Desktop.
Save haribharadwaj/11232865 to your computer and use it in GitHub Desktop.
Just an example to plot single trial and averaged dSPM in a label. Average is done in two ways: (1) evoked and then using apply_inverse or (2) single trial inverse and then average.. They all match up nicely
"""
==================================================
Compute MNE-dSPM inverse solution on single epochs
==================================================
Compute dSPM inverse solution on single trial epochs restricted
to a brain label.
"""
# Author: Hari Bharadwaj <hari@nmr.mgh.harvard.edu>
#
# License: BSD (3-clause)
print(__doc__)
import numpy as np
import matplotlib.pyplot as plt
import mne
from mne.datasets import sample
from mne.fiff import Raw, pick_types
from mne.minimum_norm import apply_inverse_epochs, read_inverse_operator
from mne.minimum_norm import apply_inverse
data_path = sample.data_path()
fname_inv = data_path + '/MEG/sample/sample_audvis-meg-oct-6-meg-inv.fif'
fname_raw = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
fname_event = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'
label_name = 'Aud-lh'
fname_label = data_path + '/MEG/sample/labels/%s.label' % label_name
event_id, tmin, tmax = 1, -0.2, 0.5
# Use the same inverse operator when inspecting single trials Vs. evoked
snr = 3.0 # Standard assumption for average data but using it for sing trial
lambda2 = 1.0 / snr ** 2
method = "dSPM" # use dSPM method (could also be MNE or sLORETA)
# Load data
inverse_operator = read_inverse_operator(fname_inv)
label = mne.read_label(fname_label)
raw = Raw(fname_raw)
events = mne.read_events(fname_event)
# Set up pick list
include = []
# Add a bad channel
raw.info['bads'] += ['EEG 053'] # bads + 1 more
# pick MEG channels
picks = pick_types(raw.info, meg=True, eeg=False, stim=False, eog=True,
include=include, exclude='bads')
# Read epochs
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks,
baseline=(None, 0), reject=dict(mag=4e-12, grad=4000e-13,
eog=150e-6))
evoked = epochs.average()
# Compute inverse solution and stcs for each epoch
# Use the same inverse operator as with evoked data (i.e., set nave)
stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, method, label,
pick_ori="normal", nave = evoked.nave)
stc_evoked = apply_inverse(evoked, inverse_operator, lambda2, method,
pick_ori = "normal")
stc_evoked_label = stc_evoked.in_label(label)
mean_stc = sum(stcs) / len(stcs)
label_mean = np.mean(mean_stc.data, axis=0)
label_mean_evoked = np.mean(stc_evoked_label.data, axis = 0)
###############################################################################
# Viewing single trial dSPM and average dSPM for unflipped pooling over label
# Single trial
plt.figure()
for k, stc_trial in enumerate(stcs):
plt.plot(1e3*stcs[k].times, np.mean(stcs[k].data, axis = 0).T,'k--')
plt.hold(True)
# Single trial inverse then average.. making linewidth large to not be masked
plt.plot(1e3 * stcs[0].times, label_mean,'r',linewidth = 5)
plt.hold(True)
# Evoked and then inverse
plt.plot(1e3 * stcs[0].times, label_mean_evoked,'b',linewidth = 3)
plt.xlabel('time (ms)')
plt.ylabel('dSPM value')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment