fuzzycomparevtu.py 3.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
import argparse
from xml.dom import minidom

# fuzzy compare XML tree from XML strings
def isFuzzyEqualXml(xml1, xml2, absolute, relative):
    dom1 = minidom.parseString(xml1)
    dom2 = minidom.parseString(xml2)
    return isFuzzyEqualNode(dom1.documentElement, dom2.documentElement, absolute, relative)

# fuzzy compare of XML nodes
def isFuzzyEqualNode(node1, node2, absolute, relative):
Thomas Fetzer's avatar
Thomas Fetzer committed
12
    noError = True
13
14
15
16
    if node1.tagName != node2.tagName:
        print 'The name of the node differs in ', node1.tagName, ' and ', node2.tagName
        return False
    if sorted(node1.attributes.items()) != sorted(node2.attributes.items()):
Thomas Fetzer's avatar
[ctest]    
Thomas Fetzer committed
17
        print 'Attributes differ in node ', node1.tagName
18
19
20
21
22
23
        return False
    if len(node1.childNodes) != len(node2.childNodes):
        print 'Number of children differs in node ', node1.tagName
        return False
    for node1child, node2child in zip(node1.childNodes, node2.childNodes):
        if node1child.nodeType != node2child.nodeType:
24
            print 'Node type differs in ', node1.tagName
25
26
            return False
        if node1child.nodeType == node1child.TEXT_NODE and not isFuzzyEqualText(node1child.data, node2child.data, absolute, relative):
27
            print 'Data differs in parameter ', node2.attributes.items(), ' at node ', node2child.nodeType
Thomas Fetzer's avatar
Thomas Fetzer committed
28
            noError = False
Thomas Fetzer's avatar
Thomas Fetzer committed
29
            continue
30
        if node1child.nodeType == node1child.ELEMENT_NODE and not isFuzzyEqualNode(node1child, node2child, absolute, relative):
Thomas Fetzer's avatar
Thomas Fetzer committed
31
32
33
            noError = False
            continue
    return noError
34
35
36
37
38
39
40
41
42

# fuzzy compare of text consisting of whitespace separated numbers
def isFuzzyEqualText(text1, text2, absolute, relative):
    list1 = text1.split()
    list2 = text2.split()
    # difference only in whitespace?
    if (list1 == list2):
        return True
    # compare number by number
Thomas Fetzer's avatar
Thomas Fetzer committed
43
44
    maximum1 = 0.0
    maximum2 = 0.0
45
46
47
48
49
    for number1, number2 in zip(list1, list2):
        number1 = float(number1)
        number2 = float(number2)
        if (abs(number1 - number2) > absolute 
            and (number2 == 0.0 or abs(abs(number1 / number2) - 1.0) > relative)):
Thomas Fetzer's avatar
Thomas Fetzer committed
50
51
52
53
54
55
            if (abs(number1 - number2) > abs(maximum1 - maximum2)):
                maximum1 = float(number1)
                maximum2 = float(number2)
    if (abs(maximum1 - maximum2) > 0.0):
        print 'Difference is too large between', maximum1, ' and ', maximum2
        return False
56
57
    return True

Thomas Fetzer's avatar
[ctest]    
Thomas Fetzer committed
58
# main program
59
# handle arguments and print help message
60
parser = argparse.ArgumentParser(description='Fuzzy compare of two VTK\
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    (Visualization Toolkit) files. The files are accepted if for every\
    value the difference is below the absolute error or below the\
    relative error or below both.')
parser.add_argument('vtu_file_1', type=open,
    help='first file to compare')
parser.add_argument('vtu_file_2', type=open,
    help='second file to compare')
parser.add_argument('-r', '--relative', type=float, default=1e-2,
    help='maximum relative error (default=1e-2)')
parser.add_argument('-a', '--absolute', type=float, default=1e-9,
    help='maximum relative error (default=1e-9)')
args = parser.parse_args()

# fuzzy compare
if (isFuzzyEqualXml(args.vtu_file_1.read(), args.vtu_file_2.read(), args.absolute, args.relative)):
    exit
else:
    exit(1)