Passed
Push — master ( 8df986...d057bb )
by Simon
01:28 queued 11s
created

tests.test_optimizers.test_max_score   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 79
Duplicated Lines 70.89 %

Importance

Changes 0
Metric Value
eloc 52
dl 56
loc 79
rs 10
c 0
b 0
f 0
wmc 3

3 Functions

Rating   Name   Duplication   Size   Complexity  
A objective_function() 0 3 1
A test_max_score_1() 28 28 1
A test_max_score_0() 28 28 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 pytest
2
import time
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 ._parametrize import optimizers
9
10
11
def objective_function(para):
12
    score = -para["x1"] * para["x1"]
13
    return score
14
15
16
search_space = {
17
    "x1": np.arange(0, 100000, 0.1),
18
}
19
20
21 View Code Duplication
@pytest.mark.parametrize(*optimizers)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
22
def test_max_score_0(Optimizer):
23
    def objective_function(para):
24
        score = -para["x1"] * para["x1"]
25
        return score
26
27
    search_space = {
28
        "x1": np.arange(0, 100, 0.1),
29
    }
30
31
    max_score = -9999
32
33
    opt = Optimizer(
34
        search_space,
35
        initialize={"warm_start": [{"x1": 99}]},
36
    )
37
    opt.search(
38
        objective_function,
39
        n_iter=100,
40
        max_score=max_score,
41
    )
42
43
    print("\n Results head \n", opt.results.head())
44
    print("\n Results tail \n", opt.results.tail())
45
46
    print("\nN iter:", len(opt.results))
47
48
    assert -100 > opt.best_score > max_score
49
50
51 View Code Duplication
@pytest.mark.parametrize(*optimizers)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
52
def test_max_score_1(Optimizer):
53
    def objective_function(para):
54
        score = -para["x1"] * para["x1"]
55
        time.sleep(0.01)
56
        return score
57
58
    search_space = {
59
        "x1": np.arange(0, 100, 0.1),
60
    }
61
62
    max_score = -9999
63
64
    c_time = time.time()
65
    opt = Optimizer(search_space, initialize={"warm_start": [{"x1": 99}]})
66
    opt.search(
67
        objective_function,
68
        n_iter=100000,
69
        max_score=max_score,
70
    )
71
    diff_time = time.time() - c_time
72
73
    print("\n Results head \n", opt.results.head())
74
    print("\n Results tail \n", opt.results.tail())
75
76
    print("\nN iter:", len(opt.results))
77
78
    assert diff_time < 1
79