start new dicom diff

This commit is contained in:
Ross
2024-02-07 14:25:50 +00:00
parent 9ff395325f
commit e21321adc2
2 changed files with 55 additions and 1 deletions
+50
View File
@@ -218,3 +218,53 @@ def get_image_hash(img, dataset=None, hash_type="blake3", direct_pixel_data=True
return hash, is_dicom
def compare_dicom_elements(element1: pydicom.DataElement, element2: pydicom.DataElement):
differences = {}
# Compare the element tags
try:
if element1.tag != element2.tag:
differences['tag'] = (element1.tag, element2.tag)
except AttributeError: # private elements won't/might not? have a tag
pass
# Compare the VR (Value Representation)
if element1.VR != element2.VR:
differences['VR'] = (element1.VR, element2.VR)
# Compare the value
if element1.value != element2.value:
differences['value'] = (element1.value, element2.value)
# Recursively compare the child elements
if element1.VR == 'SQ' and element2.VR == 'SQ':
for i, (item1, item2) in enumerate(zip(element1.value, element2.value)):
item_differences = compare_dicom_elements(item1, item2)
if item_differences:
differences[f'item{i+1}'] = item_differences
return differences
def compare_dicom_datasets(dataset1: pydicom.Dataset, dataset2: pydicom.Dataset):
differences = {}
# Compare the dataset elements
for element1 in dataset1:
element2 = dataset2.get(element1.tag)
if element2 is None:
differences[element1.tag] = ('missing', element1.value)
else:
element_differences = compare_dicom_elements(element1, element2)
if element_differences:
differences[element1.tag] = element_differences
# Check for any extra elements in dataset2
for element2 in dataset2:
element1 = dataset1.get(element2.tag)
if element1 is None:
differences[element2.tag] = ('extra', element2.value)
return differences