Note

Go to the end to download the full example code

# Computing various MNE solutions#

This example shows example fixed- and free-orientation source localizations produced by the minimum-norm variants implemented in MNE-Python: MNE, dSPM, sLORETA, and eLORETA.

```
# Author: Eric Larson <larson.eric.d@gmail.com>
#
# License: BSD-3-Clause
```

```
import mne
from mne.datasets import sample
from mne.minimum_norm import make_inverse_operator, apply_inverse
print(__doc__)
data_path = sample.data_path()
subjects_dir = data_path / 'subjects'
# Read data (just MEG here for speed, though we could use MEG+EEG)
meg_path = data_path / 'MEG' / 'sample'
fname_evoked = meg_path / 'sample_audvis-ave.fif'
evoked = mne.read_evokeds(fname_evoked, condition='Right Auditory',
baseline=(None, 0))
fname_fwd = meg_path / 'sample_audvis-meg-oct-6-fwd.fif'
fname_cov = meg_path / 'sample_audvis-cov.fif'
fwd = mne.read_forward_solution(fname_fwd)
cov = mne.read_cov(fname_cov)
# crop for speed in these examples
evoked.crop(0.05, 0.15)
```

```
Reading /home/circleci/mne_data/MNE-sample-data/MEG/sample/sample_audvis-ave.fif ...
Read a total of 4 projection items:
PCA-v1 (1 x 102) active
PCA-v2 (1 x 102) active
PCA-v3 (1 x 102) active
Average EEG reference (1 x 60) active
Found the data of interest:
t = -199.80 ... 499.49 ms (Right Auditory)
0 CTF compensation matrices available
nave = 61 - aspect type = 100
Projections have already been applied. Setting proj attribute to True.
Applying baseline correction (mode: mean)
Reading forward solution from /home/circleci/mne_data/MNE-sample-data/MEG/sample/sample_audvis-meg-oct-6-fwd.fif...
Reading a source space...
Computing patch statistics...
Patch information added...
Distance information added...
[done]
Reading a source space...
Computing patch statistics...
Patch information added...
Distance information added...
[done]
2 source spaces read
Desired named matrix (kind = 3523) not available
Read MEG forward solution (7498 sources, 306 channels, free orientations)
Source spaces transformed to the forward solution coordinate frame
366 x 366 full covariance (kind = 1) found.
Read a total of 4 projection items:
PCA-v1 (1 x 102) active
PCA-v2 (1 x 102) active
PCA-v3 (1 x 102) active
Average EEG reference (1 x 60) active
```

## Fixed orientation#

First let’s create a fixed-orientation inverse, with the default weighting.

```
inv = make_inverse_operator(evoked.info, fwd, cov, loose=0., depth=0.8,
verbose=True)
```

```
Computing inverse operator with 305 channels.
305 out of 306 channels remain after picking
Selected 305 channels
Creating the depth weighting matrix...
203 planar channels
limit = 7265/7498 = 10.037797
scale = 2.52065e-08 exp = 0.8
Picked elements from a free-orientation depth-weighting prior into the fixed-orientation one
Average patch normals will be employed in the rotation to the local surface coordinates....
Converting to surface-based source orientations...
[done]
Whitening the forward solution.
Created an SSP operator (subspace dimension = 3)
Computing rank from covariance with rank=None
Using tolerance 3.3e-13 (2.2e-16 eps * 305 dim * 4.8 max singular value)
Estimated rank (mag + grad): 302
MEG: rank 302 computed from 305 data channels with 3 projectors
Setting small MEG eigenvalues to zero (without PCA)
Creating the source covariance matrix
Adjusting source covariance matrix.
Computing SVD of whitened and weighted lead field matrix.
largest singular value = 4.65922
scaling factor to adjust the trace = 6.72259e+18 (nchan = 305 nzero = 3)
```

Let’s look at the current estimates using MNE. We’ll take the absolute value of the source estimates to simplify the visualization.

```
snr = 3.0
lambda2 = 1.0 / snr ** 2
kwargs = dict(initial_time=0.08, hemi='lh', subjects_dir=subjects_dir,
size=(600, 600), clim=dict(kind='percent', lims=[90, 95, 99]),
smoothing_steps=7)
stc = abs(apply_inverse(evoked, inv, lambda2, 'MNE', verbose=True))
brain = stc.plot(figure=1, **kwargs)
brain.add_text(0.1, 0.9, 'MNE', 'title', font_size=14)
```

