Portfolio Adaptation β This repository is a modernized, sanitized reimplementation of a research collaboration between the AI Center for Cardiology at Sheba Medical Center and Philips Research North America. All real patient data has been removed; a synthetic data generator is provided for demonstration.
Automated detection of cardiac pacemakers from 12-lead ECG signals using a Convolutional Transformer Network (CTN). The model identifies pacing spikes and classifies ECG recordings as paced vs. non-paced.
- Pacemaker patients require specialized ECG interpretation
- Pacing spikes can mimic or obscure pathological patterns
- Automated detection supports triage and reduces specialist workload
- Originally developed for the PhysioNet 2020 Challenge framework
| Metric | Performance |
|---|---|
| ROC-AUC | >0.95 on held-out test folds |
| Challenge Metric | Competitive with PhysioNet 2020 top entries |
| Cross-validation | 5-fold stratified, patient-level splits |
Input: 12-lead ECG (12 Γ 7500 samples @ 500Hz = 15 seconds)
β
βΌ
βββββββββββββββββββββββββββββββββββββββ
β Conv1d Encoder (6 layers) β
β 12 β 128 β 256 β 256 β 256 β 256 β
β Strides: 3,3,2,2,1,1 (Γ20 downsample) β
βββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββ
β Transformer Encoder (8 layers) β
β d_model=256, 8 heads, d_ff=2048 β
β Positional Encoding + Dropout(0.1) β
βββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββ
β Global Pooling β FC(256β64) β FC β
β Multi-label output (27 SNOMED codes) β
βββββββββββββββββββββββββββββββββββββββ
Key Design Choices:
- Conv1d front-end learns translation-invariant ECG features
- Transformer captures long-range temporal dependencies
- QRS-aligned windowing option for beat-level attention
- Over-sampling handles severe class imbalance (paced βͺ non-paced)
- PhysioNet 2020 Challenge β Public ECG databases
- PTB-XL β Large public 12-lead ECG database
- Sheba Medical Center β Private annotated cohort (used with permission)
# Generate demo dataset
python src/generate_synthetic_data.py ./demo_data 50Generates WFDB-format records (.hea + .dat) with realistic:
- P/QRS/T wave morphology
- Baseline wander, muscle noise, powerline interference
- Pacemaker spikes (2ms, high-amplitude, pre-R peak)
- Configurable paced/non-paced ratio
- Python 3.10+
- CUDA 11.8+ (for GPU training) or CPU-only
- 8GB+ RAM
# Clone
git clone https://github.com/yazanus94-dot/ECG-Pacemaker-Detection.git
cd ECG-Pacemaker-Detection
# Create environment
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Generate synthetic demo data
python src/generate_synthetic_data.py ./demo_data 30# Build
docker build -t ecg-pacemaker .
# Run with GPU
docker run --gpus all -it ecg-pacemaker
# Verify environment
docker run --gpus all ecg-pacemaker# Train on synthetic data (demo)
python src/train_model.py ./demo_data ./output_models
# Original training script (requires real data)
python src/train_12ECG_classifier.py /path/to/physionet_data ./resultsTraining Features:
- 5-fold stratified cross-validation (pre-defined folds)
- Noam learning rate schedule (Transformer-style)
- Early stopping (patience=10, monitor=val AUROC)
- TensorBoard logging
- Class-weighted BCE loss for imbalance
output_models/
βββ ctn/
βββ fold_0/
β βββ ctn.tar # Best model checkpoint
β βββ thrs.txt # Optimal thresholds
β βββ log_fold_0.csv # Training logs
β βββ val_0_results.csv
βββ fold_1/ ...
βββ ...
# Single record inference
python src/driver.py ./model_checkpoint ./input_record_dir ./output_predictions
# Batch inference via run_12ECG_classifier
python src/run_12ECG_classifier.py ./model ./test_data ./predictionsOutput Format (PhysioNet Challenge):
# Classes
10370003,426783006
# Labels (binary)
0,1
# Scores (probabilities)
0.123,0.987ECG-Pacemaker-Detection/
βββ src/
β βββ model.py # CTN architecture
β βββ train_12ECG_classifier.py # Main training pipeline
β βββ train_model.py # Training entry point
β βββ driver.py # Inference driver
β βββ dataloader.py # QRS-aligned & padding datasets
β βββ utils.py # Config, constants, data loading
β βββ optimizer.py # Noam optimizer
β βββ over_sampling.py # SMOTE-style over-sampling
β βββ under_sampling.py # Under-sampling
β βββ generate_synthetic_data.py # π Synthetic ECG generator
β βββ StratifiedKFold.py # Custom CV splits
β βββ run_12ECG_classifier.py # Challenge submission format
β βββ eval/ # PhysioNet evaluation metrics
β βββ feats/ # Feature engineering modules
βββ demo_data/ # Generated synthetic data (gitignored)
βββ requirements.txt # Modern dependencies
βββ Dockerfile # CUDA 12.1 + Python 3.10
βββ LICENSE # BSD-2-Clause + attribution
βββ README.md
- Resampling β 500 Hz uniform
- Bandpass filter β 3β45 Hz FIR (order=150)
- Polarity correction β Per-lead template median
- Normalization β Per-sequence [-1, 1]
- Windowing β 15s windows (QRS-aligned or random)
| Technique | Implementation |
|---|---|
| Over-sampling | Replicate minority class records in training folds |
| Class weights | BCEWithLogitsLoss(pos_weight=...) |
| Threshold optimization | Per-class sweep on validation set |
- Primary: Macro AUROC, Challenge Metric (PhysioNet 2020)
- Secondary: FΞ² (Ξ²=2), GΞ², Geometric Mean
- Per-class: AUROC, AUPRC, Sensitivity, Specificity
| Limitation | Mitigation |
|---|---|
| Synthetic data β real clinical distribution | Use for architecture validation only |
| Legacy code comments in Hebrew | Preserved for attribution |
| 27 SNOMED codes but only 2 target classes | Full multi-label ready, demo uses binary |
| CUDA 10.1 β 12.1 migration untested on original weights | Retrain with modern stack |
Code: BSD 2-Clause (PhysioNet 2020) β see LICENSE
Research Collaboration:
- Sheba Medical Center β AI Center for Cardiology
- Philips Research North America
Original Researchers:
Dvora Rothman, Elisheva Tufik, Sigal Sina, Gal Goshen, Raizy Kellerman, Michal Cohen-Shelly, Avi Sabbag (Sheba) Sara Mariani, Annamalai Natarajan, Asif Rahman, Yale Chang, Jonathan Rubin, Shruti Vij, Gregory Boverman (Philips)
Data Sources:
- PhysioNet 2020 Challenge (BSD-2-Clause)
- PTB-XL Database
- Sheba Medical Center private cohort (not redistributed)
Conference: Israel Cardiology Association Conference, May 2025 (poster)
This repository was adapted for portfolio demonstration by Yazan Al-Jabari (@yazanus94-dot).
Changes from Original:
- β Removed all patient data (CSV, feature zips, demographics)
- β Added synthetic ECG data generator
- β Modernized dependencies (PyTorch 2.1+, NumPy 1.24+, Python 3.10+)
- β Updated Dockerfile (CUDA 12.1, Ubuntu 22.04)
- β Added comprehensive documentation
- β Preserved all original attribution and licensing
Disclaimer: This is a technical demonstration of the ML pipeline architecture. It does not contain real patient data and should not be used for clinical decision-making.
- PhysioNet 2020 Challenge: Classification of 12-lead ECGs
- PTB-XL: A large publicly available ECG dataset
- "Attention is All You Need" β Transformer architecture
- ECG signal processing with
wfdbandbiosppy
Built with β€οΈ for AI in Healthcare β Portfolio Project by Yazan Al-Jabari