Services for reading DICOM files, RT structures, and dose files, as well as tools for converting numpy prediction masks back to an RT structure.
Project description
DicomRTTool
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.
Installation
pip install DicomRTTool
For the interactive image viewer (requires matplotlib):
pip install "DicomRTTool[viewer]"
Supported Python versions: 3.10, 3.11, 3.12, 3.13.
Quick Start
from pathlib import Path
from DicomRTTool.ReaderWriter import DicomReaderWriter, ROIAssociationClass
dicom_path = 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 for the first index that contains all target ROIs.
reader.set_index(reader.indexes_with_contours[0])
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 Image
Reading extra DICOM tags
from 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,
)
Resetting state between uses
DicomReaderWriter instances can be reused across multiple corpora; call
the appropriate reset method before walking a fresh folder tree or
swapping target ROIs:
reader.reset() # wipe everything (images, RTs, masks, cached UIDs)
reader.reset_rts() # clear ROI bookkeeping only; keep loaded images
reader.reset_mask() # re-allocate an empty mask after changing Contour_Names
Writing predictions back to an RT structure
import numpy as np
# 4-channel one-hot prediction matching the loaded image shape:
# (slices, rows, cols, num_classes + 1) — channel 0 is background.
predictions = np.zeros((*reader.ArrayDicom.shape, 3), dtype=np.float32)
# ... populate `predictions` from your model ...
reader.prediction_array_to_RT(
prediction_array=predictions,
output_dir="/path/to/output",
ROI_Names=["organ_a", "organ_b"],
)
What's new in v4.0
- Python 3.10+ required (3.8 / 3.9 are end-of-life).
- Public state-reset API:
reset(),reset_rts(),reset_mask()— replaces the v3__reset__/__reset_mask__/__reset_RTs__accessors. - Deprecated v3 names removed:
down_folder→walk_through_folders,where_are_RTs→where_is_ROI,with_annotations→prediction_array_to_RT, plus the__set_iteration__and__set_description__setters renamed toset_iteration/set_description. SeeCHANGELOG.mdfor the full list and migration notes. - Excel → CSV for both bulk-export helpers, dropping the
openpyxldependency:characterize_data_to_excelis nowcharacterize_data_to_csv, andwrite_parallel(excel_file=…)is nowwrite_parallel(index_file=…)accepting a.csvpath. struct_pydicom_string_keysplumbing finally works — historically the parameter was accepted but the values never reached the parsed RT records.- Architecture: the original
ReaderWriter.pygod-class has been partly extracted into a new internal_internal/package. The publicDicomReaderWriterAPI is unchanged. - Hermetic test suite: every DICOM file the tests need is generated in a tmp directory at session start from analytical primitives. No external corpus, no network, no caches — the full suite runs in ~6 seconds and validates against analytically-known volume truth.
- Tooling: ruff replaces flake8; PyPI Trusted Publishing replaces the PYPI_TOKEN secret; CI matrix expanded to ubuntu + windows × four Python versions; pre-commit config added.
License
Citation
If you find this code useful, please reference the publication and the GitHub page.
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 dicomrttool-4.0.0.tar.gz.
File metadata
- Download URL: dicomrttool-4.0.0.tar.gz
- Upload date:
- Size: 52.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
194e0ac2b4532aa9961032a37efb1426d6027eda47b3c856427565d34a1844de
|
|
| MD5 |
bf064df8c8d505b6032d787f5a67ab68
|
|
| BLAKE2b-256 |
4a24ca26f8d162665f47113d5c3875dd6dc4cd9a77942c9c6d20f3de5cef44b9
|
Provenance
The following attestation bundles were made for dicomrttool-4.0.0.tar.gz:
Publisher:
python-publish.yml on brianmanderson/Dicom_RT_and_Images_to_Mask
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dicomrttool-4.0.0.tar.gz -
Subject digest:
194e0ac2b4532aa9961032a37efb1426d6027eda47b3c856427565d34a1844de - Sigstore transparency entry: 1421608184
- Sigstore integration time:
-
Permalink:
brianmanderson/Dicom_RT_and_Images_to_Mask@0ba543369a0223cbbee4528777c87f803bc1bf94 -
Branch / Tag:
refs/tags/v4.0.0 - Owner: https://github.com/brianmanderson
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@0ba543369a0223cbbee4528777c87f803bc1bf94 -
Trigger Event:
push
-
Statement type:
File details
Details for the file dicomrttool-4.0.0-py3-none-any.whl.
File metadata
- Download URL: dicomrttool-4.0.0-py3-none-any.whl
- Upload date:
- Size: 43.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
970fd7ba6267b5b8110412a708bc08914e685a7197207c138c8592764d4dea3d
|
|
| MD5 |
972b8a17fbd57a38d71eb1840c9e9e3b
|
|
| BLAKE2b-256 |
3812d34457264b520a2f634fe50e29b318b4916a3bd09280beaca81b4941c032
|
Provenance
The following attestation bundles were made for dicomrttool-4.0.0-py3-none-any.whl:
Publisher:
python-publish.yml on brianmanderson/Dicom_RT_and_Images_to_Mask
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dicomrttool-4.0.0-py3-none-any.whl -
Subject digest:
970fd7ba6267b5b8110412a708bc08914e685a7197207c138c8592764d4dea3d - Sigstore transparency entry: 1421608275
- Sigstore integration time:
-
Permalink:
brianmanderson/Dicom_RT_and_Images_to_Mask@0ba543369a0223cbbee4528777c87f803bc1bf94 -
Branch / Tag:
refs/tags/v4.0.0 - Owner: https://github.com/brianmanderson
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@0ba543369a0223cbbee4528777c87f803bc1bf94 -
Trigger Event:
push
-
Statement type: