{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "\n# Compute full spectrum source space connectivity between labels\n\n\nThe connectivity is computed between 4 labels across the spectrum\nbetween 5 and 40 Hz.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# Authors: Alexandre Gramfort \n#\n# License: BSD (3-clause)\n\nimport matplotlib.pyplot as plt\n\nimport mne\nfrom mne.datasets import sample\nfrom mne.minimum_norm import apply_inverse_epochs, read_inverse_operator\nfrom mne.connectivity import spectral_connectivity\n\nprint(__doc__)\n\ndata_path = sample.data_path()\nsubjects_dir = data_path + '/subjects'\nfname_inv = data_path + '/MEG/sample/sample_audvis-meg-oct-6-meg-inv.fif'\nfname_raw = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'\nfname_event = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'\n\n# Load data\ninverse_operator = read_inverse_operator(fname_inv)\nraw = mne.io.read_raw_fif(fname_raw)\nevents = mne.read_events(fname_event)\n\n# Add a bad channel\nraw.info['bads'] += ['MEG 2443']\n\n# Pick MEG channels\npicks = mne.pick_types(raw.info, meg=True, eeg=False, stim=False, eog=True,\n exclude='bads')\n\n# Define epochs for left-auditory condition\nevent_id, tmin, tmax = 1, -0.2, 0.5\nepochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks,\n baseline=(None, 0), reject=dict(mag=4e-12, grad=4000e-13,\n eog=150e-6))\n\n# Compute inverse solution and for each epoch. By using \"return_generator=True\"\n# stcs will be a generator object instead of a list.\nsnr = 1.0 # use lower SNR for single epochs\nlambda2 = 1.0 / snr ** 2\nmethod = \"dSPM\" # use dSPM method (could also be MNE or sLORETA)\nstcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, method,\n pick_ori=\"normal\", return_generator=True)\n\n# Read some labels\nnames = ['Aud-lh', 'Aud-rh', 'Vis-lh', 'Vis-rh']\nlabels = [mne.read_label(data_path + '/MEG/sample/labels/%s.label' % name)\n for name in names]\n\n# Average the source estimates within each label using sign-flips to reduce\n# signal cancellations, also here we return a generator\nsrc = inverse_operator['src']\nlabel_ts = mne.extract_label_time_course(stcs, labels, src, mode='mean_flip',\n return_generator=True)\n\nfmin, fmax = 5., 40.\nsfreq = raw.info['sfreq'] # the sampling frequency\n\ncon, freqs, times, n_epochs, n_tapers = spectral_connectivity(\n label_ts, method='wpli2_debiased', mode='multitaper', sfreq=sfreq,\n fmin=fmin, fmax=fmax, mt_adaptive=True, n_jobs=1)\n\nn_rows, n_cols = con.shape[:2]\nfig, axes = plt.subplots(n_rows, n_cols, sharex=True, sharey=True)\nplt.suptitle('Between labels connectivity')\nfor i in range(n_rows):\n for j in range(i + 1):\n if i == j:\n axes[i, j].set_axis_off()\n continue\n\n axes[i, j].plot(freqs, con[i, j, :])\n axes[j, i].plot(freqs, con[i, j, :])\n\n if j == 0:\n axes[i, j].set_ylabel(names[i])\n axes[0, i].set_title(names[i])\n if i == (n_rows - 1):\n axes[i, j].set_xlabel(names[j])\n axes[i, j].set_xlim([fmin, fmax])\n axes[j, i].set_xlim([fmin, fmax])\n\n # Show band limits\n for f in [8, 12, 18, 35]:\n axes[i, j].axvline(f, color='k')\n axes[j, i].axvline(f, color='k')\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" } } } }