{
  "nbformat_minor": 0, 
  "nbformat": 4, 
  "cells": [
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "%matplotlib inline"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "\n# Permutation T-test on sensor data\n\n\nOne tests if the signal significantly deviates from 0\nduring a fixed time window of interest. Here computation\nis performed on MNE sample dataset between 40 and 60 ms.\n\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "# Authors: Alexandre Gramfort <alexandre.gramfort@telecom-paristech.fr>\n#\n# License: BSD (3-clause)\n\nimport numpy as np\n\nimport mne\nfrom mne import io\nfrom mne.stats import permutation_t_test\nfrom mne.datasets import sample\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'\nevent_id = 1\ntmin = -0.2\ntmax = 0.5\n\n#   Setup for reading the raw data\nraw = io.read_raw_fif(raw_fname)\nevents = mne.read_events(event_fname)\n\n#   Set up pick list: MEG + STI 014 - bad channels (modify to your needs)\ninclude = []  # or stim channel ['STI 014']\nraw.info['bads'] += ['MEG 2443', 'EEG 053']  # bads + 2 more\n\n# pick MEG Gradiometers\npicks = mne.pick_types(raw.info, meg='grad', eeg=False, stim=False, eog=True,\n                       include=include, exclude='bads')\nepochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks,\n                    baseline=(None, 0), reject=dict(grad=4000e-13, eog=150e-6))\ndata = epochs.get_data()\ntimes = epochs.times\n\ntemporal_mask = np.logical_and(0.04 <= times, times <= 0.06)\ndata = np.mean(data[:, :, temporal_mask], axis=2)\n\nn_permutations = 50000\nT0, p_values, H0 = permutation_t_test(data, n_permutations, n_jobs=1)\n\nsignificant_sensors = picks[p_values <= 0.05]\nsignificant_sensors_names = [raw.ch_names[k] for k in significant_sensors]\n\nprint(\"Number of significant sensors : %d\" % len(significant_sensors))\nprint(\"Sensors names : %s\" % significant_sensors_names)"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "View location of significantly active sensors\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "evoked = mne.EvokedArray(-np.log10(p_values)[:, np.newaxis],\n                         epochs.info, tmin=0.)\n\n# Extract mask and indices of active sensors in layout\nstats_picks = mne.pick_channels(evoked.ch_names, significant_sensors_names)\nmask = p_values[:, np.newaxis] <= 0.05\n\nevoked.plot_topomap(ch_type='grad', times=[0], scale=1,\n                    time_format=None, cmap='Reds', vmin=0., vmax=np.max,\n                    unit='-log10(p)', cbar_fmt='-%0.1f', mask=mask,\n                    size=3, show_names=lambda x: x[4:] + ' ' * 20)"
      ], 
      "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"
      }
    }
  }
}