This project involves the preprocessing, feature extraction, and classification of EEG signals using machine learning. The pipeline is designed to collect EEG data from a Neurosity EEG headset, preprocess the data to remove noise and artifacts, extract relevant features, and classify the signals using a Support Vector Machine (SVM) classifier.
The project is structured as follows:
collector_trial.py: Script to collect raw EEG data from the Neurosity headset.preprocessing.py: Contains methods for preprocessing the raw EEG data.feature_extraction.py: Contains methods for extracting features from the preprocessed EEG data.SVM.py: Script for training and evaluating an SVM classifier on the extracted features.windowing.py: Contains methods for windowing the EEG data.config/settings.py: Contains configuration parameters for the project.files/unfiltered/: Directory to store collected raw EEG data..env: Contains credentials for the Neurosity EEG headset (not included in the repository for security reasons).
-
Clone the repository:
git clone https://github.com/yourusername/eeg-pipeline.git cd eeg-pipeline -
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the required packages:
pip install -r requirements.txt
-
Set up environment variables:
Create a
.envfile in the root directory with the following content:NEUROSITY_DEVICE_ID=your_device_id NEUROSITY_EMAIL=your_email NEUROSITY_PASSWORD=your_password
To collect raw EEG data using the Neurosity headset, run the collector_trial.py script:
python collector_trial.pyYou will need to ensure your headset is on and streaming before running this. You also need to define the name of the JSON file you wish to store the resultant data in along with the time duration you want to record. This will save the collected data in the files/unfiltered/ directory.
The preprocessing steps are defined in the preprocessing.py file. Each preprocessing step is a separate method:
bandpass_filternotch_filterbaseline_correctionremove_artifacts_icaepochingzscore_normalizationdownsample
The preprocess_eeg method applies these steps based on the configuration specified in config/settings.py.
The feature extraction methods are defined in the feature_extraction.py file. Features include:
- Power Spectral Density (PSD)
- Band Powers
- Peak Frequency
- Spectral Entropy
- Mean and Median Frequency
- Bandwidth
- Hjorth Parameters
- Spectral Flatness
- Statistical Features
The extract_features_from_file and extract_features_from_files methods handle feature extraction from single or multiple files.
The SVM.py script trains and evaluates an SVM classifier on the extracted features:
python SVM.pyThis script combines the extracted features, splits the data into training and testing sets, trains the SVM classifier, and evaluates its performance.
The configuration parameters are defined in config/settings.py:
-
Neurosity Credentials:
NEUROSITY_DEVICE_ID = os.getenv("NEUROSITY_DEVICE_ID") NEUROSITY_EMAIL = os.getenv("NEUROSITY_EMAIL") NEUROSITY_PASSWORD = os.getenv("NEUROSITY_PASSWORD")
-
Windowing Parameters:
WINDOW_LEN = 1 OVERLAP = 0.5 EPOCH_TIME = 0.0625 SAMPLING_FREQ = 256 DOWNSAMPLE_FREQ = 128
-
Electrode Configuration:
channel_position_config = { 'CP3': 0, 'C3': 1, 'F5': 2, 'PO3': 3, 'PO4': 4, 'F6': 5, 'C4': 6, 'CP4': 7 } electrode_config = { 'CP3': True, 'C3': True, 'F5': True, 'PO3': True, 'PO4': True, 'F6': True, 'C4': True, 'CP4': True } -
Preprocessing Configuration:
preprocessing_config = { 'bandpass': True, 'notch': True, 'baseline_correction': False, 'z_score': True, 'ICA_artefacts': True, 'downsample': False } -
Feature Extraction Configuration:
feature_config = { "psd": True, "band_powers": True, "peak_frequency": True, "spectral_entropy": True, "mean_median_frequency": True, "bandwidth": True, "hjorth_parameters": True, "spectral_flatness": True, "statistical_features": True }
Please feel free to fork and use this code for your own EEG-ML projects! Here is a link to Neurosity's homepage: https://neurosity.co