Passed
Push — master ( b8f359...927840 )
by Simon
03:10
created

HyperGradientTrafo.get_list_positions()   A

Complexity

Conditions 5

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nop 3
dl 0
loc 15
rs 9.3333
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 Converter:
10
    def __init__(self, search_space):
11
        self.search_space = search_space
12
        self.para_names = list(self.search_space.keys())
13
14
    def value2position(self, value):
15
        position = []
16
        for n, space_dim in enumerate(self.search_space_values):
17
            pos = np.abs(value[n] - space_dim).argmin()
18
            position.append(pos)
19
20
        return np.array(position).astype(int)
21
22
    def value2para(self, value):
23
        para = {}
24
        for key, p_ in zip(self.para_names, value):
25
            para[key] = p_
26
27
        return para
28
29
    def position2value(self, position):
30
        value = []
31
32
        for n, space_dim in enumerate(self.search_space_values):
33
            value.append(space_dim[position[n]])
34
35
        return np.array(value)
36
37
    def positions2values(self, positions):
38
        values_temp = []
39
        positions_np = np.array(positions)
40
41
        for n, space_dim in enumerate(self.search_space_values):
42
            pos_1d = positions_np[:, n]
43
            value_ = np.take(space_dim, pos_1d, axis=0)
44
            values_temp.append(value_)
45
46
        values = list(np.array(values_temp).T)
47
        return values
48
49
    def para2value(self, para):
50
        value = []
51
        for para_name in self.para_names:
52
            value.append(para[para_name])
53
54
        return np.array(value)
55
56
    def _memory2dataframe(self, memory_dict):
57
        positions = [np.array(pos).astype(int) for pos in list(memory_dict.keys())]
58
        scores = list(memory_dict.values())
59
60
        memory_positions = pd.DataFrame(positions, columns=self.para_names)
61
        memory_positions["score"] = scores
62
63
        return memory_positions
64
65
66
class HyperGradientTrafo(Converter):
67
    def __init__(self, search_space):
68
        super().__init__(search_space)
69
        self.search_space_values = list(self.search_space.values())
70
71
        search_space_positions = {}
72
        for key in search_space.keys():
73
            search_space_positions[key] = np.array(range(len(search_space[key])))
74
        self.search_space_positions = search_space_positions
75
76
    def trafo_initialize(self, initialize):
77
        if "warm_start" in list(initialize.keys()):
78
            warm_start = initialize["warm_start"]
79
            warm_start_gfo = []
80
            for warm_start_ in warm_start:
81
                value = self.para2value(warm_start_)
82
                position = self.value2position(value)
83
                pos_para = self.value2para(position)
84
85
                warm_start_gfo.append(pos_para)
86
87
            initialize["warm_start"] = warm_start_gfo
88
89
        return initialize
90
91
    def get_list_positions(self, list1_values, search_dim):
92
        list_positions = []
93
94
        for value2 in list1_values:
95
            pos_appended = False
96
            for value1 in search_dim:
97
                if value1 == value2:
98
                    list_positions.append(search_dim.index(value1))
99
                    pos_appended = True
100
                    break
101
102
            if not pos_appended:
103
                list_positions.append(None)
104
105
        return list_positions
106
107
    def trafo_memory_warm_start(self, results):
108
        if results is None:
109
            return results
110
111
        df_positions_dict = {}
112
        for para_name in self.para_names:
113
            list1_values = list(results[para_name].values)
114
            search_dim = self.search_space[para_name]
115
            """
116
            list1_positions = [
117
                search_dim.index(value) if value in search_dim else None
118
                for value in list1_values
119
            ]
120
            """
121
122
            """
123
            list1_positions = [
124
                search_dim.index(value1)
125
                for value2 in list1_values
126
                for value1 in search_dim
127
                if value1 == value2
128
            ]
129
            """
130
            list1_positions = self.get_list_positions(list1_values, search_dim)
131
132
            # remove None
133
            # list1_positions_ = [x for x in list1_positions if x is not None]
134
            df_positions_dict[para_name] = list1_positions
135
136
        results_new = pd.DataFrame(df_positions_dict)
137
        results_new["score"] = results["score"]
138
        results_new.dropna(how="any", inplace=True)
139
140
        return results_new
141