Passed
Pull Request — master (#101)
by Simon
01:32
created

tests_old.test_warm_starts.test_warm_start_smbo   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 158
Duplicated Lines 36.71 %

Importance

Changes 0
Metric Value
eloc 108
dl 58
loc 158
rs 10
c 0
b 0
f 0
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A class2.__init__() 0 2 1
A class1.__init__() 0 2 1

11 Functions

Rating   Name   Duplication   Size   Complexity  
A test_warm_start_smbo_0() 0 18 1
A func2() 0 2 1
A numpy_f1() 0 2 1
A class_f1() 0 2 1
A test_warm_start_smbo_1() 20 20 1
A numpy_f2() 0 2 1
A func1() 0 2 1
A class_f2() 0 2 1
A objective_function() 0 3 1
A test_warm_start_smbo_2() 19 19 1
A test_warm_start_smbo_3() 19 19 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import time
2
import pytest
3
import numpy as np
4
import pandas as pd
5
6
from hyperactive import (
7
    Hyperactive,
8
)
9
10
from hyperactive.optimizers import (
11
    BayesianOptimizer,
12
    TreeStructuredParzenEstimators,
13
    ForestOptimizer,
14
)
15
16
17
def func1():
18
    pass
19
20
21
def func2():
22
    pass
23
24
25
class class1:
26
    def __init__(self):
27
        pass
28
29
30
class class2:
31
    def __init__(self):
32
        pass
33
34
35
def class_f1():
36
    return class1
37
38
39
def class_f2():
40
    return class2
41
42
43
def numpy_f1():
44
    return np.array([0, 1])
45
46
47
def numpy_f2():
48
    return np.array([1, 0])
49
50
51
search_space = {
52
    "x0": list(range(-3, 3)),
53
    "x1": list(np.arange(-1, 1, 0.001)),
54
    "string0": ["str0", "str1"],
55
    "function0": [func1, func2],
56
    "class0": [class_f1, class_f2],
57
    "numpy0": [numpy_f1, numpy_f2],
58
}
59
60
61
def objective_function(opt):
62
    score = -opt["x1"] * opt["x1"]
63
    return score
64
65
66
smbo_opts = [
67
    BayesianOptimizer,
68
    TreeStructuredParzenEstimators,
69
    ForestOptimizer,
70
]
71
72
initialize = {"random": 1}
73
n_iter = 3
74
75
76
@pytest.mark.parametrize("smbo_opt", smbo_opts)
77
def test_warm_start_smbo_0(smbo_opt):
78
    hyper0 = Hyperactive()
79
    hyper0.add_search(objective_function, search_space, n_iter=n_iter)
80
    hyper0.run()
81
82
    search_data0 = hyper0.search_data(objective_function)
83
    smbo_opt_ = smbo_opt(warm_start_smbo=search_data0)
84
85
    hyper1 = Hyperactive()
86
    hyper1.add_search(
87
        objective_function,
88
        search_space,
89
        n_iter=n_iter,
90
        optimizer=smbo_opt_,
91
        initialize=initialize,
92
    )
93
    hyper1.run()
94
95
96 View Code Duplication
@pytest.mark.parametrize("smbo_opt", smbo_opts)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
97
def test_warm_start_smbo_1(smbo_opt):
98
    hyper0 = Hyperactive(distribution="pathos")
99
    hyper0.add_search(
100
        objective_function,
101
        search_space,
102
        n_iter=n_iter,
103
        n_jobs=2,
104
        initialize=initialize,
105
    )
106
    hyper0.run()
107
108
    search_data0 = hyper0.search_data(objective_function)
109
    smbo_opt_ = smbo_opt(warm_start_smbo=search_data0)
110
111
    hyper1 = Hyperactive()
112
    hyper1.add_search(
113
        objective_function, search_space, n_iter=n_iter, optimizer=smbo_opt_
114
    )
115
    hyper1.run()
116
117
118 View Code Duplication
@pytest.mark.parametrize("smbo_opt", smbo_opts)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
119
def test_warm_start_smbo_2(smbo_opt):
120
    hyper0 = Hyperactive()
121
    hyper0.add_search(objective_function, search_space, n_iter=n_iter)
122
    hyper0.run()
123
124
    search_data0 = hyper0.search_data(objective_function)
125
    smbo_opt_ = smbo_opt(warm_start_smbo=search_data0)
126
127
    hyper1 = Hyperactive(distribution="joblib")
128
    hyper1.add_search(
129
        objective_function,
130
        search_space,
131
        n_iter=n_iter,
132
        n_jobs=2,
133
        optimizer=smbo_opt_,
134
        initialize=initialize,
135
    )
136
    hyper1.run()
137
138
139 View Code Duplication
@pytest.mark.parametrize("smbo_opt", smbo_opts)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
140
def test_warm_start_smbo_3(smbo_opt):
141
    hyper0 = Hyperactive(distribution="pathos")
142
    hyper0.add_search(objective_function, search_space, n_iter=n_iter, n_jobs=2)
143
    hyper0.run()
144
145
    search_data0 = hyper0.search_data(objective_function)
146
    smbo_opt_ = smbo_opt(warm_start_smbo=search_data0)
147
148
    hyper1 = Hyperactive(distribution="joblib")
149
    hyper1.add_search(
150
        objective_function,
151
        search_space,
152
        n_iter=n_iter,
153
        n_jobs=2,
154
        optimizer=smbo_opt_,
155
        initialize=initialize,
156
    )
157
    hyper1.run()
158