Compute a sparse inverse solution using the Gamma-Map empirical Bayesian method

See [1] for details.

References

[1]D. Wipf, S. Nagarajan “A unified Bayesian framework for MEG/EEG source imaging”, Neuroimage, Volume 44, Number 3, pp. 947-966, Feb. 2009. DOI: 10.1016/j.neuroimage.2008.02.059
# Author: Martin Luessi <mluessi@nmr.mgh.harvard.edu>
#         Daniel Strohmeier <daniel.strohmeier@tu-ilmenau.de>
#
# License: BSD (3-clause)

import numpy as np

import mne
from mne.datasets import sample
from mne.inverse_sparse import gamma_map, make_stc_from_dipoles
from mne.viz import (plot_sparse_source_estimates,
                     plot_dipole_locations, plot_dipole_amplitudes)

print(__doc__)

data_path = sample.data_path()
subjects_dir = data_path + '/subjects'
fwd_fname = data_path + '/MEG/sample/sample_audvis-meg-eeg-oct-6-fwd.fif'
evoked_fname = data_path + '/MEG/sample/sample_audvis-ave.fif'
cov_fname = data_path + '/MEG/sample/sample_audvis-cov.fif'

# Read the evoked response and crop it
condition = 'Left visual'
evoked = mne.read_evokeds(evoked_fname, condition=condition,
                          baseline=(None, 0))
evoked.crop(tmin=-50e-3, tmax=300e-3)

# Read the forward solution
forward = mne.read_forward_solution(fwd_fname)

# Read noise noise covariance matrix and regularize it
cov = mne.read_cov(cov_fname)
cov = mne.cov.regularize(cov, evoked.info)

# Run the Gamma-MAP method with dipole output
alpha = 0.5
dipoles, residual = gamma_map(
    evoked, forward, cov, alpha, xyz_same_gamma=True, return_residual=True,
    return_as_dipoles=True)

Out:

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 (Left visual)
        0 CTF compensation matrices available
        nave = 67 - 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-eeg-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)
    Desired named matrix (kind = 3523) not available
    Read EEG forward solution (7498 sources, 60 channels, free orientations)
    MEG and EEG forward solutions combined
    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
8 projection items activated
    MAG regularization : 0.1
    Created an SSP operator for MAG (dimension = 3)
    GRAD regularization : 0.1
    EEG regularization : 0.1
    Created an SSP operator for EEG (dimension = 1)
    SEEG regularization : None
    ECOG regularization : None
    HBO regularization : None
    HBR regularization : None
    Average patch normals will be employed in the rotation to the local surface coordinates....
    Converting to surface-based source orientations...
    [done]
Computing inverse operator with 364 channels.
    Created an SSP operator (subspace dimension = 4)
