OptimizerAttributes._add_result_attributes()   C
last analyzed

Complexity

Conditions 11

Size

Total Lines 47
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 33
nop 8
dl 0
loc 47
rs 5.4
c 0
b 0
f 0

How to fix   Complexity    Many Parameters   

Complexity

Complex classes like hyperactive.optimizers.strategies.optimizer_attributes.OptimizerAttributes._add_result_attributes() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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
"""optimizer_attributes module for Hyperactive optimization."""
2
3
# Email: [email protected]
4
# License: MIT License
5
6
import pandas as pd
7
8
9
class OptimizerAttributes:
10
    """OptimizerAttributes class."""
11
12
    def __init__(self):
13
        self.best_para = None
14
        self.best_score = None
15
        self.best_since_iter = None
16
        self.eval_times = None
17
        self.iter_times = None
18
        self.search_data = None
19
        self.random_seed = None
20
21
    def _add_result_attributes(
22
        self,
23
        best_para,
24
        best_score,
25
        best_since_iter,
26
        eval_times,
27
        iter_times,
28
        search_data,
29
        random_seed,
30
    ):
31
        if self.best_para is None:
32
            self.best_para = best_para
33
        else:
34
            if best_score > self.best_score:
35
                self.best_para = best_para
36
37
        if self.best_score is None:
38
            self.best_score = best_score
39
        else:
40
            if best_score > self.best_score:
41
                self.best_score = best_score
42
43
        if self.best_since_iter is None:
44
            self.best_since_iter = best_since_iter
45
        else:
46
            if best_score > self.best_score:
47
                self.best_since_iter = best_since_iter
48
49
        if self.eval_times is None:
50
            self.eval_times = eval_times
51
        else:
52
            self.eval_times = self.eval_times + eval_times
53
54
        if self.iter_times is None:
55
            self.iter_times = iter_times
56
        else:
57
            self.iter_times = self.iter_times + eval_times
58
59
        if self.search_data is None:
60
            self.search_data = search_data
61
        else:
62
            self.search_data = pd.concat(
63
                [self.search_data, search_data], ignore_index=True
64
            )
65
66
        if self.random_seed is None:
67
            self.random_seed = random_seed
68