Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shorthand function to merge two pre-existing RTStruct structure sets #70

Merged
merged 4 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rt_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .rtstruct import RTStruct
from .rtstruct_builder import RTStructBuilder
from .rtstruct_merger import RTStructMerger
33 changes: 33 additions & 0 deletions rt_utils/rtstruct_merger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from .rtstruct import RTStruct
from .rtstruct_builder import RTStructBuilder
from . import ds_helper, image_helper

class RTStructMerger:


@staticmethod
def merge_rtstructs(dicom_series_path: str, rt_struct_path1: str,
rt_struct_path2: str) -> RTStruct:
"""
Method to merge two existing RTStruct files belonging to same series data, returning them as one RTStruct
"""

rtstruct1 = RTStructBuilder.create_from(dicom_series_path, rt_struct_path1)
rtstruct2 = RTStructBuilder.create_from(dicom_series_path, rt_struct_path2)

for roi_contour_seq, struct_set_roi_seq, rt_roi_observation_seq in zip(rtstruct1.ds.ROIContourSequence, rtstruct1.ds.StructureSetROISequence, rtstruct1.ds.RTROIObservationsSequence):
roi_number = len(rtstruct2.ds.StructureSetROISequence) + 1
roi_contour_seq.ReferencedROINumber = roi_number
struct_set_roi_seq.ROINumber = roi_number
rt_roi_observation_seq.ReferencedROINumber = roi_number

# check for ROI name duplication
for struct_set_roi_seq2 in rtstruct2.ds.StructureSetROISequence:
if struct_set_roi_seq.ROIName == struct_set_roi_seq2.ROIName:
struct_set_roi_seq += "_2"

rtstruct2.ds.ROIContourSequence.append(roi_contour_seq)
rtstruct2.ds.StructureSetROISequence.append(struct_set_roi_seq)
rtstruct2.ds.RTROIObservationsSequence.append(rt_roi_observation_seq)

return rtstruct2