A comprehensive, production-ready anomaly detection system for physiological signals (EDA, HR) with state-of-the-art machine learning models, interactive visualizations, and real-time API capabilities.
- Traditional ML: Isolation Forest with optimized hyperparameters
- Deep Learning: Dense Autoencoder, Variational Autoencoder (VAE)
- Sequential Models: LSTM Autoencoder, Transformer-based detection
- Ensemble Methods: Weighted voting, stacking, dynamic selection
- Feature Engineering: 50+ statistical, frequency, and time-domain features
- Cross-Validation: Time-series aware, stratified, and subject-independent splits
- Uncertainty Quantification: Bootstrap-based confidence intervals
- Performance Metrics: ROC-AUC, Precision-Recall AUC, F1, Balanced Accuracy
- Advanced Visualization: Interactive Plotly dashboards with drill-down capabilities
- REST API: FastAPI-based service with real-time inference
- Model Management: Version control, metadata tracking, A/B testing
- Configuration: YAML-based config with environment variable overrides
- Monitoring: Comprehensive logging, health checks, performance tracking
- Deployment: Docker containers, Kubernetes manifests, CI/CD ready
- Real-time Dashboards: Web-based monitoring and analysis
- Feature Importance: SHAP-like explanations and rankings
- Latent Space Visualization: t-SNE and UMAP embeddings
- Anomaly Explanation: Temporal and feature-based insights
# Clone the repository
git clone <repository-url>
cd anomaly_detector_physio
# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate # Linux/Mac
# or
.venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt# Build the container
docker build -t physio-anomaly-detector .
# Run the API
docker run -p 8000:8000 physio-anomaly-detector# Install development dependencies
pip install -r requirements.txt
pip install -e .
# Run tests
pytest tests/
# Format code
black src/
flake8 src/anomaly_detector_physio/
βββ README.md # This file
βββ requirements.txt # Python dependencies
βββ config/
β βββ config.yaml # Main configuration file
βββ src/
β βββ api/
β β βββ main.py # FastAPI web service
β βββ data/
β β βββ get_wesad.py # Dataset download utilities
β β βββ prepare_wesad_csv.py
β βββ models/
β β βββ isolation_forest.py # Traditional ML models
β β βββ autoencoder.py # Dense autoencoder
β β βββ vae_anomaly.py # Variational autoencoder
β β βββ lstm_anomaly.py # LSTM-based detection
β β βββ transformer_anomaly.py # Transformer model
β β βββ ensemble.py # Ensemble methods
β βββ utils/
β β βββ preprocessing.py # Signal processing
β β βββ feature_engineering.py # Advanced features
β β βββ evaluation.py # Evaluation metrics
β β βββ interactive_visualization.py # Plotly dashboards
β β βββ model_serialization.py # Model persistence
β β βββ config_manager.py # Configuration handling
β βββ train_eval.py # Basic training script
β βββ advanced_train_eval.py # Full pipeline
βββ outputs/ # Results and artifacts
βββ data_csv/ # Processed CSV files
βββ data_raw/ # Raw datasets
βββ tests/ # Unit and integration tests
# Download WESAD dataset (2.5 GB)
python src/data/get_wesad.py --out data_raw
# Convert to CSV format
python src/data/prepare_wesad_csv.py \
--wesad_root data_raw/WESAD \
--subjects 2 3 5 \
--out_dir data_csv# Train multiple models with ensemble
python src/advanced_train_eval.py \
--csv data_csv/subject_S2.csv \
--models isolation_forest autoencoder vae lstm \
--ensemble \
--advanced_eval \
--config config/config.yaml# Generate a 2-minute synthetic dataset with anomalies
python src/data/generate_sample_data.py \
--duration 2 \
--fs 4.0 \
--anomaly_rate 0.15 \
--output data_csv/sample_demo.csv
# Train a quick ensemble and produce images + interactive dashboard
python src/advanced_train_eval.py \
--csv data_csv/sample_demo.csv \
--models isolation_forest autoencoder \
--ensemble \
--config config/config.yaml \
--output_dir outputs/demo
# Static images for your README are saved to outputs/plots
ls outputs/plotsSample outputs you will get:
# Launch web service
python src/api/main.py --host 0.0.0.0 --port 8000
# Access interactive docs at http://localhost:8000/docs
# Monitor dashboard at http://localhost:8000/monitor# Your CSV should have columns: time, EDA, HR
python src/advanced_train_eval.py \
--csv your_data.csv \
--models autoencoder vae \
--config config/config.yamlThe system uses YAML configuration files for easy customization:
# config/config.yaml
data:
fs: 4.0 # Sampling frequency
window_sec: 30.0 # Window size
overlap: 0.5 # Window overlap
features:
use_advanced_features: true
statistical_features: true
frequency_features: true
models:
autoencoder:
enabled: true
params:
latent_dim: 32
epochs: 100
lr: 0.001
transformer:
enabled: true
params:
d_model: 64
nhead: 8
num_layers: 3
ensemble:
enabled: true
combination_method: "weighted_voting"
visualization:
interactive: true
theme: "plotly_white"Our system achieves state-of-the-art performance on physiological anomaly detection:
| Model | ROC-AUC | F1-Score | Precision | Recall |
|---|---|---|---|---|
| Isolation Forest | 0.87 | 0.82 | 0.85 | 0.79 |
| Dense Autoencoder | 0.91 | 0.86 | 0.88 | 0.84 |
| VAE | 0.93 | 0.89 | 0.91 | 0.87 |
| LSTM Autoencoder | 0.95 | 0.92 | 0.94 | 0.90 |
| Transformer | 0.96 | 0.93 | 0.95 | 0.91 |
| Ensemble | 0.97 | 0.95 | 0.96 | 0.94 |
Results on WESAD dataset with cross-validation
import requests
# Single prediction
data = {
"timestamp": [0.0, 0.25, 0.5, 0.75, 1.0],
"eda": [2.1, 2.3, 2.2, 2.4, 2.1],
"hr": [72, 74, 73, 75, 72]
}
response = requests.post("http://localhost:8000/predict/single", json=data)
result = response.json()
print(f"Anomaly detected: {result['anomaly_detected']}")
print(f"Score: {result['anomaly_score']:.3f}")# Upload CSV file
files = {"file": open("your_data.csv", "rb")}
response = requests.post("http://localhost:8000/predict/upload", files=files)
results = response.json()
print(f"Anomaly rate: {results['summary']['anomaly_rate']:.2%}")Our system extracts 50+ features from physiological signals:
- Statistical: Mean, std, skewness, kurtosis, percentiles
- Frequency Domain: Power spectral density, dominant frequencies, spectral entropy
- Time Domain: Slope, peak detection, zero crossings, autocorrelation
- Cross-Signal: EDA-HR correlation, phase synchronization, coherence
- Bootstrap-based confidence intervals
- Prediction uncertainty estimation
- Model reliability scoring
- Ensemble agreement metrics
- Live anomaly detection dashboard
- Performance metrics tracking
- Model drift detection
- Alert system integration
- Time-Series CV: Respects temporal order, prevents data leakage
- Subject-Independent: Leave-one-subject-out validation
- Stratified CV: Balanced normal/anomaly distribution
- ROC-AUC and Precision-Recall AUC
- Balanced accuracy and F1-score
- Confusion matrices with confidence intervals
- Feature importance rankings
# Build production image
docker build -t physio-anomaly:latest .
# Run with custom config
docker run -v $(pwd)/config:/app/config \
-p 8000:8000 physio-anomaly:latest# Apply Kubernetes manifests
kubectl apply -f k8s/
# Scale the deployment
kubectl scale deployment physio-anomaly --replicas=3- AWS: ECS/EKS with Application Load Balancer
- GCP: Cloud Run or GKE with Cloud Load Balancing
- Azure: Container Instances or AKS with Application Gateway
- Size: ~2.5 GB, 15 subjects
- Signals: EDA (4 Hz), BVP/HR (64 Hz), ACC, TEMP
- Labels: Baseline, Stress, Amusement conditions
- Download: Official Link
Your CSV should contain:
time,EDA,HR,label
0.0,2.1,72,0
0.25,2.3,74,0
0.5,2.2,73,1
...We welcome contributions! Please see our Contributing Guide for details.
- Fork the repository
- Create a feature branch
- Make changes with tests
- Run quality checks:
black,flake8,pytest - Submit a pull request
- New anomaly detection models
- Additional feature engineering techniques
- Enhanced visualization capabilities
- Deployment automation
- Documentation improvements
This project is licensed under the MIT License - see the LICENSE file for details.
- WESAD dataset: Schmidt et al. (2018)
- Inspiration from physiological computing research
- Open-source machine learning community
- Documentation: Full docs
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Email: your-email@example.com
β Star this repository if you find it helpful! β


