-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigit_input.py
93 lines (82 loc) · 3.08 KB
/
digit_input.py
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
import rasterization_tools
from timer import Timer
from properties import RUN, BITMAP_THRESHOLD
from folder import Folder
import json
from os.path import join
import matplotlib.pyplot as plt
import numpy as np
from features import move_distance, orientation_calc, bitmap_count
class Digit:
COUNT = 0
def __init__(self, desc, label, seed):
self.timestamp, self.elapsed_time = Timer.get_timestamps()
self.id = Digit.COUNT
self.run = RUN
self.seed = seed
# TODO
self.features = {}
self.tool = "DeepHyperion"
self.xml_desc = desc
self.purified = rasterization_tools.rasterize_in_memory(self.xml_desc)
self.expected_label = label
self.predicted_label = None
self.confidence = None
self.rank = np.inf
self.selected_counter = 0
self.placed_mutant = 0
Digit.COUNT += 1
def to_dict(self):
return {'id': str(self.id),
'seed': str(self.seed),
'expected_label': str(self.expected_label),
'predicted_label': str(self.predicted_label),
'misbehaviour': self.is_misbehavior(),
'performance': str(self.confidence),
'timestamp': str(self.timestamp),
'elapsed': str(self.elapsed_time),
'tool' : str(self.tool),
'run' : str(self.run),
'features': {
"moves": move_distance(self),
"orientation": orientation_calc(self,0),
"bitmaps": bitmap_count(self, BITMAP_THRESHOLD)
},
'rank': str(self.rank),
'selected': str(self.selected_counter),
'placed_mutant': str(self.placed_mutant)
}
def dump(self, filename):
data = self.to_dict()
filedest = filename+".json"
with open(filedest, 'w') as f:
(json.dump(data, f, sort_keys=True, indent=4))
def save_png(self, filename):
plt.imsave(filename+'.png', self.purified.reshape(28, 28), cmap='gray', format='png')
def save_npy(self, filename):
np.save(filename, self.purified)
test_img = np.load(filename+'.npy')
diff = self.purified - test_img
assert(np.linalg.norm(diff) == 0)
def save_svg(self, filename):
data = self.xml_desc
filedest = filename + ".svg"
with open(filedest, 'w') as f:
f.write(data)
def is_misbehavior(self):
if self.expected_label == self.predicted_label:
return False
else:
return True
def export(self, all=False):
if all:
dst = join(Folder.DST_ALL, "mbr"+str(self.id))
else:
dst = join(Folder.DST_ARC, "mbr"+str(self.id))
self.dump(dst)
self.save_npy(dst)
# self.save_png(dst)
self.save_svg(dst)
def clone(self):
clone_digit = Digit(self.xml_desc, self.expected_label, self.seed)
return clone_digit