3. Epoching#

We are typically not interested in the full continuous EEG recording, but only in the EEG activity that happens around certain events of interest. Such events could be the onset of a stimulus or the onset of a response.

The events are usually stored in the EEG data as “triggers” (also called “markers” or “annotations”). These are numerical codes at certain timepoints that indicate the onset of an event. We can use them to “cut” the continuous EEG recording into smaller segments (called epochs) that begin a few hundred milliseconds before the event (to have a neutral “baseline” period) and end a few hundred milliseconds after the event (to capture brain activity related to the event).

Continuous, epoched, and averaged EEG

Fig. 3.1 Continuous, epoched, and averaged EEG. Source: Luck (2022)#

Goals

  • Extracting event information from the data

  • Segmenting the data into epochs

  • Applying baseline correction

  • Rejecting high-amplitude epochs

  • Visualizing the epoched data

3.1. Load Python modules#

As before, we’ll make extensive use of the MNE-Python package(Gramfort et al, 2013). We’ll also use a new package called pandas (more on that below).

# %pip install mne hu-neuro-pipeline pandas
import pandas as pd
from mne import (Epochs, events_from_annotations, merge_events,
                 set_bipolar_reference)
from mne.io import read_raw
from mne.preprocessing import ICA
from pipeline.datasets import get_erpcore

3.2. Recreate preprocessing#

We repeat the preprocessing steps from the previous chapter in condensed form (without any intermediate plots).

Hide code cell source
# Download data
files_dict = get_erpcore('N170', participants='sub-004', path='data')
raw_file = files_dict['raw_files'][0]
log_file = files_dict['log_files'][0]

# Preprocessing
raw = read_raw(raw_file, preload=True)
raw = set_bipolar_reference(raw, anode='FP1', cathode='VEOG_lower',
                            ch_name='VEOG', drop_refs=False)
raw = set_bipolar_reference(raw, anode='HEOG_right', cathode='HEOG_left',
                            ch_name='HEOG', drop_refs=False)
raw = raw.set_channel_types({'VEOG': 'eog', 'HEOG': 'eog'})
raw = raw.drop_channels(['VEOG_lower', 'HEOG_right', 'HEOG_left'])
raw = raw.set_montage('biosemi64', match_case=False)
raw = raw.filter(l_freq=0.1, h_freq=30.0)
raw_copy = raw.copy().filter(l_freq=1.0, h_freq=None, verbose=False)
ica = ICA(n_components=15)
ica = ica.fit(raw_copy)
eog_indices, eog_scores = ica.find_bads_eog(raw, ch_name=['VEOG', 'HEOG'],
                                            verbose=False)
ica.exclude = eog_indices
raw = ica.apply(raw)
raw = raw.set_eeg_reference('average')
Hide code cell output
Reading /home/runner/work/intro-to-eeg/intro-to-eeg/ipynb/data/erpcore/N170/sub-004/eeg/sub-004_task-N170_eeg.fdt
Reading 0 ... 649215  =      0.000 ...   633.999 secs...
EEG channel type selected for re-referencing
Creating RawArray with float64 data, n_channels=1, n_times=649216
    Range : 0 ... 649215 =      0.000 ...   633.999 secs
Ready.
Added the following bipolar channels:
VEOG
EEG channel type selected for re-referencing
Creating RawArray with float64 data, n_channels=1, n_times=649216
    Range : 0 ... 649215 =      0.000 ...   633.999 secs
Ready.
Added the following bipolar channels:
HEOG
Filtering raw data in 1 contiguous segment
Setting up band-pass filter from 0.1 - 30 Hz

FIR filter parameters
---------------------
Designing a one-pass, zero-phase, non-causal bandpass filter:
- Windowed time-domain design (firwin) method
- Hamming window with 0.0194 passband ripple and 53 dB stopband attenuation
- Lower passband edge: 0.10
- Lower transition bandwidth: 0.10 Hz (-6 dB cutoff frequency: 0.05 Hz)
- Upper passband edge: 30.00 Hz
- Upper transition bandwidth: 7.50 Hz (-6 dB cutoff frequency: 33.75 Hz)
- Filter length: 33793 samples (33.001 s)

Fitting ICA to data using 30 channels (please be patient, this may take a while)
Selecting by number: 15 components
Fitting ICA took 9.5s.
Applying ICA to Raw instance
    Transforming to ICA space (15 components)
    Zeroing out 2 ICA components
    Projecting back using 30 PCA components
EEG channel type selected for re-referencing
Applying average reference.
Applying a custom ('EEG',) reference.
[Parallel(n_jobs=1)]: Done  17 tasks      | elapsed:    0.3s

This gives us the cleaned, continuous EEG data as a Raw object.

raw
General
Measurement date Unknown
Experimenter Unknown
Participant Unknown
Channels
Digitized points 33 points
Good channels 30 EEG, 2 EOG
Bad channels None
EOG channels VEOG, HEOG
ECG channels Not available
Data
Sampling frequency 1024.00 Hz
Highpass 0.10 Hz
Lowpass 30.00 Hz
Filenames sub-004_task-N170_eeg.fdt
Duration 00:10:34 (HH:MM:SS)

3.3. Extract events#

We can extract the event codes that are stored within the raw data using the events_from_annotations() function.

events, event_id = events_from_annotations(raw)
Used Annotations descriptions: ['1', '10', '101', '102', '103', '104', '105', '106', '107', '108', '109', '11', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119', '12', '120', '121', '122', '123', '124', '125', '126', '127', '128', '129', '13', '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '14', '140', '141', '142', '143', '144', '145', '146', '147', '148', '149', '15', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '16', '160', '161', '162', '163', '164', '165', '166', '167', '168', '169', '17', '170', '171', '172', '173', '174', '175', '176', '177', '178', '179', '18', '180', '19', '2', '20', '201', '202', '21', '22', '23', '24', '25', '26', '27', '28', '29', '3', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '4', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '5', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '6', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '7', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '8', '80', '9']

This functions returns two outputs:

  • events is a Numpy array with three columns: the sample number (timepoint) of each event, the duration of the event, and the event code (here indicating the specific face or car stimulus)

  • event_id: a dictionary mapping the event codes to human-readable names

events
array([[  4506,      0,     95],
       [  8822,      0,    115],
       [  9257,      0,     94],
       ...,
       [641985,      0,     94],
       [643049,      0,     49],
       [643419,      0,     94]])
