Passed
Push — master ( c3642f...74e28f )
by Simon
01:43
created

hyperactive.print_results.PrintResults.__init__()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 3
dl 0
loc 3
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
7
indent = "  "
8
9
10
class PrintResults:
11
    def __init__(self, opt_pros, verbosity):
12
        self.opt_pros = opt_pros
13
        self.verbosity = verbosity
14
15
    def _print_times(self, eval_time, iter_time, n_iter):
16
        opt_time = iter_time - eval_time
17
        iterPerSec = n_iter / iter_time
18
19
        print(
20
            indent,
21
            "Evaluation time   :",
22
            eval_time,
23
            "sec",
24
            indent,
25
            "[{} %]".format(round(eval_time / iter_time * 100, 2)),
26
        )
27
        print(
28
            indent,
29
            "Optimization time :",
30
            opt_time,
31
            "sec",
32
            indent,
33
            "[{} %]".format(round(opt_time / iter_time * 100, 2)),
34
        )
35
        if iterPerSec >= 1:
36
            print(
37
                indent,
38
                "Iteration time    :",
39
                iter_time,
40
                "sec",
41
                indent,
42
                "[{} iter/sec]".format(round(iterPerSec, 2)),
43
            )
44
        else:
45
            secPerIter = iter_time / n_iter
46
            print(
47
                indent,
48
                "Iteration time    :",
49
                iter_time,
50
                "sec",
51
                indent,
52
                "[{} sec/iter]".format(round(secPerIter, 2)),
53
            )
54
        print(" ")
55
56
    def align_para_names(self, para_names):
57
        str_lengths = [len(str_) for str_ in para_names]
58
        max_length = max(str_lengths)
59
60
        para_names_align = {}
61
        for para_name, str_length in zip(para_names, str_lengths):
62
            added_spaces = max_length - str_length
63
            para_names_align[para_name] = " " * added_spaces
64
65
        return para_names_align
66
67
    def _print_results(
68
        self,
69
        objective_function,
70
        best_score,
71
        best_para,
72
        best_iter,
73
        best_additional_results,
74
        random_seed,
75
    ):
76
        print("\nResults: '{}'".format(objective_function.__name__), " ")
77
        if best_para is None:
78
            print(indent, "Best score:", best_score, " ")
79
            print(indent, "Best parameter set:", best_para, " ")
80
            print(indent, "Best iteration:", best_iter, " ")
81
82
        else:
83
            print(indent, "Best score:", best_score, " ")
84
85
            if best_additional_results:
86
                print(indent, "Best additional results:")
87
                add_results_names = list(best_additional_results.keys())
88
                add_results_names_align = self.align_para_names(add_results_names)
89
90
                for best_additional_result in best_additional_results.keys():
91
                    added_spaces = add_results_names_align[best_additional_result]
92
                    print(
93
                        indent,
94
                        indent,
95
                        "'{}'".format(best_additional_result),
96
                        "{}:".format(added_spaces),
97
                        best_additional_results[best_additional_result],
98
                        " ",
99
                    )
100
101
            if best_para:
102
                print(indent, "Best parameter set:")
103
                para_names = list(best_para.keys())
104
                para_names_align = self.align_para_names(para_names)
105
106
                for para_key in best_para.keys():
107
                    added_spaces = para_names_align[para_key]
108
                    print(
109
                        indent,
110
                        indent,
111
                        "'{}'".format(para_key),
112
                        "{}:".format(added_spaces),
113
                        best_para[para_key],
114
                        " ",
115
                    )
116
117
            print(indent, "Best iteration:", best_iter, " ")
118
119
        print(" ")
120
        print(indent, "Random seed:", random_seed, " ")
121
        print(" ")
122
123
    def print_process(self, results, nth_process):
124
        verbosity = self.verbosity
125
        objective_function = self.opt_pros[nth_process].objective_function
126
        search_space = self.opt_pros[nth_process].s_space.search_space
127
128
        search_data = results["search_data"]
129
130
        best_sample = search_data.iloc[search_data["score"].idxmax()]
131
132
        best_score = best_sample["score"]
133
        best_values = best_sample[list(search_space.keys())]
134
        best_para = dict(zip(list(search_space.keys()), best_values))
135
        best_additional_results_df = best_sample.drop(
136
            ["score"] + list(search_space.keys())
137
        )
138
        best_additional_results = best_additional_results_df.to_dict()
139
140
        best_iter = results["best_iter"]
141
        eval_times = results["eval_times"]
142
        iter_times = results["iter_times"]
143
        random_seed = results["random_seed"]
144
145
        n_iter = self.opt_pros[nth_process].n_iter
146
147
        eval_time = np.array(eval_times).sum()
148
        iter_time = np.array(iter_times).sum()
149
150
        if "print_results" in verbosity:
151
            self._print_results(
152
                objective_function,
153
                best_score,
154
                best_para,
155
                best_iter,
156
                best_additional_results,
157
                random_seed,
158
            )
159
160
        if "print_times" in verbosity:
161
            self._print_times(eval_time, iter_time, n_iter)
162