Passed
Push — master ( 7ebd63...8bd9a3 )
by Simon
06:11
created

align_para_names()   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nop 1
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import numpy as np
6
7
indent = "  "
8
9
10
def _print_times(eval_time, iter_time, n_iter):
11
12
    opt_time = iter_time - eval_time
13
    iterPerSec = n_iter / iter_time
14
15
    print(
16
        indent,
17
        "Evaluation time   :",
18
        eval_time,
19
        "sec",
20
        indent,
21
        "[{} %]".format(round(eval_time / iter_time * 100, 2)),
22
    )
23
    print(
24
        indent,
25
        "Optimization time :",
26
        opt_time,
27
        "sec",
28
        indent,
29
        "[{} %]".format(round(opt_time / iter_time * 100, 2)),
30
    )
31
    if iterPerSec >= 1:
32
        print(
33
            indent,
34
            "Iteration time    :",
35
            iter_time,
36
            "sec",
37
            indent,
38
            "[{} iter/sec]".format(round(iterPerSec, 2)),
39
        )
40
    else:
41
        secPerIter = iter_time / n_iter
42
        print(
43
            indent,
44
            "Iteration time    :",
45
            iter_time,
46
            "sec",
47
            indent,
48
            "[{} sec/iter]".format(round(secPerIter, 2)),
49
        )
50
    print(" ")
51
52
53
def align_para_names(para_names):
54
    str_lengths = [len(str_) for str_ in para_names]
55
    max_length = max(str_lengths)
56
57
    para_names_align = {}
58
    for para_name, str_length in zip(para_names, str_lengths):
59
        added_spaces = max_length - str_length
60
        para_names_align[para_name] = " " * added_spaces
61
62
    return para_names_align
63
64
65
def _print_results(objective_function, score_best, para_best, random_seed):
66
    print("\nResults: '{}'".format(objective_function.__name__), " ")
67
    if para_best is None:
68
        print(indent, "Best score:", score_best, " ")
69
        print(indent, "Best parameter:", para_best, " ")
70
    else:
71
        para_names = list(para_best.keys())
72
        para_names_align = align_para_names(para_names)
73
74
        print(indent, "Best score:", score_best, " ")
75
        print(indent, "Best parameter:")
76
        for para_key in para_best.keys():
77
            added_spaces = para_names_align[para_key]
78
            print(
79
                indent,
80
                indent,
81
                "'{}'".format(para_key),
82
                "{}:".format(added_spaces),
83
                para_best[para_key],
84
                " ",
85
            )
86
    print(" ")
87
    print(indent, "Random seed:", random_seed, " ")
88
    print(" ")
89
90
91
def print_info(
92
    verbosity,
93
    objective_function,
94
    score_best,
95
    para_best,
96
    eval_times,
97
    iter_times,
98
    n_iter,
99
    random_seed,
100
):
101
102
    eval_time = np.array(eval_times).sum()
103
    iter_time = np.array(iter_times).sum()
104
105
    if "print_results" in verbosity:
106
        _print_results(objective_function, score_best, para_best, random_seed)
107
108
    if "print_times" in verbosity:
109
        _print_times(eval_time, iter_time, n_iter)
110