event_id
{'1': 1,
 '10': 2,
 '101': 3,
 '102': 4,
 '103': 5,
 '104': 6,
 '105': 7,
 '106': 8,
 '107': 9,
 '108': 10,
 '109': 11,
 '11': 12,
 '110': 13,
 '111': 14,
 '112': 15,
 '113': 16,
 '114': 17,
 '115': 18,
 '116': 19,
 '117': 20,
 '118': 21,
 '119': 22,
 '12': 23,
 '120': 24,
 '121': 25,
 '122': 26,
 '123': 27,
 '124': 28,
 '125': 29,
 '126': 30,
 '127': 31,
 '128': 32,
 '129': 33,
 '13': 34,
 '130': 35,
 '131': 36,
 '132': 37,
 '133': 38,
 '134': 39,
 '135': 40,
 '136': 41,
 '137': 42,
 '138': 43,
 '139': 44,
 '14': 45,
 '140': 46,
 '141': 47,
 '142': 48,
 '143': 49,
 '144': 50,
 '145': 51,
 '146': 52,
 '147': 53,
 '148': 54,
 '149': 55,
 '15': 56,
 '150': 57,
 '151': 58,
 '152': 59,
 '153': 60,
 '154': 61,
 '155': 62,
 '156': 63,
 '157': 64,
 '158': 65,
 '159': 66,
 '16': 67,
 '160': 68,
 '161': 69,
 '162': 70,
 '163': 71,
 '164': 72,
 '165': 73,
 '166': 74,
 '167': 75,
 '168': 76,
 '169': 77,
 '17': 78,
 '170': 79,
 '171': 80,
 '172': 81,
 '173': 82,
 '174': 83,
 '175': 84,
 '176': 85,
 '177': 86,
 '178': 87,
 '179': 88,
 '18': 89,
 '180': 90,
 '19': 91,
 '2': 92,
 '20': 93,
 '201': 94,
 '202': 95,
 '21': 96,
 '22': 97,
 '23': 98,
 '24': 99,
 '25': 100,
 '26': 101,
 '27': 102,
 '28': 103,
 '29': 104,
 '3': 105,
 '30': 106,
 '31': 107,
 '32': 108,
 '33': 109,
 '34': 110,
 '35': 111,
 '36': 112,
 '37': 113,
 '38': 114,
 '39': 115,
 '4': 116,
 '40': 117,
 '41': 118,
 '42': 119,
 '43': 120,
 '44': 121,
 '45': 122,
 '46': 123,
 '47': 124,
 '48': 125,
 '49': 126,
 '5': 127,
 '50': 128,
 '51': 129,
 '52': 130,
 '53': 131,
 '54': 132,
 '55': 133,
 '56': 134,
 '57': 135,
 '58': 136,
 '59': 137,
 '6': 138,
 '60': 139,
 '61': 140,
 '62': 141,
 '63': 142,
 '64': 143,
 '65': 144,
 '66': 145,
 '67': 146,
 '68': 147,
 '69': 148,
 '7': 149,
 '70': 150,
 '71': 151,
 '72': 152,
 '73': 153,
 '74': 154,
 '75': 155,
 '76': 156,
 '77': 157,
 '78': 158,
 '79': 159,
 '8': 160,
 '80': 161,
 '9': 162}

An alternative way to get the events is to use an explicit events file that accompanies the raw data. This is useful if there are no events stored in the raw data or if they are erroneous (as is actually the case here for the N170 dataset!).

For instance, the EEG data from each participant in the ERP CORE datasets (e.g., sub-004_task-N170_eeg.set) is accompanied by a tab-separated events file (e.g., sub-004_task-N170_events.tsv):

