{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Generate a functional label from source estimates\n\nThreshold source estimates and produce a functional label. The label\nis typically the region of interest that contains high values.\nHere we compare the average time course in the anatomical label obtained\nby FreeSurfer segmentation and the average time course from the\nfunctional label. As expected the time course in the functional\nlabel yields higher values.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Author: Luke Bloy \n# Alex Gramfort \n# License: BSD-3-Clause\n# Copyright the MNE-Python contributors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport mne\nfrom mne.datasets import sample\nfrom mne.minimum_norm import apply_inverse, read_inverse_operator\n\nprint(__doc__)\n\ndata_path = sample.data_path()\nfname_inv = data_path / \"MEG\" / \"sample\" / \"sample_audvis-meg-oct-6-meg-inv.fif\"\nfname_evoked = data_path / \"MEG\" / \"sample\" / \"sample_audvis-ave.fif\"\nsubjects_dir = data_path / \"subjects\"\nsubject = \"sample\"\n\nsnr = 3.0\nlambda2 = 1.0 / snr**2\nmethod = \"dSPM\" # use dSPM method (could also be MNE or sLORETA)\n\n# Compute a label/ROI based on the peak power between 80 and 120 ms.\n# The label bankssts-lh is used for the comparison.\naparc_label_name = \"bankssts-lh\"\ntmin, tmax = 0.080, 0.120\n\n# Load data\nevoked = mne.read_evokeds(fname_evoked, condition=0, baseline=(None, 0))\ninverse_operator = read_inverse_operator(fname_inv)\nsrc = inverse_operator[\"src\"] # get the source space\n\n# Compute inverse solution\nstc = apply_inverse(evoked, inverse_operator, lambda2, method, pick_ori=\"normal\")\n\n# Make an STC in the time interval of interest and take the mean\nstc_mean = stc.copy().crop(tmin, tmax).mean()\n\n# use the stc_mean to generate a functional label\n# region growing is halted at 60% of the peak value within the\n# anatomical label / ROI specified by aparc_label_name\nlabel = mne.read_labels_from_annot(\n subject, parc=\"aparc\", subjects_dir=subjects_dir, regexp=aparc_label_name\n)[0]\nstc_mean_label = stc_mean.in_label(label)\ndata = np.abs(stc_mean_label.data)\nstc_mean_label.data[data < 0.6 * np.max(data)] = 0.0\n\n# 8.5% of original source space vertices were omitted during forward\n# calculation, suppress the warning here with verbose='error'\nfunc_labels, _ = mne.stc_to_label(\n stc_mean_label,\n src=src,\n smooth=True,\n subjects_dir=subjects_dir,\n connected=True,\n verbose=\"error\",\n)\n\n# take first as func_labels are ordered based on maximum values in stc\nfunc_label = func_labels[0]\n\n# load the anatomical ROI for comparison\nanat_label = mne.read_labels_from_annot(\n subject, parc=\"aparc\", subjects_dir=subjects_dir, regexp=aparc_label_name\n)[0]\n\n# extract the anatomical time course for each label\nstc_anat_label = stc.in_label(anat_label)\npca_anat = stc.extract_label_time_course(anat_label, src, mode=\"pca_flip\")[0]\n\nstc_func_label = stc.in_label(func_label)\npca_func = stc.extract_label_time_course(func_label, src, mode=\"pca_flip\")[0]\n\n# flip the pca so that the max power between tmin and tmax is positive\npca_anat *= np.sign(pca_anat[np.argmax(np.abs(pca_anat))])\npca_func *= np.sign(pca_func[np.argmax(np.abs(pca_anat))])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "plot the time courses....\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.figure()\nplt.plot(\n 1e3 * stc_anat_label.times, pca_anat, \"k\", label=f\"Anatomical {aparc_label_name}\"\n)\nplt.plot(\n 1e3 * stc_func_label.times, pca_func, \"b\", label=f\"Functional {aparc_label_name}\"\n)\nplt.legend()\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "plot brain in 3D with mne.viz.Brain if available\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "brain = stc_mean.plot(hemi=\"lh\", subjects_dir=subjects_dir)\nbrain.show_view(\"lateral\")\n\n# show both labels\nbrain.add_label(anat_label, borders=True, color=\"k\")\nbrain.add_label(func_label, borders=True, color=\"b\")" ] } ], "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 }