Completed
Push — master ( 2b17b1...77ef8d )
by Simon
03:52
created

hyperactive.memory.memory_helper   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 105
dl 0
loc 183
rs 9.84
c 0
b 0
f 0
wmc 32

12 Functions

Rating   Name   Duplication   Size   Complexity  
A query_yes_no() 0 13 4
A _get_func_str() 0 2 1
A delete_model_dataset() 0 8 2
A _get_hash() 0 2 1
A _get_file_path() 0 8 1
B connect_model_IDs() 0 27 5
A _split_key_value() 0 11 3
A split_model_IDs() 0 25 5
A delete_model() 0 9 3
A _connect_key2value() 0 8 2
A reset_memory() 0 10 4
A _get_model_hash() 0 2 1
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import os
6
import sys
7
import glob
8
import json
9
import shutil
10
import hashlib
11
import inspect
12
13
14
current_path = os.path.realpath(__file__)
15
meta_learn_path, _ = current_path.rsplit("/", 1)
16
meta_path = meta_learn_path + "/meta_data/"
17
18
"""
19
def get_best_models(X, y):
20
    # TODO: model_dict   key:model   value:score
21
22
    return model_dict
23
24
25
def get_model_search_config(model):
26
    # TODO
27
    return search_config
28
29
30
def get_model_init_config(model):
31
    # TODO
32
    return init_config
33
"""
34
35
36
def reset_memory():
37
    if query_yes_no():
38
        dirs = next(os.walk(meta_path))[1]
39
        for dir in dirs:
40
            shutil.rmtree(meta_path + dir)
41
42
        with open(meta_path + "model_connections.json", "w") as f:
43
            json.dump({}, f, indent=4)
44
45
        print("Memory reset successful")
46
47
48
def query_yes_no():
49
    valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
50
    question = "Delete the entire long term memory?"
51
52
    while True:
53
        sys.stdout.write(question + " [y/n] ")
54
        choice = input().lower()
55
        if choice == "":
56
            return False
57
        elif choice in valid:
58
            return valid[choice]
59
        else:
60
            sys.stdout.write("Please respond with 'yes' or 'no' " "(or 'y' or 'n').\n")
61
62
63
def delete_model(model):
64
    model_hash = _get_model_hash(model)
65
    path = meta_path + str(model_hash)
66
67
    if os.path.exists(path) and os.path.isdir(path):
68
        shutil.rmtree(meta_path + str(model_hash))
69
        print("Model data successfully removed")
70
    else:
71
        print("Model data not found in memory")
72
73
74
def delete_model_dataset(model, X, y):
75
    csv_file = _get_file_path(model, X, y)
76
77
    if os.path.exists(csv_file):
78
        os.remove(csv_file)
79
        print("Model data successfully removed")
80
    else:
81
        print("Model data not found in memory")
82
83
84
def connect_model_IDs(model1, model2):
85
    # do checks if search space has same dim
86
87
    with open(meta_path + "model_connections.json") as f:
88
        data = json.load(f)
89
90
    model1_hash = _get_model_hash(model1)
91
    model2_hash = _get_model_hash(model2)
92
93
    if model1_hash in data:
94
        key_model = model1_hash
95
        value_model = model2_hash
96
        data = _connect_key2value(data, key_model, value_model)
97
    else:
98
        data[model1_hash] = [model2_hash]
99
        print("IDs successfully connected")
100
101
    if model2_hash in data:
102
        key_model = model2_hash
103
        value_model = model1_hash
104
        data = _connect_key2value(data, key_model, value_model)
105
    else:
106
        data[model2_hash] = [model1_hash]
107
        print("IDs successfully connected")
108
109
    with open(meta_path + "model_connections.json", "w") as f:
110
        json.dump(data, f, indent=4)
111
112
113
def _connect_key2value(data, key_model, value_model):
114
    if value_model in data[key_model]:
115
        print("IDs of models are already connected")
116
    else:
117
        data[key_model].append(value_model)
118
        print("IDs successfully connected")
119
120
    return data
121
122
123
def _split_key_value(data, key_model, value_model):
124
    if value_model in data[key_model]:
125
        data[key_model].remove(value_model)
126
127
        if len(data[key_model]) == 0:
128
            del data[key_model]
129
        print("ID connection successfully deleted")
130
    else:
131
        print("IDs of models are not connected")
132
133
    return data
134
135
136
def split_model_IDs(model1, model2):
137
    # TODO: do checks if search space has same dim
138
139
    with open(meta_path + "model_connections.json") as f:
140
        data = json.load(f)
141
142
    model1_hash = _get_model_hash(model1)
143
    model2_hash = _get_model_hash(model2)
144
145
    if model1_hash in data:
146
        key_model = model1_hash
147
        value_model = model2_hash
148
        data = _split_key_value(data, key_model, value_model)
149
    else:
150
        print("IDs of models are not connected")
151
152
    if model2_hash in data:
153
        key_model = model2_hash
154
        value_model = model1_hash
155
        data = _split_key_value(data, key_model, value_model)
156
    else:
157
        print("IDs of models are not connected")
158
159
    with open(meta_path + "model_connections.json", "w") as f:
160
        json.dump(data, f, indent=4)
161
162
163
def _get_file_path(model, X, y):
164
    func_path_ = _get_model_hash(model) + "/"
165
    func_path = meta_path + func_path_
166
167
    feature_hash = _get_hash(X)
168
    label_hash = _get_hash(y)
169
170
    return func_path + (feature_hash + "_" + label_hash + "_.csv")
171
172
173
def _get_model_hash(model):
174
    return str(_get_hash(_get_func_str(model).encode("utf-8")))
175
176
177
def _get_func_str(func):
178
    return inspect.getsource(func)
179
180
181
def _get_hash(object):
182
    return hashlib.sha1(object).hexdigest()
183