This project focuses on exploring/analyzing ECG data with literature-backed methods with the MIT-BIH Arrhythmia Database (PhysioNet). There is also focus on making these scripts more production ready in terms of how other teams can use the code developed.
NOTE: This project is now concluded. For a deep dive into methods and insights please visit this article.
Project Information
Setup and Usage
- 0.5 - 30 Hz Filtering
- Notch filtering
- Detrend and baseline correction
- R-peak Detection
- Write modules for Pam-Tompson QRS complex detection
- Calibrate peaks after detection
- Validate detected peaks with annotations as ground truth
- Rahul Kher (2019) Signal Processing Techniques for Removing Noise from ECG Signals. J Biomed Eng 1: 1-9
- https://martager.github.io/bbsig/ecg-preprocessing/
- Extract R peak waveforms and other key features
- Build table with labels from annotations
- Extract RR interval features: pre/post RR, delta RR, RR ratio, local RR mean, deviation from local mean
- Extract QRS morphology features: Q-R interval, R-S interval, Q-R amplitude, R-S amplitude
- Extract wavelet features and use them with XGBoost
- Logistic regression with R waveforms only: Macro F1 = 0.41
- Logistic regression with waveforms + RR features: Macro F1 = 0.44
- Gradient boosting with waveforms + RR features: Macro F1 = 0.529
- SMOTE oversampling: Macro F1 = 0.520 (dropped: slowed training, no improvement)
- Feature ablation: waves + RR + QRS outperforms subsets; QRS features add value
- XGBoost with waveforms + RR + QRS features: Macro F1 = 0.577
- XGBoost hyperparameter optimization: randomized search with patient-wise CV
- RR irregularity feature (coefficient of variation of RR intervals)
- MLflow experiment tracking: all runs tracked with metrics, parameters, and artifacts for reproducibility and comparison across experiments
- Config-driven experiments: new experiments require only a config.yaml entry, no code changes
- Label encoding and artifact logging: LabelEncoder saved as MLflow artifact for consistent eval
- Finalized config file for ablation use in app
- Create initial relevant visuals for N vs A vs V beats:
- Waveform shape
- pre_rr vs post_rr intervals scatter distribution
- rr_irregularity violin plot
- QRS beat variation violin
- Change of rr_irregularity over time
- Feature correlation matrix to show redundancy
No Python, no uv, no dependency setup required :just Docker.
- Install Docker Desktop for your platform
- Pull the image:
docker pull theopensourceguy/arrhythmia_ml- Download the raw data from PhysioNet and extract it into
./data/rawin whatever folder you'll be running commands from
That's it. See Using Docker for how to run scripts.
-
Install the following before going further:
-
Clone the repository:
git clone https://github.com/ShekharNarayanan/arrhythmia_ml.git- Navigate to the repository:
cd arrhythmia_ml- Install dependencies:
uv syncAlways run Docker commands from the same directory. Your data should be in
./data/rawand MLflow runs will be saved to./mlrunsin that directory.
NOTE: Experiment names are specified in config.yaml
Train a model:
docker run -v ./data:/app/data -v ./mlruns:/app/mlruns theopensourceguy/arrhythmia_ml uv run python -m scripts.train --exp <exp_name>Evaluate results:
docker run -v ./data:/app/data -v ./mlruns:/app/mlruns theopensourceguy/arrhythmia_ml uv run python -m scripts.eval --exp <exp_name>Track experiments in MLflow UI:
docker run -v ./mlruns:/app/mlruns -p 5000:5000 theopensourceguy/arrhythmia_ml uv run mlflow ui --host 0.0.0.0Then open http://localhost:5000 in your browser.
Validate peak detection:
docker run -v ./data:/app/data -v ./peak_detection_plots:/app/peak_detection_plots theopensourceguy/arrhythmia_ml uv run python validate_detected_peaks.pyVisualize features:
docker run -v ./data:/app/data -v ./feature_plots:/app/feature_plots theopensourceguy/arrhythmia_ml uv run python visualize_features.py-
Make changes in
config.yamlto specify your settings (beats to classify, preprocessing parameters, ML experiments etc.) -
Train a model:
uv run python -m scripts.train --exp <insert exp from config>This will create the mlruns folder. MLflow will track your experiments using it.
- Evaluate results:
uv run python -m scripts.eval --exp <insert exp from config>- Track experiments in MLflow UI:
uv run mlflow ui- Validate peak detection:
uv run python validate_detected_peaks.py- Visualize features:
uv run python visualize_features.pyFor any questions, you can contact me on LinkedIn.