Skip to content

MithraKL/stress-detection-wesad

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stress Detection — WESAD End-to-End Pipeline

Open In Colab

A complete machine-learning pipeline for physiological stress detection using the WESAD (Wearable Stress and Affect Detection) dataset.


Overview

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.


Pipeline

Raw Signals → Preprocessing → Feature Extraction → LOSO Cross-Validation → ONNX Export
  1. Dependency setup — auto-installs XGBoost, skl2onnx, onnxruntime
  2. Dataset loading — synthetic WESAD-faithful simulator (no files needed) or real .pkl loader
  3. Signal preprocessing — zero-phase Butterworth filters (bandpass ECG/BVP, lowpass EDA)
  4. Feature extraction — 60 s windows, 50% overlap; statistical + HRV + EDA features
  5. EDA — signal plots, feature distributions, correlation heatmap
  6. Model training — XGBoost, MLP, Random Forest with Leave-One-Subject-Out (LOSO) cross-validation
  7. Evaluation — accuracy, F1-macro, ROC-AUC, confusion matrix
  8. Feature importance — Gini importances (tree models) / permutation importance (MLP)
  9. Real-time inference — confidence-gated prediction (threshold = 0.60; below → "inconclusive")
  10. ONNX export — cross-platform deployment with numerical consistency check

Quick Start

Option 1 — Google Colab (recommended)

Click the Open in Colab badge above. All dependencies are auto-installed in the first cell.

Option 2 — Local

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.ipynb

Using the Real WESAD Dataset

The notebook ships with a synthetic simulator that runs without any files. To use the real dataset:

  1. Request access at WESAD @ UCI
  2. Download and extract to a folder named WESAD/
  3. In the notebook, uncomment the load_subject() block and set WESAD_ROOT

Results (Simulated Data)

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.


Outputs

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

Next Steps

  • Replace simulate_subject() with real WESAD .pkl loader
  • Add PyTorch LSTM for raw-signal end-to-end learning
  • Wrap best_model in a FastAPI /predict endpoint
  • Add MLflow experiment tracking
  • Add SHAP for per-prediction feature attribution
  • Personalise model per subject with transfer learning

Requirements

See requirements.txt. Core dependencies:

  • numpy, pandas, scipy
  • scikit-learn
  • xgboost
  • matplotlib, seaborn
  • skl2onnx, onnxruntime, onnxmltools

License

MIT — see LICENSE.

About

End-to-end physiological stress detection pipeline using WESAD dataset — multi-modal signal processing (ECG, EDA, BVP), feature extraction, XGBoost/MLP/Random Forest with LOSO cross-validation, and ONNX export.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors