import pydicom
from pydicom.datadict import keyword_dict
# Lots of code skipped here, following function is actually part of a class
# self.dcm is output from pydicom.read_file()
def sync_referenced_tag(self, keyword, old_value, new_value):
"""
Check if there is a Referenced tag with matching value, then set to new_value if so
:param keyword: DICOM tag keyword
:type keyword: str
:param old_value: if Referenced+keyword tag value is this value, update to new_value
:param new_value: new value of tag if connected
"""
tag = keyword_dict.get("Referenced%s" % keyword)
if tag is not None:
# Edit top-level tag value of dataset's original value is provided old_value
# self.init_tag_values only contain edited tag values
if self.init_tag_values.get(tag) == old_value or \
(tag in list(self.dcm) and self.get_tag_value(tag) == old_value):
self.edit_tag(tag, new_value)
# Find all top-level sequences with names that begin with 'Referenced'
# iterate through all keywords that start with 'Referenced', of each sequence
# if value matches old_value and its tag matches 'Referenced'+keyword, set to new_value
for key in self.dcm.trait_names():
if key.startswith('Referenced'):
dcm_item = getattr(self.dcm, key)
if isinstance(dcm_item, pydicom.sequence.Sequence):
for seq_item in dcm_item:
seq_keys = [sk for sk in seq_item.trait_names() if sk.startswith('Referenced')]
for sk in seq_keys:
if getattr(seq_item, sk) == old_value and sk == 'Referenced' + keyword:
setattr(seq_item, sk, new_value)
Study iUID (shared by all the below)
CTs
Series iUID (same for all slices)
->SS -> Refd Frame of Reference Seq -> RT Refd Study Seq -> RT Refd Series Seq -> refs the Series iUID and then -> Contours each ref individual CT slice SOPiUID
-> RTPlan -> Refd Structure Set Seq (n=1 only) -> Refd SOPiUID
-> RTDose-> Refd RT Plan Seq (n=**) -> SOP iUID
** n=1 unless DoseSummationType = “MULTI_PLAN”
-> RTImage -> Refd RT Plan Seq -> Refd SOPiUID