```
Preparing the inverse operator for use...
Scaled noise and source covariance from nave = 1 to nave = 61
Created the regularized inverter
Created an SSP operator (subspace dimension = 3)
Created the whitener using a noise covariance matrix with rank 302 (3 small eigenvalues omitted)
Applying inverse operator to "Right Auditory"...
Picked 305 channels from the data
Computing inverse...
Eigenleads need to be weighted ...
Computing residual...
Explained 78.4% variance
[done]
Using control points [1.28047949e-10 1.72734061e-10 2.97200565e-10]
```

Next let’s use the default noise normalization, dSPM:

```
stc = abs(apply_inverse(evoked, inv, lambda2, 'dSPM', verbose=True))
brain = stc.plot(figure=2, **kwargs)
brain.add_text(0.1, 0.9, 'dSPM', 'title', font_size=14)
```

```
Preparing the inverse operator for use...
Scaled noise and source covariance from nave = 1 to nave = 61
Created the regularized inverter
Created an SSP operator (subspace dimension = 3)
Created the whitener using a noise covariance matrix with rank 302 (3 small eigenvalues omitted)
Computing noise-normalization factors (dSPM)...
[done]
Applying inverse operator to "Right Auditory"...
Picked 305 channels from the data
Computing inverse...
Eigenleads need to be weighted ...
Computing residual...
Explained 78.4% variance
dSPM...
[done]
Using control points [ 4.56295843 6.27742998 12.00597191]
```

And sLORETA:

```
stc = abs(apply_inverse(evoked, inv, lambda2, 'sLORETA', verbose=True))
brain = stc.plot(figure=3, **kwargs)
brain.add_text(0.1, 0.9, 'sLORETA', 'title', font_size=14)
```

```
Preparing the inverse operator for use...
Scaled noise and source covariance from nave = 1 to nave = 61
Created the regularized inverter
Created an SSP operator (subspace dimension = 3)
Created the whitener using a noise covariance matrix with rank 302 (3 small eigenvalues omitted)
Computing noise-normalization factors (sLORETA)...
[done]
Applying inverse operator to "Right Auditory"...
Picked 305 channels from the data
Computing inverse...
Eigenleads need to be weighted ...
Computing residual...
Explained 78.4% variance
sLORETA...
[done]
Using control points [1.65359441 2.22223215 3.63030546]
```

And finally eLORETA:

```
stc = abs(apply_inverse(evoked, inv, lambda2, 'eLORETA', verbose=True))
brain = stc.plot(figure=4, **kwargs)
brain.add_text(0.1, 0.9, 'eLORETA', 'title', font_size=14)
del inv
```

```
Preparing the inverse operator for use...
Scaled noise and source covariance from nave = 1 to nave = 61
Created the regularized inverter
Created an SSP operator (subspace dimension = 3)
Created the whitener using a noise covariance matrix with rank 302 (3 small eigenvalues omitted)
Computing optimized source covariance (eLORETA)...
Fitting up to 20 iterations...
Converged on iteration 11 (7.1e-07 < 1e-06)
Updating inverse with weighted eigen leads
[done]
Applying inverse operator to "Right Auditory"...
Picked 305 channels from the data
Computing inverse...
Eigenleads already weighted ...
Computing residual...
Explained 78.0% variance
[done]
Using control points [1.34003429e-10 1.79067976e-10 2.93243713e-10]
```

## Free orientation#

Now let’s not constrain the orientation of the dipoles at all by creating a free-orientation inverse.

```
inv = make_inverse_operator(evoked.info, fwd, cov, loose=1., depth=0.8,
verbose=True)
del fwd
```

```
Computing inverse operator with 305 channels.
305 out of 306 channels remain after picking
Selected 305 channels
Creating the depth weighting matrix...
203 planar channels
limit = 7265/7498 = 10.037797
scale = 2.52065e-08 exp = 0.8
Whitening the forward solution.
Created an SSP operator (subspace dimension = 3)
Computing rank from covariance with rank=None
Using tolerance 3.3e-13 (2.2e-16 eps * 305 dim * 4.8 max singular value)
Estimated rank (mag + grad): 302
MEG: rank 302 computed from 305 data channels with 3 projectors
Setting small MEG eigenvalues to zero (without PCA)
Creating the source covariance matrix
Adjusting source covariance matrix.
Computing SVD of whitened and weighted lead field matrix.
largest singular value = 4.61893
scaling factor to adjust the trace = 1.8713e+19 (nchan = 305 nzero = 3)
```

