Skip to content

Latest commit

Β 

History

9 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 Advanced Physiological Anomaly Detection

Python 3.8+ License: MIT Code style: black

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.

πŸš€ Key Features

πŸ”¬ Advanced Models

  • 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

πŸ“Š Comprehensive Evaluation

  • 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

🏭 Production Ready

  • 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

πŸ“ˆ Interactive Analysis

  • 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

πŸ“¦ Installation

Quick Start (Recommended)

# 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

Docker Installation

# Build the container
docker build -t physio-anomaly-detector .

# Run the API
docker run -p 8000:8000 physio-anomaly-detector

Development Setup

# Install development dependencies
pip install -r requirements.txt
pip install -e .

# Run tests
pytest tests/

# Format code
black src/
flake8 src/

πŸ—‚οΈ Project Structure

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

🎯 Quick Start Guide

1. Download and Prepare Data

# 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

2. Train Models with Advanced Features

# 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

2a. Instant Demo (Generates outputs and images)

# 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/plots

Sample outputs you will get:

Anomalies Over Time

ROC and PR Curves

Confusion Matrix

3. Start Real-time API

# 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

4. Use Your Own Data

# Your CSV should have columns: time, EDA, HR
python src/advanced_train_eval.py \
  --csv your_data.csv \
  --models autoencoder vae \
  --config config/config.yaml

πŸ”§ Configuration

The 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"

πŸ“Š Model Performance

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

🌐 API Usage

Real-time Prediction

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}")

Batch Processing

# 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%}")

πŸ“ˆ Advanced Features

Feature Engineering

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

Uncertainty Quantification

  • Bootstrap-based confidence intervals
  • Prediction uncertainty estimation
  • Model reliability scoring
  • Ensemble agreement metrics

Real-time Monitoring

  • Live anomaly detection dashboard
  • Performance metrics tracking
  • Model drift detection
  • Alert system integration

πŸ§ͺ Evaluation and Validation

Cross-Validation Strategies

  • Time-Series CV: Respects temporal order, prevents data leakage
  • Subject-Independent: Leave-one-subject-out validation
  • Stratified CV: Balanced normal/anomaly distribution

Performance Metrics

  • ROC-AUC and Precision-Recall AUC
  • Balanced accuracy and F1-score
  • Confusion matrices with confidence intervals
  • Feature importance rankings

πŸš€ Deployment

Docker Deployment

# 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

Kubernetes Deployment

# Apply Kubernetes manifests
kubectl apply -f k8s/

# Scale the deployment
kubectl scale deployment physio-anomaly --replicas=3

Cloud Deployment

  • AWS: ECS/EKS with Application Load Balancer
  • GCP: Cloud Run or GKE with Cloud Load Balancing
  • Azure: Container Instances or AKS with Application Gateway

πŸ“š Datasets

WESAD (Wearable Stress and Affect Detection)

  • Size: ~2.5 GB, 15 subjects
  • Signals: EDA (4 Hz), BVP/HR (64 Hz), ACC, TEMP
  • Labels: Baseline, Stress, Amusement conditions
  • Download: Official Link

Custom Data Format

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

πŸ§‘β€πŸ’» Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Workflow

  1. Fork the repository
  2. Create a feature branch
  3. Make changes with tests
  4. Run quality checks: black, flake8, pytest
  5. Submit a pull request

Areas for Contribution

  • New anomaly detection models
  • Additional feature engineering techniques
  • Enhanced visualization capabilities
  • Deployment automation
  • Documentation improvements

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • WESAD dataset: Schmidt et al. (2018)
  • Inspiration from physiological computing research
  • Open-source machine learning community

πŸ“ž Support


⭐ Star this repository if you find it helpful! ⭐

About

No description, website, or topics provided.

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages