Skip to content
quritPublic

About

A minimal Python library to facilitate the creation and manipulation of DICOM RTStructs.

Topics

Resources

Contributing

Stars

259 stars

Watchers

7 watching

Forks

Repository files navigation

A minimal Python library for RT Struct manipulation

Python version PyPI version Documentation status PyPI - License


DOI

RT-Utils reads and writes the DICOM RT Structure Set, the file a treatment planning system stores contoured anatomy in. It turns an RTStruct into a NumPy mask you can compute on, and turns a mask from a segmentation model back into a conformant RTStruct that a planning system or viewer will open.

Installation

pip install rt-utils

NIfTI conversion needs SimpleITK, which is an optional extra:

pip install "rt-utils[nifti]"

Python 3.9 and newer.

Writing a mask into a structure set

from rt_utils import RTStructBuilder

rtstruct = RTStructBuilder.create_new(dicom_series_path="./series")

# Your segmentation, as a boolean array shaped (Rows, Columns, Slices)
rtstruct.add_roi(mask=MASK_FROM_ML_MODEL, name="GTV", color=[255, 0, 255])

rtstruct.save("new-rt-struct")

An ROI written from a liver segmentation model, as viewed in Slicer. (The underlying patient data has been hidden.)

Reading one back as a mask

from rt_utils import RTStructBuilder

rtstruct = RTStructBuilder.create_from(
    dicom_series_path="./series",
    rt_struct_path="./series/rt-struct.dcm",
)

print(rtstruct.get_roi_names())
mask = rtstruct.get_roi_mask_by_name("GTV")   # (Rows, Columns, Slices), bool

Mask layout

A mask is indexed (Rows, Columns, Slices):

axis index runs
0 row down a column
1 column along a row
2 slice through the series in ascending slice position

The first two axes are ordered exactly as pydicom reads pixel_array, so a mask produced by segmenting the series already has the right layout. A mask from elsewhere, a NIfTI volume in particular, carries its own axis order and direction and may need rearranging first: see Mask layout, and use nifti_to_mask rather than guessing a transpose.

What else it does

📖 Documentation

rt-utils.readthedocs.io: quickstart, guides, concepts and the full API reference.

Worth reading before your first add_roi: Mask layout. If something already looks wrong, start at Troubleshooting.

Examples

A comparison between RT-Utils and other conversion techniques is in this repository.

The real-world example from the JOSS paper was generated with RTUTILS_read_world_example_for_PET.ipynb.

Contributing

Contributions are welcome. Please review our CONTRIBUTING guidelines, and see the changelog for what has changed recently.

How to Cite

If you are incorporating RT-Utils into your projects, kindly include the following citation:

DOI

Shrestha, A., A. Watkins, F. Yousefirizi, A. Rahmim, C. Uribe, "RT-utils: A minimal python library for RT-struct manipulation." Journal of Open Source Software, Vol. 10 (No. 107), pp. 7361, (2025). doi:10.21105/joss.07361.

Read the full paper: RT-utils: A Minimal Python Library for RT-struct Manipulation.

About

A minimal Python library to facilitate the creation and manipulation of DICOM RTStructs.

Topics

Resources

Contributing

Stars

259 stars

Watchers

7 watching

Forks

Releases

Used by

Contributors

Languages