Skip to main content

pycemrg

Core utilities for cardiac medical image analysis workflows.

License: MIT Python 3.8+

Overview

pycemrg provides a stateless, configuration-driven foundation for building reproducible medical imaging pipelines. It handles common tasks like model versioning, anatomical label translation, path management, and safe command execution—letting you focus on scientific workflows rather than boilerplate.

Design Philosophy: Libraries provide stateless logic; orchestrators handle I/O. All paths are explicit, no magic configuration, no hidden state.

Features

  • Model Management: Download, cache, and version ML models with SHA256 integrity verification
  • Label Management: Translate between human-readable anatomical names and integer segmentation values
  • Path Management: Generate consistent output paths with configurable prefix/suffix patterns
  • Command Execution: Safely run external tools (meshtool, openCARP) with validation and error handling
  • Configuration Scaffolding: Generate template YAML configs via CLI or programmatic API
  • CARPentry Integration: Specialized runner for openCARP ecosystem with environment isolation

Installation

pip install pycemrg

Quick Start

Generate Configuration Templates

# Create a labels configuration template
pycemrg init-labels --output config/labels.yaml --num-labels 10

# Create a models configuration template
pycemrg init-models --output config/models.yaml

Manage Anatomical Labels

from pycemrg.data import LabelManager

# Load your anatomical label definitions
labels = LabelManager("config/labels.yaml")

# Translate between names and integer values
lv_value = labels.get_value("LV_myo")  # Returns: 2

# Resolve groups (including recursive definitions)
chamber_values = labels.get_values_from_names(["ventricles", "atria"])
# Returns: [2, 3, 4, 5] (sorted, deduplicated)

# Generate tag strings for command-line tools
tags = labels.get_tags_string(["LV_myo", "RV_myo"])  # Returns: "2,3"

Example labels.yaml:

labels:
  LV_myo: 2
  RV_myo: 3
  LA_wall: 4
  RA_wall: 5

groups:
  ventricles:
    - LV_myo
    - RV_myo
  atria:
    - LA_wall
    - RA_wall

Manage ML Models

from pycemrg.models import ModelManager

# Initialize with your models manifest
models = ModelManager("config/models.yaml")

# Get path to model weights (downloads/caches automatically)
model_path = models.get_model_path("segmentation_model")
# First call: Downloads, verifies SHA256, extracts → ~/.cache/pycemrg/...
# Subsequent calls: Returns cached path instantly

# Use specific version
legacy_path = models.get_model_path("segmentation_model", version="v1.0")

Example models.yaml:

segmentation_model:
  default: v2.0
  versions:
    v2.0:
      url: "https://example.com/models/seg_v2.0.zip"
      sha256: "abc123..."
      unzipped_target_path: "checkpoints/model.pth"

Execute Commands Safely

from pycemrg.system import CommandRunner

runner = CommandRunner()

# Run with output validation
runner.run(
    cmd=['meshtool', 'extract', 'mesh', '-msh=heart'],
    expected_outputs=[Path('heart_epi.surf')]
)

# Handle tools that write warnings to stderr
runner.run(
    cmd=['legacy_tool', '--process', 'data.txt'],
    ignore_errors=["WARNING: deprecated flag"]
)

Documentation

Related Packages

pycemrg is the stable core of a suite of cardiac imaging tools:

  • pycemrg-image-analysis: Medical image processing workflows (segmentation, meshing)
  • pycemrg-model-creation: Cardiac mesh generation and UVC coordinate systems
  • pycemrg-interpolation: Functional autoencoder-based image interpolation

Development

Local Installation

# Clone and install in editable mode
git clone https://github.com/YOUR-ORG/pycemrg.git
cd pycemrg
pip install -e ".[dev]"

# Run tests
pytest tests/

Contributing

This library follows strict architectural principles:

  • Stateless logic: No hidden state, all dependencies injected
  • Contract-driven: Complex workflows use dataclass contracts
  • Path-agnostic: Libraries never derive paths; orchestrators provide them explicitly

See Architecture Guidelines for details.

License

MIT License - See LICENSE file for details.

Citation

If you use pycemrg in your research, please cite:

@software{pycemrg2025,
  title = {pycemrg: Core utilities for cardiac medical image analysis},
  author = {[Your Name]},
  year = {2025},
  url = {https://github.com/YOUR-ORG/pycemrg}
}

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycemrg-0.1.3.tar.gz (24.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycemrg-0.1.3-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

Details for the file pycemrg-0.1.3.tar.gz.

File metadata

  • Download URL: pycemrg-0.1.3.tar.gz
  • Upload date:
  • Size: 24.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for pycemrg-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e686a753aa86d6630239a59bdacdc9a778d09c2dccc17ff7dc61047cf8c908b8
MD5 ed404c6ea123030fda9635b820e492c6
BLAKE2b-256 81ff448c22c5754707cd969d33b4807be48b1ff6c9d11abde23b4416a6e92746

See more details on using hashes here.

File details

Details for the file pycemrg-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pycemrg-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 27.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for pycemrg-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0cfeb65a553e25373787b93e517eeb8d4c3c3950fa6205cf45f115c9bcff10d5
MD5 243e704953dc85a4b5d54ea3a2d25ad3
BLAKE2b-256 bcfd71d91b191ca3d85ea8ac5bbbbe76043c604509b3dacf68d615f42aef24c5

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page