A complete machine-learning pipeline for physiological stress detection using the WESAD (Wearable Stress and Affect Detection) dataset.
This project classifies three physiological states — Baseline, Stress, and Amusement — from multi-modal wearable sensor signals:
| Signal | Device | Sampling Rate |
|---|---|---|
| ECG (Electrocardiogram) | RespiBAN (chest) | 700 Hz |
| EDA (Electrodermal Activity) | Empatica E4 (wrist) | 4 Hz |
| BVP (Blood Volume Pulse) | Empatica E4 (wrist) | 64 Hz |
Reference: Schmidt, P. et al. (2018). WESAD: A Multimodal Dataset for Wearable Stress and Affect Detection. ACM ICMI.
Raw Signals → Preprocessing → Feature Extraction → LOSO Cross-Validation → ONNX Export
- Dependency setup — auto-installs XGBoost, skl2onnx, onnxruntime
- Dataset loading — synthetic WESAD-faithful simulator (no files needed) or real
.pklloader - Signal preprocessing — zero-phase Butterworth filters (bandpass ECG/BVP, lowpass EDA)
- Feature extraction — 60 s windows, 50% overlap; statistical + HRV + EDA features
- EDA — signal plots, feature distributions, correlation heatmap
- Model training — XGBoost, MLP, Random Forest with Leave-One-Subject-Out (LOSO) cross-validation
- Evaluation — accuracy, F1-macro, ROC-AUC, confusion matrix
- Feature importance — Gini importances (tree models) / permutation importance (MLP)
- Real-time inference — confidence-gated prediction (threshold = 0.60; below →
"inconclusive") - ONNX export — cross-platform deployment with numerical consistency check
Click the Open in Colab badge above. All dependencies are auto-installed in the first cell.
git clone https://github.com/MithraKL/stress-detection-wesad.git
cd stress-detection-wesad
pip install -r requirements.txt
jupyter notebook stress_detection_WESAD_colab.ipynbThe notebook ships with a synthetic simulator that runs without any files. To use the real dataset:
- Request access at WESAD @ UCI
- Download and extract to a folder named
WESAD/ - In the notebook, uncomment the
load_subject()block and setWESAD_ROOT
| Model | Accuracy | F1-Macro | ROC-AUC |
|---|---|---|---|
| XGBoost | ~0.92 | ~0.91 | ~0.98 |
| Random Forest | ~0.90 | ~0.89 | ~0.97 |
| MLP | ~0.88 | ~0.87 | ~0.96 |
Results will vary with the real WESAD dataset and actual LOSO splits.
All figures and model files are saved to /content/ in Colab:
| File | Description |
|---|---|
signal_overview.png |
ECG / EDA / BVP with condition overlay |
filter_effect.png |
Raw vs filtered signal comparison |
feature_distributions.png |
Per-class feature histograms |
correlation_heatmap.png |
Feature correlation matrix |
boxplots.png |
Feature distributions by condition |
model_comparison.png |
Accuracy / F1 / AUC bar chart |
confusion_auc.png |
Confusion matrix + ROC curves |
feature_importance.png |
Top features (Gini / permutation) |
stress_model.onnx |
Exported best model for deployment |
- Replace
simulate_subject()with real WESAD.pklloader - Add PyTorch LSTM for raw-signal end-to-end learning
- Wrap
best_modelin a FastAPI/predictendpoint - Add MLflow experiment tracking
- Add SHAP for per-prediction feature attribution
- Personalise model per subject with transfer learning
See requirements.txt. Core dependencies:
numpy,pandas,scipyscikit-learnxgboostmatplotlib,seabornskl2onnx,onnxruntime,onnxmltools
MIT — see LICENSE.