Keywords: ECG classification, structured morphology-rhythm interfaces, Mamba / SSM, fixed-seed ROCKET-family features, calibration, perturbation robustness, mapped external evaluation.
This is the Official PyTorch Implementation of the paper: "ECG-RAMBA: A Protocol-Faithful Evaluation of Structured Morphology-Rhythm ECG Modeling" Hai Duong Nguyen, Xuan-The Tran (2025)
π Paper (ArXiv) | π€ Model Weights | π Experiments
Pretrained checkpoints are provided here:
Expected files:
fold1_best.ptfold2_best.ptfold3_best.ptfold4_best.ptfold5_best.pt
Recommended: verify SHA256 checksums if you mirror weights to ensure integrity.
- [2026-01-10]: Code and pre-trained weights released.
- [2025-12-30]: Paper available on ArXiv.
Deep learning has achieved strong performance for electrocardiogram (ECG) classification within individual datasets, yet behavior across heterogeneous acquisition settings remains difficult to characterize. This repository tests whether explicit morphology and rhythm interfaces provide benefits that survive matched comparisons in discrimination, calibration, perturbation robustness, and target-label adaptation.
We evaluate ECG-RAMBA, a framework with explicit morphology and rhythm interfaces that are re-integrated through context-aware fusion. The interface is an architectural hypothesis; representation probes and CKA are audits, not proof of mechanistic separation.
Key Contributions:
- Structured Interfaces: Combines a fixed-seed ROCKET-family MAX+PPV morphology transform, checkpoint-compatible rhythm/statistical conditioning, and long-range contextual modeling with Bi-Mamba.
- Matched Controls: Uses same-fold learned comparators, structured ablations, paired record-level uncertainty estimates, and explicit claim boundaries.
-
Pooling Audit: Freezes power-mean pooling at
$Q=3$ and reports sensitivity against alternative aggregation rules rather than claiming universal optimality. - External and Adaptation Audits: Reports dataset-specific mapped tasks, zero-target-label inference, score calibration, and frozen-encoder head adaptation as distinct protocols.
ECG-RAMBA exposes morphology and rhythm as explicit model interfaces:
- Morphology Stream: Uses a fixed-seed ROCKET-family ternary-kernel transform with MAX+PPV summaries. It is not the canonical MiniRocket algorithm.
- Rhythm Stream: Uses the rhythm/statistical inputs implemented by the frozen checkpoint contract. Reserved amplitude slots remain zero; the released checkpoints do not implement a full RMSSD/SDNN/LF-HF feature set.
Leverages State Space Models (SSM) to model long-range dependencies across 5000-timepoint whole-signal ECGs with linear computational complexity
Uses a numerically stable power-mean operator with the frozen operating point
The external protocol records model behavior without expanding it into a deployment claim:
- Zero-Target-Label Inference: Evaluates frozen weights before any target-label use.
-
Fixed Threshold (
$\tau=0.5$ ): No dataset-specific threshold tuning required. - Group-Aware Protocol: Uses dataset-specific group splits and separates score calibration from frozen-encoder head adaptation.
git clone https://github.com/BrianNguyen29/ECG-RAMBA.git
cd ECG-RAMBA
pip install -r requirements.txt
# Inference with pre-trained weights (ensure models/fold1_best.pt exists)
python scripts/eval_zeroshot.py --ckpt models/fold1_best.pt| Component | Requirement |
|---|---|
| Python | 3.10+ |
| CUDA | 11.8+ (for mamba-ssm) |
| GPU VRAM | 10GB+ (20GB recommended) |
# 1. Clone repository
git clone https://github.com/BrianNguyen29/ECG-RAMBA.git
cd ECG-RAMBA
# 2. Install dependencies
pip install -r requirements.txtNote: Full ECG-RAMBA inference requires a compatible
mamba-ssmruntime and is run on CUDA in the supported revision workflow. Metric recomputation, protocol gates, paired ledgers, and final evidence generation can run on CPU after prediction artifacts are available.
This repository supports standard ECG benchmarks. Download from PhysioNet and organize as follows:
data/
βββ chapman/ # ~45k records (.mat and .hea files)
βββ cpsc2021/ # Annotation-aligned AF/AFL mapped-window evaluation
βββ ptbxl/ # Mapped-superclass external evaluation
- Chapman-Shaoxing (large-scale 12-lead ECG)
- CPSC 2021 (annotation-aligned AF/AFL mapped-window task)
- PTB-XL (multi-label ECG classification)
See data/README.md for preprocessing steps and file structure.
python scripts/train.py- Config:
configs/config.py - Logs:
reports/logs/ - Checkpoints:
models/fold*_best.pt
# Out-of-Fold evaluation (Chapman)
python scripts/eval_oof.py
# Legacy script name; external outputs require dataset-specific protocol gates
python scripts/eval_zeroshot.pyCPU runtimes are supported for the post-inference audit stages documented in EXPERIMENTS.md. Do not use the command below as a substitute for the CUDA/Mamba inference contract.
python -u scripts/revision/47_forensic_notebook_audit.py --canonical-root reports/revisionFor detailed reproduction instructions, see EXPERIMENTS.md.
The reviewed Notebook 00-07 sequence can be executed from Windows through WSL2 and the official Google Colab CLI. The launcher separates CPU feature preparation from A100 inference, preserves executed notebooks and logs, reuses canonical Drive artifacts, and leaves failed sessions active for diagnosis.
See
docs/revision_plan/colab_cli_pipeline.md
for authentication, staged execution, resume, and cleanup commands.
ECG-RAMBA/
βββ configs/ # Centralized configuration
βββ data/ # Dataset storage (Git-ignored)
βββ models/ # Pre-trained weights & checkpoints
βββ notebooks/ # Demo & exploratory notebooks
βββ reports/ # Figures and experimental logs
βββ scripts/ # Training and evaluation scripts
βββ src/ # Core source code
β βββ model.py # ECGRamba
β βββ layers.py # BiMamba, Perceiver, Fusion blocks
β βββ features.py # Fixed-seed ROCKET-family and rhythm/statistical features
β βββ data_loader.py # Chapman data pipeline
β βββ utils.py # Metrics, losses, EMA
βββ web_app/ # Research visualization and inference demo
The repository includes a React/FastAPI research interface for waveform inspection and model-output visualization. It is a software demonstration, not a validated medical device or clinical decision-support system.
-
Waveform Workbench:
- 12-Lead Visualization: High-fidelity rendering (500Hz) with medical grid system (5mm/1mm).
- Focus Analysis: Interactive zoom, pan, and single-lead detailed inspection.
-
Digital Calipers: Precision measurement tools for
$\Delta t$ (ms) and$\Delta V$ (mV).
-
AI Integration:
- Interactive Inference: Mamba2-backed model-output visualization for uploaded research records.
- Saliency View: Gradient-based maps for exploratory inspection of model sensitivity on the waveform.
- Confidence Scoring: Probability distribution over 4 diagnostic classes (Normal, AFib, GSVT, SB).
-
Reporting & Workflow:
- PDF Export: One-click generation of research-session summaries for uploaded files.
-
Patient Queue: Drag-and-drop file upload (
.mat,.csv,.json) and history tracking.
cd web_app
# backend
cd backend
pip install -r requirements.txt
uvicorn main:app --reload
# frontend
cd ../frontend
npm install
npm run devIf you use this code or model in your research, please cite:
@article{nguyen2025ecg,
title={ECG-RAMBA: A Protocol-Faithful Evaluation of Structured Morphology-Rhythm ECG Modeling},
author={Nguyen, Hai Duong and Tran, Xuan-The},
journal={arXiv preprint arXiv:2512.23347},
year={2025},
url={https://arxiv.org/abs/2512.23347}
}This project is licensed under the MIT License. See LICENSE for details.
We welcome contributions! Please read CONTRIBUTING.md for guidelines.
We thank the PhysioNet team for hosting the Chapman-Shaoxing, CPSC-2021, and PTB-XL datasets.

