Passed
Push — master ( f292df...1dadef )
by Simon
01:02
created

optimization_metadata.memory_conv   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 49
dl 0
loc 81
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A convert_dataframe() 0 28 3
A memory_dict2dataframe() 0 17 2
A dataframe2memory_dict() 0 18 2
A intersection() 0 3 1
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
def intersection(lst1, lst2):
10
    inter = [value for value in lst1 if value in lst2]
11
    return inter
12
13
14
def convert_dataframe(dataframe1, search_space1, search_space2):
15
    dataframe2 = dataframe1.copy()
16
17
    for para1 in search_space1:
18
        if para1 not in search_space2:
19
            dataframe2.drop(para1, axis=1, inplace=True)
20
            continue
21
22
        search_elements1 = search_space1[para1]
23
        search_elements2 = search_space2[para1]
24
25
        inter = intersection(search_elements1, search_elements2)
26
27
        indices_A = [search_elements1.index(x) for x in inter]
28
        indices_B = [search_elements2.index(x) for x in inter]
29
30
        conv_dict = dict(zip(indices_A, indices_B))
31
32
        col = dataframe2[para1]
33
        col_conv = col.map(conv_dict)
34
        col_conv = col_conv.dropna(how="any")
35
        col_conv = col_conv.astype(int)
36
37
        dataframe2[para1] = col_conv
38
39
    dataframe2 = dataframe2.dropna(how="any", axis=0)
40
41
    return dataframe2
42
43
44
def memory_dict2dataframe(memory_dict, search_space):
45
    columns = list(search_space.keys())
46
47
    if not bool(memory_dict):
48
        print("Memory dictionary is empty.")
49
        return pd.DataFrame([], columns=columns)
50
51
    pos_tuple_list = list(memory_dict.keys())
52
    result_list = list(memory_dict.values())
53
54
    results_df = pd.DataFrame(result_list)
55
    np_pos = np.array(pos_tuple_list)
56
57
    pd_pos = pd.DataFrame(np_pos, columns=columns)
58
    dataframe = pd.concat([pd_pos, results_df], axis=1)
59
60
    return dataframe
61
62
63
def dataframe2memory_dict(dataframe, search_space):
64
    columns = list(search_space.keys())
65
66
    if dataframe.empty:
67
        print("Memory dataframe is empty.")
68
        return {}
69
70
    positions = dataframe[columns]
71
    scores = dataframe.drop(columns, axis=1)
72
73
    scores = scores.to_dict("records")
74
    positions_list = positions.values.tolist()
75
76
    # list of lists into list of tuples
77
    pos_tuple_list = list(map(tuple, positions_list))
78
    memory_dict = dict(zip(pos_tuple_list, scores))
79
80
    return memory_dict
81
82