-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-tests.py
More file actions
executable file
·125 lines (97 loc) · 3.84 KB
/
Copy pathrun-tests.py
File metadata and controls
executable file
·125 lines (97 loc) · 3.84 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python3
import os;
import sys;
import subprocess;
import glob;
import re as regex;
testDirectory = "./test/";
# These metrics print out Absoulte columns, which is expected.
supportedMetrics = ["mae", "mse", "rmse", "pae"];
def main():
errors = 0
for filename in os.listdir(testDirectory):
if filename.endswith(".markup"):
markupFilename = testDirectory + filename;
basename = regex.sub(r'(.+)\.markup', r'\1', filename);
sourceFilename = testDirectory + basename + '.source.jpg';
oracleFilename = testDirectory + basename + '.node.oracle.jpg';
testFilename = testDirectory + basename + '.node.test.jpg';
try:
runNode(sourceFilename, testFilename, markupFilename);
compareOutputs(basename, oracleFilename, testFilename);
except RuntimeError as msg:
print(basename + ':', msg, file=sys.stderr);
errors += 1
continue;
sys.exit(errors)
def readMarkupFile(markupFilename):
f = open(markupFilename, 'r');
markup = f.read();
return markup;
def processComparison(compareOutput):
# Sample compareOutput:
# Image Difference (MeanAbsoluteError):
# Normalized Absolute
# ============ ==========
# Red: 0.0000000000 0.0
# Green: 0.0000000000 0.0
# Blue: 0.0000000000 0.0
# Total: 0.0000000000 0.0
channelNameIndex = 0;
absoluteColumnIndex = 2;
totalChannelName = 'Total:';
for line in compareOutput.split('\n'):
# Strip trailing and leading spaces and replace multiple spaces
# for parsing
lineSet = regex.sub(r'\s+', ' ', line.strip()).split(' ');
if lineSet[channelNameIndex] == totalChannelName:
absoluteError = lineSet[absoluteColumnIndex];
return absoluteError;
# If we're here, the Total line did not exist as expected
errMsg = totalChannelName + ' row not found in comparison.';
raise RuntimeError(errMsg);
def compareOutputs(basename, oracleFilename, destinationFilename):
metric = "mae";
if metric not in supportedMetrics:
errMsg = metric + " is not a supported metric.";
raise RuntimeError(errMsg);
cmd = ["gm","compare","-metric",metric,oracleFilename,destinationFilename];
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE);
(out, err) = proc.communicate();
out = out.decode().strip()
retCode = proc.returncode;
if retCode != 0:
errMsg = "Compare invocation failed with exit code: " + str(retCode);
errMsg += "\nTo reproduce run:\n";
errMsg += ' '.join(cmd);
raise RuntimeError(errMsg);
try:
absoluteError = float(processComparison(out));
if absoluteError != 0.0:
errMsg = basename + ': Image difference error = ' + str(absoluteError);
raise RuntimeError(errMsg);
else:
print(basename + ': test comparison passed.');
os.remove(destinationFilename);
except RuntimeError as runtimeErr:
errMsg = 'Comparison string processing failed\n' \
+ str(runtimeErr);
raise RuntimeError(errMsg);
def runNode(sourceFilename, destinationFilename, markupFilename):
markup = readMarkupFile(markupFilename).strip();
cmd = ["bash", "node-markup.sh", "--input", sourceFilename, "--output",
destinationFilename, "--markup", markup, "--debug"];
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE);
(out, err) = proc.communicate();
out = out.decode().strip();
if proc.returncode != 0:
errMsg = 'node-markup invocation failed';
print(out,err)
raise RuntimeError(errMsg);
if out != markup:
print("Before: ", markup)
print("After: ", out)
errMsg = "node-markup modified the markup string";
raise RuntimeError(errMsg);
if __name__ == "__main__":
main()