estimated rank (mag + grad): 302
Setting small MEG eigenvalues to zero.
Not doing PCA for MEG.
estimated rank (eeg): 58
Setting small EEG eigenvalues to zero.
Not doing PCA for EEG.
Reducing data rank to 360
Total rank is 360
Whitening lead field matrix.
Applying loose dipole orientations. Loose value of 0.2.
Whitening data matrix.
Iteration: 0     active set size: 22494  convergence: 9.055e-01
Iteration: 8     active set size: 22236  convergence: 1.858e-01
Iteration: 9     active set size: 20715  convergence: 1.375e-01
Iteration: 10    active set size: 17181  convergence: 1.062e-01
Iteration: 11    active set size: 12999  convergence: 8.285e-02
Iteration: 12    active set size: 10107  convergence: 6.587e-02
Iteration: 13    active set size: 8313   convergence: 5.332e-02
Iteration: 14    active set size: 7209   convergence: 4.381e-02
Iteration: 15    active set size: 6354   convergence: 3.643e-02
Iteration: 16    active set size: 5700   convergence: 3.059e-02
Iteration: 17    active set size: 5145   convergence: 2.590e-02
Iteration: 18    active set size: 4701   convergence: 2.210e-02
Iteration: 19    active set size: 4293   convergence: 1.899e-02
Iteration: 20    active set size: 4005   convergence: 1.643e-02
Iteration: 21    active set size: 3693   convergence: 1.431e-02
Iteration: 22    active set size: 3432   convergence: 1.255e-02
Iteration: 23    active set size: 3153   convergence: 1.107e-02
Iteration: 24    active set size: 2958   convergence: 9.829e-03
Iteration: 25    active set size: 2745   convergence: 8.780e-03
Iteration: 26    active set size: 2562   convergence: 7.890e-03
Iteration: 27    active set size: 2397   convergence: 7.133e-03
Iteration: 28    active set size: 2226   convergence: 6.484e-03
Iteration: 29    active set size: 2067   convergence: 6.063e-03
Iteration: 30    active set size: 1935   convergence: 5.886e-03
Iteration: 31    active set size: 1836   convergence: 5.721e-03
Iteration: 32    active set size: 1725   convergence: 5.564e-03
Iteration: 33    active set size: 1614   convergence: 5.433e-03
Iteration: 34    active set size: 1512   convergence: 5.327e-03
Iteration: 35    active set size: 1437   convergence: 5.222e-03
Iteration: 36    active set size: 1350   convergence: 5.115e-03
Iteration: 37    active set size: 1245   convergence: 5.005e-03
Iteration: 38    active set size: 1200   convergence: 4.892e-03
Iteration: 39    active set size: 1128   convergence: 4.775e-03
Iteration: 40    active set size: 1077   convergence: 4.654e-03
Iteration: 41    active set size: 1011   convergence: 4.530e-03
Iteration: 42    active set size: 975    convergence: 4.402e-03
Iteration: 43    active set size: 927    convergence: 4.271e-03
Iteration: 44    active set size: 876    convergence: 4.138e-03
Iteration: 45    active set size: 840    convergence: 4.002e-03
Iteration: 46    active set size: 798    convergence: 3.866e-03
Iteration: 47    active set size: 744    convergence: 3.730e-03
Iteration: 48    active set size: 705    convergence: 3.594e-03
Iteration: 49    active set size: 687    convergence: 3.459e-03
Iteration: 50    active set size: 657    convergence: 3.325e-03
Iteration: 51    active set size: 627    convergence: 3.194e-03
Iteration: 52    active set size: 600    convergence: 3.065e-03
Iteration: 53    active set size: 588    convergence: 2.939e-03
Iteration: 54    active set size: 570    convergence: 2.816e-03
Iteration: 55    active set size: 552    convergence: 2.698e-03
Iteration: 56    active set size: 528    convergence: 2.583e-03
Iteration: 57    active set size: 501    convergence: 2.472e-03
Iteration: 58    active set size: 486    convergence: 2.365e-03
Iteration: 59    active set size: 462    convergence: 2.263e-03
Iteration: 60    active set size: 441    convergence: 2.164e-03
Iteration: 61    active set size: 417    convergence: 2.070e-03
Iteration: 62    active set size: 408    convergence: 1.980e-03
Iteration: 63    active set size: 402    convergence: 1.894e-03
Iteration: 64    active set size: 387    convergence: 1.812e-03
Iteration: 65    active set size: 375    convergence: 1.735e-03
Iteration: 66    active set size: 345    convergence: 1.660e-03
Iteration: 67    active set size: 339    convergence: 1.590e-03
Iteration: 68    active set size: 333    convergence: 1.523e-03
Iteration: 69    active set size: 330    convergence: 1.459e-03
Iteration: 70    active set size: 327    convergence: 1.399e-03
Iteration: 71    active set size: 312    convergence: 1.341e-03
Iteration: 72    active set size: 306    convergence: 1.287e-03
Iteration: 73    active set size: 297    convergence: 1.235e-03
Iteration: 74    active set size: 288    convergence: 1.186e-03
Iteration: 75    active set size: 273    convergence: 1.139e-03
Iteration: 76    active set size: 261    convergence: 1.095e-03
Iteration: 77    active set size: 252    convergence: 1.053e-03
Iteration: 78    active set size: 240    convergence: 1.013e-03
Iteration: 79    active set size: 234    convergence: 9.748e-04
Iteration: 80    active set size: 222    convergence: 9.388e-04
Iteration: 81    active set size: 219    convergence: 9.045e-04
Iteration: 82    active set size: 213    convergence: 8.719e-04
Iteration: 83    active set size: 210    convergence: 8.408e-04
Iteration: 84    active set size: 207    convergence: 8.113e-04
Iteration: 85    active set size: 198    convergence: 7.832e-04
Iteration: 86    active set size: 192    convergence: 7.564e-04
Iteration: 87    active set size: 186    convergence: 7.308e-04
Iteration: 88    active set size: 180    convergence: 7.065e-04
Iteration: 89    active set size: 174    convergence: 6.832e-04
Iteration: 91    active set size: 168    convergence: 6.398e-04
Iteration: 92    active set size: 159    convergence: 6.196e-04
Iteration: 93    active set size: 153    convergence: 6.002e-04
Iteration: 94    active set size: 150    convergence: 5.817e-04
Iteration: 97    active set size: 147    convergence: 5.307e-04
Iteration: 98    active set size: 141    convergence: 5.150e-04
Iteration: 99    active set size: 132    convergence: 5.001e-04
Iteration: 100   active set size: 129    convergence: 4.857e-04
Iteration: 101   active set size: 126    convergence: 4.719e-04
Iteration: 103   active set size: 123    convergence: 4.459e-04
Iteration: 104   active set size: 120    convergence: 4.337e-04
Iteration: 105   active set size: 117    convergence: 4.219e-04
Iteration: 106   active set size: 111    convergence: 4.105e-04
Iteration: 107   active set size: 108    convergence: 3.996e-04
Iteration: 108   active set size: 105    convergence: 3.891e-04
Iteration: 110   active set size: 102    convergence: 3.692e-04
Iteration: 113   active set size: 99     convergence: 3.418e-04
Iteration: 114   active set size: 93     convergence: 3.333e-04
Iteration: 115   active set size: 90     convergence: 3.251e-04
Iteration: 119   active set size: 87     convergence: 2.948e-04
Iteration: 120   active set size: 84     convergence: 2.879e-04
Iteration: 123   active set size: 75     convergence: 2.682e-04
Iteration: 125   active set size: 69     convergence: 2.561e-04
Iteration: 127   active set size: 66     convergence: 2.447e-04
Iteration: 133   active set size: 63     convergence: 2.142e-04
Iteration: 136   active set size: 60     convergence: 2.007e-04
Iteration: 154   active set size: 57     convergence: 1.386e-04
Iteration: 155   active set size: 54     convergence: 1.359e-04
Iteration: 168   active set size: 51     convergence: 1.056e-04
Iteration: 177   active set size: 48     convergence: 8.902e-05
Iteration: 178   active set size: 45     convergence: 8.737e-05
Iteration: 181   active set size: 42     convergence: 8.259e-05
Iteration: 211   active set size: 39     convergence: 4.756e-05
Iteration: 225   active set size: 36     convergence: 3.690e-05
Iteration: 252   active set size: 33     convergence: 2.269e-05
Iteration: 291   active set size: 30     convergence: 1.127e-05
Iteration: 303   active set size: 27     convergence: 9.090e-06
Iteration: 310   active set size: 24     convergence: 8.019e-06
Iteration: 313   active set size: 21     convergence: 7.600e-06
Iteration: 340   active set size: 18     convergence: 4.687e-06
Iteration: 342   active set size: 15     convergence: 4.522e-06
Iteration: 381   active set size: 12     convergence: 2.250e-06
Iteration: 427   active set size: 12     convergence: 9.879e-07