%cat data/erpcore/N170/sub-004/eeg/sub-004_task-N170_events.tsv
onset	duration	sample	trial_type	stim_file	value
4.4004	0.0	4507	response	n/a	202
8.6152	0.3	8823	stimulus	n/a	39
9.0400	0.0	9258	response	n/a	201
10.1318	0.3	10376	stimulus	n/a	178
10.4883	0.0	10741	response	n/a	201
11.5977	0.3	11877	stimulus	n/a	28
11.9600	0.0	12248	response	n/a	201
13.1641	0.3	13481	stimulus	n/a	167
13.5283	0.0	13854	response	n/a	201
14.7295	0.3	15084	stimulus	n/a	79
15.1680	0.0	15533	response	n/a	201
16.2793	0.3	16671	stimulus	n/a	149
16.7119	0.0	17114	response	n/a	201
17.7451	0.3	18172	stimulus	n/a	21
17.9922	0.0	18425	response	n/a	201
19.2783	0.3	19742	stimulus	n/a	130
19.6240	0.0	20096	response	n/a	201
20.7939	0.3	21294	stimulus	n/a	76
21.0635	0.0	21570	response	n/a	201
22.2109	0.3	22745	stimulus	n/a	169
22.6641	0.0	23209	response	n/a	201
23.8096	0.3	24382	stimulus	n/a	8
24.1836	0.0	24765	response	n/a	201
25.2930	0.3	25901	stimulus	n/a	36
25.6641	0.0	26281	response	n/a	201
26.8418	0.3	27487	stimulus	n/a	32
27.2637	0.0	27919	response	n/a	201
28.2920	0.3	28972	stimulus	n/a	33
28.6318	0.0	29320	response	n/a	201
29.8906	0.3	30609	stimulus	n/a	159
30.3359	0.0	31065	response	n/a	201
31.3408	0.3	32094	stimulus	n/a	73
31.7197	0.0	32482	response	n/a	201
32.8730	0.3	33663	stimulus	n/a	12
33.2480	0.0	34047	response	n/a	201
34.3066	0.3	35131	stimulus	n/a	14
34.6797	0.0	35513	response	n/a	201
35.8223	0.3	36683	stimulus	n/a	153
36.2158	0.0	37086	response	n/a	201
37.2549	0.3	38150	stimulus	n/a	146
37.5996	0.0	38503	response	n/a	201
38.6543	0.3	39583	stimulus	n/a	13
38.9834	0.0	39920	response	n/a	201
40.2373	0.3	41204	stimulus	n/a	110
40.5430	0.0	41517	response	n/a	202
41.7539	0.3	42757	stimulus	n/a	114
42.1992	0.0	43213	response	n/a	201
43.2363	0.3	44275	stimulus	n/a	23
43.6074	0.0	44655	response	n/a	201
44.7695	0.3	45845	stimulus	n/a	120
45.2549	0.0	46342	response	n/a	201
46.2852	0.3	47397	stimulus	n/a	155
46.6230	0.0	47743	response	n/a	201
47.8848	0.3	49035	stimulus	n/a	47
48.2236	0.0	49382	response	n/a	201
49.2842	0.3	50468	stimulus	n/a	72
49.7354	0.0	50930	response	n/a	202
50.8838	0.3	52106	stimulus	n/a	177
51.2393	0.0	52470	response	n/a	201
52.3662	0.3	53624	stimulus	n/a	49
52.7754	0.0	54043	response	n/a	201
53.8662	0.3	55160	stimulus	n/a	118
54.2314	0.0	55534	response	n/a	201
55.2822	0.3	56610	stimulus	n/a	136
55.6475	0.0	56984	response	n/a	201
56.7148	0.3	58077	stimulus	n/a	55
57.0713	0.0	58442	response	n/a	201
58.1807	0.3	59578	stimulus	n/a	61
58.5029	0.0	59908	response	n/a	201
59.7305	0.3	61165	stimulus	n/a	67
60.0547	0.0	61497	response	n/a	201
61.2461	0.3	62717	stimulus	n/a	30
61.5830	0.0	63062	response	n/a	201
62.7793	0.3	64287	stimulus	n/a	176
63.1592	0.0	64676	response	n/a	201
64.2451	0.3	65788	stimulus	n/a	150
64.6230	0.0	66175	response	n/a	201
65.8281	0.3	67409	stimulus	n/a	62
66.1748	0.0	67764	response	n/a	201
67.2441	0.3	68859	stimulus	n/a	119
67.6553	0.0	69280	response	n/a	201
75.0586	0.3	76861	stimulus	n/a	4
75.5430	0.0	77357	response	n/a	201
76.5078	0.3	78345	stimulus	n/a	70
76.8711	0.0	78717	response	n/a	202
77.9902	0.3	79863	stimulus	n/a	64
78.2949	0.0	80175	response	n/a	201
79.5234	0.3	81433	stimulus	n/a	38
79.9424	0.0	81862	response	n/a	201
80.9395	0.3	82883	stimulus	n/a	134
81.5029	0.0	83460	response	n/a	201
82.3721	0.3	84350	stimulus	n/a	29
82.7266	0.0	84713	response	n/a	201
83.7715	0.3	85783	stimulus	n/a	80
84.1826	0.0	86204	response	n/a	201
85.3379	0.3	87387	stimulus	n/a	173
85.7100	0.0	87768	response	n/a	201
86.8379	0.3	88923	stimulus	n/a	45
87.2627	0.0	89358	response	n/a	201
88.3701	0.3	90492	stimulus	n/a	175
88.7744	0.0	90906	response	n/a	201
89.7695	0.3	91925	stimulus	n/a	104
90.1982	0.0	92364	response	n/a	201
91.2363	0.3	93427	stimulus	n/a	56
91.6543	0.0	93855	response	n/a	201
92.7188	0.3	94945	stimulus	n/a	25
93.1660	0.0	95403	response	n/a	201
94.2852	0.3	96549	stimulus	n/a	113
94.6621	0.0	96935	response	n/a	202
95.7842	0.3	98084	stimulus	n/a	170
96.2227	0.0	98533	response	n/a	201
97.3506	0.3	99688	stimulus	n/a	57
97.7744	0.0	100122	response	n/a	201
98.7666	0.3	101138	stimulus	n/a	20
99.1426	0.0	101523	response	n/a	201
100.2656	0.3	102673	stimulus	n/a	6
100.5742	0.0	102989	response	n/a	201
101.6992	0.3	104141	stimulus	n/a	143
102.0459	0.0	104496	response	n/a	201
103.1650	0.3	105642	stimulus	n/a	5
103.4785	0.0	105963	response	n/a	201
104.6309	0.3	107143	stimulus	n/a	127
105.0381	0.0	107560	response	n/a	201
106.1143	0.3	108662	stimulus	n/a	125
106.4775	0.0	109034	response	n/a	201
107.6465	0.3	110231	stimulus	n/a	11
107.9736	0.0	110566	response	n/a	201
109.2129	0.3	111835	stimulus	n/a	135
109.5820	0.0	112213	response	n/a	201
110.6953	0.3	113353	stimulus	n/a	102
111.0781	0.0	113745	response	n/a	201
112.1455	0.3	114838	stimulus	n/a	58
112.4941	0.0	115195	response	n/a	201
113.6611	0.3	116390	stimulus	n/a	1
114.0137	0.0	116751	response	n/a	201
115.1436	0.3	117908	stimulus	n/a	144
115.5576	0.0	118332	response	n/a	201
116.6436	0.3	119444	stimulus	n/a	165
116.9902	0.0	119799	response	n/a	201
118.2266	0.3	121065	stimulus	n/a	158
118.5654	0.0	121412	response	n/a	201
119.7090	0.3	122583	stimulus	n/a	162
120.0381	0.0	122920	response	n/a	201
121.1582	0.3	124067	stimulus	n/a	10
121.5176	0.0	124435	response	n/a	201
122.7080	0.3	125654	stimulus	n/a	63
122.9492	0.0	125901	response	n/a	201
124.1240	0.3	127104	stimulus	n/a	78
124.4219	0.0	127409	response	n/a	201
125.6738	0.3	128691	stimulus	n/a	151
126.0293	0.0	129055	response	n/a	201
127.1895	0.3	130243	stimulus	n/a	172
127.5254	0.0	130587	response	n/a	201
128.7559	0.3	131847	stimulus	n/a	166
129.0693	0.0	132168	response	n/a	201
130.2881	0.3	133416	stimulus	n/a	145
130.5898	0.0	133725	response	n/a	201
131.8379	0.3	135003	stimulus	n/a	156
132.1094	0.0	135281	response	n/a	201
133.3545	0.3	136556	stimulus	n/a	109
133.6934	0.0	136903	response	n/a	201
139.3857	0.3	142732	stimulus	n/a	142
139.7969	0.0	143153	response	n/a	201
140.8018	0.3	144182	stimulus	n/a	53
141.1250	0.0	144513	response	n/a	201
142.2510	0.3	145666	stimulus	n/a	26
142.6133	0.0	146037	response	n/a	201
143.7002	0.3	147150	stimulus	n/a	3
143.9736	0.0	147430	response	n/a	201
145.2334	0.3	148720	stimulus	n/a	139
145.4375	0.0	148929	response	n/a	202
146.7490	0.3	150272	stimulus	n/a	65
147.0449	0.0	150575	response	n/a	201
148.2656	0.3	151825	stimulus	n/a	103
148.6211	0.0	152189	response	n/a	202
149.7646	0.3	153360	stimulus	n/a	17
150.0449	0.0	153647	response	n/a	201
151.2979	0.3	154930	stimulus	n/a	160
151.5410	0.0	155179	response	n/a	201
152.8643	0.3	156534	stimulus	n/a	179
153.1729	0.0	156850	response	n/a	201
154.3799	0.3	158086	stimulus	n/a	148
154.6934	0.0	158407	response	n/a	201
155.9297	0.3	159673	stimulus	n/a	154
156.2207	0.0	159971	response	n/a	201
157.4453	0.3	161225	stimulus	n/a	174
157.8125	0.0	161601	response	n/a	201
159.0117	0.3	162829	stimulus	n/a	24
159.3887	0.0	163215	response	n/a	201
160.5947	0.3	164450	stimulus	n/a	152
160.9570	0.0	164821	response	n/a	201
162.0107	0.3	165900	stimulus	n/a	161
162.3730	0.0	166271	response	n/a	201
163.4766	0.3	167401	stimulus	n/a	27
163.7969	0.0	167729	response	n/a	201
164.9600	0.3	168920	stimulus	n/a	141
165.3809	0.0	169351	response	n/a	201
166.4092	0.3	170404	stimulus	n/a	9
166.7002	0.0	170702	response	n/a	201
167.8252	0.3	171854	stimulus	n/a	69
168.1729	0.0	172210	response	n/a	202
169.2412	0.3	173304	stimulus	n/a	50
169.5723	0.0	173643	response	n/a	201
170.6572	0.3	174754	stimulus	n/a	2
171.1084	0.0	175216	response	n/a	201
172.1572	0.3	176290	stimulus	n/a	180
172.5322	0.0	176674	response	n/a	201
173.7393	0.3	177910	stimulus	n/a	133
174.0527	0.0	178231	response	n/a	201
175.2725	0.3	179480	stimulus	n/a	157
175.5967	0.0	179812	response	n/a	201
176.8389	0.3	181084	stimulus	n/a	138
177.1562	0.0	181409	response	n/a	201
178.2549	0.3	182534	stimulus	n/a	147
178.5889	0.0	182876	response	n/a	201
179.8379	0.3	184155	stimulus	n/a	42
180.1807	0.0	184506	response	n/a	201
181.2373	0.3	185588	stimulus	n/a	66
181.5400	0.0	185898	response	n/a	201
182.6367	0.3	187021	stimulus	n/a	37
182.9160	0.0	187307	response	n/a	201
184.2197	0.3	188642	stimulus	n/a	164
184.6045	0.0	189036	response	n/a	201
185.8184	0.3	190279	stimulus	n/a	163
186.1084	0.0	190576	response	n/a	201
187.3018	0.3	191798	stimulus	n/a	46
187.7158	0.0	192222	response	n/a	201
188.7842	0.3	193316	stimulus	n/a	18
189.2041	0.0	193746	response	n/a	201
190.3174	0.3	194886	stimulus	n/a	52
190.5801	0.0	195155	response	n/a	201
191.8828	0.3	196489	stimulus	n/a	60
192.1562	0.0	196769	response	n/a	201
193.4160	0.3	198059	stimulus	n/a	34
193.6924	0.0	198342	response	n/a	201
194.8818	0.3	199560	stimulus	n/a	16
195.1396	0.0	199824	response	n/a	201
196.4482	0.3	201164	stimulus	n/a	171
196.7637	0.0	201487	response	n/a	201
197.9648	0.3	202717	stimulus	n/a	71
198.3164	0.0	203077	response	n/a	201
208.2607	0.3	213260	stimulus	n/a	59
209.0518	0.0	214070	response	n/a	202
209.6768	0.3	214710	stimulus	n/a	15
210.1162	0.0	215160	response	n/a	201
211.1768	0.3	216246	stimulus	n/a	168
211.5400	0.0	216618	response	n/a	201
212.5928	0.3	217696	stimulus	n/a	77
213.0117	0.0	218125	response	n/a	201
214.0586	0.3	219197	stimulus	n/a	19
214.3721	0.0	219518	response	n/a	201
215.6250	0.3	220801	stimulus	n/a	122
215.8916	0.0	221074	response	n/a	202
217.1738	0.3	222387	stimulus	n/a	31
217.5557	0.0	222778	response	n/a	201
218.6904	0.3	223940	stimulus	n/a	35
219.0352	0.0	224293	response	n/a	201
220.2568	0.3	225544	stimulus	n/a	147
220.6836	0.0	225981	response	n/a	201
221.7227	0.3	227045	stimulus	n/a	108
222.0752	0.0	227406	response	n/a	201
223.2383	0.3	228597	stimulus	n/a	22
223.6191	0.0	228987	response	n/a	201
224.6719	0.3	230065	stimulus	n/a	68
225.0352	0.0	230437	response	n/a	201
226.2539	0.3	231685	stimulus	n/a	144
226.5557	0.0	231994	response	n/a	201
227.6875	0.3	233153	stimulus	n/a	40
227.9316	0.0	233403	response	n/a	201
229.1035	0.3	234603	stimulus	n/a	41
229.4277	0.0	234935	response	n/a	202
230.6191	0.3	236155	stimulus	n/a	124
230.8828	0.0	236425	response	n/a	201
232.2188	0.3	237793	stimulus	n/a	7
232.5068	0.0	238088	response	n/a	201
233.7852	0.3	239397	stimulus	n/a	112
234.1230	0.0	239743	response	n/a	201
235.3174	0.3	240966	stimulus	n/a	15
235.7236	0.0	241382	response	n/a	201
236.8506	0.3	242536	stimulus	n/a	22
237.0830	0.0	242774	response	n/a	201
238.4170	0.3	244140	stimulus	n/a	18
238.7549	0.0	244486	response	n/a	201
239.8828	0.3	245641	stimulus	n/a	128
240.2988	0.0	246067	response	n/a	201
241.4824	0.3	247279	stimulus	n/a	149
241.7148	0.0	247517	response	n/a	202
242.9980	0.3	248831	stimulus	n/a	137
243.4111	0.0	249254	response	n/a	201
244.4814	0.3	250350	stimulus	n/a	31
244.9395	0.0	250819	response	n/a	201
246.0635	0.3	251970	stimulus	n/a	38
246.4111	0.0	252326	response	n/a	202
247.4805	0.3	253421	stimulus	n/a	13
247.8105	0.0	253759	response	n/a	201
248.9961	0.3	254973	stimulus	n/a	74
249.2832	0.0	255267	response	n/a	201
250.4961	0.3	256509	stimulus	n/a	140
250.8105	0.0	256831	response	n/a	201
251.9785	0.3	258027	stimulus	n/a	132
252.3867	0.0	258445	response	n/a	201
253.4443	0.3	259528	stimulus	n/a	126
253.7627	0.0	259854	response	n/a	201
254.9941	0.3	261115	stimulus	n/a	17
255.2910	0.0	261419	response	n/a	201
256.5771	0.3	262736	stimulus	n/a	116
257.0352	0.0	263205	response	n/a	201
257.9932	0.3	264186	stimulus	n/a	172
258.2744	0.0	264474	response	n/a	202
259.5088	0.3	265738	stimulus	n/a	75
259.7705	0.0	266006	response	n/a	201
261.0420	0.3	267308	stimulus	n/a	107
261.4502	0.0	267726	response	n/a	201
262.5576	0.3	268860	stimulus	n/a	115
262.9150	0.0	269226	response	n/a	201
264.1074	0.3	270447	stimulus	n/a	16
264.4590	0.0	270807	response	n/a	201
265.5068	0.3	271880	stimulus	n/a	54
265.8350	0.0	272216	response	n/a	201
266.9229	0.3	273330	stimulus	n/a	40
267.3701	0.0	273788	response	n/a	201
293.9639	0.3	301020	stimulus	n/a	131
294.4580	0.0	301526	response	n/a	201
295.3633	0.3	302453	stimulus	n/a	101
295.6895	0.0	302787	response	n/a	201
296.8291	0.3	303954	stimulus	n/a	174
297.1299	0.0	304262	response	n/a	201
298.2617	0.3	305421	stimulus	n/a	5
298.5615	0.0	305728	response	n/a	201
299.7617	0.3	306957	stimulus	n/a	129
300.1699	0.0	307375	response	n/a	201
301.3105	0.3	308543	stimulus	n/a	51
301.6260	0.0	308866	response	n/a	201
302.8936	0.3	310164	stimulus	n/a	156
303.2334	0.0	310512	response	n/a	201
304.3926	0.3	311699	stimulus	n/a	121
304.7617	0.0	312077	response	n/a	201
305.9258	0.3	313269	stimulus	n/a	48
306.3057	0.0	313658	response	n/a	201
307.4258	0.3	314805	stimulus	n/a	111
307.8174	0.0	315206	response	n/a	201
308.9746	0.3	316391	stimulus	n/a	117
309.3301	0.0	316755	response	n/a	201
310.4580	0.3	317910	stimulus	n/a	106
310.7617	0.0	318221	response	n/a	201
311.8740	0.3	319360	stimulus	n/a	157
312.1855	0.0	319679	response	n/a	201
313.3398	0.3	320861	stimulus	n/a	44
313.6895	0.0	321219	response	n/a	201
314.8223	0.3	322379	stimulus	n/a	123
315.2979	0.0	322866	response	n/a	201
316.4053	0.3	324000	stimulus	n/a	105
316.7695	0.0	324373	response	n/a	201
317.9053	0.3	325536	stimulus	n/a	135
318.2012	0.0	325839	response	n/a	201
319.4541	0.3	327122	stimulus	n/a	43
319.8018	0.0	327478	response	n/a	201
320.8867	0.3	328589	stimulus	n/a	154
321.2969	0.0	329009	response	n/a	201
322.4863	0.3	330227	stimulus	n/a	176
322.8330	0.0	330582	response	n/a	201
323.9697	0.3	331746	stimulus	n/a	53
324.3213	0.0	332106	response	n/a	201
325.4189	0.3	333230	stimulus	n/a	39
325.7129	0.0	333531	response	n/a	201
326.8848	0.3	334731	stimulus	n/a	162
327.2412	0.0	335096	response	n/a	201
328.3848	0.3	336267	stimulus	n/a	3
328.6816	0.0	336571	response	n/a	201
329.8340	0.3	337751	stimulus	n/a	132
330.1855	0.0	338111	response	n/a	202
331.2832	0.3	339235	stimulus	n/a	71
331.5615	0.0	339520	response	n/a	201
332.7666	0.3	340754	stimulus	n/a	76
333.0410	0.0	341035	response	n/a	201
334.1992	0.3	342221	stimulus	n/a	118
334.6172	0.0	342649	response	n/a	201
335.6982	0.3	343756	stimulus	n/a	127
336.1211	0.0	344189	response	n/a	201
337.1650	0.3	345258	stimulus	n/a	28
337.4814	0.0	345582	response	n/a	201
338.5977	0.3	346725	stimulus	n/a	130
339.0488	0.0	347187	response	n/a	201
340.0137	0.3	348175	stimulus	n/a	112
340.3125	0.0	348481	response	n/a	201
341.5303	0.3	349728	stimulus	n/a	155
341.8652	0.0	350071	response	n/a	201
342.9961	0.3	351229	stimulus	n/a	165
343.2812	0.0	351521	response	n/a	201
344.4121	0.3	352679	stimulus	n/a	151
344.7207	0.0	352995	response	n/a	201
345.8779	0.3	354180	stimulus	n/a	27
346.3291	0.0	354642	response	n/a	201
347.3613	0.3	355699	stimulus	n/a	35
347.7529	0.0	356100	response	n/a	201
348.8936	0.3	357268	stimulus	n/a	113
349.2969	0.0	357681	response	n/a	201
350.4600	0.3	358872	stimulus	n/a	21
350.8408	0.0	359262	response	n/a	202
351.8926	0.3	360339	stimulus	n/a	14
352.3525	0.0	360810	response	n/a	201
397.4766	0.3	407017	stimulus	n/a	141
398.3438	0.0	407905	response	n/a	201
398.8760	0.3	408450	stimulus	n/a	36
399.2236	0.0	408806	response	n/a	201
400.3252	0.3	409934	stimulus	n/a	70
400.6553	0.0	410272	response	n/a	202
401.7578	0.3	411401	stimulus	n/a	56
402.1191	0.0	411771	response	n/a	201
403.3076	0.3	412988	stimulus	n/a	134
403.7354	0.0	413426	response	n/a	201
404.8076	0.3	414524	stimulus	n/a	164
405.1758	0.0	414901	response	n/a	201
406.2236	0.3	415974	stimulus	n/a	73
406.5518	0.0	416310	response	n/a	201
407.7227	0.3	417509	stimulus	n/a	74
407.9990	0.0	417792	response	n/a	201
409.2725	0.3	419096	stimulus	n/a	166
409.5752	0.0	419406	response	n/a	202
410.8555	0.3	420717	stimulus	n/a	161
411.2793	0.0	421151	response	n/a	201
412.2881	0.3	422184	stimulus	n/a	47
412.6797	0.0	422585	response	n/a	202
413.8369	0.3	423770	stimulus	n/a	133
414.1992	0.0	424141	response	n/a	201
415.2539	0.3	425221	stimulus	n/a	67
415.5996	0.0	425575	response	n/a	201
416.7695	0.3	426773	stimulus	n/a	78
417.0791	0.0	427090	response	n/a	201
418.3193	0.3	428360	stimulus	n/a	34
418.6230	0.0	428671	response	n/a	201
419.8848	0.3	429963	stimulus	n/a	59
420.2549	0.0	430342	response	n/a	201
421.4844	0.3	431601	stimulus	n/a	19
421.8076	0.0	431932	response	n/a	201
423.0508	0.3	433205	stimulus	n/a	7
423.3516	0.0	433513	response	n/a	201
424.5830	0.3	434774	stimulus	n/a	2
424.8955	0.0	435094	response	n/a	201
426.0664	0.3	436293	stimulus	n/a	10
426.3994	0.0	436634	response	n/a	201
427.5322	0.3	437794	stimulus	n/a	126
427.9434	0.0	438215	response	n/a	201
429.0654	0.3	439364	stimulus	n/a	6
429.5352	0.0	439845	response	n/a	201
430.5645	0.3	440899	stimulus	n/a	60
431.0312	0.0	441377	response	n/a	201
432.1143	0.3	442486	stimulus	n/a	138
432.5029	0.0	442884	response	n/a	202
433.5303	0.3	443936	stimulus	n/a	45
433.8789	0.0	444293	response	n/a	201
435.0127	0.3	445454	stimulus	n/a	115
435.3906	0.0	445841	response	n/a	201
436.5459	0.3	447024	stimulus	n/a	171
436.8867	0.0	447373	response	n/a	202
438.0283	0.3	448542	stimulus	n/a	55
438.3184	0.0	448839	response	n/a	201
439.4619	0.3	450010	stimulus	n/a	107
439.8467	0.0	450404	response	n/a	201
440.9941	0.3	451579	stimulus	n/a	72
441.3428	0.0	451936	response	n/a	201
442.3936	0.3	453012	stimulus	n/a	122
442.8232	0.0	453452	response	n/a	201
443.8936	0.3	454548	stimulus	n/a	25
444.2783	0.0	454942	response	n/a	201
445.3926	0.3	456083	stimulus	n/a	150
445.7744	0.0	456474	response	n/a	201
446.9258	0.3	457653	stimulus	n/a	131
447.3027	0.0	458039	response	n/a	201
448.4746	0.3	459239	stimulus	n/a	159
448.7988	0.0	459571	response	n/a	201
450.0576	0.3	460860	stimulus	n/a	140
450.4229	0.0	461234	response	n/a	201
451.5742	0.3	462413	stimulus	n/a	9
451.9590	0.0	462807	response	n/a	201
453.1230	0.3	463999	stimulus	n/a	57
453.3984	0.0	464281	response	n/a	201
454.6895	0.3	465603	stimulus	n/a	109
454.9902	0.0	465911	response	n/a	202
456.2559	0.3	467207	stimulus	n/a	77
456.5742	0.0	467533	response	n/a	201
476.6816	0.3	488123	stimulus	n/a	42
477.0859	0.0	488537	response	n/a	201
478.0977	0.3	489573	stimulus	n/a	62
478.4141	0.0	489897	response	n/a	201
479.4971	0.3	491006	stimulus	n/a	178
479.9658	0.0	491486	response	n/a	201
480.8965	0.3	492439	stimulus	n/a	48
481.2539	0.0	492805	response	n/a	201
482.4629	0.3	494043	stimulus	n/a	110
482.7422	0.0	494329	response	n/a	202
483.9287	0.3	495544	stimulus	n/a	153
484.3418	0.0	495967	response	n/a	201
485.5117	0.3	497165	stimulus	n/a	44
485.8936	0.0	497556	response	n/a	201
487.0449	0.3	498735	stimulus	n/a	101
487.4297	0.0	499129	response	n/a	201
488.5938	0.3	500321	stimulus	n/a	173
488.9658	0.0	500702	response	n/a	201
490.1602	0.3	501925	stimulus	n/a	180
490.4541	0.0	502226	response	n/a	201
491.6426	0.3	503443	stimulus	n/a	32
491.9658	0.0	503774	response	n/a	201
493.1094	0.3	504945	stimulus	n/a	152
493.4219	0.0	505265	response	n/a	201
494.6914	0.3	506565	stimulus	n/a	105
495.0381	0.0	506920	response	n/a	201
496.1914	0.3	508101	stimulus	n/a	20
496.5176	0.0	508435	response	n/a	201
497.7402	0.3	509687	stimulus	n/a	66
498.0371	0.0	509991	response	n/a	201
499.3066	0.3	511291	stimulus	n/a	1
499.6299	0.0	511622	response	n/a	201
500.8066	0.3	512827	stimulus	n/a	137
501.2051	0.0	513235	response	n/a	201
502.3887	0.3	514447	stimulus	n/a	168
502.7012	0.0	514767	response	n/a	201
503.8223	0.3	515915	stimulus	n/a	124
504.1973	0.0	516299	response	n/a	201
505.3047	0.3	517433	stimulus	n/a	30
505.7334	0.0	517872	response	n/a	201
506.7539	0.3	518917	stimulus	n/a	114
507.1572	0.0	519330	response	n/a	201
508.2373	0.3	520436	stimulus	n/a	24
508.6289	0.0	520837	response	n/a	201
509.7529	0.3	521988	stimulus	n/a	79
510.1172	0.0	522361	response	n/a	202
511.2188	0.3	523489	stimulus	n/a	136
511.6055	0.0	523885	response	n/a	201
512.7520	0.3	525059	stimulus	n/a	169
513.1016	0.0	525417	response	n/a	201
514.2178	0.3	526560	stimulus	n/a	41
514.5811	0.0	526932	response	n/a	201
515.6514	0.3	528028	stimulus	n/a	4
515.9336	0.0	528317	response	n/a	201
517.1338	0.3	529546	stimulus	n/a	158
517.5654	0.0	529988	response	n/a	201
518.6494	0.3	531098	stimulus	n/a	49
518.9326	0.0	531388	response	n/a	201
520.1162	0.3	532600	stimulus	n/a	163
520.5088	0.0	533002	response	n/a	201
521.6152	0.3	534135	stimulus	n/a	111
521.9971	0.0	534526	response	n/a	201
523.0820	0.3	535637	stimulus	n/a	142
523.3809	0.0	535943	response	n/a	201
524.6309	0.3	537223	stimulus	n/a	106
524.9170	0.0	537516	response	n/a	201
526.1143	0.3	538742	stimulus	n/a	68
526.3887	0.0	539023	response	n/a	202
527.5967	0.3	540260	stimulus	n/a	128
528.0371	0.0	540711	response	n/a	201
529.1963	0.3	541898	stimulus	n/a	103
529.5488	0.0	542259	response	n/a	201
530.6455	0.3	543382	stimulus	n/a	51
531.0605	0.0	543807	response	n/a	201
532.0615	0.3	544832	stimulus	n/a	29
532.4209	0.0	545200	response	n/a	201
533.5449	0.3	546351	stimulus	n/a	61
533.9170	0.0	546732	response	n/a	201
535.0938	0.3	547937	stimulus	n/a	175
535.4443	0.0	548296	response	n/a	202
569.3320	0.3	582997	stimulus	n/a	63
569.7959	0.0	583472	response	n/a	201
570.7480	0.3	584447	stimulus	n/a	52
571.0918	0.0	584799	response	n/a	201
572.2979	0.3	586034	stimulus	n/a	33
572.6523	0.0	586397	response	n/a	201
573.7139	0.3	587484	stimulus	n/a	75
574.0596	0.0	587838	response	n/a	201
575.2295	0.3	589036	stimulus	n/a	119
575.5801	0.0	589395	response	n/a	201
576.7959	0.3	590640	stimulus	n/a	23
577.1074	0.0	590959	response	n/a	201
578.3115	0.3	592192	stimulus	n/a	80
578.6436	0.0	592532	response	n/a	201
579.8779	0.3	593796	stimulus	n/a	65
580.1875	0.0	594113	response	n/a	201
581.4443	0.3	595400	stimulus	n/a	58
581.7480	0.0	595711	response	n/a	201
582.8770	0.3	596867	stimulus	n/a	179
583.2754	0.0	597275	response	n/a	201
584.4434	0.3	598471	stimulus	n/a	37
584.7881	0.0	598824	response	n/a	201
585.9258	0.3	599989	stimulus	n/a	146
586.2920	0.0	600364	response	n/a	201
587.3750	0.3	601473	stimulus	n/a	54
587.7080	0.0	601814	response	n/a	201
588.8418	0.3	602975	stimulus	n/a	12
589.1396	0.0	603280	response	n/a	201
590.4238	0.3	604595	stimulus	n/a	11
590.7080	0.0	604886	response	n/a	201
591.8906	0.3	606097	stimulus	n/a	117
592.2275	0.0	606442	response	n/a	201
593.3730	0.3	607615	stimulus	n/a	145
593.6680	0.0	607917	response	n/a	202
594.8057	0.3	609082	stimulus	n/a	160
595.1953	0.0	609481	response	n/a	201
596.2891	0.3	610601	stimulus	n/a	8
596.6436	0.0	610964	response	n/a	201
597.7051	0.3	612051	stimulus	n/a	139
598.0039	0.0	612357	response	n/a	202
599.3047	0.3	613689	stimulus	n/a	123
599.6270	0.0	614019	response	n/a	201
600.7539	0.3	615173	stimulus	n/a	69
601.1396	0.0	615568	response	n/a	201
602.3037	0.3	616760	stimulus	n/a	129
602.6992	0.0	617165	response	n/a	201
603.8691	0.3	618363	stimulus	n/a	116
604.1875	0.0	618689	response	n/a	201
605.3857	0.3	619916	stimulus	n/a	108
605.6592	0.0	620196	response	n/a	201
606.9688	0.3	621537	stimulus	n/a	121
607.2275	0.0	621802	response	n/a	201
608.5010	0.3	623106	stimulus	n/a	43
608.8350	0.0	623448	response	n/a	201
609.9668	0.3	624607	stimulus	n/a	170
610.3389	0.0	624988	response	n/a	201
611.4502	0.3	626126	stimulus	n/a	120
611.7549	0.0	626438	response	n/a	201
612.9658	0.3	627678	stimulus	n/a	102
613.2588	0.0	627978	response	n/a	201
614.4492	0.3	629197	stimulus	n/a	177
614.7627	0.0	629518	response	n/a	201
616.0322	0.3	630818	stimulus	n/a	46
616.3633	0.0	631157	response	n/a	201
617.5312	0.3	632353	stimulus	n/a	50
617.8271	0.0	632656	response	n/a	201
618.9639	0.3	633820	stimulus	n/a	148
619.3066	0.0	634171	response	n/a	201
620.5303	0.3	635424	stimulus	n/a	64
620.9072	0.0	635810	response	n/a	201
622.0967	0.3	637028	stimulus	n/a	125
622.3945	0.0	637333	response	n/a	202
623.4961	0.3	638461	stimulus	n/a	26
623.8672	0.0	638841	response	n/a	201
625.0283	0.3	640030	stimulus	n/a	167
625.3711	0.0	640381	response	n/a	201
626.5781	0.3	641617	stimulus	n/a	104
626.9385	0.0	641986	response	n/a	201
627.9775	0.3	643050	stimulus	n/a	143
628.3389	0.0	643420	response	n/a	201

