{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Compute a sparse inverse solution using the Gamma-MAP empirical Bayesian method\n\nSee :footcite:`WipfNagarajan2009` for details.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Author: Martin Luessi \n# Daniel Strohmeier \n#\n# License: BSD-3-Clause\n# Copyright the MNE-Python contributors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nimport mne\nfrom mne.datasets import sample\nfrom mne.inverse_sparse import gamma_map, make_stc_from_dipoles\nfrom mne.viz import (\n plot_dipole_amplitudes,\n plot_dipole_locations,\n plot_sparse_source_estimates,\n)\n\ndata_path = sample.data_path()\nsubjects_dir = data_path / \"subjects\"\nmeg_path = data_path / \"MEG\" / \"sample\"\nfwd_fname = meg_path / \"sample_audvis-meg-eeg-oct-6-fwd.fif\"\nevoked_fname = meg_path / \"sample_audvis-ave.fif\"\ncov_fname = meg_path / \"sample_audvis-cov.fif\"\n\n# Read the evoked response and crop it\ncondition = \"Left visual\"\nevoked = mne.read_evokeds(evoked_fname, condition=condition, baseline=(None, 0))\nevoked.crop(tmin=-50e-3, tmax=300e-3)\n\n# Read the forward solution\nforward = mne.read_forward_solution(fwd_fname)\n\n# Read noise noise covariance matrix and regularize it\ncov = mne.read_cov(cov_fname)\ncov = mne.cov.regularize(cov, evoked.info, rank=None)\n\n# Run the Gamma-MAP method with dipole output\nalpha = 0.5\ndipoles, residual = gamma_map(\n evoked,\n forward,\n cov,\n alpha,\n xyz_same_gamma=True,\n return_residual=True,\n return_as_dipoles=True,\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot dipole activations\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plot_dipole_amplitudes(dipoles)\n\n# Plot dipole location of the strongest dipole with MRI slices\nidx = np.argmax([np.max(np.abs(dip.amplitude)) for dip in dipoles])\nplot_dipole_locations(\n dipoles[idx],\n forward[\"mri_head_t\"],\n \"sample\",\n subjects_dir=subjects_dir,\n mode=\"orthoview\",\n idx=\"amplitude\",\n)\n\n# # Plot dipole locations of all dipoles with MRI slices\n# for dip in dipoles:\n# plot_dipole_locations(dip, forward['mri_head_t'], 'sample',\n# subjects_dir=subjects_dir, mode='orthoview',\n# idx='amplitude')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Show the evoked response and the residual for gradiometers\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ylim = dict(grad=[-120, 120])\nevoked.pick(picks=\"grad\", exclude=\"bads\")\nevoked.plot(\n titles=dict(grad=\"Evoked Response Gradiometers\"),\n ylim=ylim,\n proj=True,\n time_unit=\"s\",\n)\n\nresidual.pick(picks=\"grad\", exclude=\"bads\")\nresidual.plot(\n titles=dict(grad=\"Residuals Gradiometers\"), ylim=ylim, proj=True, time_unit=\"s\"\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generate stc from dipoles\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "stc = make_stc_from_dipoles(dipoles, forward[\"src\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "View in 2D and 3D (\"glass\" brain like 3D plot)\nShow the sources as spheres scaled by their strength\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scale_factors = np.max(np.abs(stc.data), axis=1)\nscale_factors = 0.5 * (1 + scale_factors / np.max(scale_factors))\n\nplot_sparse_source_estimates(\n forward[\"src\"],\n stc,\n bgcolor=(1, 1, 1),\n modes=[\"sphere\"],\n opacity=0.1,\n scale_factors=(scale_factors, None),\n fig_name=\"Gamma-MAP\",\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## References\n.. footbibliography::\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }