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
+5 -1
View File
@@ -117,7 +117,7 @@ import plotly.express as px
from django.core.cache import cache
from django.conf import settings
from helpers.images import image_as_base64, print_dicom
from helpers.images import compare_dicom_datasets, image_as_base64, print_dicom
import logging
from copy import deepcopy
@@ -1063,9 +1063,13 @@ def image_diff(request):
s = print_dicom(second_ds, join=False)
diff = difflib.HtmlDiff().make_table(f, s)
#dict_diff = compare_dicom_datasets(first_ds, second_ds)
return HttpResponse(diff)
#return HttpResponse(f"<pre>{dict_diff}</pre><br/>{diff}")
return HttpResponse("Fail")
+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