Motor Imagery BCI
Overview
This application is a Brain-Computer Interface (BCI) system designed to classify motor imagery tasks, including left hand, right hand, and neutral state, using EEG data. The system connects to Explore Pro device via the Lab Streaming Layer (LSL) protocol, processes the EEG signals, and classifies the data using machine learning models, including a Transformer-based neural network.
The application supports real-time classification. It includes tools for data collection, preprocessing, model training, and evaluation. The code for the application can be found in our explorepy/examples section on github.
Features
-
Real-Time EEG Data Acquisition: Connects to an EEG device via LSL for real-time data streaming.
-
Motor Imagery Classification: Classifies EEG data into three states: left hand movement, right hand movement, and neutral state.
-
Transformer Model: Implements a Transformer-based neural network for EEG signal classification.
-
Multiple Classifiers: Supports Linear Discriminant Analysis (LDA), Support Vector Machines (SVM), Random Forest (RF), and the custom Transformer model.
-
Data Preprocessing: Includes bandpass filtering for mu and beta bands and feature extraction using band power.
-
Model Training and Evaluation: Allows training and evaluation of multiple classifiers with visualization of results.
-
Real-Time BCI Operation: Runs in continuous mode for real-time classification and control.
Requirements
Usage
Collect Calibration Data
To train the classifiers, you need to collect calibration data. Run the following command and follow the on-screen instructions:
python main.py
Choose option 1 to train and compare all classifiers.
The system will prompt you to perform left hand, right hand, and neutral state tasks.
File Structure
motor-imagery-bci/
├── models/ # Saved classifier models
├── dataset/ # Saved calibration datasets
├── main.py # Main application script
├── README.md # This file
Classifiers
The application supports the following classifiers:
-
Linear Discriminant Analysis (LDA)
-
Support Vector Machine (SVM)
-
Random Forest (RF)
-
Transformer Model
Customizing the Transformer Model
The Transformer model is defined in the EEGTransformer class. You can customize the following parameters:
-
input_dim: Input feature dimension. -
num_classes: Number of output classes.Default:
3 -
d_model: Dimension of the model.Default:
64 -
nhead: Number of attention heads.Default:
8 -
num_layers: Number of Transformer encoder layers.Default:
4 -
dropout: Dropout rate.Default:
0.2
Example:
model = EEGTransformer(input_dim=64, num_classes=3, d_model=128, nhead=8, num_layers=6, dropout=0.3)
Data Preprocessing
The EEG data is preprocessed as follows:
-
Bandpass Filtering: Applied to extract mu (
8-12 Hz) and beta (16-24 Hz) bands. -
Feature Extraction: Band power is calculated for each frequency band.
-
Dataset Preparation: Features and labels are saved in a CSV file for training.
Real-Time Operation
In real-time mode, the application:
-
Continuously collects EEG data from the LSL stream.
-
Applies preprocessing and feature extraction.
-
Classifies the data using the selected model.
-
Displays the detected state, either left hand, right hand, or neutral, and the certainty level.
Troubleshooting
LSL Stream Not Found
-
Ensure your device is properly connected and streaming data.
-
Verify the stream name in the
MotorImageryBCIclass initialization.
Future Improvements
While the current system is functional, several enhancements can improve its accuracy, real-time performance, and adaptability.
Advanced Deep Learning Models
-
Replace traditional classifiers, including LDA, SVM, and RF, with more powerful architectures like EEGNet, ConvLSTM, or Graph Neural Networks (GNNs).
-
Optimize the Transformer model by using EEG-specialized architectures such as TS-Transformer or EEGFormer for better time-series representation.
Enhanced Feature Extraction
-
Incorporate Common Spatial Patterns (CSP) to improve the separation of motor imagery classes.
-
Use Riemannian geometry-based approaches to extract more robust spatial features.