Skip to main content

pyRadPlan

pyRadPlan is an open-source radiotherapy treatment planning toolkit designed for interoperability with matRad.

Development is lead by the Radiotherapy Optimization group at the German Cancer Research Center (DKFZ)

Concept and Goals

pyRadPlan is a multi-modality treatment planning toolkit in python born from the established Matlab-based toolkit matRad. As such, pyRadPlan aims to provide a framework as well as tools for combining dose calculation with optimization with focus on ion planning.

Data Structures

pyRadPlan uses a similar datastructure and workflow concept as in matRad, while trying to ensure that the corresponding datastructures can be easily imported and exported from/to matRad. This facilitates the application of either algortihms from matRad or native pyRadPlan at any stage of the treatment planning workflow.

To enforce valid datastructures, we perform validation and serialization with pydantic. Datastructures and algorithms rely mostly on SimpleITK, numpy, and scipy for internal data representation and processing.

Scripting and API

Minimal Script using the Top-Level API

A minimal script is very similar to matRad's example matRad.m script:

from importlib import resources
import pymatreader
from pyRadPlan import load_patient, IonPlan, generate_stf, calc_dose_influence, fluence_optimization

#  Read patient from provided TG119.mat file and validate data
tg119_path = resources.files("pyRadPlan.data.phantoms").joinpath("TG119.mat")
ct, cst = load_patient(tg119_path)

# Create a plan object
pln = IonPlan(radiation_mode="protons", machine="Generic")

# Generate Steering Geometry ("stf")
stf = generate_stf(ct, cst, pln)

# Calculate Dose Influence Matrix ("dij")
dij = calc_dose_influence(ct, cst, stf, pln)

# Fluence Optimization (objectives loaded from "cst")
fluence = fluence_optimization(ct, cst, stf, dij, pln)

# Result
result = dij.compute_result_ct_grid(fluence)

This script uses the top-level API exposed when importing pyRadPlan. The top-level functions are designed to take the main data structures as input and configure the corresponding workflow step via the Plan using the attribute dictionaries pln.prop*:

Plan property API function Description ID
prop_stf generate_stf Create beam Geometry generator
prop_dose_calc calc_dose_influence, calc_dose_forward Calculate dose matrix / distribution engine
prop_opt fluence_optimization Optimization of beam fluences problem

The Plan properties are dictionaries. Based on their content, the top-level-api will choose the correct implementation and configure the corresponding settings. The ID can be used in the corresponding pln.prop* dictionary to identify a specific implementation. E.g., pln.prop_stf = {"generator": "IMPT"} will select the IMPT Geometry Generator if compatible to the plan. pyRadPlan will try to set all provided configuration parameters in the dictionary and use default parameters and implementations if no keys are provided.

The top level api is designed to require minimal programming experience and to run the same planning workflows with different configurations by just changing the corresponding plan object.

Low-level API

Instead of using above top-level workflow functions and a central plan configuration, one can also built custom workflows instantiating the necessary algorithm objects directly, for example:

...
from pyRadPlan.stf import StfGeneratorIMPT
# Create a plan object
pln = IonPlan(radiation_mode="protons", machine="Generic")

# Equivalent Top Level API configuration
# pln.prop_stf = {"gantry_angles": [90, 270], "couch_angles": [0, 0], "generator": "IMPT"}
# stf = generate_stf(ct,cst,pln)

# Low level:
stf_gen = StfGeneratorIMPT()
stf_gen.gantry_angles = [90, 270]
stf_gen.couch_angles = [0, 0]
stf = stf_gen.generate(ct,cst)

If you are interested in helping with development, get in touch, read the contributing guidelines, and the developer note below.

Contributing & Notes for Developers

pyRadPlan development uses unit-testing and code formatting via pre-commit hooks to ensure clean code. If you are a developer or want to contribute, make sure to clone the latest state via git. Then, we strongly suggest to create a virtual python environment with a suitable version and do an editable installation of pyRadPlan in dev mode: pip install -e .[dev]

Note If you are using venv to create a virtual environment in the project's root folder, we suggest to name it .venv as this folder will be automatically excluded by all formatters and linters

This will install an editable pyRadPlan module including pytest and pre-commit in addition to the standard modules.

  • pytest is used to run unit tests before publishing the code. Run on the test folder via pytest test, or choose any test file following the pytest syntax. We encourage writing at least fundamental unit tests for new code. Will also install coverage and the pytest extension to monitor coverage.
  • pre-commit allows for automatic code formatting to ensure following of PEPs (mainly PEP8 and PEP257).

After successfully running pip install -e .[dev] check in the console that pre-commit --version provides a correct response. Afterwards run pre-commit install. This reads the .pre-commit_config.yaml and adds a hook to your git repository, which whenever a commit is made the changed files are reformatted to ensure the PEP standards.

Matlab / Octave Files & Engine

If you want to interface via matRad, the simplest way is to write mat files using scipy's savemat and load them in matRad. To save a datastructure such that it can be read into Matlab and interpreted by matRad, you can call to_matrad on the structure and then pass the resulting dictionary to savemat. You can also install the matlab engine by appending [matlab] to the pip install command. However, as there is some compatibility matrix for what matlabengine version is suitable for your Matlab installation, it might be better to not install the latest version but instead install the correct matlabengine manually before. To use Octave via oct2py, analogously append [octave].

Metadata

Release files for pyRadPlan 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyRadPlan 0.2.0
File Size Uploaded
pyradplan-0.2.0.tar.gz 28.6 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyRadPlan 0.2.0
File Interpreter ABI Platform
pyRadPlan-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 57.2 MB

Release files / pyradplan-0.2.0.tar.gz

Download URL pyradplan-0.2.0.tar.gz
Size 28.6 MB
Tags Source
SHA-256 checksum
How to use checksums
1cbfd62045c3e61dbfe7b33bf0331d1eb6933d348971b49a29fae904cd68700f
BLAKE2b-256 checksum
How to use checksums
f5befccbcc70f944d46b3858893eebc8624a5a09f409a90d9ca99c85878baa74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Release files / pyRadPlan-0.2.0-py3-none-any.whl

Download URL pyRadPlan-0.2.0-py3-none-any.whl
Size 28.6 MB
Tags Python 3
SHA-256 checksum
How to use checksums
5123edc7924e9de849e0dacb7156e084e1b494fad6dee92f0bde53cf49f2a7c0
BLAKE2b-256 checksum
How to use checksums
4c7a7f76293e4cf747e609f673af185ce65b3183c386906dc58cd5badeeda593
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Release history Release notifications | RSS feed

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

This release

0.2.0 This release

2 release files

0.1.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page