|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
import os |
|
6
|
|
|
import sys |
|
7
|
|
|
import json |
|
8
|
|
|
import dill |
|
9
|
|
|
import shutil |
|
10
|
|
|
import inspect |
|
11
|
|
|
import hashlib |
|
12
|
|
|
import datetime |
|
13
|
|
|
import glob |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
def function_string(function): |
|
17
|
|
|
return inspect.getsource(function) |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
def object_hash(object): |
|
21
|
|
|
return hashlib.sha1(object).hexdigest() |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def model_id(model): |
|
25
|
|
|
return str(object_hash(function_string(model).encode("utf-8"))) |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
def get_datetime(): |
|
29
|
|
|
return datetime.datetime.now().strftime("%d.%m.%Y - %H:%M:%S:%f") |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
def is_sha1(maybe_sha): |
|
33
|
|
|
if ( |
|
34
|
|
|
not isinstance(maybe_sha, int) |
|
35
|
|
|
and not isinstance(maybe_sha, float) |
|
36
|
|
|
and not isinstance(maybe_sha, str) |
|
37
|
|
|
): |
|
38
|
|
|
if len(maybe_sha) == 40: |
|
39
|
|
|
return True |
|
40
|
|
|
|
|
41
|
|
|
return False |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
def _get_pkl_hash(hash, model_path): |
|
45
|
|
|
paths = glob.glob(model_path + hash + "*.pkl") |
|
46
|
|
|
|
|
47
|
|
|
return paths |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
def _hash2obj(search_space, model_path): |
|
51
|
|
|
hash2obj_dict = {} |
|
52
|
|
|
para_hash_list = _get_para_hash_list(search_space) |
|
53
|
|
|
|
|
54
|
|
|
for para_hash in para_hash_list: |
|
55
|
|
|
obj = _read_dill(para_hash, model_path) |
|
56
|
|
|
hash2obj_dict[para_hash] = obj |
|
57
|
|
|
|
|
58
|
|
|
return hash2obj_dict |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
def _read_dill(value, model_path): |
|
62
|
|
|
paths = _get_pkl_hash(value, model_path) |
|
63
|
|
|
assert len(paths) != 0 |
|
64
|
|
|
|
|
65
|
|
|
for path in paths: |
|
66
|
|
|
with open(path, "rb") as fp: |
|
67
|
|
|
value = dill.load(fp) |
|
68
|
|
|
value = dill.loads(value) |
|
69
|
|
|
break |
|
70
|
|
|
|
|
71
|
|
|
return value |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
def _get_para_hash_list(search_space): |
|
75
|
|
|
para_hash_list = [] |
|
76
|
|
|
for key in search_space.keys(): |
|
77
|
|
|
values = search_space[key] |
|
78
|
|
|
|
|
79
|
|
|
for value in values: |
|
80
|
|
|
if ( |
|
81
|
|
|
not isinstance(value, int) |
|
82
|
|
|
and not isinstance(value, float) |
|
83
|
|
|
and not isinstance(value, str) |
|
84
|
|
|
): |
|
85
|
|
|
|
|
86
|
|
|
para_dill = dill.dumps(value) |
|
87
|
|
|
para_hash = object_hash(para_dill) |
|
88
|
|
|
para_hash_list.append(para_hash) |
|
89
|
|
|
|
|
90
|
|
|
return para_hash_list |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
def _connect_key2value(data, key_model, value_model): |
|
94
|
|
|
if value_model in data[key_model]: |
|
95
|
|
|
print("IDs of models are already connected") |
|
96
|
|
|
else: |
|
97
|
|
|
data[key_model].append(value_model) |
|
98
|
|
|
print("IDs successfully connected") |
|
99
|
|
|
|
|
100
|
|
|
return data |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
def _split_key_value(data, key_model, value_model): |
|
104
|
|
|
if value_model in data[key_model]: |
|
105
|
|
|
data[key_model].remove(value_model) |
|
106
|
|
|
|
|
107
|
|
|
if len(data[key_model]) == 0: |
|
108
|
|
|
del data[key_model] |
|
109
|
|
|
print("ID connection successfully deleted") |
|
110
|
|
|
else: |
|
111
|
|
|
print("IDs of models are not connected") |
|
112
|
|
|
|
|
113
|
|
|
return data |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
def _reset_memory(meta_path): |
|
117
|
|
|
dirs = next(os.walk(meta_path))[1] |
|
118
|
|
|
for dir in dirs: |
|
119
|
|
|
shutil.rmtree(meta_path + dir) |
|
120
|
|
|
|
|
121
|
|
|
with open(meta_path + "model_connections.json", "w") as f: |
|
122
|
|
|
json.dump({}, f, indent=4) |
|
123
|
|
|
|
|
124
|
|
|
print("Memory reset successful") |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
def _query_yes_no(): |
|
128
|
|
|
valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False} |
|
129
|
|
|
question = "Delete the entire long term memory?" |
|
130
|
|
|
|
|
131
|
|
|
while True: |
|
132
|
|
|
sys.stdout.write(question + " [y/n] ") |
|
133
|
|
|
choice = input().lower() |
|
134
|
|
|
if choice == "": |
|
135
|
|
|
return False |
|
136
|
|
|
elif choice in valid: |
|
137
|
|
|
return valid[choice] |
|
138
|
|
|
else: |
|
139
|
|
|
sys.stdout.write("Please respond with 'yes' or 'no' " "(or 'y' or 'n').\n") |
|
140
|
|
|
|