Passed
Push — master ( 0c1576...8c2877 )
by Simon
04:40 queued 12s
created

HyperGradientConv.position2value()   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import numpy as np
6
import pandas as pd
7
8
9
class HyperGradientConv:
10
    def __init__(self, s_space):
11
        self.s_space = s_space
12
13
    def value2position(self, value: list) -> list:
14
        position = []
15
        for n, space_dim in enumerate(self.s_space.values_l):
16
            pos = np.abs(value[n] - np.array(space_dim)).argmin()
17
            position.append(int(pos))
18
19
        return position
20
21
    def value2para(self, value: list) -> dict:
22
        para = {}
23
        for key, p_ in zip(self.s_space.dim_keys, value):
24
            para[key] = p_
25
26
        return para
27
28
    def para2value(self, para: dict) -> list:
29
        value = []
30
        for para_name in self.s_space.dim_keys:
31
            value.append(para[para_name])
32
33
        return value
34
35
    def position2value(self, position):
36
        value = []
37
38
        for n, space_dim in enumerate(self.s_space.values_l):
39
            value.append(space_dim[position[n]])
40
41
        return value
42
43
    def para_func2str(self, para):
44
        para_conv = {}
45
        for dim_key in self.s_space.dim_keys:
46
            if self.s_space.data_types[dim_key] == "number":
47
                continue
48
49
            try:
50
                value_conv = para[dim_key].__name__
51
            except:
52
                value_conv = para[dim_key]
53
54
            para_conv[dim_key] = value_conv
55
56
    def value_func2str(self, value):
57
        try:
58
            return value.__name__
59
        except:
60
            return value
61
62
    def conv_para(self, para_hyper):
63
        para_gfo = {}
64
        for para in self.s_space.dim_keys:
65
            value_hyper = para_hyper[para]
66
            space_dim = list(self.s_space.func2str[para])
67
68
            if self.s_space.data_types[para] == "number":
69
                value_gfo = np.abs(value_hyper - np.array(space_dim)).argmin()
70
            else:
71
                value_hyper = self.value_func2str(value_hyper)
72
73
                if value_hyper in space_dim:
74
                    value_gfo = space_dim.index(value_hyper)
75
                else:
76
                    raise ValueError(
77
                        "'{}' was not found in '{}'".format(value_hyper, para)
78
                    )
79
80
            para_gfo[para] = value_gfo
81
        return para_gfo
82
83
    def conv_initialize(self, initialize):
84
        if "warm_start" in list(initialize.keys()):
85
            warm_start_l = initialize["warm_start"]
86
            warm_start_gfo = []
87
            for warm_start in warm_start_l:
88
                para_gfo = self.conv_para(warm_start)
89
                warm_start_gfo.append(para_gfo)
90
91
            initialize["warm_start"] = warm_start_gfo
92
93
        return initialize
94
95
    def get_list_positions(self, list1_values, search_dim):
96
        list_positions = []
97
98
        for value2 in list1_values:
99
            list_positions.append(search_dim.index(value2))
100
101
        return list_positions
102
103
    def values2positions(self, values, search_dim):
104
        return np.array(search_dim).searchsorted(values)
105
106
    def positions2results(self, positions):
107
        results_dict = {}
108
109
        for para_name in self.s_space.dim_keys:
110
            values_list = self.s_space[para_name]
111
            pos_ = positions[para_name].values
112
            values_ = [values_list[idx] for idx in pos_]
113
            results_dict[para_name] = values_
114
115
        results = pd.DataFrame.from_dict(results_dict)
116
117
        diff_list = np.setdiff1d(positions.columns, results.columns)
118
        results[diff_list] = positions[diff_list]
119
120
        return results
121
122
    def conv_memory_warm_start(self, results):
123
        if results is None:
124
            return results
125
126
        results.reset_index(inplace=True, drop=True)
127
128
        df_positions_dict = {}
129
        for dim_key in self.s_space.dim_keys:
130
            result_dim_values = list(results[dim_key].values)
131
            search_dim = self.s_space.func2str[dim_key]
132
133
            if self.s_space.data_types[dim_key] == "object":
134
                result_dim_values_tmp = []
135
                for value in result_dim_values:
136
                    try:
137
                        value = value.__name__
138
                    except:
139
                        pass
140
141
                    result_dim_values_tmp.append(value)
142
143
                result_dim_values = result_dim_values_tmp
144
145
                list1_positions = self.get_list_positions(result_dim_values, search_dim)
146
            else:
147
                list1_positions = self.values2positions(result_dim_values, search_dim)
148
149
            # remove None
150
            # list1_positions_ = [x for x in list1_positions if x is not None]
151
            df_positions_dict[dim_key] = list1_positions
152
153
        results_new = pd.DataFrame(df_positions_dict)
154
155
        results_new["score"] = results["score"]
156
        results_new.dropna(how="any", inplace=True)
157
158
        return results_new
159