From 0534c29c0343917c1ecc1090b4e0c717d068d031 Mon Sep 17 00:00:00 2001 From: MLK97 Date: Mon, 19 Feb 2024 13:06:28 +0100 Subject: [PATCH 1/2] fixed bug in contour sequence causing warning messages in some treatment plannings software --- rt_utils/ds_helper.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/rt_utils/ds_helper.py b/rt_utils/ds_helper.py index 36c4db7..7c0e0d7 100644 --- a/rt_utils/ds_helper.py +++ b/rt_utils/ds_helper.py @@ -168,9 +168,11 @@ def create_contour_sequence(roi_data: ROIData, series_data) -> Sequence: contours_coords = get_contours_coords(roi_data, series_data) for series_slice, slice_contours in zip(series_data, contours_coords): + for contour_data in slice_contours: contour = create_contour(series_slice, contour_data) - contour_sequence.append(contour) + if contour.ContourGeometricType == 'CLOSED_PLANAR' and contour.NumberOfContourPoints > 2: + contour_sequence.append(contour) return contour_sequence @@ -192,9 +194,7 @@ def create_contour(series_slice: Dataset, contour_data: np.ndarray) -> Dataset: contour.NumberOfContourPoints = ( len(contour_data) / 3 ) # Each point has an x, y, and z value - - # Rounds ContourData to 10 decimal places to ensure it is <16 bytes length, as per NEMA DICOM standard guidelines. - contour.ContourData = [round(val, 10) for val in contour_data] + contour.ContourData = contour_data return contour @@ -216,9 +216,6 @@ def get_contour_sequence_by_roi_number(ds, roi_number): # Ensure same type if str(roi_contour.ReferencedROINumber) == str(roi_number): - if hasattr(roi_contour, "ContourSequence"): - return roi_contour.ContourSequence - else: - return Sequence() + return roi_contour.ContourSequence raise Exception(f"Referenced ROI number '{roi_number}' not found") From 2c50a04a98955ad4794c4625611a404c9cad8694 Mon Sep 17 00:00:00 2001 From: MLK97 Date: Mon, 19 Feb 2024 14:02:56 +0100 Subject: [PATCH 2/2] futureproof contour_sequence append and sync changes from recent upstream commit --- rt_utils/ds_helper.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/rt_utils/ds_helper.py b/rt_utils/ds_helper.py index 7c0e0d7..8d08a6b 100644 --- a/rt_utils/ds_helper.py +++ b/rt_utils/ds_helper.py @@ -171,7 +171,7 @@ def create_contour_sequence(roi_data: ROIData, series_data) -> Sequence: for contour_data in slice_contours: contour = create_contour(series_slice, contour_data) - if contour.ContourGeometricType == 'CLOSED_PLANAR' and contour.NumberOfContourPoints > 2: + if (contour.ContourGeometricType == 'CLOSED_PLANAR' and contour.NumberOfContourPoints > 2) or contour.ContourGeometricType != 'CLOSED_PLANAR': contour_sequence.append(contour) return contour_sequence @@ -194,7 +194,9 @@ def create_contour(series_slice: Dataset, contour_data: np.ndarray) -> Dataset: contour.NumberOfContourPoints = ( len(contour_data) / 3 ) # Each point has an x, y, and z value - contour.ContourData = contour_data + + # Rounds ContourData to 10 decimal places to ensure it is <16 bytes length, as per NEMA DICOM standard guidelines. + contour.ContourData = [round(val, 10) for val in contour_data] return contour @@ -216,6 +218,9 @@ def get_contour_sequence_by_roi_number(ds, roi_number): # Ensure same type if str(roi_contour.ReferencedROINumber) == str(roi_number): - return roi_contour.ContourSequence + if hasattr(roi_contour, "ContourSequence"): + return roi_contour.ContourSequence + else: + return Sequence() raise Exception(f"Referenced ROI number '{roi_number}' not found")