Let’s look at the current estimates using MNE. We’ll take the absolute value of the source estimates to simplify the visualization.

```
stc = apply_inverse(evoked, inv, lambda2, 'MNE', verbose=True)
brain = stc.plot(figure=5, **kwargs)
brain.add_text(0.1, 0.9, 'MNE', 'title', font_size=14)
```

```
Preparing the inverse operator for use...
Scaled noise and source covariance from nave = 1 to nave = 61
Created the regularized inverter
Created an SSP operator (subspace dimension = 3)
Created the whitener using a noise covariance matrix with rank 302 (3 small eigenvalues omitted)
Applying inverse operator to "Right Auditory"...
Picked 305 channels from the data
Computing inverse...
Eigenleads need to be weighted ...
Computing residual...
Explained 78.5% variance
Combining the current components...
[done]
Using control points [7.81398589e-11 1.00293217e-10 1.59049572e-10]
```

Next let’s use the default noise normalization, dSPM:

```
stc = apply_inverse(evoked, inv, lambda2, 'dSPM', verbose=True)
brain = stc.plot(figure=6, **kwargs)
brain.add_text(0.1, 0.9, 'dSPM', 'title', font_size=14)
```

```
Preparing the inverse operator for use...
Scaled noise and source covariance from nave = 1 to nave = 61
Created the regularized inverter
Created an SSP operator (subspace dimension = 3)
Created the whitener using a noise covariance matrix with rank 302 (3 small eigenvalues omitted)
Computing noise-normalization factors (dSPM)...
[done]
Applying inverse operator to "Right Auditory"...
Picked 305 channels from the data
Computing inverse...
Eigenleads need to be weighted ...
Computing residual...
Explained 78.5% variance
Combining the current components...
dSPM...
[done]
Using control points [ 4.79225614 6.45181117 11.88420763]
```

sLORETA:

```
stc = apply_inverse(evoked, inv, lambda2, 'sLORETA', verbose=True)
brain = stc.plot(figure=7, **kwargs)
brain.add_text(0.1, 0.9, 'sLORETA', 'title', font_size=14)
```

```
Preparing the inverse operator for use...
Scaled noise and source covariance from nave = 1 to nave = 61
Created the regularized inverter
Created an SSP operator (subspace dimension = 3)
Created the whitener using a noise covariance matrix with rank 302 (3 small eigenvalues omitted)
Computing noise-normalization factors (sLORETA)...
[done]
Applying inverse operator to "Right Auditory"...
Picked 305 channels from the data
Computing inverse...
Eigenleads need to be weighted ...
Computing residual...
Explained 78.5% variance
Combining the current components...
sLORETA...
[done]
Using control points [1.65906465 2.114468 3.42240231]
```

And finally eLORETA:

```
stc = apply_inverse(evoked, inv, lambda2, 'eLORETA', verbose=True,
method_params=dict(eps=1e-4)) # larger eps just for speed
brain = stc.plot(figure=8, **kwargs)
brain.add_text(0.1, 0.9, 'eLORETA', 'title', font_size=14)
```

```
Preparing the inverse operator for use...
Scaled noise and source covariance from nave = 1 to nave = 61
Created the regularized inverter
Created an SSP operator (subspace dimension = 3)
Created the whitener using a noise covariance matrix with rank 302 (3 small eigenvalues omitted)
Computing optimized source covariance (eLORETA)...
Using independent orientation weights
Fitting up to 20 iterations (this make take a while)...
Converged on iteration 7 (6.7e-05 < 0.0001)
Updating inverse with weighted eigen leads
[done]
Applying inverse operator to "Right Auditory"...
Picked 305 channels from the data
Computing inverse...
Eigenleads already weighted ...
Computing residual...
Explained 78.2% variance
Combining the current components...
[done]
Using control points [9.41240751e-11 1.15338772e-10 1.80101709e-10]
```

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

**Estimated memory usage:** 278 MB