# surya-teja-kancharla/ecg-decision-support-system architecture

> Architecture diagram of the GitHub repository surya-teja-kancharla/ecg-decision-support-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/surya-teja-kancharla/ecg-decision-support-system
- Repository: https://github.com/surya-teja-kancharla/ecg-decision-support-system

## Overview

#### Purpose and Entry Points
ECG-DSS is a PyTorch/Streamlit clinical decision-support prototype for multi-label classification of variable-length 12-lead ECGs into nine arrhythmia-related classes. Its operational entry points are the Streamlit inference dashboard at `app.py` and the training runner at `run_phase3.py`. A trained model checkpoint is supplied as `ecg_model_v1.pth`.

#### Input Data Boundary
The system consumes preprocessed pickle ECG records rather than raw device streams. Demo inputs live in `samples/`; representative full-data layout is under `data/comparision_data/preprocessed/`. Raw PhysioNet-style ECG artifacts (`.hea` metadata and `.mat` waveforms) are represented under `data/comparision_data/raw/`, forming the acquisition-to-preprocessing boundary.

#### Preprocessing and Dataset Layer
Signal normalization, resampling/duration standardization, and serialized-record preparation are centralized in `src/preprocessing.py`. `src/dataset.py` provides lazy-loading dataset access, avoiding loading the full ECG corpus into memory and supporting scalable training/inference. The expected model input is normalized 12-lead, 250 Hz, 60-second signal data with multi-hot targets during training.

#### Model and Inference
`src/model.py` defines the ECG classifier: a 1D residual CNN for cross-lead temporal features, followed by attention pooling and independent sigmoid outputs for multi-label probabilities. `ecg_model_v1.pth` is loaded for deployment inference. The dashboard in `app.py` accepts `.pk` uploads, simulates streaming with sliding windows, performs device-aware inference, applies class-specific decision thresholds, and presents decision-support explanations.

#### Training and Imbalance Strategy
`run_phase3.py` orchestrates the model-training phase using the components in `src/train.py`, `src/dataset.py`, and `src/model.py`. Training targets independent labels rather than a mutually exclusive class, with focal-loss-oriented imbalance handling and PyTorch device selection across CUDA, Apple MPS, or CPU. `src/augmentation.py` provides ECG-oriented augmentation support at the training boundary.

#### Evaluation and Decision Calibration
`src/evaluate.py` owns multi-label evaluation concerns: Hamming loss, macro/micro F1, per-class ROC-AUC, and threshold-sensitive prediction quality. Per-label threshold optimization is an explicit post-model decision layer, separating probability representation from clinical alerting behavior. `split_train_into_train_val.py` supports creation of train/validation partitions; validation analysis is also captured in `notebooks/03_phase3_evaluation.ipynb` and `notebooks/04_phase4_validation.ipynb`.

#### Ensemble and Runtime Dependencies
`src/ensemble.py` is the extension point for combining model predictions, although the supplied deployment artifact is `ecg_model_v1.pth`. Primary runtime infrastructure is Python, PyTorch, Streamlit, NumPy/scikit-learn, and WFDB; dependencies are declared in `requirements.txt`. External data originates from PhysioNet/CPSC-style ECG sources and community-preprocessed distributions, while the deployed UI is intended for Streamlit hosting rather than a separate backend service.

Main entry points: [`src/model.py`](https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/model.py) (ECG classifier), [`app.py`](https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/app.py) (Streamlit dashboard), [`src/evaluate.py`](https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/evaluate.py) (Multi-label evaluation), [`src/train.py`](https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/train.py) (Training loop)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["ECG Data"]
  node_sources(("PhysioNet/CPSC sources<br/>external ECG source"))
  node_raw_ecg["Raw ECG artifacts<br/>waveforms + headers"]
  node_preprocessing["Preprocessing<br/>signal preparation<br/>[preprocessing.py]"]
  node_preprocessed[("Preprocessed ECG records<br/>pickle corpus")]
end

subgraph group_training["Training &amp; Validation"]
  node_dataset["Lazy ECG dataset<br/>PyTorch dataset<br/>[dataset.py]"]
  node_augmentation["ECG augmentation<br/>training transform<br/>[augmentation.py]"]
  node_splitter["Train/validation split<br/>partition utility"]
  node_training_runner["Phase 3 runner<br/>training entry point<br/>[run_phase3.py]"]
  node_trainer["Training loop<br/>PyTorch optimization<br/>[train.py]"]
end

subgraph group_model["Model &amp; Decisions"]
  node_classifier["ECG classifier<br/>1D residual CNN<br/>[model.py]"]
  node_evaluation["Multi-label evaluation<br/>metrics + calibration<br/>[evaluate.py]"]
  node_ensemble["Prediction ensemble<br/>extension point<br/>[ensemble.py]"]
end

subgraph group_runtime["Deployment Runtime"]
  node_samples["Demo ECG inputs<br/>pickle samples"]
  node_checkpoint["Deployment checkpoint<br/>trained model artifact<br/>[ecg_model_v1.pth]"]
  node_dashboard["Streamlit dashboard<br/>inference entry point<br/>[app.py]"]
  node_clinical_output(("Clinical decision support<br/>thresholded alerts"))
