{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "\n# Creating MNE objects from data arrays\n\n\nIn this simple example, the creation of MNE objects from\nnumpy arrays is demonstrated. In the last example case, a\nNEO file format is used as a source for the data.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# Author: Jaakko Leppakangas \n#\n# License: BSD (3-clause)\n\nimport numpy as np\nimport neo\n\nimport mne\n\nprint(__doc__)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Create arbitrary data\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "sfreq = 1000 # Sampling frequency\ntimes = np.arange(0, 10, 0.001) # Use 10000 samples (10s)\n\nsin = np.sin(times * 10) # Multiplied by 10 for shorter cycles\ncos = np.cos(times * 10)\nsinX2 = sin * 2\ncosX2 = cos * 2\n\n# Numpy array of size 4 X 10000.\ndata = np.array([sin, cos, sinX2, cosX2])\n\n# Definition of channel types and names.\nch_types = ['mag', 'mag', 'grad', 'grad']\nch_names = ['sin', 'cos', 'sinX2', 'cosX2']" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Create an :class:`info ` object.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# It is also possible to use info from another raw object.\ninfo = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Create a dummy :class:`mne.io.RawArray` object\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "raw = mne.io.RawArray(data, info)\n\n# Scaling of the figure.\n# For actual EEG/MEG data different scaling factors should be used.\nscalings = {'mag': 2, 'grad': 2}\n\nraw.plot(n_channels=4, scalings=scalings, title='Data from arrays',\n show=True, block=True)\n\n# It is also possible to auto-compute scalings\nscalings = 'auto' # Could also pass a dictionary with some value == 'auto'\nraw.plot(n_channels=4, scalings=scalings, title='Auto-scaled Data from arrays',\n show=True, block=True)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "EpochsArray\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "event_id = 1 # This is used to identify the events.\n# First column is for the sample number.\nevents = np.array([[200, 0, event_id],\n [1200, 0, event_id],\n [2000, 0, event_id]]) # List of three arbitrary events\n\n# Here a data set of 700 ms epochs from 2 channels is\n# created from sin and cos data.\n# Any data in shape (n_epochs, n_channels, n_times) can be used.\nepochs_data = np.array([[sin[:700], cos[:700]],\n [sin[1000:1700], cos[1000:1700]],\n [sin[1800:2500], cos[1800:2500]]])\n\nch_names = ['sin', 'cos']\nch_types = ['mag', 'mag']\ninfo = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types)\n\nepochs = mne.EpochsArray(epochs_data, info=info, events=events,\n event_id={'arbitrary': 1})\n\npicks = mne.pick_types(info, meg=True, eeg=False, misc=False)\n\nepochs.plot(picks=picks, scalings='auto', show=True, block=True)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "EvokedArray\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "nave = len(epochs_data) # Number of averaged epochs\nevoked_data = np.mean(epochs_data, axis=0)\n\nevokeds = mne.EvokedArray(evoked_data, info=info, tmin=-0.2,\n comment='Arbitrary', nave=nave)\nevokeds.plot(picks=picks, show=True, units={'mag': '-'},\n titles={'mag': 'sin and cos averaged'})" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Create epochs by windowing the raw data.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# The events are spaced evenly every 1 second.\nduration = 1.\n\n# create a fixed size events array\n# start=0 and stop=None by default\nevents = mne.make_fixed_length_events(raw, event_id, duration=duration)\nprint(events)\n\n# for fixed size events no start time before and after event\ntmin = 0.\ntmax = 0.99 # inclusive tmax, 1 second epochs\n\n# create :class:`Epochs ` object\nepochs = mne.Epochs(raw, events=events, event_id=event_id, tmin=tmin,\n tmax=tmax, baseline=None, verbose=True)\nepochs.plot(scalings='auto', block=True)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Create overlapping epochs using :func:`mne.make_fixed_length_events` (50 %\noverlap). This also roughly doubles the amount of events compared to the\nprevious event list.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "duration = 0.5\nevents = mne.make_fixed_length_events(raw, event_id, duration=duration)\nprint(events)\nepochs = mne.Epochs(raw, events=events, tmin=tmin, tmax=tmax, baseline=None,\n verbose=True)\nepochs.plot(scalings='auto', block=True)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Extracting data from NEO file\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# The example here uses the ExampleIO object for creating fake data.\n# For actual data and different file formats, consult the NEO documentation.\nreader = neo.io.ExampleIO('fakedata.nof')\nbl = reader.read(cascade=True, lazy=False)[0]\n\n# Get data from first (and only) segment\nseg = bl.segments[0]\ntitle = seg.file_origin\n\nch_names = list()\ndata = list()\nfor ai, asig in enumerate(seg.analogsignals):\n # Since the data does not contain channel names, channel indices are used.\n ch_names.append('Neo %02d' % (ai + 1,))\n # We need the ravel() here because Neo < 0.5 gave 1D, Neo 0.5 gives\n # 2D (but still a single channel).\n data.append(asig.rescale('V').magnitude.ravel())\n\ndata = np.array(data, float)\n\nsfreq = int(seg.analogsignals[0].sampling_rate.magnitude)\n\n# By default, the channel types are assumed to be 'misc'.\ninfo = mne.create_info(ch_names=ch_names, sfreq=sfreq)\n\nraw = mne.io.RawArray(data, info)\nraw.plot(n_channels=4, scalings={'misc': 1}, title='Data from NEO',\n show=True, block=True, clipping='clamp')" ], "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" } } } }