{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Compute MNE-dSPM inverse solution on evoked data in volume source space\n\nCompute dSPM inverse solution on MNE evoked dataset in a volume source\nspace and stores the solution in a nifti file for visualisation.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Author: Alexandre Gramfort \n#\n# License: BSD-3-Clause\n# Copyright the MNE-Python contributors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from nilearn.image import index_img\nfrom nilearn.plotting import plot_stat_map\n\nfrom mne import read_evokeds\nfrom mne.datasets import sample\nfrom mne.minimum_norm import apply_inverse, read_inverse_operator\n\nprint(__doc__)\n\ndata_path = sample.data_path()\nmeg_path = data_path / \"MEG\" / \"sample\"\nfname_inv = meg_path / \"sample_audvis-meg-vol-7-meg-inv.fif\"\nfname_evoked = meg_path / \"sample_audvis-ave.fif\"\n\nsnr = 3.0\nlambda2 = 1.0 / snr**2\nmethod = \"dSPM\" # use dSPM method (could also be MNE or sLORETA)\n\n# Load data\nevoked = read_evokeds(fname_evoked, condition=0, baseline=(None, 0))\ninverse_operator = read_inverse_operator(fname_inv)\nsrc = inverse_operator[\"src\"]\n\n# Compute inverse solution\nstc = apply_inverse(evoked, inverse_operator, lambda2, method)\nstc.crop(0.0, 0.2)\n\n# Export result as a 4D nifti object\nimg = stc.as_volume(src, mri_resolution=False) # set True for full MRI resolution\n\n# Save it as a nifti file\n# nib.save(img, f\"mne_{method}_inverse.nii.gz\")\n\nt1_fname = data_path / \"subjects\" / \"sample\" / \"mri\" / \"T1.mgz\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot with nilearn:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plot_stat_map(\n index_img(img, 61),\n str(t1_fname),\n threshold=8.0,\n title=f\"{method} (t={stc.times[61]:.1f} s.)\",\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 }