Completed
Push — master ( a2b32e...a81b83 )
by Simon
01:29
created

hyperactive.memory.memory_helper._get_file_path()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import os
6
import json
7
import shutil
8
import hashlib
9
import inspect
10
11
12
current_path = os.path.realpath(__file__)
13
meta_learn_path, _ = current_path.rsplit("/", 1)
14
meta_path = meta_learn_path + "/meta_data/"
15
16
"""
17
def get_best_models(X, y):
18
    # TODO: model_dict   key:model   value:score
19
20
    return model_dict
21
22
23
def get_model_search_config(model):
24
    # TODO
25
    return search_config
26
27
28
def get_model_init_config(model):
29
    # TODO
30
    return init_config
31
"""
32
33
34
def delete_model(model):
35
    model_hash = _get_model_hash(model)
36
    path = meta_path + str(model_hash)
37
38
    if os.path.exists(path) and os.path.isdir(path):
39
        shutil.rmtree(meta_path + str(model_hash))
40
        print("Model data successfully removed")
41
    else:
42
        print("Model data not found in memory")
43
44
45
def delete_model_dataset(model, X, y):
46
    csv_file = _get_file_path(model, X, y)
47
48
    if os.path.exists(csv_file):
49
        os.remove(csv_file)
50
        print("Model data successfully removed")
51
    else:
52
        print("Model data not found in memory")
53
54
55 View Code Duplication
def merge_model_IDs(model1, model2):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
56
    # do checks if search space has same dim
57
58
    with open(meta_path + "model_connections.json") as f:
59
        data = json.load(f)
60
61
    model1_hash = _get_model_hash(model1)
62
    model2_hash = _get_model_hash(model2)
63
64
    if model1_hash in data:
65
        key_model = model1_hash
66
        value_model = model2_hash
67
        data = _connect_key2value(data, key_model, value_model)
68
    elif model2_hash in data:
69
        key_model = model2_hash
70
        value_model = model1_hash
71
        data = _connect_key2value(data, key_model, value_model)
72
    else:
73
        data[model1_hash] = [model2_hash]
74
        print("IDs successfully connected")
75
76
    with open(meta_path + "model_connections.json", "w") as f:
77
        json.dump(data, f, indent=4)
78
79
80
def _connect_key2value(data, key_model, value_model):
81
    if value_model in data[key_model]:
82
        print("IDs of models are already connected")
83
    else:
84
        data[key_model].append(value_model)
85
        print("IDs successfully connected")
86
87
    return data
88
89
90
def _split_key_value(data, key_model, value_model):
91
    if value_model in data[key_model]:
92
        data[key_model].remove(value_model)
93
94
        if len(data[key_model]) == 0:
95
            del data[key_model]
96
        print("ID connection successfully deleted")
97
    else:
98
        print("IDs of models are already connected")
99
100
    return data
101
102
103 View Code Duplication
def split_model_IDs(model1, model2):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
104
    # TODO: do checks if search space has same dim
105
106
    with open(meta_path + "model_connections.json") as f:
107
        data = json.load(f)
108
109
    model1_hash = _get_model_hash(model1)
110
    model2_hash = _get_model_hash(model2)
111
112
    if model1_hash in data:
113
        key_model = model1_hash
114
        value_model = model2_hash
115
        data = _split_key_value(data, key_model, value_model)
116
    elif model2_hash in data:
117
        key_model = model2_hash
118
        value_model = model1_hash
119
        data = _split_key_value(data, key_model, value_model)
120
    else:
121
        print("IDs of models are not connected")
122
123
    with open(meta_path + "model_connections.json", "w") as f:
124
        json.dump(data, f, indent=4)
125
126
127
def _get_file_path(model, X, y):
128
    func_path_ = _get_model_hash(model) + "/"
129
    func_path = meta_path + func_path_
130
131
    feature_hash = _get_hash(X)
132
    label_hash = _get_hash(y)
133
134
    return func_path + (feature_hash + "_" + label_hash + "_.csv")
135
136
137
def _get_model_hash(model):
138
    return str(_get_hash(_get_func_str(model).encode("utf-8")))
139
140
141
def _get_func_str(func):
142
    return inspect.getsource(func)
143
144
145
def _get_hash(object):
146
    return hashlib.sha1(object).hexdigest()
147