end

node_sources -->|"acquired as"| node_raw_ecg
node_raw_ecg -->|"prepared by"| node_preprocessing
node_preprocessing -->|"serializes"| node_preprocessed
node_preprocessed -->|"partitioned for"| node_splitter
node_preprocessed -->|"lazy-loaded by"| node_dataset
node_splitter -->|"provides splits"| node_training_runner
node_training_runner -->|"orchestrates"| node_trainer
node_dataset -->|"training records"| node_augmentation
node_augmentation -->|"augmented batches"| node_trainer
node_trainer -->|"optimizes"| node_classifier
node_classifier -->|"validation probabilities"| node_evaluation
node_evaluation -.->|"calibrates deployment model"| node_checkpoint
node_classifier -->|"trained weights"| node_checkpoint
node_samples -->|"demo input"| node_dashboard
node_checkpoint -->|"loaded for inference"| node_dashboard
node_dashboard -->|"sliding-window inference"| node_classifier
node_classifier -.->|"prediction extension"| node_ensemble
node_evaluation -->|"per-label thresholds"| node_dashboard
node_dashboard -->|"presents"| node_clinical_output

click node_preprocessing "https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/preprocessing.py"
click node_dataset "https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/dataset.py"
click node_augmentation "https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/augmentation.py"
click node_splitter "https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/split_train_into_train_val.py"
click node_training_runner "https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/run_phase3.py"
click node_trainer "https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/train.py"
click node_classifier "https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/model.py"
click node_evaluation "https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/evaluate.py"
click node_ensemble "https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/ensemble.py"
click node_checkpoint "https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/ecg_model_v1.pth"
click node_dashboard "https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/app.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_sources,node_raw_ecg,node_preprocessing,node_preprocessed toneBlue
class node_dataset,node_augmentation,node_splitter,node_training_runner,node_trainer toneAmber
class node_classifier,node_evaluation,node_ensemble toneMint
class node_samples,node_checkpoint,node_dashboard,node_clinical_output toneRose
```

## Components

### ECG Data

- **PhysioNet/CPSC sources**: external ECG source
- **Raw ECG artifacts**: waveforms + headers
- [Preprocessing](https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/preprocessing.py): signal preparation · Normalizes and standardizes ECG records. · `src/preprocessing.py`
- **Preprocessed ECG records**: pickle corpus · Normalized 12-lead, 250 Hz, 60-second records.

### Training & Validation

- [Lazy ECG dataset](https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/dataset.py): PyTorch dataset · Loads records without materializing the full corpus. · `src/dataset.py`
- [ECG augmentation](https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/augmentation.py): training transform · `src/augmentation.py`
- [Train/validation split](https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/split_train_into_train_val.py): partition utility · `split_train_into_train_val.py`
- [Phase 3 runner](https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/run_phase3.py): training entry point · `run_phase3.py`
- [Training loop](https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/train.py): PyTorch optimization · Uses focal-loss-oriented multi-label training and device selection. · `src/train.py`

### Model & Decisions

- [ECG classifier](https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/model.py): 1D residual CNN · Cross-lead temporal features, attention pooling, and sigmoid labels. · `src/model.py`
- [Multi-label evaluation](https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/evaluate.py): metrics + calibration · Computes F1, Hamming loss, ROC-AUC, and per-label thresholds. · `src/evaluate.py`
- [Prediction ensemble](https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/src/ensemble.py): extension point · `src/ensemble.py`

### Deployment Runtime

- **Demo ECG inputs**: pickle samples
- [Deployment checkpoint](https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/ecg_model_v1.pth): trained model artifact · `ecg_model_v1.pth`
- [Streamlit dashboard](https://github.com/surya-teja-kancharla/ecg-decision-support-system/blob/main/app.py): inference entry point · Uploads .pk ECGs, simulates sliding windows, and explains alerts. · `app.py`
- **Clinical decision support**: thresholded alerts · Class-specific thresholds turn probabilities into displayed guidance.

## Connections

- PhysioNet/CPSC sources → Raw ECG artifacts: acquired as
- Raw ECG artifacts → Preprocessing: prepared by
- Preprocessing → Preprocessed ECG records: serializes
- Preprocessed ECG records → Train/validation split: partitioned for
- Preprocessed ECG records → Lazy ECG dataset: lazy-loaded by
- Train/validation split → Phase 3 runner: provides splits
- Phase 3 runner → Training loop: orchestrates
- Lazy ECG dataset → ECG augmentation: training records
- ECG augmentation → Training loop: augmented batches
- Training loop → ECG classifier: optimizes
- ECG classifier → Multi-label evaluation: validation probabilities
- Multi-label evaluation → Deployment checkpoint: calibrates deployment model
- ECG classifier → Deployment checkpoint: trained weights
- Demo ECG inputs → Streamlit dashboard: demo input
- Deployment checkpoint → Streamlit dashboard: loaded for inference
- Streamlit dashboard → ECG classifier: sliding-window inference
- ECG classifier → Prediction ensemble: prediction extension
- Multi-label evaluation → Streamlit dashboard: per-label thresholds
- Streamlit dashboard → Clinical decision support: presents
