tests.test_early_stop   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 338
Duplicated Lines 65.09 %

Importance

Changes 0
Metric Value
eloc 249
dl 220
loc 338
rs 10
c 0
b 0
f 0
wmc 9

9 Functions

Rating   Name   Duplication   Size   Complexity  
A test_early_stop_3() 0 31 1
A test_early_stop_1() 0 16 1
A test_early_stop_2() 0 16 1
A test_early_stop_0() 0 16 1
B test_early_stop_4() 59 59 1
B test_early_stop_6() 59 59 1
B test_early_stop_5() 51 51 1
A objective_function() 0 3 1
B test_early_stop_7() 51 51 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
from sklearn.datasets import load_breast_cancer
5
from sklearn.model_selection import cross_val_score
6
from sklearn.tree import DecisionTreeClassifier
7
8
from hyperactive import Hyperactive
9
from hyperactive.optimizers import (
10
    RandomSearchOptimizer,
11
    HillClimbingOptimizer,
12
)
13
14
15
def objective_function(para):
16
    score = -para["x1"] * para["x1"]
17
    return score
18
19
20
search_space = {
21
    "x1": list(np.arange(0, 100000, 0.1)),
22
}
23
24
25
def test_early_stop_0():
26
    early_stopping = {
27
        "n_iter_no_change": 5,
28
        "tol_abs": 0.1,
29
        "tol_rel": 0.1,
30
    }
31
32
    hyper = Hyperactive()
33
    hyper.add_search(
34
        objective_function,
35
        search_space,
36
        n_iter=1000,
37
        initialize={"warm_start": [{"x1": 0}]},
38
        early_stopping=early_stopping,
39
    )
40
    hyper.run()
41
42
43
def test_early_stop_1():
44
    early_stopping = {
45
        "n_iter_no_change": 5,
46
        "tol_abs": None,
47
        "tol_rel": 5,
48
    }
49
50
    hyper = Hyperactive()
51
    hyper.add_search(
52
        objective_function,
53
        search_space,
54
        n_iter=1000,
55
        initialize={"warm_start": [{"x1": 0}]},
56
        early_stopping=early_stopping,
57
    )
58
    hyper.run()
59
60
61
def test_early_stop_2():
62
    early_stopping = {
63
        "n_iter_no_change": 5,
64
        "tol_abs": 0.1,
65
        "tol_rel": None,
66
    }
67
68
    hyper = Hyperactive()
69
    hyper.add_search(
70
        objective_function,
71
        search_space,
72
        n_iter=1000,
73
        initialize={"warm_start": [{"x1": 0}]},
74
        early_stopping=early_stopping,
75
    )
76
    hyper.run()
77
78
79
def test_early_stop_3():
80
    def objective_function(para):
81
        score = -para["x1"] * para["x1"]
82
        return score
83
84
    search_space = {
85
        "x1": list(np.arange(0, 100, 0.1)),
86
    }
87
88
    n_iter_no_change = 5
89
    early_stopping = {
90
        "n_iter_no_change": n_iter_no_change,
91
    }
92
93
    hyper = Hyperactive()
94
    hyper.add_search(
95
        objective_function,
96
        search_space,
97
        n_iter=100000,
98
        initialize={"warm_start": [{"x1": 0}]},
99
        early_stopping=early_stopping,
100
    )
101
    hyper.run()
102
103
    search_data = hyper.search_data(objective_function)
104
    n_performed_iter = len(search_data)
105
106
    print("\n n_performed_iter \n", n_performed_iter)
107
    print("\n n_iter_no_change \n", n_iter_no_change)
108
109
    assert n_performed_iter == (n_iter_no_change + 1)
110
111
112 View Code Duplication
def test_early_stop_4():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
113
    def objective_function(para):
114
        return para["x1"]
115
116
    search_space = {
117
        "x1": list(np.arange(0, 100, 0.1)),
118
    }
119
120
    n_iter_no_change = 5
121
    early_stopping = {
122
        "n_iter_no_change": 5,
123
        "tol_abs": 0.1,
124
        "tol_rel": None,
125
    }
126
127
    start1 = {"x1": 0}
128
    start2 = {"x1": 0.1}
129
    start3 = {"x1": 0.2}
130
    start4 = {"x1": 0.3}
131
    start5 = {"x1": 0.4}
132
133
    warm_start_l = [
134
        start1,
135
        start1,
136
        start1,
137
        start1,
138
        start1,
139
        start2,
140
        start2,
141
        start2,
142
        start3,
143
        start3,
144
        start3,
145
        start4,
146
        start4,
147
        start4,
148
        start5,
149
        start5,
150
        start5,
151
    ]
152
    n_iter = len(warm_start_l)
153
154
    hyper = Hyperactive()
155
    hyper.add_search(
156
        objective_function,
157
        search_space,
158
        n_iter=n_iter,
159
        initialize={"warm_start": warm_start_l},
160
        early_stopping=early_stopping,
161
    )
