This repository was archived by the owner on Apr 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTesting.py
More file actions
176 lines (149 loc) · 5.92 KB
/
Copy pathTesting.py
File metadata and controls
176 lines (149 loc) · 5.92 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from PIL import Image
import time
from Algorithms import *
import numpy as np
import Pickler
import os
import Util
import pandas as pd
class Test():
def __init__(self, name: str, image: str):
self.name = name
with Image.open(image).convert('RGB') as img:
width, height = img.size
self.n = width * height
try:
self.image = Pickler.LoadInput(self.name)
except:
print(f"Creating new test input: ", end = "")
self.image = Util.totuple(np.array(img))
print(self.name)
Pickler.SaveInput(self.name, self.image)
try:
self.correct = Pickler.LoadOutput(self.name)
except:
print(f"Creating new test output: ", end = "")
self.correct = STF.run(self.image, STF.getAverageFormat())
print(self.name)
Pickler.SaveOutput(self.name, self.correct)
class TestData():
def __init__(self, tests: list[Test], algorithms: list[AbstractAlgorithm]):
self.ExcelWorkbook = [[0 for j in range(len(algorithms) + 2)] for i in range(len(tests) + 1)]
self.ExcelWorkbook[0][0] = "Name"
self.ExcelWorkbook[0][1] = "N (pixels)"
for i in range(len(tests)):
t = tests[i]
self.ExcelWorkbook[i + 1][0] = t.name
self.ExcelWorkbook[i + 1][1] = t.n
for i in range(len(algorithms)):
a = algorithms[i]
self.ExcelWorkbook[0][i + 2] = a.getName()
self.tests = tests
self.algorithms = algorithms
def appendTest(self, test: Test, algorithm: AbstractAlgorithm, averageTime: float):
self.ExcelWorkbook[self.tests.index(test) + 1][self.algorithms.index(algorithm) + 2] = averageTime
self.export()
def export(self):
dataFrame = pd.DataFrame(self.ExcelWorkbook)
dataFrame.to_excel('Data.xlsx', index = False, header=None)
def AssertOutputCorrect(a :np.array, b :np.array, test: Test, algorithm: AbstractAlgorithm):
if not np.array_equal(a, b):
s = a.shape
avg = Pickler.Load(Pickler.Pickles.AverageList)
for x in range(s[0]):
for y in range(s[1]):
av = a[x, y]
bv = b[x, y]
if av != bv:
p = test.image[x][y]
if (Util.distance(avg[av] ,p) != Util.distance(avg[bv], p)):
raise AssertionError(algorithm.getName() + " failed on: " + test.name)
class TestSuite():
def __init__(self, count: int):
self.tests = []
self.algorithms = []
self.count = count
def addTest(self, test: Test):
self.tests.append(test)
def addAlgorithm(self, algorithm: AbstractAlgorithm):
self.algorithms.append(algorithm)
def run(self):
data = TestData(self.tests, self.algorithms)
for test in self.tests:
print(f"{test.name}: {test.n}")
for algorithm in self.algorithms:
print(f" {algorithm.getName()}")
totalTime = 0
for i in range(self.count):
print(f" {i}: ", end='')
averageFormat = algorithm.getAverageFormat()
start = time.perf_counter_ns()
output = algorithm.run(test.image, averageFormat)
end = time.perf_counter_ns()
elapsed = end - start
print(f"{elapsed} ns")
totalTime += elapsed
if i == 0:
AssertOutputCorrect(output, test.correct, test, algorithm)
avgTime = totalTime / self.count
print(f" Average: {avgTime} ns")
data.appendTest(test, algorithm, avgTime)
data.export()
def saveOutputImages(self):
textures = Pickler.Load(Pickler.Pickles.ImageList)
for test in self.tests:
width = len(test.correct)
height = len(test.correct[0])
print()
print(f"Creating image: ", end = "")
output = Image.new("RGB", (width * 16, height * 16))
for x in range(width):
for y in range(height):
matching = textures[test.correct[x, y]]
left = x * 16
top = y * 16
with Image.open(matching).convert("RGB") as matching_image:
output.paste(matching_image, box = (left, top, left + 16, top + 16))
output.rotate(-90).save(f"OutputImages/{test.name}.png")
print(f"{test.name} with {width*16*height*16} pixels")
CONTROLLED_TESTS = False
if CONTROLLED_TESTS:
testSuite = TestSuite(1)
testSuite.addAlgorithm(SNB)
testSuite.addAlgorithm(PNB)
tests = ["Mario", "Creeper", "Bridge"]
for t in tests:
testSuite.addTest(Test(t, f"InputImages/{t}.png"))
testSuite.run()
else:
# Create a new test suite that averages on 10 runs
testSuite = TestSuite(4)
# Add all images as tests to the test suite
for file in os.listdir("InputImages/"):
if (file.endswith(".png")):
name = file.replace(".png", "")
path = "InputImages/" + file
test = Test(name, path)
testSuite.addTest(test)
# Add algorithms to the test suite
#TODO Run Later - make sure to copy excel file first
testSuite.addAlgorithm(SNB)
testSuite.addAlgorithm(PNB)
'''
testSuite.addAlgorithm(SDB)
testSuite.addAlgorithm(PDB)
testSuite.addAlgorithm(SLB)
testSuite.addAlgorithm(PLB)
testSuite.addAlgorithm(SNK)
testSuite.addAlgorithm(PNK)
testSuite.addAlgorithm(SDK)
testSuite.addAlgorithm(PDK)
testSuite.addAlgorithm(SLK)
testSuite.addAlgorithm(PLK)
testSuite.addAlgorithm(STF)
testSuite.addAlgorithm(PTF)
'''
# Save output images
#testSuite.saveOutputImages()
# Run test suite
testSuite.run()