add reasoning to the dedupe test adjustments
@@ -35,6 +35,10 @@ def test_structure_dedupe():
baseline_path = input_path.parent.parent.joinpath("baseline", input_path.name)
baseline_dcm = pydicom.read_file(str(baseline_path), force=True)
+ # NOTE: Sorting was added in due to an upgrade in shapely which
+ # resulted in a different structure insertion order. See
+ # <https://github.com/pydicom/pydicom/issues/1473#issuecomment-895622509>
+ # for more details.
input_dcm = _sort_ds(input_dcm)
baseline_dcm = _sort_ds(baseline_dcm)
@@ -65,6 +69,8 @@ def test_structure_dedupe():
def _sort_ds(ds: "pydicom.Dataset"):
+ """Sort the pydicom Dataset so that structure insertion order doesn't
+ result in an inequality"""
json_dict = ds.to_json_dict()
_sort_contour_sequences(dicom_json_dataset=json_dict)
@@ -74,6 +80,7 @@ def _sort_ds(ds: "pydicom.Dataset"):
def _sort_contour_sequences(dicom_json_dataset):
+ """Specifically sort the contour sequence ordering by SOP Instance UID"""
for key, value in dicom_json_dataset.items():
if not isinstance(value, dict):
return
@@ -83,15 +90,24 @@ def _sort_contour_sequences(dicom_json_dataset):
if isinstance(item, dict):
_sort_contour_sequences(dicom_json_dataset=item)
- # Contour Sequence
- if key == "30060040":
+ CONTOUR_SEQUENCE_TAG = "30060040"
+ if key == CONTOUR_SEQUENCE_TAG:
value["Value"] = sorted(value["Value"], key=_dict_sorting)
except KeyError:
return
def _dict_sorting(d):
- referenced_sop_instance = d["30060016"]["Value"][0]["00081155"]["Value"][0]
- contour_data = d["30060050"]["Value"]
+ """A dictionary sorting key to be utilised on a DICOM JSON representation
+ of a Contour Sequence Item"""
+ CONTOUR_IMAGE_SEQUENCE_TAG = "30060016"
+ REFERENCED_SOP_INSTANCE_UID_TAG = "00081155"
+
+ referenced_sop_instance = d[CONTOUR_IMAGE_SEQUENCE_TAG]["Value"][0][
+ REFERENCED_SOP_INSTANCE_UID_TAG
+ ]["Value"][0]
+
+ CONTOUR_DATA_TAG = "30060050"
+ contour_data = d[CONTOUR_DATA_TAG]["Value"]
return f"{referenced_sop_instance}.{str(len(contour_data)).zfill(6)}"
