optimization_metadata.memory_conv   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 47
dl 0
loc 79
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A convert_dataframe() 0 28 3
A intersection() 0 3 1
A memory_dict2dataframe() 0 16 2
A dataframe2memory_dict() 0 17 2
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
        return pd.DataFrame([], columns=columns)
49
50
    pos_tuple_list = list(memory_dict.keys())
51
    result_list = list(memory_dict.values())
52
53
    results_df = pd.DataFrame(result_list)
54
    np_pos = np.array(pos_tuple_list)
55
56
    pd_pos = pd.DataFrame(np_pos, columns=columns)
57
    dataframe = pd.concat([pd_pos, results_df], axis=1)
58
59
    return dataframe
60
61
62
def dataframe2memory_dict(dataframe, search_space):
63
    columns = list(search_space.keys())
64
65
    if dataframe.empty:
66
        return {}
67
68
    positions = dataframe[columns]
69
    scores = dataframe.drop(columns, axis=1)
70
71
    scores = scores.to_dict("records")
72
    positions_list = positions.values.tolist()
73
74
    # list of lists into list of tuples
75
    pos_tuple_list = list(map(tuple, positions_list))
76
    memory_dict = dict(zip(pos_tuple_list, scores))
77
78
    return memory_dict
79
80