shivams496/Pulsewatcher
๐ซ PulseWatcher โ ECG Anomaly Detection System
<p align="center"> <img src="https://img.shields.io/badge/Model-LSTM%20Autoencoder-00d4ff?style=for-the-badge&logo=pytorch&logoColor=white"/> <img src="https://img.shields.io/badge/Dataset-MIT--BIH%20Arrhythmia-00e87a?style=for-the-badge"/> <img src="https://img.shields.io/badge/Framework-PyTorch-ee4c2c?style=for-the-badge&logo=pytorch&logoColor=white"/> <img src="https://img.shields.io/badge/Dashboard-Streamlit-ff4b4b?style=for-the-badge&logo=streamlit&logoColor=white"/> <img src="https://img.shields.io/badge/Deployed-Hugging%20Face-ffb020?style=for-the-badge&logo=huggingface&logoColor=white"/> <img src="https://img.shields.io/badge/Type-Unsupervised-b060ff?style=for-the-badge"/> </p>
<p align="center"> Real-time ECG anomaly detection using an LSTM Autoencoder trained <strong>exclusively on normal heartbeats</strong>.<br/> Anomalies are detected through reconstruction error โ no anomaly labels required during training. </p>
<p align="center"> <a href="https://huggingface.co/spaces/shivams496/Pulsewatcher"> <img src="https://img.shields.io/badge/๐%20Live%20Demo-Hugging%20Face%20Spaces-yellow?style=for-the-badge"/> </a> </p>
How It Works
MIT-BIH Dataset (PhysioNet)
โ
โผ
Beat Segmentation + Normalisation
(187 timesteps, min-max normalised per beat)
โ
โผ
LSTM Encoder โ Latent Space (64 units) โ LSTM Decoder
โ
โผ
Reconstruction Error (MSE per timestep)
โ
โผ
Threshold (82nd percentile of normal train errors)
โ
โโโโโโดโโโโโ
โผ โผ
NORMAL ANOMALYKey insight: The model never sees anomalous beats during training. It learns only what normal looks like. When an anomalous beat is fed in, reconstruction fails โ the error spike is the detection signal.
Results
Threshold Sensitivity Analysis
Sweeping to the 82nd percentile yields Precision = 89%, Recall = 71%, F1 = 0.795 โ a 24% improvement in anomaly detection at the cost of 7% more false positives. The optimal threshold is a clinical decision, not a model decision.
Model Comparison (same test set, same threshold logic)
LSTM wins across every metric. ECG beats are temporal sequences with known structure (PโQRSโT) โ LSTM's sequential memory is a natural fit. CNN treats the signal as a spatial pattern and loses the temporal ordering that makes arrhythmias detectable.
Dashboard Features
Model Details
- Architecture: LSTM Autoencoder (encoder-decoder)
- Hidden units: 64
- Parameters: ~110K
- Input shape:
(N, 187, 1) - Training: Normal beats only (unsupervised)
- Inference: <5ms per beat on CPU
Why unsupervised? In clinical settings, labelled anomaly data is rare and expensive. Training on normal beats only allows the model to generalise to anomaly types it has never seen, as long as they deviate from normal morphology.
Project Structure
ecg-anomaly-detection/
โโโ dashboard/
โ โโโ app.py # Streamlit dashboard (all features)
โโโ src/
โ โโโ model.py # LSTM Autoencoder architecture
โ โโโ train.py # Training loop
โ โโโ evaluate.py # Threshold sweep + metrics
โ โโโ cnn_autoencoder.py # CNN baseline model
โ โโโ benchmark.py # Model comparison script
โโโ models/
โ โโโ lstm_autoencoder.pt # Trained LSTM weights
โ โโโ threshold.npy # Optimal threshold (82nd percentile)
โ โโโ train_errors.npy # Training reconstruction errors
โ โโโ metrics.json # Live metrics for dashboard
โ โโโ benchmark.json # LSTM vs CNN comparison results
โโโ requirements.txt
โโโ README.mdSetup
git clone https://github.com/shivams496/ecg-anomaly-detection
cd ecg-anomaly-detection
pip install -r requirements.txtStart the dashboard:
streamlit run dashboard/app.pyRun threshold sweep (optional โ results already saved):
python -m src.evaluateRun model benchmark:
python -m src.benchmarkDataset
MIT-BIH Arrhythmia Dataset via PhysioNet.
48 recordings ยท 30 min each ยท 360 Hz ยท 47 patients ยท 15+ arrhythmia classes
Known Limitations
- Single-beat classification only โ rhythm-level disorders (e.g. AFib) require inter-beat interval (RR) analysis, not just beat morphology.
- MIT-BIH distribution โ trained on Holter monitor recordings. Signals from different device types or electrode placements may need threshold recalibration.
- Unusual-but-benign beats โ patients with non-standard baseline ECGs may generate higher reconstruction errors without true pathology.
- Not FDA/CE approved โ for research and educational use only.
Future Work
- [ ] RR interval analysis for rhythm-level anomaly detection (AFib, heart block)
- [ ] Multi-lead ECG support (currently single-lead)
- [ ] Patient-specific threshold calibration
- [ ] HL7/FHIR integration for hospital data pipelines
- [ ] Transformer autoencoder ablation study
Interview Q&A
"Why LSTM over Transformer?" MIT-BIH beats are 187 timesteps โ short structured sequences with known temporal order (PโQRSโT). Transformers excel at long sequences with long-range dependencies. LSTM captures within-beat temporal structure naturally. The benchmark confirms it: LSTM F1 79% vs CNN F1 41%.
"Why is recall not higher?" The threshold is a clinical tuning parameter. At the 95th percentile, precision is 95% but recall is 44%. At the 82nd percentile, precision drops to 89% but recall rises to 71% (+27%). The right operating point depends on the clinical context โ a general ward might prefer high precision; a cardiac ICU might prefer high recall.
"How would this run in a hospital?" <5ms inference on CPU. Dockerised. Threshold adjustable per cohort without retraining. The Hugging Face deployment proves it runs without a GPU.
Tech Stack
Python ยท PyTorch ยท Streamlit ยท NumPy ยท SciPy ยท scikit-learn ยท ReportLab ยท Pandas ยท WFDB
B.Tech Final Year Project โ ECG Anomaly Detection
