# zhangshanwei-mm/patchecg architecture

> Architecture diagram of the GitHub repository zhangshanwei-mm/patchecg, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zhangshanwei-mm/patchecg
- Repository: https://github.com/zhangshanwei-mm/patchecg

## Overview

PatchECG is a research codebase for multi-label arrhythmia classification from digitized ECG signals, especially signals with missing leads and differing layouts. Its main workflow trains and evaluates masked, patch-based models; documented extensions use projection and ECGFounder. The repository also bundles comparison approaches for classification, forecasting, and imputation. The graph preserves those branches, but sampled source is incomplete: baseline connections rely on README descriptions and indexed references where noted, not on inspection of every implementation.

Main entry points: [`src/code_v5.py`](https://github.com/zhangshanwei-mm/patchecg/blob/main/src/code_v5.py) (Patch classification), [`baseline/TimeXer/exp/exp_my_classification.py`](https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/TimeXer/exp/exp_my_classification.py) (TimeXer experiments), [`baseline/TimeXer/data_provider/data_factory.py`](https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/TimeXer/data_provider/data_factory.py) (TimeXer data), [`src/train_model.py`](https://github.com/zhangshanwei-mm/patchecg/blob/main/src/train_model.py) (Training entry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_patch["PatchECG workflow"]
  node_train["Training entry<br/>[train_model.py]"]
  node_ecg_data["ECG data handling<br/>[helper_code.py]"]
  node_masking["Masking and augmentation<br/>[new_utils.py]"]
  node_patch_model["Patch classification<br/>[code_v5.py]"]
  node_evaluation["Model evaluation<br/>[eva_model.py]"]
  node_projection["Projection variant"]
  node_founder["ECGFounder variant<br/>[code_v5.py]"]
end

subgraph group_models["Model components"]
  node_transformer["Transformer layers<br/>[my_transformer.py]"]
  node_vit["Vision transformer<br/>[vit_transformer.py]"]
  node_training_utils["Training utilities<br/>[utils.py]"]
end

subgraph group_baselines["Comparison baselines"]
  node_medformer["Medformer classification<br/>[my_medformer.py]"]
  node_medformer_exp["Medformer experiment"]
  node_timexer_run["TimeXer runner<br/>[run.py]"]
  node_timexer_exp["TimeXer experiments"]
  node_timexer_data["TimeXer data<br/>[data_factory.py]"]
  node_timexer_loaders["Time-series loaders<br/>[data_loader.py]"]
  node_timexer_models["TimeXer models"]
  node_timexer_layers["Time-series layers"]
  node_timexer_utils["Time-series utilities"]
  node_simmtm["SimMTM classification<br/>[main.py]"]
end

subgraph group_imputation["Imputation approaches"]
  node_saits["SAITS imputation<br/>[run_models.py]"]
  node_saits_data["SAITS data preparation"]
end

node_user(("Researcher"))

node_user -->|"starts training"| node_train
node_train -.->|"trains model"| node_patch_model
node_patch_model -->|"uses ECG helpers"| node_ecg_data
node_patch_model -->|"uses utilities"| node_masking
node_patch_model -->|"imports"| node_transformer
node_patch_model -->|"imports"| node_training_utils
node_transformer -->|"imports"| node_vit
node_user -->|"evaluates model"| node_evaluation
node_user -->|"selects variant"| node_projection
node_user -->|"runs variant"| node_founder
node_user -->|"runs classification"| node_medformer_exp
node_medformer_exp -->|"uses model"| node_medformer
node_user -->|"starts experiment"| node_timexer_run
node_timexer_run -->|"dispatches task"| node_timexer_exp
node_timexer_exp -->|"loads data"| node_timexer_data
node_timexer_data -->|"selects loaders"| node_timexer_loaders
node_timexer_exp -.->|"builds model"| node_timexer_models
node_timexer_models -.->|"uses layers"| node_timexer_layers
node_timexer_data -.->|"uses utilities"| node_timexer_utils
node_user -->|"runs pretraining"| node_simmtm
node_user -->|"generates data"| node_saits_data
node_user -->|"runs imputation"| node_saits
node_saits_data -.->|"provides datasets"| node_saits

click node_train "https://github.com/zhangshanwei-mm/patchecg/blob/main/src/train_model.py"
click node_ecg_data "https://github.com/zhangshanwei-mm/patchecg/blob/main/src/helper_code.py"
click node_masking "https://github.com/zhangshanwei-mm/patchecg/blob/main/src/new_utils.py"
click node_patch_model "https://github.com/zhangshanwei-mm/patchecg/blob/main/src/code_v5.py"
click node_transformer "https://github.com/zhangshanwei-mm/patchecg/blob/main/src/my_transformer.py"
click node_vit "https://github.com/zhangshanwei-mm/patchecg/blob/main/src/vit_transformer.py"
click node_training_utils "https://github.com/zhangshanwei-mm/patchecg/blob/main/src/utils.py"
click node_evaluation "https://github.com/zhangshanwei-mm/patchecg/blob/main/src/eva_model.py"
click node_projection "https://github.com/zhangshanwei-mm/patchecg/blob/main/src/code_v5_projection.py"
click node_founder "https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/ECGFounder/PatchEncoder/src/code_v5.py"
click node_medformer "https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/Medformer/my_medformer.py"
click node_medformer_exp "https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/Medformer/exp/exp_my_classification.py"
click node_timexer_run "https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/TimeXer/run.py"
click node_timexer_exp "https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/TimeXer/exp/exp_my_classification.py"
click node_timexer_data "https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/TimeXer/data_provider/data_factory.py"
click node_timexer_loaders "https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/TimeXer/data_provider/data_loader.py"
click node_timexer_models "https://github.com/zhangshanwei-mm/patchecg/tree/main/baseline/TimeXer/models"
click node_timexer_layers "https://github.com/zhangshanwei-mm/patchecg/tree/main/baseline/TimeXer/layers"
click node_timexer_utils "https://github.com/zhangshanwei-mm/patchecg/tree/main/baseline/TimeXer/utils"
click node_simmtm "https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/SimMTM/SimMTM_Classification/code/main.py"
click node_saits "https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/SAITS-main/run_models.py"
click node_saits_data "https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/SAITS-main/dataset_generating_scripts/gene_ptb_xl_dataset_saits.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_train,node_ecg_data,node_masking,node_patch_model,node_evaluation,node_projection,node_founder toneBlue
class node_transformer,node_vit,node_training_utils toneAmber
class node_medformer,node_medformer_exp,node_timexer_run,node_timexer_exp,node_timexer_data,node_timexer_loaders,node_timexer_models,node_timexer_layers,node_timexer_utils,node_simmtm toneMint
class node_saits,node_saits_data toneRose
class node_user toneIndigo
```

## Components

### PatchECG workflow

- [Training entry](https://github.com/zhangshanwei-mm/patchecg/blob/main/src/train_model.py): `src/train_model.py`
- [ECG data handling](https://github.com/zhangshanwei-mm/patchecg/blob/main/src/helper_code.py): `src/helper_code.py`
- [Masking and augmentation](https://github.com/zhangshanwei-mm/patchecg/blob/main/src/new_utils.py): `src/new_utils.py`
- [Patch classification](https://github.com/zhangshanwei-mm/patchecg/blob/main/src/code_v5.py): `src/code_v5.py`
- [Model evaluation](https://github.com/zhangshanwei-mm/patchecg/blob/main/src/eva_model.py): `src/eva_model.py`
- [Projection variant](https://github.com/zhangshanwei-mm/patchecg/blob/main/src/code_v5_projection.py): `src/code_v5_projection.py`
- [ECGFounder variant](https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/ECGFounder/PatchEncoder/src/code_v5.py): `baseline/ECGFounder/PatchEncoder/src/code_v5.py`

### Model components

- [Transformer layers](https://github.com/zhangshanwei-mm/patchecg/blob/main/src/my_transformer.py): `src/my_transformer.py`
- [Vision transformer](https://github.com/zhangshanwei-mm/patchecg/blob/main/src/vit_transformer.py): `src/vit_transformer.py`
- [Training utilities](https://github.com/zhangshanwei-mm/patchecg/blob/main/src/utils.py): `src/utils.py`

### Comparison baselines

- [Medformer classification](https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/Medformer/my_medformer.py): `baseline/Medformer/my_medformer.py`
- [Medformer experiment](https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/Medformer/exp/exp_my_classification.py): `baseline/Medformer/exp/exp_my_classification.py`
- [TimeXer runner](https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/TimeXer/run.py): `baseline/TimeXer/run.py`
- [TimeXer experiments](https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/TimeXer/exp/exp_my_classification.py): `baseline/TimeXer/exp/exp_my_classification.py`
- [TimeXer data](https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/TimeXer/data_provider/data_factory.py): `baseline/TimeXer/data_provider/data_factory.py`
- [Time-series loaders](https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/TimeXer/data_provider/data_loader.py): `baseline/TimeXer/data_provider/data_loader.py`
- [TimeXer models](https://github.com/zhangshanwei-mm/patchecg/tree/main/baseline/TimeXer/models): `baseline/TimeXer/models`
- [Time-series layers](https://github.com/zhangshanwei-mm/patchecg/tree/main/baseline/TimeXer/layers): `baseline/TimeXer/layers`
- [Time-series utilities](https://github.com/zhangshanwei-mm/patchecg/tree/main/baseline/TimeXer/utils): `baseline/TimeXer/utils`
- [SimMTM classification](https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/SimMTM/SimMTM_Classification/code/main.py): `baseline/SimMTM/SimMTM_Classification/code/main.py`

### Imputation approaches

- [SAITS imputation](https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/SAITS-main/run_models.py): `baseline/SAITS-main/run_models.py`
- [SAITS data preparation](https://github.com/zhangshanwei-mm/patchecg/blob/main/baseline/SAITS-main/dataset_generating_scripts/gene_ptb_xl_dataset_saits.py): `baseline/SAITS-main/dataset_generating_scripts/gene_ptb_xl_dataset_saits.py`

### Other components

- **Researcher**

## Connections

- Researcher → Training entry: starts training
- Training entry → Patch classification: trains model
- Patch classification → ECG data handling: uses ECG helpers (evidence: `src/code_v5.py`)
- Patch classification → Masking and augmentation: uses utilities (evidence: `src/code_v5.py`)
- Patch classification → Transformer layers: imports (evidence: `src/code_v5.py`)
- Patch classification → Training utilities: imports (evidence: `src/code_v5.py`)
- Transformer layers → Vision transformer: imports (evidence: `src/my_transformer.py`)
- Researcher → Model evaluation: evaluates model
- Researcher → Projection variant: selects variant
- Researcher → ECGFounder variant: runs variant
- Researcher → Medformer experiment: runs classification
- Medformer experiment → Medformer classification: uses model (evidence: `baseline/Medformer/exp/exp_my_classification.py`)
- Researcher → TimeXer runner: starts experiment
- TimeXer runner → TimeXer experiments: dispatches task (evidence: `baseline/TimeXer/run.py`)
- TimeXer experiments → TimeXer data: loads data (evidence: `baseline/TimeXer/exp/exp_my_classification.py`)
- TimeXer data → Time-series loaders: selects loaders (evidence: `baseline/TimeXer/data_provider/data_factory.py`)
- TimeXer experiments → TimeXer models: builds model
- TimeXer models → Time-series layers: uses layers
- TimeXer data → Time-series utilities: uses utilities
- Researcher → SimMTM classification: runs pretraining
- Researcher → SAITS data preparation: generates data
- Researcher → SAITS imputation: runs imputation
- SAITS data preparation → SAITS imputation: provides datasets
