{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "\n\nEpoching and averaging (ERP/ERF)\n================================\n\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "import os.path as op\nimport numpy as np\n\nimport mne" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "In MNE, `epochs` refers to a collection of `single trials` or short segments\nof time locked raw data. If you haven't already, you might want to check out\n`tut_epochs_objects`. In this tutorial we take a deeper look into\nconstruction of epochs and averaging the epoch data to evoked instances.\nFirst let's read in the raw sample data.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "data_path = mne.datasets.sample.data_path()\nfname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_raw.fif')\nraw = mne.io.read_raw_fif(fname)\nraw.set_eeg_reference() # set EEG average reference" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "To create time locked epochs, we first need a set of events that contain the\ninformation about the times. In this tutorial we use the stimulus channel to\ndefine the events. Let's look at the raw data.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "order = np.arange(raw.info['nchan'])\norder[9] = 312 # We exchange the plotting order of two channels\norder[312] = 9 # to show the trigger channel as the 10th channel.\nraw.plot(n_channels=10, order=order, block=True)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Notice channel ``STI 014`` at the bottom. It is the trigger channel that\nwas used for combining all the events to a single channel. We can see that it\nhas several pulses of different amplitude throughout the recording. These\npulses correspond to different stimuli presented to the subject during the\nacquisition. The pulses have values of 1, 2, 3, 4, 5 and 32. These are the\nevents we are going to align the epochs to. To create an event list from raw\ndata, we simply call a function dedicated just for that. Since the event list\nis simply a numpy array, you can also manually create one. If you create one\nfrom an outside source (like a separate file of events), pay special\nattention in aligning the events correctly with the raw data.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "events = mne.find_events(raw)\nprint('Found %s events, first five:' % len(events))\nprint(events[:5])\n\n# Plot the events to get an idea of the paradigm\n# Specify colors and an event_id dictionary for the legend.\nevent_id = {'Auditory/Left': 1, 'Auditory/Right': 2,\n 'Visual/Left': 3, 'Visual/Right': 4,\n 'smiley': 5, 'button': 32}\ncolor = {1: 'green', 2: 'yellow', 3: 'red', 4: 'c', 5: 'black', 32: 'blue'}\n\nmne.viz.plot_events(events, raw.info['sfreq'], raw.first_samp, color=color,\n event_id=event_id)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "The event list contains three columns. The first column corresponds to\nsample number. To convert this to seconds, you should divide the sample\nnumber by the used sampling frequency. The second column is reserved for the\nold value of the trigger channel at the time of transition, but is currently\nnot in use. The third column is the trigger id (amplitude of the pulse).\n\nYou might wonder why the samples don't seem to align with the plotted data.\nFor instance, the first event has a sample number of 27977 which should\ntranslate to roughly 46.6 seconds (27977 / 600). However looking at\nthe pulses we see the first pulse at 3.6 seconds. This is because Neuromag\nrecordings have an attribute ``first_samp`` which refers to the offset\nbetween the system start and the start of the recording. Our data has a\n``first_samp`` equal to 25800. This means that the first sample you see with\n``raw.plot`` is the sample number 25800. Generally you don't need to worry\nabout this offset as it is taken into account with MNE functions, but it is\ngood to be aware of. Just to confirm, let's plot the events together with the\nraw data. Notice how the vertical lines (events) align nicely with the pulses\non `STI 014`.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "raw.plot(events=events, n_channels=10, order=order)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "In this tutorial we are only interested in triggers 1, 2, 3 and 4. These\ntriggers correspond to auditory and visual stimuli. The ``event_id`` here\ncan be an int, a list of ints or a dict. With dicts it is possible to assign\nthese ids to distinct categories. When using ints or lists this information\nis lost. First we shall define some parameters to feed to the\n:class:`mne.Epochs` constructor. The values ``tmin`` and ``tmax`` refer to\noffsets in relation to the events. Here we make epochs that collect the data\nfrom 200 ms before to 500 ms after the event.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "tmin, tmax = -0.2, 0.5\nevent_id = {'Auditory/Left': 1, 'Auditory/Right': 2,\n 'Visual/Left': 3, 'Visual/Right': 4}\n# Only pick MEG and EOG channels.\npicks = mne.pick_types(raw.info, meg=True, eeg=False, eog=True)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Now we have everything we need to construct the epochs. To get some\nmeaningful results, we also want to baseline the epochs. Baselining computes\nthe mean over the baseline period and adjusts the data accordingly. The\nepochs constructor uses a baseline period from ``tmin`` to 0.0 seconds by\ndefault, but it is wise to be explicit. That way you are less likely to end\nup with surprises along the way. ``None`` as the first element of the tuple\nrefers to the start of the time window (-200 ms in this case).\nSee :class:`mne.Epochs` for more.\n\nWe also define rejection thresholds to get rid of noisy epochs. The\nrejection thresholds are defined as peak-to-peak values within the epoch time\nwindow. They are defined as T/m for gradiometers, T for magnetometers and V\nfor EEG and EOG electrodes.\n\n
In this tutorial, we don't preprocess the data. This is not\n something you would normally do. See our `tutorials` on\n preprocessing for more.