|
1 | 1 | # pycemrg |
| 2 | + |
| 3 | +Core utilities for cardiac medical image analysis workflows. |
| 4 | + |
| 5 | +[](https://opensource.org/licenses/MIT) |
| 6 | +[](https://www.python.org/downloads/) |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +`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. |
| 11 | + |
| 12 | +**Design Philosophy:** Libraries provide stateless logic; orchestrators handle I/O. All paths are explicit, no magic configuration, no hidden state. |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +- **Model Management**: Download, cache, and version ML models with SHA256 integrity verification |
| 17 | +- **Label Management**: Translate between human-readable anatomical names and integer segmentation values |
| 18 | +- **Path Management**: Generate consistent output paths with configurable prefix/suffix patterns |
| 19 | +- **Command Execution**: Safely run external tools (meshtool, openCARP) with validation and error handling |
| 20 | +- **Configuration Scaffolding**: Generate template YAML configs via CLI or programmatic API |
| 21 | +- **CARPentry Integration**: Specialized runner for openCARP ecosystem with environment isolation |
| 22 | + |
| 23 | +## Installation |
| 24 | +```bash |
| 25 | +pip install pycemrg |
| 26 | +``` |
| 27 | + |
| 28 | +## Quick Start |
| 29 | + |
| 30 | +### Generate Configuration Templates |
| 31 | +```bash |
| 32 | +# Create a labels configuration template |
| 33 | +pycemrg init-labels --output config/labels.yaml --num-labels 10 |
| 34 | + |
| 35 | +# Create a models configuration template |
| 36 | +pycemrg init-models --output config/models.yaml |
| 37 | +``` |
| 38 | + |
| 39 | +### Manage Anatomical Labels |
| 40 | +```python |
| 41 | +from pycemrg.data import LabelManager |
| 42 | + |
| 43 | +# Load your anatomical label definitions |
| 44 | +labels = LabelManager("config/labels.yaml") |
| 45 | + |
| 46 | +# Translate between names and integer values |
| 47 | +lv_value = labels.get_value("LV_myo") # Returns: 2 |
| 48 | + |
| 49 | +# Resolve groups (including recursive definitions) |
| 50 | +chamber_values = labels.get_values_from_names(["ventricles", "atria"]) |
| 51 | +# Returns: [2, 3, 4, 5] (sorted, deduplicated) |
| 52 | + |
| 53 | +# Generate tag strings for command-line tools |
| 54 | +tags = labels.get_tags_string(["LV_myo", "RV_myo"]) # Returns: "2,3" |
| 55 | +``` |
| 56 | + |
| 57 | +**Example `labels.yaml`:** |
| 58 | +```yaml |
| 59 | +labels: |
| 60 | + LV_myo: 2 |
| 61 | + RV_myo: 3 |
| 62 | + LA_wall: 4 |
| 63 | + RA_wall: 5 |
| 64 | + |
| 65 | +groups: |
| 66 | + ventricles: |
| 67 | + - LV_myo |
| 68 | + - RV_myo |
| 69 | + atria: |
| 70 | + - LA_wall |
| 71 | + - RA_wall |
| 72 | +``` |
| 73 | +
|
| 74 | +### Manage ML Models |
| 75 | +```python |
| 76 | +from pycemrg.models import ModelManager |
| 77 | + |
| 78 | +# Initialize with your models manifest |
| 79 | +models = ModelManager("config/models.yaml") |
| 80 | + |
| 81 | +# Get path to model weights (downloads/caches automatically) |
| 82 | +model_path = models.get_model_path("segmentation_model") |
| 83 | +# First call: Downloads, verifies SHA256, extracts → ~/.cache/pycemrg/... |
| 84 | +# Subsequent calls: Returns cached path instantly |
| 85 | + |
| 86 | +# Use specific version |
| 87 | +legacy_path = models.get_model_path("segmentation_model", version="v1.0") |
| 88 | +``` |
| 89 | + |
| 90 | +**Example `models.yaml`:** |
| 91 | +```yaml |
| 92 | +segmentation_model: |
| 93 | + default: v2.0 |
| 94 | + versions: |
| 95 | + v2.0: |
| 96 | + url: "https://example.com/models/seg_v2.0.zip" |
| 97 | + sha256: "abc123..." |
| 98 | + unzipped_target_path: "checkpoints/model.pth" |
| 99 | +``` |
| 100 | +
|
| 101 | +### Execute Commands Safely |
| 102 | +```python |
| 103 | +from pycemrg.system import CommandRunner |
| 104 | + |
| 105 | +runner = CommandRunner() |
| 106 | + |
| 107 | +# Run with output validation |
| 108 | +runner.run( |
| 109 | + cmd=['meshtool', 'extract', 'mesh', '-msh=heart'], |
| 110 | + expected_outputs=[Path('heart_epi.surf')] |
| 111 | +) |
| 112 | + |
| 113 | +# Handle tools that write warnings to stderr |
| 114 | +runner.run( |
| 115 | + cmd=['legacy_tool', '--process', 'data.txt'], |
| 116 | + ignore_errors=["WARNING: deprecated flag"] |
| 117 | +) |
| 118 | +``` |
| 119 | + |
| 120 | +## Documentation |
| 121 | + |
| 122 | +- [API Reference](docs/API_reference.md) - Complete API documentation |
| 123 | +- [Architecture Guidelines](docs/pycemrg_suite_guidelines.txt) - Design principles and patterns |
| 124 | + |
| 125 | +## Related Packages |
| 126 | + |
| 127 | +`pycemrg` is the stable core of a suite of cardiac imaging tools: |
| 128 | + |
| 129 | +- **pycemrg-image-analysis**: Medical image processing workflows (segmentation, meshing) |
| 130 | +- **pycemrg-model-creation**: Cardiac mesh generation and UVC coordinate systems |
| 131 | +- **pycemrg-interpolation**: Functional autoencoder-based image interpolation |
| 132 | + |
| 133 | +## Development |
| 134 | + |
| 135 | +### Local Installation |
| 136 | +```bash |
| 137 | +# Clone and install in editable mode |
| 138 | +git clone https://github.com/YOUR-ORG/pycemrg.git |
| 139 | +cd pycemrg |
| 140 | +pip install -e ".[dev]" |
| 141 | + |
| 142 | +# Run tests |
| 143 | +pytest tests/ |
| 144 | +``` |
| 145 | + |
| 146 | +### Contributing |
| 147 | + |
| 148 | +This library follows strict architectural principles: |
| 149 | +- **Stateless logic**: No hidden state, all dependencies injected |
| 150 | +- **Contract-driven**: Complex workflows use dataclass contracts |
| 151 | +- **Path-agnostic**: Libraries never derive paths; orchestrators provide them explicitly |
| 152 | + |
| 153 | +See [Architecture Guidelines](docs/pycemrg_suite_guidelines.txt) for details. |
| 154 | + |
| 155 | +## License |
| 156 | + |
| 157 | +MIT License - See [LICENSE](LICENSE) file for details. |
| 158 | + |
| 159 | +## Citation |
| 160 | + |
| 161 | +If you use `pycemrg` in your research, please cite: |
| 162 | +```bibtex |
| 163 | +@software{pycemrg2025, |
| 164 | + title = {pycemrg: Core utilities for cardiac medical image analysis}, |
| 165 | + author = {[Your Name]}, |
| 166 | + year = {2025}, |
| 167 | + url = {https://github.com/YOUR-ORG/pycemrg} |
| 168 | +} |
| 169 | +``` |
0 commit comments