{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "\n# Compute LCMV inverse solution on evoked data in volume source space\n\n\nCompute LCMV inverse solution on an auditory evoked dataset in a volume source\nspace. It stores the solution in a nifti file for visualisation, e.g. with\nFreeview.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# Author: Alexandre Gramfort \n#\n# License: BSD (3-clause)\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nimport mne\nfrom mne.datasets import sample\nfrom mne.beamformer import lcmv\n\nfrom nilearn.plotting import plot_stat_map\nfrom nilearn.image import index_img\n\nprint(__doc__)\n\ndata_path = sample.data_path()\nraw_fname = data_path + '/MEG/sample/sample_audvis_raw.fif'\nevent_fname = data_path + '/MEG/sample/sample_audvis_raw-eve.fif'\nfname_fwd = data_path + '/MEG/sample/sample_audvis-meg-vol-7-fwd.fif'" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Get epochs\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "event_id, tmin, tmax = 1, -0.2, 0.5\n\n# Setup for reading the raw data\nraw = mne.io.read_raw_fif(raw_fname, preload=True)\nraw.info['bads'] = ['MEG 2443', 'EEG 053'] # 2 bads channels\nevents = mne.read_events(event_fname)\n\n# Set up pick list: EEG + MEG - bad channels (modify to your needs)\nleft_temporal_channels = mne.read_selection('Left-temporal')\npicks = mne.pick_types(raw.info, meg=True, eeg=False, stim=True, eog=True,\n exclude='bads', selection=left_temporal_channels)\n\n# Pick the channels of interest\nraw.pick_channels([raw.ch_names[pick] for pick in picks])\n# Re-normalize our empty-room projectors, so they are fine after subselection\nraw.info.normalize_proj()\n\n# Read epochs\nproj = False # already applied\nepochs = mne.Epochs(raw, events, event_id, tmin, tmax,\n baseline=(None, 0), preload=True, proj=proj,\n reject=dict(grad=4000e-13, mag=4e-12, eog=150e-6))\nevoked = epochs.average()\n\nforward = mne.read_forward_solution(fname_fwd)\n\n# Read regularized noise covariance and compute regularized data covariance\nnoise_cov = mne.compute_covariance(epochs, tmin=tmin, tmax=0, method='shrunk')\ndata_cov = mne.compute_covariance(epochs, tmin=0.04, tmax=0.15,\n method='shrunk')\n\n# Run free orientation (vector) beamformer. Source orientation can be\n# restricted by setting pick_ori to 'max-power' (or 'normal' but only when\n# using a surface-based source space)\nstc = lcmv(evoked, forward, noise_cov, data_cov, reg=0.05, pick_ori=None)\n\n# Save result in stc files\nstc.save('lcmv-vol')\n\nstc.crop(0.0, 0.2)\n\n# Save result in a 4D nifti file\nimg = mne.save_stc_as_volume('lcmv_inverse.nii.gz', stc,\n forward['src'], mri_resolution=False)\n\nt1_fname = data_path + '/subjects/sample/mri/T1.mgz'\n\n# Plotting with nilearn ######################################################\nplot_stat_map(index_img(img, 61), t1_fname, threshold=0.8,\n title='LCMV (t=%.1f s.)' % stc.times[61])\n\n# plot source time courses with the maximum peak amplitudes\nplt.figure()\nplt.plot(stc.times, stc.data[np.argsort(np.max(stc.data, axis=1))[-40:]].T)\nplt.xlabel('Time (ms)')\nplt.ylabel('LCMV value')\nplt.show()" ], "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" } } } }