Source localization with MNE/dSPM/sLORETA

The aim of this tutorial is to teach you how to compute and apply a linear inverse method such as MNE/dSPM/sLORETA on evoked/raw/epochs data.

import numpy as np
import matplotlib.pyplot as plt

import mne
from mne.datasets import sample
from mne.minimum_norm import (make_inverse_operator, apply_inverse,

# sphinx_gallery_thumbnail_number = 9

Process MEG data

data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'

raw =  # already has an average reference
events = mne.find_events(raw, stim_channel='STI 014')

event_id = dict(aud_r=1)  # event trigger and conditions
tmin = -0.2  # start of each epoch (200ms before the trigger)
tmax = 0.5  # end of each epoch (500ms after the trigger)['bads'] = ['MEG 2443', 'EEG 053']
picks = mne.pick_types(, meg=True, eeg=False, eog=True,
baseline = (None, 0)  # means from the first instant to t = 0
reject = dict(grad=4000e-13, mag=4e-12, eog=150e-6)

epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks,
                    baseline=baseline, reject=reject)

Compute regularized noise covariance

For more details see Computing covariance matrix.

noise_cov = mne.compute_covariance(
    epochs, tmax=0., method=['shrunk', 'empirical'])

fig_cov, fig_spectra = mne.viz.plot_cov(noise_cov,
  • ../_images/sphx_glr_plot_mne_dspm_source_localization_001.png
  • ../_images/sphx_glr_plot_mne_dspm_source_localization_002.png

Compute the evoked response

evoked = epochs.average()
evoked.plot_topomap(times=np.linspace(0.05, 0.15, 5), ch_type='mag')

# Show whitening
  • ../_images/sphx_glr_plot_mne_dspm_source_localization_003.png
  • ../_images/sphx_glr_plot_mne_dspm_source_localization_004.png
  • ../_images/sphx_glr_plot_mne_dspm_source_localization_005.png

Inverse modeling: MNE/dSPM on evoked and raw data

# Read the forward solution and compute the inverse operator

fname_fwd = data_path + '/MEG/sample/sample_audvis-meg-oct-6-fwd.fif'
fwd = mne.read_forward_solution(fname_fwd)
fwd = mne.convert_forward_solution(fwd, surf_ori=True)

# Restrict forward solution as necessary for MEG
fwd = mne.pick_types_forward(fwd, meg=True, eeg=False)

# make an MEG inverse operator
info =
inverse_operator = make_inverse_operator(info, fwd, noise_cov,
                                         loose=0.2, depth=0.8)


Compute inverse solution

method = "dSPM"
snr = 3.
lambda2 = 1. / snr ** 2
stc = apply_inverse(evoked, inverse_operator, lambda2,
                    method=method, pick_ori=None)

del fwd, epochs  # to save memory


View activation time-series

plt.plot(1e3 * stc.times,[::100, :].T)
plt.xlabel('time (ms)')
plt.ylabel('%s value' % method)

Here we use peak getter to move visualization to the time point of the peak and draw a marker at the maximum peak vertex.

vertno_max, time_max = stc.get_peak(hemi='rh')

subjects_dir = data_path + '/subjects'
brain = stc.plot(surface='inflated', hemi='rh', subjects_dir=subjects_dir,
                 clim=dict(kind='value', lims=[8, 12, 15]),
                 initial_time=time_max, time_unit='s')
brain.add_foci(vertno_max, coords_as_verts=True, hemi='rh', color='blue',

Morph data to average brain

fs_vertices = [np.arange(10242)] * 2  # fsaverage is special this way
morph_mat = mne.compute_morph_matrix('sample', 'fsaverage', stc.vertices,
                                     fs_vertices, smooth=None,
stc_fsaverage = stc.morph_precomputed('fsaverage', fs_vertices, morph_mat)
brain_fsaverage = stc_fsaverage.plot(
    surface='inflated', hemi='rh', subjects_dir=subjects_dir,
    clim=dict(kind='value', lims=[8, 12, 15]), initial_time=time_max,
    time_unit='s', size=(800, 800), smoothing_steps=5)

Dipole orientations

The pick_ori parameter of the mne.minimum_norm.apply_inverse() function controls the orientation of the dipoles. One useful setting is pick_ori='vector', which will return an estimate that does not only contain the source power at each dipole, but also the orientation of the dipoles.

stc_vec = apply_inverse(evoked, inverse_operator, lambda2,
                        method=method, pick_ori='vector')
stc_vec.plot(hemi='rh', subjects_dir=subjects_dir,
             clim=dict(kind='value', lims=[8, 12, 15]),
             initial_time=time_max, time_unit='s')

Note that there is a relationship between the orientation of the dipoles and the surface of the cortex. For this reason, we do not use an inflated cortical surface for visualization, but the original surface used to define the source space.

For more information about dipole orientations, see The role of dipole orientations in distributed source localization.


  • By changing the method parameter to ‘sloreta’ recompute the source estimates using the sLORETA method.

Total running time of the script: ( 0 minutes 57.782 seconds)

Gallery generated by Sphinx-Gallery