We can read this file using the pandas package, which offers a DataFrame type for working tabular data (similar to a data.frame or tibble in R).

log_file = files_dict['log_files'][0]
log = pd.read_csv(log_file, sep='\t')
log
onset duration sample trial_type stim_file value
0 4.4004 0.0 4507 response NaN 202
1 8.6152 0.3 8823 stimulus NaN 39
2 9.0400 0.0 9258 response NaN 201
3 10.1318 0.3 10376 stimulus NaN 178
4 10.4883 0.0 10741 response NaN 201
... ... ... ... ... ... ...
636 625.3711 0.0 640381 response NaN 201
637 626.5781 0.3 641617 stimulus NaN 104
638 626.9385 0.0 641986 response NaN 201
639 627.9775 0.3 643050 stimulus NaN 143
640 628.3389 0.0 643420 response NaN 201

641 rows × 6 columns

From this data frame, we can re-create the 3-column events array (see above), now with the correct timings and event codes. Note that by converting all columns from floats to integers, we are rounding the duration of the events from 0.3 (the actual length of the stimulus) to 0, as is common practice in ERP analysis.

events = log[['sample', 'duration', 'value']].values.astype(int)
events
array([[  4507,      0,    202],
       [  8823,      0,     39],
       [  9258,      0,    201],
       ...,
       [641986,      0,    201],
       [643050,      0,    143],
       [643420,      0,    201]])

