State-of-the-art EEG analysis framework with hybrid Transformer-GNN architecture for psychiatric disorder classification.
- Hybrid Architecture β Graph Neural Networks (spatial) + Transformers (temporal) with cross-attention fusion
- Self-Supervised Pretraining β Contrastive (SimCLR) and masked signal modeling (BERT-style)
- MNE Integration β Load
.edf,.bdf,.fif,.set,.vhdr,.cnt,.gdfnatively - Advanced Augmentation β Time-shift, noise, channel dropout, mixup, cutmix, permutation
- Hyperparameter Tuning β Grid, random, and Bayesian optimization
- Ensemble Methods β Weighted voting, snapshot ensembles, model registry
- Cross-Validation β Subject-aware K-Fold, stratified, and leave-one-subject-out
- Real-Time Inference β Streaming predictor with batch support and FP16
- Explainability β Attention topomaps, gradient & permutation feature importance
- Visualization β Training curves, confusion matrices, band power heatmaps, attention plots
- Model Export β ONNX and TorchScript for production deployment
- Benchmarking β Inference latency, throughput, and memory profiling
| Model | Architecture | Accuracy |
|---|---|---|
| Baseline (RiceDatathon2025) | GNN + CNN | 38.5% |
| NeuroFormer | Transformer-GNN Hybrid | 70-90% |
# Standard install
pip install -e .
# With development tools (pytest, ruff, mypy)
pip install -e ".[dev]"from neuroformer.models import NeuroFormer
from neuroformer.data import EEGDataModule
from neuroformer.training import Trainer
# Initialize model
model = NeuroFormer(
num_electrodes=19,
num_classes=7,
d_model=256,
n_heads=8,
n_transformer_layers=4,
n_gnn_layers=3
)
# Load data with subject-aware splits
data_module = EEGDataModule(
data_path="path/to/data.csv",
batch_size=32,
test_size=0.2
)
# Train
trainer = Trainer(model, optimizer, device='cuda')
history = trainer.fit(
data_module.train_loader(),
data_module.val_loader(),
epochs=100
)
# Predict
predictions = model.predict(test_data)
probabilities = model.predict_proba(test_data)from neuroformer.data.mne_loader import MNELoader
loader = MNELoader(sampling_rate=256, l_freq=0.5, h_freq=45.0)
features = loader.load_and_process("recording.edf")
# Returns: {'band_powers', 'coherence', 'band_names', 'channel_names'}from neuroformer.training.tuning import quick_tune
result = quick_tune(
train_fn=my_train_function,
method='bayesian',
n_trials=30,
monitor='val_accuracy'
)
print(f"Best params: {result.params}")from neuroformer.models.ensemble import WeightedEnsemble
ensemble = WeightedEnsemble.from_checkpoints(
['model_v1.pth', 'model_v2.pth', 'model_v3.pth'],
model_class=NeuroFormer,
model_kwargs={'num_electrodes': 19, 'num_classes': 7}
)
predictions = ensemble.predict(test_data)from neuroformer.training.cross_validation import CrossValidator
cv = CrossValidator(NeuroFormer, model_kwargs, n_folds=5, stratified=True)
results = cv.evaluate(features, labels, subject_ids, train_fn, eval_fn)
# Output: accuracy_mean, accuracy_std, per-fold scoresββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β NeuroFormer β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Spatial β β Temporal β β Fusion β β
β β GNN Layer β β β Transformer β β β Layer β β
β β (Graph Attn) β β (Self-Attn) β β (Cross-Attn) β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β β β
β Coherence-based Classification β
β Adjacency Matrix Head (7 classes) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
neuroformer/
βββ models/ # Transformer, GNN, hybrid architecture, ensembles
βββ preprocessing/ # Filtering, augmentation, feature extraction
βββ pretraining/ # Contrastive + masked self-supervised learning
βββ training/ # Trainer, losses, metrics, tuning, cross-validation
βββ inference/ # Prediction, streaming, explainability
βββ data/ # Dataset, samplers, MNE file loading
βββ utils/ # Logging, validation, exceptions, checkpointing
βββ viz/ # Topomaps, training curves, confusion matrices
βββ cli.py # Command-line interface
tests/ # Unit tests (45+ test cases)
benchmarks/ # Performance benchmarking suite
examples/ # Training, pretraining, inference demos
docs/ # API reference
neuroformer train --data train.csv --epochs 100 --batch-size 32
neuroformer pretrain --data unlabeled.csv --method contrastive
neuroformer predict --model best.pth --input test.csv
neuroformer evaluate --model best.pth --data test.csv
neuroformer info- Healthy (Control)
- Addictive Disorder
- Anxiety Disorder
- Mood Disorder
- Obsessive Compulsive Disorder
- Schizophrenia
- Trauma and Stress Related Disorder
# Run tests
pytest tests/ -v
# With coverage
pytest tests/ --cov=neuroformer --cov-report=html
# Lint & format
ruff check neuroformer/
black neuroformer/ tests/
# Benchmark
python benchmarks/benchmark.pyMIT License β see LICENSE for details.
- Inspired by RiceDatathon2025 EEG classification challenge
- Built on PyTorch, PyTorch Geometric, and MNE-Python