/
GamifyMap.py
91 lines (62 loc) · 2.7 KB
/
GamifyMap.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
class GamifyMap:
def __init__(self, scan_object):
'''GamifyMap handles the management of the
dictionary that contains the information about
hyperparameters, which is exchanged in and out
during the `Scan()` experiment.
'''
self.params = scan_object.param_object.params
self.scan_object = scan_object
self.generate_gamify_dict()
self.gamify_map = self.generate_gamify_dict_map()
# parse together the output file name
_folder = './' + scan_object.experiment_name + '/'
_id = scan_object._experiment_id
self._filename = _folder + _id
def run_updates(self):
for key in self.gamify_dict.keys():
for val in self.gamify_dict[key].keys():
if self.gamify_dict[key][val] != self.updated_dict[key][val]:
label = list(self.params.keys())[int(key)]
value = self.params[label][int(val)]
self.gamify_dict[key][val] = self.updated_dict[key][val]
self.scan_object.param_object.remove_is(label, value)
return self.scan_object
def back_to_original(self, gamify_from_json):
gamify_dict = {}
for key in gamify_from_json:
param_vals = {}
for val in gamify_from_json[key]:
param_vals[self.gamify_map[key][val][1]] = gamify_from_json[key][val][:2]
gamify_dict[self.gamify_map[key][val][0]] = param_vals
return gamify_dict
def generate_gamify_dict_map(self):
gamify_dict_map = {}
for i, key in enumerate(self.params.keys()):
param_vals = {}
for ii, val in enumerate(self.params[key]):
param_vals[str(ii)] = [key, val]
gamify_dict_map[str(i)] = param_vals
return gamify_dict_map
def generate_gamify_dict(self):
'''This is done once at the beginning
of the experiment.
NOTE: This will be all stringified, so an index
mapping system will be used to convert back to
actual forms later.'''
gamify_dict = {}
for i, key in enumerate(self.params.keys()):
param_vals = {}
for ii, val in enumerate(self.params[key]):
param_vals[str(ii)] = ['active', 0, str(key), str(val)]
gamify_dict[str(i)] = param_vals
self.gamify_dict = gamify_dict
def export_json(self):
import json
with open(self._filename + '.json', 'w') as fp:
json.dump(self.gamify_dict, fp)
def import_json(self):
import json
with open(self._filename + '.json', 'r') as fp:
out = json.load(fp)
self.updated_dict = out