However, these many different numerical event codes still have no obvious meaning to us. We can check what they stand for by looking at the task-N170_events.json file that accompanies the dataset:

%cat data/erpcore/N170/task-N170_events.json
{
  "value": {
    "LongName": "Event code value",
    "Levels": {
	   "1-40": "Stimulus - faces",
	   "41-80": "Stimulus - cars",
	   "101-140": "Stimulus - scrambled faces",
	   "141-180": "Stimulus - scrambled cars",
	   
	   "201": "Response - correct",
	   "202": "Response - error"
    }
  }
}

We see that triggers 1–40 correspond to face stimuli, and triggers 41–80 correspond to car stimuli. To make our epoching job easier, we’ll collapse these 80 original event codes into just two event codes: 1 for faces and 2 for cars, using MNE’s merge_events() function.

events = merge_events(events, ids=range(1, 41), new_id=1)
events = merge_events(events, ids=range(41, 81), new_id=2)
events
array([[  4507,      0,    202],
       [  8823,      0,      1],
       [  9258,      0,    201],
       ...,
       [641986,      0,    201],
       [643050,      0,    143],
       [643420,      0,    201]])

Note that range() is a built-in Python function that generates a sequence of numbers (integers) between a start and end value. For example, range(1, 41) generates the sequence 1, 2, …, 40 (the end value is not included).

