How to Use RT-Utils: A Minimal Python Library for RT Struct Manipulation

Jun 19, 2024 | Educational

Welcome to the world of RT-Utils! This Python library is designed to simplify the process of managing and manipulating RT Structs, allowing healthcare professionals to efficiently view and analyze DICOM images. In this guide, we’ll walk you through how to get started with RT-Utils, create new RT Structs, load existing ones, and even merge different RT Structs. Along the way, we’ll make sure you have all the tools you need to succeed. So, let’s dive in!

What is RT-Utils?

RT-Utils is a minimal Python library that enables users to perform a variety of tasks related to RT Struct manipulation. With it, you can:

  • Create and load RT Structs from DICOM images.
  • Extract 3D masks from RT Struct Regions of Interest (ROIs).
  • Add multiple ROIs and save the resulting RT Struct effortlessly.
  • Merge two existing RT Structs into one file.

How to Install RT-Utils

The installation of RT-Utils is straightforward. You can choose between the standard and editable installation methods:

  • Standard Installation: Run the following command in your terminal:
    pip install rt_utils
  • Editable Mode: If you want to contribute to the library design or testing, do the following:
    git clone https://github.com/qurit/rt-utils.git
    cd rt-utils
    pip install -e .

Creating New RT Structs

Creating RT Structs with RT-Utils can be achieved in just a few lines of code. Here’s how:

from rt_utils import RTStructBuilder

# Create new RT Struct with the DICOM series path
rtstruct = RTStructBuilder.create_new(dicom_series_path='test/location')

# ... Create mask through means such as ML ...

# Add the 3D mask as an ROI (Auto-generated color and description)
rtstruct.add_roi(mask=MASK_FROM_ML_MODEL)

# Add another ROI with specified color and name
rtstruct.add_roi(mask=MASK_FROM_ML_MODEL, color=[255, 0, 255], name='RT-Utils ROI!')

# Save new RT Struct
rtstruct.save('new-rt-struct')

To understand this better, think of an architect drawing up blueprints for a building. The RTStructBuilder is like a software application that lets the architect create this structure step-by-step, starting from the foundation to the finishing touches (here represented by adding regions and saving the blueprint).

Loading Existing RT Structs

Loading an existing RT Struct is just as simple as creating one:

from rt_utils import RTStructBuilder
import matplotlib.pyplot as plt

# Load existing RT Struct
rtstruct = RTStructBuilder.create_from(dicom_series_path='test/location', rt_struct_path='test/location/rt-struct.dcm')

# Print all ROI names within the image
print(rtstruct.get_roi_names())

# Loading the 3D Mask for a specified ROI
mask_3d = rtstruct.get_roi_mask_by_name('ROI NAME')

# Display one slice of the region
first_mask_slice = mask_3d[:, :, 0]
plt.imshow(first_mask_slice)
plt.show()

Imagine you are visiting a gallery where a variety of paintings (ROIs) are displayed. By using RT-Utils, you can navigate through all the paintings (or ROIs) and even take a closer look at any single masterpiece (specific mask slice) that catches your eye.

Merging Existing RT Structs

To combine two existing RT Structs, ensure both belong to the same image series. Here’s how you can merge them:

from rt_utils import RTStructMerger

# Load existing RT Structs and merge them
merged_rt_struct = RTStructMerger.merge_rtstructs(
  dicom_series_path='test/location',
  rt_struct_path1='test/location/rt-struct1.dcm',
  rt_struct_path2='test/location/rt-struct2.dcm'
)

# Save merged RT Struct
merged_rt_struct.save('merged-rt-struct')

Think of this process as combining two puzzle pieces; both pieces need to fit within the same frame for a cohesive picture (same image series). When merged, they form a new, comprehensive image that is useful for analysis.

Troubleshooting

If you encounter issues while using RT-Utils, consider the following tips:

  • Check the DICOM series path: Ensure that you are providing the correct path to your DICOM series.
  • Verify your masks: Ensure that the masks you are attempting to add conform to the expected format—an nd numpy array of type bool.
  • Look for updates: Make sure you are using the latest version of RT-Utils by checking it on PyPI.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

RT-Utils provides a powerful yet user-friendly interface for managing RT Structs, enabling medical professionals to manipulate DICOM files easily. With functions for creation, loading, and merging RT Structs, this library serves as an excellent tool to streamline workflows in medical imaging.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox