Passed
Push — master ( bf6a06...0a901a )
by Simon
01:26
created

MemoryIO.__init__()   A

Complexity

Conditions 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nop 5
dl 0
loc 27
rs 9.5
c 0
b 0
f 0
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import os
6
import glob
7
import dill
8
import datetime
9
import inspect
10
import hashlib
11
12
13
class MemoryIO:
14
    def __init__(self, _space_, _main_args_, _cand_, memory_dict):
15
        self._space_ = _space_
16
        self._main_args_ = _main_args_
17
18
        self.feature_hash = self._get_hash(_main_args_.X)
19
        self.label_hash = self._get_hash(_main_args_.y)
20
21
        self.score_col_name = "mean_test_score"
22
23
        current_path = os.path.realpath(__file__)
24
        self.meta_learn_path, _ = current_path.rsplit("/", 1)
25
26
        func_str = self._get_func_str(_cand_.func_)
27
        self.func_path_ = self._get_hash(func_str.encode("utf-8")) + "/"
28
29
        self.datetime = "run_data/" + datetime.datetime.now().strftime(
30
            "%d.%m.%Y - %H:%M:%S"
31
        )
32
33
        self.meta_path = self.meta_learn_path + "/meta_data/"
34
        self.func_path = self.meta_path + self.func_path_
35
        self.date_path = self.meta_path + self.func_path_ + self.datetime + "/"
36
37
        if not os.path.exists(self.date_path):
38
            os.makedirs(self.date_path, exist_ok=True)
39
40
        self.hash2obj = self._hash2obj()
41
42
    def _get_func_str(self, func):
43
        return inspect.getsource(func)
44
45
    def _get_hash(self, object):
46
        return hashlib.sha1(object).hexdigest()
47
48
    """
49
    def is_sha1(maybe_sha):
50
        if len(maybe_sha) != 40:
51
            return False
52
        try:
53
            sha_int = int(maybe_sha, 16)
54
        except ValueError:
55
            return False
56
        return True
57
    """
58
59
    def _read_dill(self, value):
60
        paths = self._get_pkl_hash(value)
61
        for path in paths:
62
            with open(path, "rb") as fp:
63
                value = dill.load(fp)
64
                value = dill.loads(value)
65
                break
66
67
        return value
68
69
    def _get_pkl_hash(self, hash):
70
        paths = glob.glob(self.func_path + hash + "*.pkl")
71
72
        return paths
73
74
    def _hash2obj(self):
75
        hash2obj_dict = {}
76
        para_hash_list = self._get_para_hash_list()
77
78
        for para_hash in para_hash_list:
79
            obj = self._read_dill(para_hash)
80
            hash2obj_dict[para_hash] = obj
81
82
        return hash2obj_dict
83
84
    def _get_para_hash_list(self):
85
        para_hash_list = []
86
        for key in self._space_.search_space.keys():
87
            values = self._space_.search_space[key]
88
89
            for value in values:
90
                if (
91
                    not isinstance(value, int)
92
                    and not isinstance(value, float)
93
                    and not isinstance(value, str)
94
                ):
95
96
                    para_dill = dill.dumps(value)
97
                    para_hash = self._get_hash(para_dill)
98
                    para_hash_list.append(para_hash)
99
100
        return para_hash_list
101