We also need to update the event_id dictionary accordingly, mapping the new event codes to human-readable condition labels.

event_id = {'face': 1, 'car': 2}

3.4. Epoching#

No we’re ready to segment our raw data into epochs, using the events and event_id that we just created. We’ll use a time window of 1 s, starting 200 ms before the event onset.

For now, we’ll skip baseline correction (this would be enabled by default) because we want to show the effect of baseline correction later.

epochs = Epochs(raw, events, event_id, tmin=-0.2, tmax=0.8, baseline=None)
Not setting metadata
160 matching events found
No baseline correction applied
0 projection items activated
epochs.get_data().shape
Using data from preloaded Raw for 160 events and 1025 original time points ...
0 bad epochs dropped
(160, 32, 1025)

We see that the dimensions have changed, from EEG channels × timepoints (raw) to epochs × EEG channels × timepoints (epochs).

Just as the raw data, the epochs also have a plot() method to visualize the data.

_ = epochs.plot(events=True)
Using matplotlib as 2D backend.
Using data from preloaded Raw for 20 events and 1025 original time points ...
../_images/f789882929133b76277d1f0c254c8e8126a2ef7ad4bdf6ebb046cb009b83c763.png

Another effective way of visualizing all epochs in the dataset is the so-called ERP image plot. This is a heatmap with time on the x-axis, epochs on the y-axis, and the EEG amplitude represented by color.

