This repository contains research code for the paper:
PRIME: Personalized Real-time Inference of Momentary Excitability from Human EEG
by Haxel*, Ahola, Kapoor, Ziemann, and Macke† (2025).
PRIME (Personalized Real-time Inference of Momentary Excitability) is a deep neural network that predicts cortical excitability, modeled as TMS-evoked potential (TEP) amplitudes, from raw EEG signals in real-time. The framework incorporates transfer learning and continual adaptation to automatically identify personalized brain-state biomarkers for individualized optimization of stimulation timing. Unlike MEP-based approaches that are limited to motor cortex, PRIME enables brain state-dependent stimulation across any cortical region by targeting cortical excitability without spinal confounds.
To run the experiments, first install all requirements. We recommend creating a conda environment. A GPU is recommended for optimal performance.
git clone [email protected]:user/PRIME.git
cd PRIME
conda create --name prime python=3.11
conda activate prime
pip install -e . # install from requirements.txt
# optional: install jupyter notebook
pip install jupyter
PRIME was evaluated using TMS-EEG data from the European Research Council-funded ConnectToBrain project. The data comprise recordings from 50 healthy adults with concurrent EEG and EMG recordings during single-pulse TMS targeting the left primary motor cortex.
Note: The dataset is not publicly available but can be requested from the corresponding authors for research purposes, subject to ethical approval and data agreements.
The core functionality is implemented across several key modules:
train_transfer.py
: Main training functions for population-level pretraining and online adaptationtta_wrapper.py
: Test-time adaptation wrapper implementing the continual learning frameworkmodels/
: Neural network architectures including PRIME and benchmark modelspreprocessing/
: EEG/TMS data preprocessing pipelines and TEP extractioninterpretability.py
: Neurophysiological feature analysis tools
# Core PRIME training phases
phases = {
"PRE-ZS": "Population pretraining only (zero-shot)",
"PRE-CAL": "Pretraining + subject calibration",
"PRE-FT": "Complete PRIME framework with continual adaptation",
"SS-CAL": "Subject-specific calibration only",
"SS-FT": "Subject-specific training from scratch"
}
- Population-level Pretraining: Train on subset of subjects using 2-fold cross-validation
- Subject-specific Calibration: Adapt pretrained model using first 100 trials from test subject
- Online Continual Adaptation: Deploy with trial-by-trial updates using sliding window of recent trials
The figures/
directory contains scripts to generate analysis data:
create_csv_fig1cd.py
: Hierarchical learning performance comparisoncreate_csv_fig3b.py
: Architecture comparison analysiscreate_csv_fig4.py
: Hyperparameter sensitivity analysiscreate_csv_figs1.py
: Comprehensive framework evaluationcreate_csvs_fig5_figs2.py
: Neurophysiological interpretability analysis
Run these scripts to generate the necessary CSV files in the figures/
directory.
Use the Jupyter notebooks to create figures from the generated CSV data:
fig1cd.ipynb
: Performance across training conditionsfig3b.ipynb
: Model architecture comparisonsfig4.ipynb
: Optimization and generalizability analysisfig5_figs2.ipynb
: Feature interpretability and brain mapsfigs1.ipynb
: Supplementary latency analysis
- CPU: Multi-core recommended (8+ cores optimal)
- GPU: Consumer GPU (RTX 2080Ti or equivalent) for real-time performance
- Memory: 8GB+ RAM for data processing
The preprocessing workflow simulates real-time constraints:
- Calibration Phase: Establish subject-specific parameters from initial trials
- Application Phase: Apply fixed parameters to subsequent trials
- TEP Extraction: Current dipole modeling for single-trial amplitudes
- Real-time Compatibility: All steps optimized for online deployment
@article{haxel_ahola2025prime,
title={PRIME: Personalized Real-time Inference of Momentary Excitability from Human EEG},
author={Lisa Haxel and Oskari Ahola and Jaivardhan Kapoor and Ulf Ziemann and Jakob H. Macke},
journal={},
year={2025}
}
Please open a GitHub issue for questions, or send an email to [email protected].
This research was conducted under ethical approval from the University of Tübingen (810/2021BO2) and Helsinki University Hospital (HUS/1198/2016). De-identified data is available from the corresponding authors upon reasonable request, subject to ethical approval and data agreements.