Passed
Push — master ( 08aa78...571090 )
by Simon
02:09 queued 12s
created

hyperactive.print_info.print_info()   A

Complexity

Conditions 3

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nop 8
dl 0
loc 19
rs 9.65
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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, best_score, best_para, best_iter):
66
    print("\nResults: '{}'".format(objective_function.__name__), " ")
67
    if best_para is None:
68
        print(indent, "Best score:", best_score, " ")
69
        print(indent, "Best parameter set:", best_para, " ")
70
        print(indent, "Best iteration:", best_iter, " ")
71
72
    else:
73
        para_names = list(best_para.keys())
74
        para_names_align = align_para_names(para_names)
75
76
        print(indent, "Best score:", best_score, " ")
77
        print(indent, "Best parameter set:")
78
79
        for para_key in best_para.keys():
80
            added_spaces = para_names_align[para_key]
81
            print(
82
                indent,
83
                indent,
84
                "'{}'".format(para_key),
85
                "{}:".format(added_spaces),
86
                best_para[para_key],
87
                " ",
88
            )
89
        print(indent, "Best iteration:", best_iter, " ")
90
91
    print(" ")
92
93
94
def print_info(
95
    verbosity,
96
    objective_function,
97
    best_score,
98
    best_para,
99
    best_iter,
100
    eval_times,
101
    iter_times,
102
    n_iter,
103
):
104
105
    eval_time = np.array(eval_times).sum()
106
    iter_time = np.array(iter_times).sum()
107
108
    if "print_results" in verbosity:
109
        _print_results(objective_function, best_score, best_para, best_iter)
110
111
    if "print_times" in verbosity:
112
        _print_times(eval_time, iter_time, n_iter)
113