162
    hyper.run()
163
164
    search_data = hyper.search_data(objective_function)
165
    n_performed_iter = len(search_data)
166
167
    print("\n n_performed_iter \n", n_performed_iter)
168
    print("\n n_iter_no_change \n", n_iter_no_change)
169
170
    assert n_performed_iter == n_iter
171
172
173 View Code Duplication
def test_early_stop_5():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
174
    def objective_function(para):
175
        return para["x1"]
176
177
    search_space = {
178
        "x1": list(np.arange(0, 100, 0.01)),
179
    }
180
181
    n_iter_no_change = 5
182
    early_stopping = {
183
        "n_iter_no_change": n_iter_no_change,
184
        "tol_abs": 0.1,
185
        "tol_rel": None,
186
    }
187
188
    start1 = {"x1": 0}
189
    start2 = {"x1": 0.09}
190
    start3 = {"x1": 0.20}
191
192
    warm_start_l = [
193
        start1,
194
        start1,
195
        start1,
196
        start1,
197
        start1,
198
        start2,
199
        start2,
200
        start2,
201
        start3,
202
        start3,
203
        start3,
204
    ]
205
    n_iter = len(warm_start_l)
206
207
    hyper = Hyperactive()
208
    hyper.add_search(
209
        objective_function,
210
        search_space,
211
        n_iter=n_iter,
212
        initialize={"warm_start": warm_start_l},
213
        early_stopping=early_stopping,
214
    )
215
    hyper.run()
216
217
    search_data = hyper.search_data(objective_function)
218
    n_performed_iter = len(search_data)
219
220
    print("\n n_performed_iter \n", n_performed_iter)
221
    print("\n n_iter_no_change \n", n_iter_no_change)
222
223
    assert n_performed_iter == (n_iter_no_change + 1)
224
225
226 View Code Duplication
def test_early_stop_6():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
227
    def objective_function(para):
228
        return para["x1"]
229
230
    search_space = {
231
        "x1": list(np.arange(0, 100, 0.01)),
232
    }
233
234
    n_iter_no_change = 5
235
    early_stopping = {
236
        "n_iter_no_change": 5,
237
        "tol_abs": None,
238
        "tol_rel": 10,
239
    }
240
241
    start1 = {"x1": 1}
242
    start2 = {"x1": 1.1}
243
    start3 = {"x1": 1.22}
244
    start4 = {"x1": 1.35}
245
    start5 = {"x1": 1.48}
246
247
    warm_start_l = [
248
        start1,
249
        start1,
250
        start1,
251
        start1,
252
        start1,
253
        start2,
254
        start2,
255
        start2,
256
        start3,
257
        start3,
258
        start3,
259
        start4,
260
        start4,
261
        start4,
262
        start5,
263
        start5,
264
        start5,
265
    ]
266
    n_iter = len(warm_start_l)
267
268
    hyper = Hyperactive()
269
    hyper.add_search(
270
        objective_function,
271
        search_space,
272
        n_iter=n_iter,
273
        initialize={"warm_start": warm_start_l},
274
        early_stopping=early_stopping,
275
    )
276
    hyper.run()
277
278
    search_data = hyper.search_data(objective_function)
279
    n_performed_iter = len(search_data)
280
281
    print("\n n_performed_iter \n", n_performed_iter)
282
    print("\n n_iter_no_change \n", n_iter_no_change)
283
284
    assert n_performed_iter == n_iter
285
286
287 View Code Duplication
def test_early_stop_7():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
288
    def objective_function(para):
289
        return para["x1"]
290
291
    search_space = {
292
        "x1": list(np.arange(0, 100, 0.01)),
293
    }
294
295
    n_iter_no_change = 5
296
    early_stopping = {
297
        "n_iter_no_change": n_iter_no_change,
298
        "tol_abs": None,
299
        "tol_rel": 10,
300
    }
301
302
    start1 = {"x1": 1}
303
    start2 = {"x1": 1.09}
304
    start3 = {"x1": 1.20}
305
306
    warm_start_l = [
307
        start1,
308
        start1,
309
        start1,
310
        start1,
311
        start1,
312
        start2,
313
        start2,
314
        start2,
315
        start3,
316
        start3,
317
        start3,
318
    ]
319
    n_iter = len(warm_start_l)
320
321
    hyper = Hyperactive()
322
    hyper.add_search(
323
        objective_function,
324
        search_space,
325
        n_iter=n_iter,
326
        initialize={"warm_start": warm_start_l},
327
        early_stopping=early_stopping,
328
    )
329
    hyper.run()
330
331
    search_data = hyper.search_data(objective_function)
332
    n_performed_iter = len(search_data)
333
334
    print("\n n_performed_iter \n", n_performed_iter)
335
    print("\n n_iter_no_change \n", n_iter_no_change)
336
337
    assert n_performed_iter == (n_iter_no_change + 1)
338