{
  "nbformat_minor": 0, 
  "nbformat": 4, 
  "cells": [
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "%matplotlib inline"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "\n\nCreating MNE-Python's data structures from scratch\n==================================================\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "from __future__ import print_function\n\nimport mne\nimport numpy as np"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "------------------------------------------------------\nCreating :class:`Info <mne.Info>` objects\n------------------------------------------------------\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>for full documentation on the `Info` object, see\n          `tut_info_objects`. See also\n          `sphx_glr_auto_examples_io_plot_objects_from_arrays.py`.</p></div>\n\nNormally, :class:`mne.Info` objects are created by the various\n`data import functions <ch_convert>`.\nHowever, if you wish to create one from scratch, you can use the\n:func:`mne.create_info` function to initialize the minimally required\nfields. Further fields can be assigned later as one would with a regular\ndictionary.\n\nThe following creates the absolute minimum info structure:\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "# Create some dummy metadata\nn_channels = 32\nsampling_rate = 200\ninfo = mne.create_info(n_channels, sampling_rate)\nprint(info)"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "You can also supply more extensive metadata:\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "# Names for each channel\nchannel_names = ['MEG1', 'MEG2', 'Cz', 'Pz', 'EOG']\n\n# The type (mag, grad, eeg, eog, misc, ...) of each channel\nchannel_types = ['grad', 'grad', 'eeg', 'eeg', 'eog']\n\n# The sampling rate of the recording\nsfreq = 1000  # in Hertz\n\n# The EEG channels use the standard naming strategy.\n# By supplying the 'montage' parameter, approximate locations\n# will be added for them\nmontage = 'standard_1005'\n\n# Initialize required fields\ninfo = mne.create_info(channel_names, sfreq, channel_types, montage)\n\n# Add some more information\ninfo['description'] = 'My custom dataset'\ninfo['bads'] = ['Pz']  # Names of bad channels\n\nprint(info)"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "<div class=\"alert alert-info\"><h4>Note</h4><p>When assigning new values to the fields of an\n          :class:`mne.Info` object, it is important that the\n          fields are consistent:\n\n          - The length of the channel information field `chs` must be\n            `nchan`.\n          - The length of the `ch_names` field must be `nchan`.\n          - The `ch_names` field should be consistent with the `name` field\n            of the channel information contained in `chs`.</p></div>\n\n---------------------------------------------\nCreating :class:`Raw <mne.io.Raw>` objects\n---------------------------------------------\n\nTo create a :class:`mne.io.Raw` object from scratch, you can use the\n:class:`mne.io.RawArray` class, which implements raw data that is backed by a\nnumpy array. The correct units for the data are:\n\n- V: eeg, eog, seeg, emg, ecg, bio, ecog\n- T: mag\n- T/m: grad\n- M: hbo, hbr\n- Am: dipole\n- AU: misc\n\nThe :class:`mne.io.RawArray` constructor simply takes the data matrix and\n:class:`mne.Info` object:\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "# Generate some random data\ndata = np.random.randn(5, 1000)\n\n# Initialize an info structure\ninfo = mne.create_info(\n    ch_names=['MEG1', 'MEG2', 'EEG1', 'EEG2', 'EOG'],\n    ch_types=['grad', 'grad', 'eeg', 'eeg', 'eog'],\n    sfreq=100\n)\n\ncustom_raw = mne.io.RawArray(data, info)\nprint(custom_raw)"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "---------------------------------------------\nCreating :class:`Epochs <mne.Epochs>` objects\n---------------------------------------------\n\nTo create an :class:`mne.Epochs` object from scratch, you can use the\n:class:`mne.EpochsArray` class, which uses a numpy array directly without\nwrapping a raw object. The array must be of `shape(n_epochs, n_chans,\nn_times)`. The proper units of measure are listed above.\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "# Generate some random data: 10 epochs, 5 channels, 2 seconds per epoch\nsfreq = 100\ndata = np.random.randn(10, 5, sfreq * 2)\n\n# Initialize an info structure\ninfo = mne.create_info(\n    ch_names=['MEG1', 'MEG2', 'EEG1', 'EEG2', 'EOG'],\n    ch_types=['grad', 'grad', 'eeg', 'eeg', 'eog'],\n    sfreq=sfreq\n)"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "It is necessary to supply an \"events\" array in order to create an Epochs\nobject. This is of `shape(n_events, 3)` where the first column is the sample\nnumber (time) of the event, the second column indicates the value from which\nthe transition is made from (only used when the new value is bigger than the\nold one), and the third column is the new event value.\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "# Create an event matrix: 10 events with alternating event codes\nevents = np.array([\n    [0, 0, 1],\n    [1, 0, 2],\n    [2, 0, 1],\n    [3, 0, 2],\n    [4, 0, 1],\n    [5, 0, 2],\n    [6, 0, 1],\n    [7, 0, 2],\n    [8, 0, 1],\n    [9, 0, 2],\n])"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "More information about the event codes: subject was either smiling or\nfrowning\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "event_id = dict(smiling=1, frowning=2)"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "Finally, we must specify the beginning of an epoch (the end will be inferred\nfrom the sampling frequency and n_samples)\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "# Trials were cut from -0.1 to 1.0 seconds\ntmin = -0.1"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "Now we can create the :class:`mne.EpochsArray` object\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "custom_epochs = mne.EpochsArray(data, info, events, tmin, event_id)\n\nprint(custom_epochs)\n\n# We can treat the epochs object as we would any other\n_ = custom_epochs['smiling'].average().plot()"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "---------------------------------------------\nCreating :class:`Evoked <mne.Evoked>` Objects\n---------------------------------------------\nIf you already have data that is collapsed across trials, you may also\ndirectly create an evoked array.  Its constructor accepts an array of\n`shape(n_chans, n_times)` in addition to some bookkeeping parameters.\nThe proper units of measure for the data are listed above.\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "# The averaged data\ndata_evoked = data.mean(0)\n\n# The number of epochs that were averaged\nnave = data.shape[0]\n\n# A comment to describe to evoked (usually the condition name)\ncomment = \"Smiley faces\"\n\n# Create the Evoked object\nevoked_array = mne.EvokedArray(data_evoked, info, tmin,\n                               comment=comment, nave=nave)\nprint(evoked_array)\n_ = evoked_array.plot()"
      ], 
      "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"
      }
    }
  }
}