{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "\n# Compute MxNE with time-frequency sparse prior\n\n\nThe TF-MxNE solver is a distributed inverse method (like dSPM or sLORETA)\nthat promotes focal (sparse) sources (such as dipole fitting techniques).\nThe benefit of this approach is that:\n\n - it is spatio-temporal without assuming stationarity (sources properties\n can vary over time)\n - activations are localized in space, time and frequency in one step.\n - with a built-in filtering process based on a short time Fourier\n transform (STFT), data does not need to be low passed (just high pass\n to make the signals zero mean).\n - the solver solves a convex optimization problem, hence cannot be\n trapped in local minima.\n\nReferences:\n\nA. Gramfort, D. Strohmeier, J. Haueisen, M. Hamalainen, M. Kowalski\nTime-Frequency Mixed-Norm Estimates: Sparse M/EEG imaging with\nnon-stationary source activations\nNeuroimage, Volume 70, 15 April 2013, Pages 410-422, ISSN 1053-8119,\nDOI: 10.1016/j.neuroimage.2012.12.051.\n\nA. Gramfort, D. Strohmeier, J. Haueisen, M. Hamalainen, M. Kowalski\nFunctional Brain Imaging with M/EEG Using Structured Sparsity in\nTime-Frequency Dictionaries\nProceedings Information Processing in Medical Imaging\nLecture Notes in Computer Science, 2011, Volume 6801/2011,\n600-611, DOI: 10.1007/978-3-642-22092-0_49\nhttps://doi.org/10.1007/978-3-642-22092-0_49\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.minimum_norm import make_inverse_operator, apply_inverse\nfrom mne.inverse_sparse import tf_mixed_norm\nfrom mne.viz import plot_sparse_source_estimates\n\nprint(__doc__)\n\ndata_path = sample.data_path()\nsubjects_dir = data_path + '/subjects'\nfwd_fname = data_path + '/MEG/sample/sample_audvis-meg-eeg-oct-6-fwd.fif'\nave_fname = data_path + '/MEG/sample/sample_audvis-no-filter-ave.fif'\ncov_fname = data_path + '/MEG/sample/sample_audvis-shrunk-cov.fif'\n\n# Read noise covariance matrix\ncov = mne.read_cov(cov_fname)\n\n# Handling average file\ncondition = 'Left visual'\nevoked = mne.read_evokeds(ave_fname, condition=condition, baseline=(None, 0))\nevoked = mne.pick_channels_evoked(evoked)\n# We make the window slightly larger than what you'll eventually be interested\n# in ([-0.05, 0.3]) to avoid edge effects.\nevoked.crop(tmin=-0.1, tmax=0.4)\n\n# Handling forward solution\nforward = mne.read_forward_solution(fwd_fname, force_fixed=False,\n surf_ori=True)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Run solver\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# alpha_space regularization parameter is between 0 and 100 (100 is high)\nalpha_space = 50. # spatial regularization parameter\n# alpha_time parameter promotes temporal smoothness\n# (0 means no temporal regularization)\nalpha_time = 1. # temporal regularization parameter\n\nloose, depth = 0.2, 0.9 # loose orientation & depth weighting\n\n# Compute dSPM solution to be used as weights in MxNE\ninverse_operator = make_inverse_operator(evoked.info, forward, cov,\n loose=loose, depth=depth)\nstc_dspm = apply_inverse(evoked, inverse_operator, lambda2=1. / 9.,\n method='dSPM')\n\n# Compute TF-MxNE inverse solution\nstc, residual = tf_mixed_norm(evoked, forward, cov, alpha_space, alpha_time,\n loose=loose, depth=depth, maxit=200, tol=1e-4,\n weights=stc_dspm, weights_min=8., debias=True,\n wsize=16, tstep=4, window=0.05,\n return_residual=True)\n\n# Crop to remove edges\nstc.crop(tmin=-0.05, tmax=0.3)\nevoked.crop(tmin=-0.05, tmax=0.3)\nresidual.crop(tmin=-0.05, tmax=0.3)\n\n# Show the evoked response and the residual for gradiometers\nylim = dict(grad=[-120, 120])\nevoked.pick_types(meg='grad', exclude='bads')\nevoked.plot(titles=dict(grad='Evoked Response: Gradiometers'), ylim=ylim,\n proj=True)\n\nresidual.pick_types(meg='grad', exclude='bads')\nresidual.plot(titles=dict(grad='Residuals: Gradiometers'), ylim=ylim,\n 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 opacity=0.1, fig_name=\"TF-MxNE (cond %s)\"\n % condition, modes=['sphere'], scale_factors=[1.])\n\ntime_label = 'TF-MxNE time=%0.2f ms'\nclim = dict(kind='value', lims=[10e-9, 15e-9, 20e-9])\nbrain = stc.plot('sample', 'inflated', 'rh', views='medial',\n clim=clim, time_label=time_label, smoothing_steps=5,\n subjects_dir=subjects_dir, initial_time=150, time_unit='ms')\nbrain.add_label(\"V1\", color=\"yellow\", scalar_thresh=.5, borders=True)\nbrain.add_label(\"V2\", color=\"red\", scalar_thresh=.5, borders=True)" ], "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" } } } }