We’ll create this plot for a single EEG channel ('PO8') that is typically sensitive to the N170 face effect.

_ = epochs.plot_image(picks='PO8')
Using data from preloaded Raw for 160 events and 1025 original time points ...
Not setting metadata
160 matching events found
No baseline correction applied
0 projection items activated
../_images/f31e66b8f63c0b10ad4f825d7cf7dfb7f1c41ec78c947d275ab9bf2252816527.png

This type of plot could also be used to check for interesting patterns in the data, e.g., by sorting the epochs on the y-axis by some variables of interest (e.g., stimulus condition or reaction time).

3.5. Baseline correction#

Some of the epochs have a large offset (vertical shift) compared to the other epochs, which can happen due to technical or physiological drifts at some channels.

We can remove these offsets by applying a baseline correction. This works by, separately for each channel, subtracting the mean of the baseline activity from each timepoint in the epoch. The baseline activity is typically defined as the 200 ms prior to the event onset.

epochs = epochs.apply_baseline((-0.2, 0.0))
_ = epochs.plot(events=True)
Applying baseline correction (mode: mean)
Using data from preloaded Raw for 20 events and 1025 original time points ...
../_images/2e00b9991d1f024557b0d6d14528b99f1d1512a7d41068a3e0dd60be61b81cc6.png

