{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Plot single trial activity, grouped by ROI and sorted by RT\n\nThis will produce what is sometimes called an event related\npotential / field (ERP/ERF) image.\n\nThe EEGLAB example file, which contains an experiment with button press\nresponses to simple visual stimuli, is read in and response times are calculated.\nRegions of Interest are determined by the channel types (in 10/20 channel\nnotation, even channels are right, odd are left, and 'z' are central). The\nmedian and the Global Field Power within each channel group is calculated,\nand the trials are plotted, sorting by response time.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: Jona Sassenhagen \n#\n# License: BSD-3-Clause\n# Copyright the MNE-Python contributors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import mne\nfrom mne.channels import make_1020_channel_selections\nfrom mne.event import define_target_events\n\nprint(__doc__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load EEGLAB example data (a small EEG dataset)\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data_path = mne.datasets.testing.data_path()\nfname = data_path / \"EEGLAB\" / \"test_raw.set\"\n\nevent_id = {\"rt\": 1, \"square\": 2} # must be specified for str events\nraw = mne.io.read_raw_eeglab(fname)\n\nmapping = {\n \"EEG 000\": \"Fpz\",\n \"EEG 001\": \"EOG1\",\n \"EEG 002\": \"F3\",\n \"EEG 003\": \"Fz\",\n \"EEG 004\": \"F4\",\n \"EEG 005\": \"EOG2\",\n \"EEG 006\": \"FC5\",\n \"EEG 007\": \"FC1\",\n \"EEG 008\": \"FC2\",\n \"EEG 009\": \"FC6\",\n \"EEG 010\": \"T7\",\n \"EEG 011\": \"C3\",\n \"EEG 012\": \"C4\",\n \"EEG 013\": \"Cz\",\n \"EEG 014\": \"T8\",\n \"EEG 015\": \"CP5\",\n \"EEG 016\": \"CP1\",\n \"EEG 017\": \"CP2\",\n \"EEG 018\": \"CP6\",\n \"EEG 019\": \"P7\",\n \"EEG 020\": \"P3\",\n \"EEG 021\": \"Pz\",\n \"EEG 022\": \"P4\",\n \"EEG 023\": \"P8\",\n \"EEG 024\": \"PO7\",\n \"EEG 025\": \"PO3\",\n \"EEG 026\": \"POz\",\n \"EEG 027\": \"PO4\",\n \"EEG 028\": \"PO8\",\n \"EEG 029\": \"O1\",\n \"EEG 030\": \"Oz\",\n \"EEG 031\": \"O2\",\n}\nraw.rename_channels(mapping)\nraw.set_channel_types({\"EOG1\": \"eog\", \"EOG2\": \"eog\"})\nraw.set_montage(\"standard_1020\")\n\nevents = mne.events_from_annotations(raw, event_id)[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create Epochs\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# define target events:\n# 1. find response times: distance between \"square\" and \"rt\" events\n# 2. extract A. \"square\" events B. followed by a button press within 700 msec\ntmax = 0.7\nsfreq = raw.info[\"sfreq\"]\nreference_id, target_id = 2, 1\nnew_events, rts = define_target_events(\n events, reference_id, target_id, sfreq, tmin=0.0, tmax=tmax, new_id=2\n)\n\nepochs = mne.Epochs(raw, events=new_events, tmax=tmax + 0.1, event_id={\"square\": 2})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot using :term:`global field power`\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Parameters for plotting\norder = rts.argsort() # sorting from fast to slow trials\n\nselections = make_1020_channel_selections(epochs.info, midline=\"12z\")\n\n# The actual plots (GFP)\nepochs.plot_image(\n group_by=selections,\n order=order,\n sigma=1.5,\n overlay_times=rts / 1000.0,\n combine=\"gfp\",\n ts_args=dict(vlines=[0, rts.mean() / 1000.0]),\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot using median\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "epochs.plot_image(\n group_by=selections,\n order=order,\n sigma=1.5,\n overlay_times=rts / 1000.0,\n combine=\"median\",\n ts_args=dict(vlines=[0, rts.mean() / 1000.0]),\n)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }