hyperactive.results.Results.search_data()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nop 2
1
"""Results handling for hyperparameter optimization.
2
3
Author: Simon Blanke
4
Email: [email protected]
5
License: MIT License
6
"""
7
8
import numpy as np
9
import pandas as pd
10
11
12
class Results:
13
    """Results class."""
14
15
    def __init__(self, results_list, opt_pros):
16
        self.results_list = results_list
17
        self.opt_pros = opt_pros
18
19
        self.objFunc2results = {}
20
        self.search_id2results = {}
21
22
    def _sort_results_objFunc(self, objective_function):
23
        best_score = -np.inf
24
        best_para = None
25
        search_data = None
26
27
        search_data_list = []
28
29
        for results_ in self.results_list:
30
            nth_process = results_["nth_process"]
31
32
            opt = self.opt_pros[nth_process]
33
            objective_function_ = opt.objective_function
34
            search_space_ = opt.s_space()
35
            params = list(search_space_.keys())
36
37
            if objective_function_ != objective_function:
38
                continue
39
40
            if results_["best_score"] > best_score:
41
                best_score = results_["best_score"]
42
                best_para = results_["best_para"]
43
44
            search_data = results_["search_data"]
45
            search_data["eval_times"] = results_["eval_times"]
46
            search_data["iter_times"] = results_["iter_times"]
47
48
            search_data_list.append(search_data)
49
50
        if len(search_data_list) > 0:
51
            search_data = pd.concat(search_data_list)
52
53
        self.objFunc2results[objective_function] = {
54
            "best_para": best_para,
55
            "best_score": best_score,
56
            "search_data": search_data,
57
            "params": params,
0 ignored issues
show
introduced by
The variable params does not seem to be defined in case the for loop on line 29 is not entered. Are you sure this can never be the case?
Loading history...
58
        }
59
60
    def _get_result(self, id_, result_name):
61
        if id_ not in self.objFunc2results:
62
            self._sort_results_objFunc(id_)
63
64
        search_data = self.objFunc2results[id_][result_name]
65
66
        return search_data
67
68
    def best_para(self, id_):
69
        """Best Para function."""
70
        best_para_ = self._get_result(id_, "best_para")
71
72
        if best_para_ is not None:
73
            return best_para_
74
75
        raise ValueError("objective function name not recognized")
76
77
    def best_score(self, id_):
78
        """Best Score function."""
79
        best_score_ = self._get_result(id_, "best_score")
80
81
        if best_score_ != -np.inf:
82
            return best_score_
83
84
        raise ValueError("objective function name not recognized")
85
86
    def search_data(self, id_):
87
        """Search Data function."""
88
        search_data = self._get_result(id_, "search_data")
89
90
        if search_data is not None:
91
            return search_data
92
93
        raise ValueError("objective function name not recognized")
94