ECG generation and modeling experiments
Project description
ECGEN
ECG Generation Framework — PyTorch Lightning implementations of generative models for 12-lead electrocardiogram (ECG) signal synthesis and latent representation learning.
Overview
ECGEN provides two deep generative models for ECG signals, both trained primarily on the MIMIC-IV-ECG dataset (12-lead, 5000 samples/signal):
| Model | Type | Leads | Purpose |
|---|---|---|---|
| VAE | Variational Autoencoder | 12 | Unsupervised latent representation & reconstruction |
| Pulse2Pulse | WaveGAN (WGAN-GP) | 8 | Conditional ECG signal generation |
Installation
# From source
git clone https://github.com/vlbthambawita/ECGEN.git
cd ECGEN
pip install -e .
# With HuggingFace Hub support (for checkpoint uploads)
pip install 'ecgen[hf]'
Requirements: Python ≥ 3.8, PyTorch, PyTorch Lightning
Models
VAE — Variational Autoencoder
1D convolutional VAE with residual blocks for learning compact ECG representations. The encoder maps a 12-lead signal to a Gaussian latent space; the decoder reconstructs the signal from sampled latents.
Architecture: ResidualBlock1D → Encoder1D → latent (μ, σ) → Decoder1D
Loss: reconstruction_loss + kl_weight × KL_divergence
from ecgen.models import VAELightning, VAEConfig
config = VAEConfig(
in_channels=12,
base_channels=64,
latent_channels=8,
channel_multipliers=(1, 2, 4, 4),
num_res_blocks=2,
lr=1e-4,
kl_weight=1e-4,
)
model = VAELightning(config)
# Generate new ECG signals
samples = model.sample(n_samples=16, seq_length=5000) # (16, 12, 5000)
Pulse2Pulse — WaveGAN
Wasserstein GAN with gradient penalty for conditional ECG generation. Uses the first 8 leads of MIMIC-IV-ECG signals.
from ecgen.models import Pulse2PulseGAN, Pulse2PulseConfig
config = Pulse2PulseConfig(
model_size=50,
num_channels=8,
seq_length=5000,
lr=1e-4,
lmbda=10.0, # gradient penalty weight
n_critic=5, # discriminator steps per generator step
)
model = Pulse2PulseGAN(config)
Training
VAE
# Config-driven (recommended)
python scripts/train_vae_mimic.py --config configs/experiments/vae_mimic.yaml
# Quick test with a small subset
python scripts/train_vae_mimic.py \
--data-dir /path/to/mimic-iv-ecg \
--max-samples 1000 --max-epochs 10 --batch-size 16
# Resume from checkpoint
python scripts/train_vae_mimic.py \
--config configs/experiments/vae_mimic.yaml \
--resume runs/vae_mimic/seed_42/checkpoints/last.ckpt
Pulse2Pulse
python -m ecgen.training.train --config configs/experiments/pulse2pulse_mimic.yaml
Run outputs are saved to runs/{experiment_name}/seed_{seed}/ containing:
checkpoints/— best and last model weightssamples/— generated ECG batchestb/— TensorBoard logs
Config format
All experiments use a YAML config with target + params for dynamic instantiation:
experiment:
name: vae_mimic
seed: 42
model:
target: ecgen.models.vae.VAELightning
params:
config:
in_channels: 12
latent_channels: 8
kl_weight: 0.0001
data:
target: ecgen.data.mimic_dataset.MIMICIVECGDataset
params:
mimic_path: /path/to/mimic-iv-ecg
batch_size: 32
max_samples: null # null = full dataset
trainer:
max_epochs: 100
accelerator: gpu
devices: [0]
Uploading Checkpoints to HuggingFace
pip install 'ecgen[hf]'
huggingface-cli login
From Python:
from ecgen.f import upload_hf, upload_hf_single
# Upload using a YAML config (supports subcategories like vae/ptbxl/)
upload_hf("configs/upload_checkpoints.yaml")
# Upload a single checkpoint
upload_hf_single(
local_path="runs/vae/ptbxl/best.ckpt",
repo_path="vae/ptbxl/best.ckpt",
repo_id="your_username/ECGEN",
)
From CLI:
python scripts/upload_checkpoints_to_hf.py --dry-run # preview
python scripts/upload_checkpoints_to_hf.py # upload
Edit configs/upload_checkpoints.yaml to define which checkpoints map to which paths in the HF repo.
Package Structure
src/ecgen/
├── models/ — VAE and Pulse2Pulse model definitions
├── data/ — MIMIC-IV-ECG dataset loaders and data modules
├── training/ — Training loop, losses, metrics, callbacks
├── f/ — Functional utilities (upload_hf, upload_hf_single)
└── utils/ — Seeding, I/O, logging helpers
Releasing to PyPI
The package is published as ecgen automatically on tagged commits:
# Bump __version__ in src/ecgen/__init__.py, commit, then:
git tag v0.2.0
git push origin v0.2.0
GitHub Actions builds and publishes to PyPI via OIDC trusted publishing (no API token required).
License
See LICENSE.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ecgen-0.7.0.tar.gz.
File metadata
- Download URL: ecgen-0.7.0.tar.gz
- Upload date:
- Size: 27.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61d9e81ab62a91d24294ceb75b348a421ecf0fa41a7492550c5db28c0abf8a13
|
|
| MD5 |
e4b7dd1a9bd438dcc02fc92d63b2d829
|
|
| BLAKE2b-256 |
43b90f8ac4d5b2d6330f6a6437570ceedd7545cdacce7f9e4bb5505312eda365
|
Provenance
The following attestation bundles were made for ecgen-0.7.0.tar.gz:
Publisher:
publish.yml on vlbthambawita/ECGEN
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ecgen-0.7.0.tar.gz -
Subject digest:
61d9e81ab62a91d24294ceb75b348a421ecf0fa41a7492550c5db28c0abf8a13 - Sigstore transparency entry: 1246693112
- Sigstore integration time:
-
Permalink:
vlbthambawita/ECGEN@59606d20ad728f10c20da89ca17cd23febe2e1ad -
Branch / Tag:
refs/tags/v0.7.0 - Owner: https://github.com/vlbthambawita
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@59606d20ad728f10c20da89ca17cd23febe2e1ad -
Trigger Event:
push
-
Statement type:
File details
Details for the file ecgen-0.7.0-py3-none-any.whl.
File metadata
- Download URL: ecgen-0.7.0-py3-none-any.whl
- Upload date:
- Size: 27.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d577d35039210a18fd854a75b568f3b1c1fce0c0845fef2c4ad7de03cecd9b41
|
|
| MD5 |
041af299bf75f22e87bd07fe3149dc19
|
|
| BLAKE2b-256 |
27ecbfa518bc6fdf58b80c24e36ce653379f1ead8e96f2419c89b3da2866869c
|
Provenance
The following attestation bundles were made for ecgen-0.7.0-py3-none-any.whl:
Publisher:
publish.yml on vlbthambawita/ECGEN
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ecgen-0.7.0-py3-none-any.whl -
Subject digest:
d577d35039210a18fd854a75b568f3b1c1fce0c0845fef2c4ad7de03cecd9b41 - Sigstore transparency entry: 1246693292
- Sigstore integration time:
-
Permalink:
vlbthambawita/ECGEN@59606d20ad728f10c20da89ca17cd23febe2e1ad -
Branch / Tag:
refs/tags/v0.7.0 - Owner: https://github.com/vlbthambawita
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@59606d20ad728f10c20da89ca17cd23febe2e1ad -
Trigger Event:
push
-
Statement type: