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# -- class DicomTreeCompare ------------------------------------------------------------------{{{-
14
16
17 def __init__(self):
18 self.printDifferences = True
20 self.outputFunction = MLAB.logError
21 self._hadDifferences = False
22
23 def hadDifferences(self):
24 return self._hadDifferences
25
26 def tagName(self, tag):
27 name = tag.tagName()
28 if not name:
29 name = tag.id()
30 elif name == "PrivateCreator":
31 name += " '" + tag.toString() + "' " + tag.id()
32 else:
33 name += " " + tag.id()
34 return name
35
36 def dicomPathToString(self, path):
37 result = ""
38 for tag, index in path:
39 result += f"in {self.tagName(tag)}, position {index} "
40 return result
41
42 def tagValueToString(self, tag):
43 if tag.isBinary():
44 return f"<{tag.numberOfValues()} items>"
45 else:
46 return f"'{tag.toString()}'"
47
48 def dicomTagError(self, path, message, tag = None):
49 if self.printDifferences:
50 if not self._hadDifferences or not self.printOnlyFirstDifference:
51 tagString = (", tag " + self.tagName(tag)) if tag else ""
52 msg = self.dicomPathToString(path) + message + tagString
53 self.outputFunction(msg)
54 self._hadDifferences = True
55
56 def compareTags(self, tag1, tag2, path):
57 if tag1.vr() != tag2.vr():
58 self.dicomTagError(path, f"Different VRs ({tag1.vr()} and {tag2.vr()})", tag1)
59 return False
60 elif tag1.isSequence():
61 n1 = tag1.numberOfSequenceItems()
62 n2 = tag2.numberOfSequenceItems()
63 if n1 != n2:
64 self.dicomTagError(path, f"Tags have different number of sequence items ({n1} and {n2})", tag1)
65 return False
66 else:
67 ok = True
68 for i in range(tag1.numberOfSequenceItems()):
69 ok = self.compareTrees(tag1.getSequenceItem(i), tag2.getSequenceItem(i), path + ((tag1, i),)) and ok
70 return ok
71 elif not tag1.isEqual(tag2):
72 s1 = self.tagValueToString(tag1)
73 s2 = self.tagValueToString(tag2)
74 self.dicomTagError(path, f"Different values ({s1} != {s2})", tag1)
75 return False
76 else:
77 return True
78
79 def compareTrees(self, tree1, tree2, path = ()):
80 ok = True
81 if tree1 and not tree2:
82 self.dicomTagError(path, "Second tree not existing")
83 return False
84 elif tree2 and not tree1:
85 self.dicomTagError(path, "First tree not existing")
86 return False
87 tags1 = list(tree1.getTags())
88 tags2 = list(tree2.getTags())
89 tag1 = tags1.pop(0) if tags1 else None
90 tag2 = tags2.pop(0) if tags2 else None
91 while tag1 or tag2:
92 if not tag2 or tag1.groupId() < tag2.groupId() or (tag1.groupId() == tag2.groupId() and tag1.elementId() < tag2.elementId()):
93 self.dicomTagError(path, "Extra tag in first tree", tag1)
94 ok = False
95 tag1 = tags1.pop(0) if tags1 else None
96 elif not tag1 or tag1.groupId() > tag2.groupId() or (tag1.groupId() == tag2.groupId() and tag1.elementId() > tag2.elementId()):
97 self.dicomTagError(path, "Extra tag in second tree", tag2)
98 ok = False
99 tag2 = tags2.pop(0) if tags2 else None
100 else:
101 ok = self.compareTags(tag1, tag2, path) and ok
102 tag1 = tags1.pop(0) if tags1 else None
103 tag2 = tags2.pop(0) if tags2 else None
104 return ok
105# --------------------------------------------------------------------------------------------}}}-
106
107# -- def compareDicomTrees -------------------------------------------------------------------{{{-
108
120def compareDicomTrees(tree1, tree2, printDifferences=True, printOnlyFirstDifference=False, outputFunction=MLAB.logError):
121 dc = DicomTreeCompare()
122 dc.printDifferences = printDifferences
123 dc.printOnlyFirstDifference = printOnlyFirstDifference
124 dc.outputFunction = outputFunction
125 return dc.compareTrees(tree1, tree2)
126# --------------------------------------------------------------------------------------------}}}-
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.