3.6. Rejecting bad epochs#

Despite all our data cleaning efforts (filtering, ICA, referencing, baseline correction), some epochs will still contain large-amplitude artifacts, e.g., due to movements or technical glitches.

We can “reject” (delete) these epochs from the data by using the drop_bad() method (or, alternatively, the reject=... argument of the Epochs constructor). This allows us to specify a peak-to-peak-threshold (in volts). If, at any channel, the difference between the minimum and maximum amplitude in an epoch exceeds this threshold, the epoch will be rejected (deleted).

Depending on how clean the dataset is, this threshold will typically be between 50 and 200 µV. Note that the lower the threshold (that is, the more epochs we’re rejecting), the cleaner the remaining epochs will be, but the fewer epochs we’ll have left, potentially reducing our statistical power.

epochs = epochs.drop_bad({'eeg': 100e-6})
Using data from preloaded Raw for 160 events and 1025 original time points ...
    Rejecting  epoch based on EEG : ['F8']
    Rejecting  epoch based on EEG : ['F3', 'C5', 'P9', 'PO7', 'P10', 'PO8', 'O2']
    Rejecting  epoch based on EEG : ['P9', 'P10', 'PO8', 'O2']
    Rejecting  epoch based on EEG : ['F3', 'F4']
    Rejecting  epoch based on EEG : ['F8']
5 bad epochs dropped

We see that the ERP image now has a reduced number of epochs (rows), but the color patterns are much clearer (as some large-amplitude epochs have been removed):

_ = epochs.plot_image(picks='PO8')
Using data from preloaded Raw for 155 events and 1025 original time points ...
Not setting metadata
155 matching events found
No baseline correction applied
0 projection items activated
../_images/9c126e8abe6f112509f53659632e6f0936382e7383fab4a6d7fb66f3e25ea55d.png

3.7. Exercises#

  1. Repeat the preprocessing (first code cell) for a different ERP CORE experiment (valid experiment names are 'N170', 'MMN', 'N2pc', 'N400', 'P3', or 'ERN'). Then check the task-<experiment>_events.json file and construct the correct events and event_id variables. Finally, create epochs from the data and plot the ERP image for a channel that is typically sensitive to the effect of interest (check Kappenman et al (2021), Table 1, for a suggestion).

%cat data/erpcore/.../task-..._events.json
# Your code goes here
...

3.8. Further reading#

3.9. References#

Gramfort et al., 2013

Gramfort, A., Luessi, M., Larson, E., Engemann, D. A., Strohmeier, D., Brodbeck, C., … Hämäläinen, M. (2013). MEG and EEG data analysis with MNE-Python. Frontiers in Neuroscience, 7, 267. doi:10.3389/fnins.2013.00267

Kappenman et al., 2021

Kappenman, E. S., Farrens, J. L., Zhang, W., Stewart, A. X., & Luck, S. J. (2021). ERP CORE: An open resource for human event-related potential research. NeuroImage, 225, 117465. doi:10.1016/j.neuroimage.2020.117465

Luck, 2022

Luck, S. J. (2022). A very brief introduction to EEG and ERPs. Applied Event-Related Potential Data Analysis. LibreTexts. https://doi.org/10.18115/D5QG92.

Newman, 2020

Newman, A. J. (2020). Neural Data Science in Python. Dalhousie University. https://neuraldatascience.io.