{
  "nbformat_minor": 0, 
  "nbformat": 4, 
  "cells": [
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "%matplotlib inline"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "\n# Compute seed based time-frequency connectivity in sensor space\n\n\nComputes the connectivity between a seed-gradiometer close to the visual cortex\nand all other gradiometers. The connectivity is computed in the time-frequency\ndomain using Morlet wavelets and the debiased Squared Weighted Phase Lag Index\n[1]_ is used as connectivity metric.\n\n.. [1] Vinck et al. \"An improved index of phase-synchronization for electro-\n       physiological data in the presence of volume-conduction, noise and\n       sample-size bias\" NeuroImage, vol. 55, no. 4, pp. 1548-1565, Apr. 2011.\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "# Author: Martin Luessi <mluessi@nmr.mgh.harvard.edu>\n#\n# License: BSD (3-clause)\n\nimport numpy as np\n\nimport mne\nfrom mne import io\nfrom mne.connectivity import spectral_connectivity, seed_target_indices\nfrom mne.datasets import sample\nfrom mne.time_frequency import AverageTFR\n\nprint(__doc__)"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "Set parameters\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "data_path = sample.data_path()\nraw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'\nevent_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'\n\n# Setup for reading the raw data\nraw = io.read_raw_fif(raw_fname)\nevents = mne.read_events(event_fname)\n\n# Add a bad channel\nraw.info['bads'] += ['MEG 2443']\n\n# Pick MEG gradiometers\npicks = mne.pick_types(raw.info, meg='grad', eeg=False, stim=False, eog=True,\n                       exclude='bads')\n\n# Create epochs for left-visual condition\nevent_id, tmin, tmax = 3, -0.2, 0.5\nepochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks,\n                    baseline=(None, 0), reject=dict(grad=4000e-13, eog=150e-6),\n                    preload=True)\n\n# Use 'MEG 2343' as seed\nseed_ch = 'MEG 2343'\npicks_ch_names = [raw.ch_names[i] for i in picks]\n\n# Create seed-target indices for connectivity computation\nseed = picks_ch_names.index(seed_ch)\ntargets = np.arange(len(picks))\nindices = seed_target_indices(seed, targets)\n\n# Define wavelet frequencies and number of cycles\ncwt_frequencies = np.arange(7, 30, 2)\ncwt_n_cycles = cwt_frequencies / 7.\n\n# Run the connectivity analysis using 2 parallel jobs\nsfreq = raw.info['sfreq']  # the sampling frequency\ncon, freqs, times, _, _ = spectral_connectivity(\n    epochs, indices=indices,\n    method='wpli2_debiased', mode='cwt_morlet', sfreq=sfreq,\n    cwt_frequencies=cwt_frequencies, cwt_n_cycles=cwt_n_cycles, n_jobs=1)\n\n# Mark the seed channel with a value of 1.0, so we can see it in the plot\ncon[np.where(indices[1] == seed)] = 1.0\n\n# Show topography of connectivity from seed\ntitle = 'WPLI2 - Visual - Seed %s' % seed_ch\n\nlayout = mne.find_layout(epochs.info, 'meg')  # use full layout\n\ntfr = AverageTFR(epochs.info, con, times, freqs, len(epochs))\ntfr.plot_topo(fig_facecolor='w', font_color='k', border='k')"
      ], 
      "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"
      }
    }
  }
}