Published! See the Technical Note and please cite it if you find this work useful.
DOI: https://doi.org/10.1016/j.prro.2021.02.003
Convert DICOM images and RT structures into NIfTI files, NumPy arrays, and SimpleITK image handles — and convert prediction masks back into RT structures.
pip install DicomRTToolFor the interactive image viewer (requires matplotlib):
pip install "DicomRTTool[viewer]"Supported Python versions: 3.8, 3.10, 3.12+
from DicomRTTool.ReaderWriter import DicomReaderWriter, ROIAssociationClass
dicom_path = "/path/to/dicom"
reader = DicomReaderWriter(description="Examples", arg_max=True)
reader.walk_through_folders(dicom_path)
# Inspect available ROIs
all_rois = reader.return_rois(print_rois=True)
# Define target ROIs with optional name aliases
contour_names = ["tumor"]
associations = [ROIAssociationClass("tumor", ["tumor_mr", "tumor_ct"])]
reader.set_contour_names_and_associations(
contour_names=contour_names, associations=associations
)
# Load images and masks
reader.get_images_and_mask()
image_numpy = reader.ArrayDicom # NumPy image array
mask_numpy = reader.mask # NumPy mask array
image_handle = reader.dicom_handle # SimpleITK Image
mask_handle = reader.annotation_handle # SimpleITK Imagefrom pydicom.tag import Tag
plan_keys = {"MyNamedRTPlan": Tag((0x300a, 0x002))}
image_keys = {"MyPatientName": "0010|0010"}
reader = DicomReaderWriter(
description="Examples",
arg_max=True,
plan_pydicom_string_keys=plan_keys,
image_sitk_string_keys=image_keys,
)- Modern Python packaging —
pyproject.tomlreplacessetup.py - Type annotations throughout — better IDE support and documentation
loggingmodule replacesprint()— configurable verbosityconcurrent.futuresreplaces rawthreading.Queue— cleaner parallelismdataclassesfor all internal data models- No bare
except:— proper exception handling everywhere - Deprecated
pandas.DataFrame.appendreplaced withpd.concat - Full backward compatibility — existing import paths still work
If you find this code useful, please reference the publication and the GitHub page.