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

See Wipf et al. “A unified Bayesian framework for MEG/EEG source imaging.” NeuroImage, vol. 44, no. 3, pp. 947?66, Mar. 2009.

  • ../../_images/sphx_glr_plot_gamma_map_inverse_001.png
  • ../../_images/sphx_glr_plot_gamma_map_inverse_002.png
  • ../../_images/sphx_glr_plot_gamma_map_inverse_003.png
  • ../../_images/sphx_glr_plot_gamma_map_inverse_004.png

Out:

Reading /home/ubuntu/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/ubuntu/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
    Converting to surface-based source orientations...
    Average patch normals will be employed in the rotation to the local surface coordinates....
[done]
    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
    EEG regularization : 0.1
    Created an SSP operator for EEG (dimension = 1)
    MAG regularization : 0.1
    Created an SSP operator for MAG (dimension = 3)
    GRAD regularization : 0.1
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.053e-01
Iteration: 8     active set size: 22323  convergence: 1.780e-01
Iteration: 9     active set size: 20868  convergence: 1.290e-01
Iteration: 10    active set size: 17409  convergence: 1.007e-01
Iteration: 11    active set size: 13170  convergence: 9.034e-02
Iteration: 12    active set size: 10143  convergence: 8.072e-02
Iteration: 13    active set size: 8388   convergence: 7.193e-02
Iteration: 14    active set size: 7236   convergence: 6.412e-02
Iteration: 15    active set size: 6348   convergence: 5.725e-02
Iteration: 16    active set size: 5700   convergence: 5.125e-02
Iteration: 17    active set size: 5160   convergence: 4.603e-02
Iteration: 18    active set size: 4671   convergence: 4.147e-02
Iteration: 19    active set size: 4281   convergence: 3.750e-02
Iteration: 20    active set size: 3948   convergence: 3.402e-02
Iteration: 21    active set size: 3633   convergence: 3.098e-02
Iteration: 22    active set size: 3408   convergence: 2.830e-02
Iteration: 23    active set size: 3135   convergence: 2.591e-02
Iteration: 24    active set size: 2922   convergence: 2.377e-02
Iteration: 25    active set size: 2679   convergence: 2.184e-02
Iteration: 26    active set size: 2517   convergence: 2.010e-02
Iteration: 27    active set size: 2307   convergence: 1.851e-02
Iteration: 28    active set size: 2157   convergence: 1.705e-02
Iteration: 29    active set size: 2022   convergence: 1.571e-02
Iteration: 30    active set size: 1884   convergence: 1.448e-02
Iteration: 31    active set size: 1782   convergence: 1.334e-02
Iteration: 32    active set size: 1653   convergence: 1.229e-02
Iteration: 33    active set size: 1548   convergence: 1.132e-02
Iteration: 34    active set size: 1467   convergence: 1.042e-02
Iteration: 35    active set size: 1359   convergence: 9.596e-03
Iteration: 36    active set size: 1302   convergence: 8.831e-03
Iteration: 37    active set size: 1224   convergence: 8.126e-03
Iteration: 38    active set size: 1155   convergence: 7.477e-03
Iteration: 39    active set size: 1071   convergence: 6.879e-03
Iteration: 40    active set size: 1014   convergence: 6.330e-03
Iteration: 41    active set size: 969    convergence: 5.826e-03
Iteration: 42    active set size: 909    convergence: 5.363e-03
Iteration: 43    active set size: 846    convergence: 4.938e-03
Iteration: 44    active set size: 789    convergence: 4.550e-03
Iteration: 45    active set size: 759    convergence: 4.193e-03
Iteration: 46    active set size: 714    convergence: 3.867e-03
Iteration: 47    active set size: 690    convergence: 3.569e-03
Iteration: 48    active set size: 663    convergence: 3.295e-03
Iteration: 49    active set size: 633    convergence: 3.045e-03
Iteration: 50    active set size: 615    convergence: 2.816e-03
Iteration: 51    active set size: 591    convergence: 2.605e-03
Iteration: 52    active set size: 558    convergence: 2.413e-03
Iteration: 53    active set size: 540    convergence: 2.237e-03
Iteration: 54    active set size: 525    convergence: 2.075e-03
Iteration: 55    active set size: 501    convergence: 1.926e-03
Iteration: 56    active set size: 486    convergence: 1.790e-03
Iteration: 57    active set size: 471    convergence: 1.665e-03
Iteration: 58    active set size: 459    convergence: 1.549e-03
Iteration: 59    active set size: 444    convergence: 1.443e-03
Iteration: 60    active set size: 420    convergence: 1.346e-03
Iteration: 61    active set size: 405    convergence: 1.269e-03
Iteration: 62    active set size: 393    convergence: 1.198e-03
Iteration: 63    active set size: 369    convergence: 1.133e-03
Iteration: 64    active set size: 351    convergence: 1.071e-03
Iteration: 65    active set size: 348    convergence: 1.014e-03
Iteration: 66    active set size: 336    convergence: 9.612e-04
Iteration: 67    active set size: 324    convergence: 9.116e-04
Iteration: 68    active set size: 306    convergence: 8.652e-04
Iteration: 69    active set size: 294    convergence: 8.218e-04
Iteration: 70    active set size: 282    convergence: 7.812e-04
Iteration: 71    active set size: 273    convergence: 7.432e-04
Iteration: 72    active set size: 258    convergence: 7.077e-04
Iteration: 73    active set size: 246    convergence: 6.743e-04
Iteration: 74    active set size: 240    convergence: 6.431e-04
Iteration: 75    active set size: 228    convergence: 6.137e-04
Iteration: 76    active set size: 216    convergence: 5.862e-04
Iteration: 77    active set size: 204    convergence: 5.603e-04
Iteration: 78    active set size: 195    convergence: 5.360e-04
Iteration: 80    active set size: 192    convergence: 4.916e-04
Iteration: 81    active set size: 186    convergence: 4.713e-04
Iteration: 82    active set size: 180    convergence: 4.522e-04
Iteration: 83    active set size: 177    convergence: 4.341e-04
Iteration: 84    active set size: 174    convergence: 4.171e-04
Iteration: 85    active set size: 171    convergence: 4.011e-04
Iteration: 86    active set size: 159    convergence: 3.859e-04
Iteration: 87    active set size: 153    convergence: 3.715e-04
Iteration: 88    active set size: 147    convergence: 3.580e-04
Iteration: 89    active set size: 144    convergence: 3.451e-04
Iteration: 91    active set size: 138    convergence: 3.214e-04
Iteration: 92    active set size: 135    convergence: 3.104e-04
Iteration: 93    active set size: 126    convergence: 3.001e-04
Iteration: 96    active set size: 123    convergence: 2.719e-04
Iteration: 97    active set size: 117    convergence: 2.634e-04
Iteration: 99    active set size: 114    convergence: 2.477e-04
Iteration: 101   active set size: 111    convergence: 2.334e-04
Iteration: 102   active set size: 108    convergence: 2.267e-04
Iteration: 104   active set size: 102    convergence: 2.143e-04
Iteration: 105   active set size: 99     convergence: 2.085e-04
Iteration: 106   active set size: 90     convergence: 2.029e-04
Iteration: 108   active set size: 87     convergence: 1.925e-04
Iteration: 115   active set size: 84     convergence: 1.624e-04
Iteration: 117   active set size: 81     convergence: 1.552e-04
Iteration: 119   active set size: 75     convergence: 1.486e-04
Iteration: 120   active set size: 72     convergence: 1.454e-04
Iteration: 121   active set size: 69     convergence: 1.424e-04
Iteration: 127   active set size: 66     convergence: 1.264e-04
Iteration: 130   active set size: 63     convergence: 1.196e-04
Iteration: 131   active set size: 60     convergence: 1.175e-04
Iteration: 135   active set size: 57     convergence: 1.097e-04
Iteration: 136   active set size: 54     convergence: 1.079e-04
Iteration: 143   active set size: 51     convergence: 9.666e-05
Iteration: 147   active set size: 48     convergence: 9.121e-05
Iteration: 148   active set size: 42     convergence: 8.994e-05
Iteration: 151   active set size: 39     convergence: 8.634e-05
Iteration: 156   active set size: 36     convergence: 8.094e-05
Iteration: 190   active set size: 33     convergence: 5.690e-05
Iteration: 224   active set size: 30     convergence: 4.381e-05
Iteration: 242   active set size: 27     convergence: 3.939e-05
Iteration: 282   active set size: 24     convergence: 3.121e-05
Iteration: 305   active set size: 21     convergence: 2.736e-05
Iteration: 389   active set size: 18     convergence: 1.706e-05
Iteration: 494   active set size: 15     convergence: 9.581e-06
Iteration: 536   active set size: 12     convergence: 7.628e-06
Iteration: 917   active set size: 12     convergence: 9.960e-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
combining the current components...
Total number of active sources: 4

# Author: Martin Luessi <mluessi@nmr.mgh.harvard.edu>
#
# License: BSD (3-clause)

import numpy as np

import mne
from mne.datasets import sample
from mne.inverse_sparse import gamma_map
from mne.viz import plot_sparse_source_estimates

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, surf_ori=True,
                                    force_fixed=False)

# 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
alpha = 0.5
stc, residual = gamma_map(evoked, forward, cov, alpha, xyz_same_gamma=True,
                          return_residual=True)

# 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")

# 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)

residual.pick_types(meg='grad', exclude='bads')
residual.plot(titles=dict(grad='Residuals Gradiometers'), ylim=ylim,
              proj=True)

Total running time of the script: ( 1 minutes 10.858 seconds)

Generated by Sphinx-Gallery