Skip to content

Commit

Permalink
Add shorthand function to merge two pre-existing RTStruct structure s…
Browse files Browse the repository at this point in the history
…ets (qurit#70)

✨ Add class to merge two pre-existing RTStruct structure sets
  • Loading branch information
rheg49 committed Feb 1, 2023
1 parent b70ad40 commit f95e997
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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

0 comments on commit f95e997

Please sign in to comment.