Measuring radiotherapy doses - plotting visualizations
Project description
DoseMetrics
A Python library for measuring radiotherapy doses and creating interactive visualizations for radiation therapy treatment planning and analysis.
๐ Documentation | ๐ Try Live Demo | ๐ป GitHub
Overview
DoseMetrics provides tools for analyzing radiation dose distributions, calculating dose-volume histograms (DVH), evaluating treatment plan quality, and creating publication-ready visualizations. This library is designed for medical physicists, radiation oncologists, and researchers working with radiotherapy treatment planning data.
Package Structure
dosemetrics.metrics: Core dose calculation functions (DVH, scores, etc.)dosemetrics.io: File I/O utilities for reading dose and mask datadosemetrics.utils: Utility functions for compliance, plotting, etc.- App-specific code moved to separate
dosemetrics_apppackage (not distributed on PyPI)
Features
- Dose Analysis: Calculate and analyze 3D dose distributions
- DVH Generation: Create dose-volume histograms for organs at risk (OARs) and targets
- Quality Metrics: Compute conformity indices, homogeneity indices, and other plan quality metrics
- Compliance Checking: Evaluate dose constraints and treatment plan compliance
- Interactive Visualizations: Generate interactive plots using Plotly and Streamlit
- Comparative Analysis: Compare predicted vs. actual dose distributions
- Geometric Analysis: Compute spatial differences and overlaps between structures
- Export Capabilities: Save results in various formats (CSV, PDF, PNG)
- Command Line Interface: Basic CLI for common operations
Quick Start
Installation
Install it easily using pip:
pip install dosemetrics
For development, clone the repository and run make setup (uses uv when available to install dependencies and prepare a virtual environment):
git clone https://github.com/contouraid/dosemetrics.git
cd dosemetrics
make setup
# or manually if you prefer:
# ./scripts/setup_repo.sh
Interactive Web Application
Launch the interactive Streamlit application using the Makefile:
make app
# or
make run
Or use the shell script directly:
./scripts/run_streamlit_app.sh
Alternatively, run manually from the root directory:
PYTHONPATH=src streamlit run src/dosemetrics_app/app.py
Note: The application includes password authentication that is currently disabled for development/testing. If you need to enable authentication, uncomment the authentication check in src/dosemetrics_app/app.py and configure the secrets.toml file with user passwords.
This provides a user-friendly interface for uploading NIfTI files, analyzing dose distributions, and generating reports.
Makefile Commands
The project includes a Makefile for common operations:
make help # Show all available commands
make setup # Initial setup and install dependencies
make test # Run all tests
make run # Run the Streamlit app locally
make docs # Serve documentation locally
make deploy # Deploy to Hugging Face Space
make clean # Clean up cache and temporary files
make info # Show project information
See make help for a complete list of commands.
Documentation
Comprehensive documentation is available at contouraid.github.io/dosemetrics
Building Documentation Locally
# Install documentation dependencies
pip install -e ".[docs]"
# Serve with live-reload
make docs
# or
mkdocs serve
Usage Examples
Basic DVH Analysis
import dosemetrics
# Load dose and structure data (using the new structure)
dose_data = dosemetrics.read_from_nifti("path/to/dose.nii.gz")
structures = {"PTV": dosemetrics.read_from_nifti("path/to/ptv.nii.gz")}
# Generate DVH
dvh_df = dosemetrics.dvh_by_structure(dose_data, structures)
# Plot DVH
dosemetrics.plot_dvh(dose_data, structures, "output.pdf")
Quality Metrics Calculation
# Calculate dose summary statistics
quality_metrics = dosemetrics.dose_summary(dose_data, structures)
print(f"Quality metrics: {quality_metrics}")
Command Line Interface
# Generate DVH from command line
dosemetrics dvh dose.nii.gz structure1.nii.gz structure2.nii.gz -o dvh_results.csv
# Compute quality metrics
dosemetrics quality dose.nii.gz structure1.nii.gz structure2.nii.gz -o quality_report.csv
# Check version
dosemetrics --version
Compliance Checking
# Define dose constraints
constraints = {
"Brainstem": {"max_dose": 54, "unit": "Gy"},
"Spinal_Cord": {"max_dose": 45, "unit": "Gy"},
"Parotid_L": {"mean_dose": 26, "unit": "Gy"}
}
# Check compliance
compliance_results = dm.compliance.check_constraints(
dose_data, structures, constraints
)
Project Structure
dosemetrics/
โโโ src/dosemetrics/ # Core library modules (new structure)
โ โโโ io/ # Data I/O utilities
โ โ โโโ data_io.py # File loading and data reading
โ โโโ metrics/ # Core dose calculations
โ โ โโโ dvh.py # DVH calculation
โ โ โโโ exposure.py # Exposure metrics (formerly metrics.py)
โ โ โโโ scores.py # Scoring algorithms
โ โโโ utils/ # Utility functions
โ โโโ comparison.py # Plan comparison tools
โ โโโ compliance.py # Constraint checking
โ โโโ plot.py # Visualization tools
โโโ src/dosemetrics_app/ # Streamlit web application
โ โโโ app.py # Main application entry point
โ โโโ tabs/ # Application tab modules
โ โโโ variations.py # Variations analysis tab
โโโ examples/ # Usage examples and scripts
โโโ tests/ # Organized unit tests
โ โโโ data_io/ # Tests for I/O functionality
โ โโโ metrics/ # Tests for metrics calculations
โ โโโ utils/ # Tests for utility functions
โโโ data/ # Sample data for testing
Examples
The examples/ directory contains comprehensive examples:
- DVH Analysis: Generate and compare dose-volume histograms
- Quality Assessment: Calculate treatment plan quality indices
- Geometric Analysis: Compute structure overlaps and distances
- Interactive Plotting: Create interactive visualizations
- Report Generation: Generate automated treatment plan reports
Run any example script:
python examples/plot_dvh_interactive.py
python examples/compare_quality_index.py
python examples/generate_dvh_family.py
Supported Data Formats
- NIfTI:
.nii,.nii.gzfiles
Development
Running Tests
Execute the test suite to ensure everything works correctly:
python -m unittest discover -s tests -p "test_*.py"
Contributing
We welcome contributions! Please see our contributing guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Requirements
- Python 3.9 or higher
- See
pyproject.tomlfor complete dependency list
Documentation
For detailed API documentation and tutorials, visit our documentation site (coming soon).
Citation
If you use DoseMetrics in your research, please cite:
@software{dosemetrics2024,
author = {Kamath, Amith},
title = {DoseMetrics: A Python Library for Radiotherapy Dose Analysis},
url = {https://github.com/contouraid/dosemetrics},
version = {0.2.0},
year = {2024}
}
License
This project is licensed under the Creative Commons Attribution-ShareAlike-NonCommercial 4.0 International License - see the LICENSE file for details.
Non-Commercial Use: This software is freely available for academic, research, and personal use. Commercial use requires explicit written permission from the copyright holder.
For commercial licensing inquiries, please contact the folks at contouraid.
Contributors
- Amith Kamath - Lead Developer - amithjkamath
Acknowledgments
- Medical physics community for guidance and feedback
- Open source medical imaging libraries that make this work possible
- Contributors and users who help improve the library
Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Project details
Release history Release notifications | RSS feed
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 dosemetrics-0.3.0.tar.gz.
File metadata
- Download URL: dosemetrics-0.3.0.tar.gz
- Upload date:
- Size: 94.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
889dc1c6f8615a50204b452bb9dcf97af09f0620732e705961294b5911362ea0
|
|
| MD5 |
9aaf5f19cbb78c6b6a42c6dcd1f9f01a
|
|
| BLAKE2b-256 |
5453d4540edcd5ed4a85835dab346b7e5cb092f04aacd08ecb4181d757d10fbe
|
File details
Details for the file dosemetrics-0.3.0-py3-none-any.whl.
File metadata
- Download URL: dosemetrics-0.3.0-py3-none-any.whl
- Upload date:
- Size: 100.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a342ac3eb8d590577de1d5b92ec3d3905afa63dc8d4b5a43bef9f11a629dca9f
|
|
| MD5 |
1d06df95e09e602c1e0ef4e846429511
|
|
| BLAKE2b-256 |
2866c47ba8579e77262632a991be30eb9a922482ab3b1cdcf27711c7af06d866
|