script.py 5.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# ================================================================================================
# 
# This file is part of the CAMPVis Software Framework.
# 
# If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
#      Christian Schulte zu Berge <christian.szb@in.tum.de>
#      Chair for Computer Aided Medical Procedures
#      Technische Universitaet Muenchen
#      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
# 
# For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
# 
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
# except in compliance with the License. You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software distributed under the 
# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
# either express or implied. See the License for the specific language governing permissions 
# and limitations under the License.
# 
# ================================================================================================

import os
26
import os.path
27
28
import xml.etree.ElementTree as et
import numpy as np
29
import shutil as fio
30

31
from skimage import io, color
32
33
from xml.etree import ElementTree
from xml.dom import minidom
34
from skimage.measure import structural_similarity as ssim
35
36
37
38
39
40

# Return a pretty-printed XML string for the Element
def prettify(elem):
    rough_string = ElementTree.tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="  ")
41
42
43
44

refDir = 'reference/';
testDir = 'testruns/';
resultDir = 'results/';
45
failedDir = 'failed/';
46
47
if (not os.path.exists(resultDir)) :
        os.mkdir(resultDir)
48
49
50

casesDir = os.listdir(refDir);

51
52
53
54
55
56
# Find or create results.xml file created by test-campvis
xmlFile = "result.xml";
if (os.path.isfile(xmlFile)) :
    tree = et.parse(xmlFile);
    root = tree.getroot();
else :
57
    # XML
58
59
60
61
    root = et.Element("testsuites", {"tests":"0", "failures":"0", "disabled":"0", 
    "errors":"0", "timestamp":"2014-08-24T01:35:42", "time":"0", "name":"AllTests"}); 
    tree = et.ElementTree(root);
    
62
63
64
65
66
67
68
69
70
71
curTestDir = testDir;
resultSaveDir = resultDir;
for case in casesDir :
    refCaseDir = refDir + case + "/";
    testCaseDir = curTestDir + case + "/";
    resCaseDir = resultSaveDir + case + "/";
    # if no corresponding test directory - continue
    if (not os.path.exists(testCaseDir)) :
        continue;
    if (not os.path.exists(resCaseDir)) :
72
        os.mkdir(resCaseDir)
73
74
75

    files = os.listdir(refCaseDir)
    if (len(files) != 0) :
76
        # XML
77
78
79
80
81
82
83
84
        suite = et.SubElement(root, "testsuite", {"name":refCaseDir, "tests":"0", 
        "failures":"0", "disabled":"0", "errors":"0", "time":"0"});

    i = 0;
    for file in files :
        refFilePath = refCaseDir + file;
        testFilePath = testCaseDir + file;
        resFilePath = resCaseDir + file;
85
        alphaFilePath = resFilePath[:-4]+"_aplha"+resFilePath[-4:];
86
87
88
89
90
91
92
93
94
        # Check existence of test file       
        if (not os.path.isfile(testFilePath)) :
            continue;

        if (refFilePath[-4:] != ".jpg" and refFilePath[-4:] != ".png" 
        and refFilePath[-4:] != ".tif") :
            continue;

        ref = io.imread(refFilePath);
95
        testim = io.imread(testFilePath);
96
        # Check dimension of the file before finding difference
97
98
        if (ref.shape == testim.shape) :
            test = ref-testim;
99
        else :
100
            print "i'm in else! HELP me!"
101
            test = ref;
102
103
104
105
106
        # Store fully opaque image    
        rgb = test[:, :, : 3];
        alpha = test[:, :, 3:];
        opaque = [[[255]*alpha.shape[2]] * alpha.shape[1]] * alpha.shape[0]
        #io.imsave(resFilePath, rgb);
107
108
        io.imsave(resFilePath, np.concatenate((rgb, opaque), axis=2));
        io.imsave(alphaFilePath, alpha);
109
110
111
112
113
114
115
        # Calculate MSE and SSIM
        mse = np.linalg.norm(test);
        reff = color.rgb2gray(ref);
        testf = color.rgb2gray(test);
        ssimval = ssim(reff, testf);

        # XML
116
117
118
119
120
121
        case = et.SubElement(suite, "testcase", {"name":file, "status":"run", 
        "time":"0", "classname":refCaseDir});
        suite.set("tests", str(int(suite.get("tests"))+1));
        root.set("tests", str(int(root.get("tests"))+1));

        if (np.sum(test) != 0) :
122
            # Prepare and write messages to show in stacktrace
123
124
            failure = et.SubElement(case, "failure", {"message":"", "type":""});
            failure.set("message", "Image difference is not 0");
125
126
127
128
129
130
131
132
133
134
            alphamsg = "differ in both RGB and aplha channels";
            if (sum(1 for x in rgb if x.any() > 0) == 0):
                alphamsg = "differ in transparency level only";
            elif (sum(1 for x in alpha if x.any() > 0) == 0) :
                alphamsg = "differ in RGB channels only";

            failure.text = "Reference and test images differ in " \
            + str(sum(1 for x in test if x.any() > 0)) + " pixel/s\n" \
            + "and images " + alphamsg \
            + "\nMSE: " + str(mse) + " SSIM: " + str(ssimval);
135
136
137

            suite.set("failures", str(int(suite.get("failures")) + 1));
            root.set("failures", str(int(root.get("failures")) + 1));
138
            
139
            # Copy artifacts to failed directory
140
141
142
            if (not os.path.exists(failedDir + refCaseDir)) :
                os.makedirs(failedDir + refCaseDir);
            fio.copy(refFilePath, failedDir + refFilePath);
143
            
144
145
146
            if (not os.path.exists(failedDir + testCaseDir)) :
                os.makedirs(failedDir + testCaseDir);
            fio.copy(testFilePath, failedDir + testFilePath);
147
            
148
149
150
            if (not os.path.exists(failedDir + resCaseDir)) :
                os.makedirs(failedDir + resCaseDir);
            fio.copy(resFilePath, failedDir + resFilePath);
151
152
            fio.copy(alphaFilePath, failedDir + alphaFilePath);
        #break;
153
tree.write(xmlFile);