Skip to content

Event related potential (ERP) classification

Event related potential (ERP) experiments measure the brain response to a stimulus or an event. Since the brain response to a single stimulus is usually not very visible in a single EEG trial, many trials are traditionally performed and the signals are averaged to improve the signal-to-noise ratio. Machine learning makes it possible to instead classify the response to a single stimulus. This example pipeline, EventRelatedPotentials.pyp, does that for an example data set of a flanker task, in which the participant's response was either correct or incorrect. The pipeline learns to tell the brain responses to the correct and the incorrect responses apart.

The classifier is a hierarchical discriminant component analysis (HDCA), which is a classifier that is well suited for ERPs. It is calibrated on a training recording, FlankerTrain.set, and then classifies a stream of data from a second recording, FlankerTest.set, trial by trial, as it comes in. Both are example data sets that ship with NeuroPype. The same data is used in the calibrate on prerecorded data example, which uses logistic regression instead of HDCA, and in the flanker ERP analysis example, which analyzes the ERPs of the recording.

ERP classification pipeline

The pipeline can be divided into four parts, which we discuss in the following: data acquisition and calibration, preprocessing, classification, and the output of the results.

Data Acquisition and Calibration

Includes: Import SET, LSL Input, Dejitter Timestamps and Inject Calibration Data

Calibration and streaming data inputs

The data acquisition is the same as in the calibrate on prerecorded data example, which describes it in detail. The Inject Calibration Data node first lets the calibration recording through (from the Import SET node titled Import Calibration Data), so that the adaptive and machine learning nodes can train on it, and then lets through the streaming data, which comes from the LSL Input node and Dejitter Timestamps. Its delay_streaming_packets property is enabled, so that no streaming data is lost on the tick on which the calibration data passes.

The three nodes at the bottom of the pipeline (Import SET titled Import Test Data, Stream Data and LSL Output) play back the test recording as an LSL stream called OutStreamData (with its markers as a second stream, OutStreamData-markers), so that the example can be run without any hardware. See Data input in the example pipelines for more on this setup and for how to use your own live data. With live data, the calibration recording should come from the same device (with the same channels) as the live stream.

Preprocessing

Includes: Assign Targets, Select Range, FIR Filter, Decimate, Segmentation and Spectral Selection

Target assignment and channel selection

  • Assign Targets assigns a target value to the markers that the classifier is trained on: its mapping assigns 1 to the marker incorrect and 0 to the marker correct (from the Marker column, its iv_column). The markers must exist in the data.
  • Select Range removes the last channel (selection of :-1 on the space axis, using indices), which is not an EEG channel in this data set.

Feature extraction and classification nodes

  • FIR Filter is a low-pass filter (frequencies of 40 and 45 Hz), and Decimate is the node used to downsample the data after it. Its factor is 1 here, so that it does not change the data, but you can increase it to reduce the amount of data that the classifier processes (see the documentation of the node: it only accepts integer factors, and it needs to be preceded by a low-pass filter that removes the frequencies that cannot be represented after decimation, which is why the FIR Filter is here).
  • Segmentation cuts the data into one segment (a trial, along the instance axis) for each target marker, from 0.2 seconds before to 0.8 seconds after each marker (time_bounds). Its online_epoching property is marker-locked, which extracts the segments around the markers in both the calibration and the streaming data. (The alternative, sliding, extracts a single segment at the end of the data on every tick, and is used when there are no markers.)
  • Spectral Selection filters the segmented data to the frequencies from 0.1 to 15 Hz (frequencies), which isolates the slow cortical potentials of the ERP from higher-frequency content.

Classification

Includes: Hierarchical Discriminant Component Analysis

The Hierarchical Discriminant Component Analysis node (HDCA, last node in the previous screenshot) learns a hierarchical linear model of the time-domain signal of the trials. It can be thought of as a hierarchical version of linear discriminant analysis (LDA): first, a classifier is applied to each time slice of the segments individually, which reduces the data to a single dimension per time slice, and then a second classifier is applied to this set of one-dimensional features. It performs well on segments at full resolution, i.e., with little reduction of the channels and of the sampling rate. A shrinkage regularization is used for each of the two stages (shrinkage_within and shrinkage_across, 0.05 and 0.07). The class_weights are the prior probabilities of the two classes that the model assumes (0.5 each here). Its probabilistic property is enabled, so for each trial it outputs the probability of each class, instead of just the class label. It trains on the calibration data, and then classifies the trials of the streaming data. See the documentation of the node for more.

Output

Includes: LSL Output, Inspect Packet, Set Axis Values, Bar Plot, Measure Loss and Select Range

The results of the classification are used in several ways (see the screenshot of the whole pipeline):

  • An LSL Output node sends them out over LSL, as a stream called OutStream-2 (with a stream_type of Control), to be picked up by another pipeline or application in real time.
  • An Inspect Packet node shows the numerical values in a window (with the trials as rows, row_axis of instance, and the classes as columns).

Bar plot of the predictions

  • A Set Axis Values node names the two classes correct and incorrect, and a Bar Plot shows the predicted probability of each (with the y_limits set to 0 to 1).

Measurement of the error

  • The Measure Loss node compares the predictions with the true target values of the trials (the TargetValue field, cond_field) and computes the misclassification rate (MCR, the loss_metrics property), accumulated over the trials as they come in. The result is put on a statistic axis (the stats-axis output_format), from which a Select Range node selects the value item, which is shown on a second Bar Plot.