TestCenter Reference
DicomTreeCompare.py
Go to the documentation of this file.
2# Copyright 2021, MeVis Medical Solutions AG
3#
4# The user may use this file in accordance with the license agreement provided with
5# the Software or, alternatively, in accordance with the terms contained in a
6# written agreement between the user and MeVis Medical Solutions AG.
7#
8# For further information use the contact form at https://www.mevislab.de/contact
9#
10
11from mevis import MLAB
12
13
14# -- class DicomTreeCompare ------------------------------------------------------------------{{{-
15
17
18 def __init__(self):
19 self.printDifferences = True
21 self.outputFunction = MLAB.logError
22 self._hadDifferences = False
23
24 def hadDifferences(self):
25 return self._hadDifferences
26
27 def tagName(self, tag):
28 name = tag.tagName()
29 if not name:
30 name = tag.id()
31 elif name == "PrivateCreator":
32 name += " '" + tag.toString() + "' " + tag.id()
33 else:
34 name += " " + tag.id()
35 return name
36
37 def dicomPathToString(self, path):
38 result = ""
39 for tag, index in path:
40 result += f"in {self.tagName(tag)}, position {index} "
41 return result
42
43 def tagValueToString(self, tag):
44 if tag.isBinary():
45 return f"<{tag.numberOfValues()} items>"
46 else:
47 return f"'{tag.toString()}'"
48
49 def dicomTagError(self, path, message, tag=None):
50 if self.printDifferences:
51 if not self._hadDifferences or not self.printOnlyFirstDifference:
52 tagString = (", tag " + self.tagName(tag)) if tag else ""
53 msg = self.dicomPathToString(path) + message + tagString
54 self.outputFunction(msg)
55 self._hadDifferences = True
56
57 def compareTags(self, tag1, tag2, path):
58 if tag1.vr() != tag2.vr():
59 self.dicomTagError(path, f"Different VRs ({tag1.vr()} and {tag2.vr()})", tag1)
60 return False
61 elif tag1.isSequence():
62 n1 = tag1.numberOfSequenceItems()
63 n2 = tag2.numberOfSequenceItems()
64 if n1 != n2:
65 self.dicomTagError(path, f"Tags have different number of sequence items ({n1} and {n2})", tag1)
66 return False
67 else:
68 ok = True
69 for i in range(tag1.numberOfSequenceItems()):
70 ok = self.compareTrees(tag1.getSequenceItem(i), tag2.getSequenceItem(i), path + ((tag1, i),)) and ok
71 return ok
72 elif not tag1.isEqual(tag2):
73 s1 = self.tagValueToString(tag1)
74 s2 = self.tagValueToString(tag2)
75 self.dicomTagError(path, f"Different values ({s1} != {s2})", tag1)
76 return False
77 else:
78 return True
79
80 def compareTrees(self, tree1, tree2, path=()):
81 ok = True
82 if tree1 and not tree2:
83 self.dicomTagError(path, "Second tree not existing")
84 return False
85 elif tree2 and not tree1:
86 self.dicomTagError(path, "First tree not existing")
87 return False
88 tags1 = list(tree1.getTags())
89 tags2 = list(tree2.getTags())
90 tag1 = tags1.pop(0) if tags1 else None
91 tag2 = tags2.pop(0) if tags2 else None
92 while tag1 or tag2:
93 if (
94 not tag2
95 or tag1.groupId() < tag2.groupId()
96 or (tag1.groupId() == tag2.groupId() and tag1.elementId() < tag2.elementId())
97 ):
98 self.dicomTagError(path, "Extra tag in first tree", tag1)
99 ok = False
100 tag1 = tags1.pop(0) if tags1 else None
101 elif (
102 not tag1
103 or tag1.groupId() > tag2.groupId()
104 or (tag1.groupId() == tag2.groupId() and tag1.elementId() > tag2.elementId())
105 ):
106 self.dicomTagError(path, "Extra tag in second tree", tag2)
107 ok = False
108 tag2 = tags2.pop(0) if tags2 else None
109 else:
110 ok = self.compareTags(tag1, tag2, path) and ok
111 tag1 = tags1.pop(0) if tags1 else None
112 tag2 = tags2.pop(0) if tags2 else None
113 return ok
114
115
116# --------------------------------------------------------------------------------------------}}}-
117
118
119# -- def compareDicomTrees -------------------------------------------------------------------{{{-
120
133 tree1, tree2, printDifferences=True, printOnlyFirstDifference=False, outputFunction=MLAB.logError
134):
135 dc = DicomTreeCompare()
136 dc.printDifferences = printDifferences
137 dc.printOnlyFirstDifference = printOnlyFirstDifference
138 dc.outputFunction = outputFunction
139 return dc.compareTrees(tree1, tree2)
140
141
142# --------------------------------------------------------------------------------------------}}}-
Helper class, please use compareDicomTrees()
dicomTagError(self, path, message, tag=None)
compareDicomTrees(tree1, tree2, printDifferences=True, printOnlyFirstDifference=False, outputFunction=MLAB.logError)
Compare the two given DICOM trees (of type MLABDicomTree) for equality.