{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Define target events based on time lag, plot evoked response\n\nThis script shows how to define higher order events based on\ntime lag between reference and target events. For\nillustration, we will put face stimuli presented into two\nclasses, that is 1) followed by an early button press\n(within 590 milliseconds) and followed by a late button\npress (later than 590 milliseconds). Finally, we will\nvisualize the evoked responses to both 'quickly-processed'\nand 'slowly-processed' face stimuli.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: Denis Engemann \n#\n# License: BSD-3-Clause\n# Copyright the MNE-Python contributors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nimport mne\nfrom mne import io\nfrom mne.datasets import sample\nfrom mne.event import define_target_events\n\nprint(__doc__)\n\ndata_path = sample.data_path()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set parameters\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "meg_path = data_path / \"MEG\" / \"sample\"\nraw_fname = meg_path / \"sample_audvis_filt-0-40_raw.fif\"\nevent_fname = meg_path / \"sample_audvis_filt-0-40_raw-eve.fif\"\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: EEG + STI 014 - bad channels (modify to your needs)\ninclude = [] # or stim channels ['STI 014']\nraw.info[\"bads\"] += [\"EEG 053\"] # bads\n\n# pick MEG channels\npicks = mne.pick_types(\n raw.info,\n meg=\"mag\",\n eeg=False,\n stim=False,\n eog=True,\n include=include,\n exclude=\"bads\",\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find stimulus event followed by quick button presses\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reference_id = 5 # presentation of a smiley face\ntarget_id = 32 # button press\nsfreq = raw.info[\"sfreq\"] # sampling rate\ntmin = 0.1 # trials leading to very early responses will be rejected\ntmax = 0.59 # ignore face stimuli followed by button press later than 590 ms\nnew_id = 42 # the new event id for a hit. If None, reference_id is used.\nfill_na = 99 # the fill value for misses\n\nevents_, lag = define_target_events(\n events, reference_id, target_id, sfreq, tmin, tmax, new_id, fill_na\n)\n\nprint(events_) # The 99 indicates missing or too late button presses\n\n# besides the events also the lag between target and reference is returned\n# this could e.g. be used as parametric regressor in subsequent analyses.\n\nprint(lag[lag != fill_na]) # lag in milliseconds\n\n# #############################################################################\n# Construct epochs\n\ntmin_ = -0.2\ntmax_ = 0.4\nevent_id = dict(early=new_id, late=fill_na)\n\nepochs = mne.Epochs(\n raw,\n events_,\n event_id,\n tmin_,\n tmax_,\n picks=picks,\n baseline=(None, 0),\n reject=dict(mag=4e-12),\n)\n\n# average epochs and get an Evoked dataset.\n\nearly, late = (epochs[k].average() for k in event_id)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "View evoked response\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "times = 1e3 * epochs.times # time in milliseconds\ntitle = \"Evoked response followed by {} button press\"\n\nfig, axes = plt.subplots(2, 1)\nearly.plot(axes=axes[0], time_unit=\"s\")\naxes[0].set(title=title.format(\"late\"), ylabel=\"Evoked field (fT)\")\nlate.plot(axes=axes[1], time_unit=\"s\")\naxes[1].set(title=title.format(\"early\"), ylabel=\"Evoked field (fT)\")\nplt.show()" ] } ], "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 }