{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "\n================================================================\nCompute sparse inverse solution with mixed norm: MxNE and irMxNE\n================================================================\n\nRuns (ir)MxNE (L1/L2 [1]_ or L0.5/L2 [2]_ mixed norm) inverse solver.\nL0.5/L2 is done with irMxNE which allows for sparser\nsource estimates with less amplitude bias due to the non-convexity\nof the L0.5/L2 mixed norm penalty.\n\nReferences\n----------\n.. [1] Gramfort A., Kowalski M. and Hamalainen, M.\n \"Mixed-norm estimates for the M/EEG inverse problem using accelerated\n gradient methods\", Physics in Medicine and Biology, 2012.\n https://doi.org/10.1088/0031-9155/57/7/1937.\n\n.. [2] Strohmeier D., Haueisen J., and Gramfort A.\n \"Improved MEG/EEG source localization with reweighted mixed-norms\",\n 4th International Workshop on Pattern Recognition in Neuroimaging,\n Tuebingen, 2014. 10.1109/PRNI.2014.6858545\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# Author: Alexandre Gramfort \n#\n# License: BSD (3-clause)\n\nimport mne\nfrom mne.datasets import sample\nfrom mne.inverse_sparse import mixed_norm\nfrom mne.minimum_norm import make_inverse_operator, apply_inverse\nfrom mne.viz import plot_sparse_source_estimates\n\nprint(__doc__)\n\ndata_path = sample.data_path()\nfwd_fname = data_path + '/MEG/sample/sample_audvis-meg-eeg-oct-6-fwd.fif'\nave_fname = data_path + '/MEG/sample/sample_audvis-ave.fif'\ncov_fname = data_path + '/MEG/sample/sample_audvis-shrunk-cov.fif'\nsubjects_dir = data_path + '/subjects'\n\n# Read noise covariance matrix\ncov = mne.read_cov(cov_fname)\n# Handling average file\ncondition = 'Left Auditory'\nevoked = mne.read_evokeds(ave_fname, condition=condition, baseline=(None, 0))\nevoked.crop(tmin=0, tmax=0.3)\n# Handling forward solution\nforward = mne.read_forward_solution(fwd_fname, surf_ori=True)\n\nylim = dict(eeg=[-10, 10], grad=[-400, 400], mag=[-600, 600])\nevoked.plot(ylim=ylim, proj=True)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Run solver\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "alpha = 50 # regularization parameter between 0 and 100 (100 is high)\nloose, depth = 0.2, 0.9 # loose orientation & depth weighting\nn_mxne_iter = 10 # if > 1 use L0.5/L2 reweighted mixed norm solver\n# if n_mxne_iter > 1 dSPM weighting can be avoided.\n\n# Compute dSPM solution to be used as weights in MxNE\ninverse_operator = make_inverse_operator(evoked.info, forward, cov,\n loose=None, depth=depth, fixed=True)\nstc_dspm = apply_inverse(evoked, inverse_operator, lambda2=1. / 9.,\n method='dSPM')\n\n# Compute (ir)MxNE inverse solution\nstc, residual = mixed_norm(\n evoked, forward, cov, alpha, loose=loose, depth=depth, maxit=3000,\n tol=1e-4, active_set_size=10, debias=True, weights=stc_dspm,\n weights_min=8., n_mxne_iter=n_mxne_iter, return_residual=True)\nresidual.plot(ylim=ylim, proj=True)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "View in 2D and 3D (\"glass\" brain like 3D plot)\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "plot_sparse_source_estimates(forward['src'], stc, bgcolor=(1, 1, 1),\n fig_name=\"MxNE (cond %s)\" % condition,\n opacity=0.1)\n\n# and on the fsaverage brain after morphing\nstc_fsaverage = stc.morph(subject_from='sample', subject_to='fsaverage',\n grade=None, sparse=True, subjects_dir=subjects_dir)\nsrc_fsaverage_fname = subjects_dir + '/fsaverage/bem/fsaverage-ico-5-src.fif'\nsrc_fsaverage = mne.read_source_spaces(src_fsaverage_fname)\n\nplot_sparse_source_estimates(src_fsaverage, stc_fsaverage, bgcolor=(1, 1, 1),\n opacity=0.1)" ], "outputs": [], "metadata": { "collapsed": false } } ], "metadata": { "kernelspec": { "display_name": "Python 2", "name": "python2", "language": "python" }, "language_info": { "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "file_extension": ".py", "version": "2.7.13", "pygments_lexer": "ipython2", "codemirror_mode": { "version": 2, "name": "ipython" } } } }