Convergence reached !

4 projection items deactivated
Created an SSP operator (subspace dimension = 4)
4 projection items activated
SSP projectors applied...
0 projection items deactivated
[done]

Plot dipole activations

plot_dipole_amplitudes(dipoles)

# Plot dipole location of the strongest dipole with MRI slices
idx = np.argmax([np.max(np.abs(dip.amplitude)) for dip in dipoles])
plot_dipole_locations(dipoles[idx], forward['mri_head_t'], 'sample',
                      subjects_dir=subjects_dir, mode='orthoview',
                      idx='amplitude')

# # Plot dipole locations of all dipoles with MRI slices
# for dip in dipoles:
#     plot_dipole_locations(dip, forward['mri_head_t'], 'sample',
#                           subjects_dir=subjects_dir, mode='orthoview',
#                           idx='amplitude')
  • ../../_images/sphx_glr_plot_gamma_map_inverse_001.png
  • ../../_images/sphx_glr_plot_gamma_map_inverse_002.png

Show the evoked response and the residual for gradiometers

ylim = dict(grad=[-120, 120])
evoked.pick_types(meg='grad', exclude='bads')
evoked.plot(titles=dict(grad='Evoked Response Gradiometers'), ylim=ylim,
            proj=True, time_unit='s')

residual.pick_types(meg='grad', exclude='bads')
residual.plot(titles=dict(grad='Residuals Gradiometers'), ylim=ylim,
              proj=True, time_unit='s')
  • ../../_images/sphx_glr_plot_gamma_map_inverse_003.png
  • ../../_images/sphx_glr_plot_gamma_map_inverse_004.png

Generate stc from dipoles

stc = make_stc_from_dipoles(dipoles, forward['src'])

Out:

Converting dipoles into a SourceEstimate.
[done]

View in 2D and 3D (“glass” brain like 3D plot) Show the sources as spheres scaled by their strength

scale_factors = np.max(np.abs(stc.data), axis=1)
scale_factors = 0.5 * (1 + scale_factors / np.max(scale_factors))

plot_sparse_source_estimates(
    forward['src'], stc, bgcolor=(1, 1, 1),
    modes=['sphere'], opacity=0.1, scale_factors=(scale_factors, None),
    fig_name="Gamma-MAP")
  • ../../_images/sphx_glr_plot_gamma_map_inverse_005.png
  • ../../_images/sphx_glr_plot_gamma_map_inverse_006.png

Out:

Total number of active sources: 4

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

Gallery generated by Sphinx-Gallery