tests.test_random_state   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 206
Duplicated Lines 56.8 %

Importance

Changes 0
Metric Value
wmc 8
eloc 138
dl 117
loc 206
rs 10
c 0
b 0
f 0

8 Functions

Rating   Name   Duplication   Size   Complexity  
A test_random_state_n_jobs_2() 21 22 1
A test_random_state_1() 25 26 1
A test_random_state_n_jobs_0() 0 25 1
A test_random_state_0() 25 26 1
A test_random_state_2() 25 26 1
A test_no_random_state_0() 0 24 1
A test_random_state_n_jobs_1() 21 22 1
A objective_function() 0 4 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
"""Test module for random state functionality."""
2
3
import numpy as np
4
5
from hyperactive import Hyperactive
6
7
8
def objective_function(opt):
9
    """Two-dimensional quadratic objective function for random state testing."""
10
    score = -(opt["x1"] * opt["x1"] + opt["x2"] * opt["x2"])
11
    return score
12
13
14
search_space = {
15
    "x1": list(np.arange(-1000, 1000, 0.1)),
16
    "x2": list(np.arange(-1000, 1000, 0.1)),
17
}
18
19
20
err = 0.001
21
22
23
def test_random_state_n_jobs_0():
24
    """Test random state behavior with n_jobs=2."""
25
    n_jobs = 2
26
27
    hyper = Hyperactive()
28
    hyper.add_search(
29
        objective_function,
30
        search_space,
31
        n_iter=5,
32
        initialize={"random": 1},
33
        random_state=1,
34
        n_jobs=n_jobs,
35
    )
36
    hyper.run()
37
38
    results = hyper.search_data(objective_function)
39
40
    no_dup = results.drop_duplicates(subset=list(search_space.keys()))
41
    print("no_dup", no_dup)
42
    print("results", results)
43
44
    print(int(len(results) / n_jobs))
45
    print(len(no_dup))
46
47
    assert int(len(results) / n_jobs) != len(no_dup)
48
49
50 View Code Duplication
def test_random_state_n_jobs_1():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
51
    """Test random state behavior with n_jobs=3."""
52
    n_jobs = 3
53
54
    hyper = Hyperactive()
55
    hyper.add_search(
56
        objective_function,
57
        search_space,
58
        n_iter=5,
59
        initialize={"random": 1},
60
        random_state=1,
61
        n_jobs=n_jobs,
62
    )
63
    hyper.run()
64
65
    results = hyper.search_data(objective_function)
66
67
    no_dup = results.drop_duplicates(subset=list(search_space.keys()))
68
    print("no_dup", no_dup)
69
    print("results", results)
70
71
    assert int(len(results) / n_jobs) != len(no_dup)
72
73
74 View Code Duplication
def test_random_state_n_jobs_2():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
75
    """Test random state behavior with n_jobs=4."""
76
    n_jobs = 4
77
78
    hyper = Hyperactive()
79
    hyper.add_search(
80
        objective_function,
81
        search_space,
82
        n_iter=5,
83
        initialize={"random": 1},
84
        random_state=1,
85
        n_jobs=n_jobs,
86
    )
87
    hyper.run()
88
89
    results = hyper.search_data(objective_function)
90
91
    no_dup = results.drop_duplicates(subset=list(search_space.keys()))
92
    print("no_dup", no_dup)
93
    print("results", results)
94
95
    assert int(len(results) / n_jobs) != len(no_dup)
96
97
98 View Code Duplication
def test_random_state_0():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
99
    """Test reproducibility with same random state."""
100
    hyper0 = Hyperactive()
101
    hyper0.add_search(
102
        objective_function,
103
        search_space,
104
        n_iter=10,
105
        initialize={"random": 1},
106
        random_state=1,
107
    )
108
    hyper0.run()
109
110
    hyper1 = Hyperactive()
111
    hyper1.add_search(
112
        objective_function,
113
        search_space,
114
        n_iter=10,
115
        initialize={"random": 1},
116
        random_state=1,
117
    )
118
    hyper1.run()
119
120
    best_score0 = hyper0.best_score(objective_function)
121
    best_score1 = hyper1.best_score(objective_function)
122
123
    assert abs(best_score0 - best_score1) < err
124
125
126 View Code Duplication
def test_random_state_1():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
127
    """Test reproducibility with same random state (different value)."""
128
    hyper0 = Hyperactive()
129
    hyper0.add_search(
130
        objective_function,
131
        search_space,
132
        n_iter=10,
133
        initialize={"random": 1},
134
        random_state=10,
135
    )
136
    hyper0.run()
137
138
    hyper1 = Hyperactive()
139
    hyper1.add_search(
140
        objective_function,
141
        search_space,
142
        n_iter=10,
143
        initialize={"random": 1},
144
        random_state=10,
145
    )
146
    hyper1.run()
147
148
    best_score0 = hyper0.best_score(objective_function)
149
    best_score1 = hyper1.best_score(objective_function)
150
151
    assert abs(best_score0 - best_score1) < err
152
153
154 View Code Duplication
def test_random_state_2():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
155
    """Test different results with different random states."""
156
    hyper0 = Hyperactive()
157
    hyper0.add_search(
158
        objective_function,
159
        search_space,
160
        n_iter=10,
161
        initialize={"random": 1},
162
        random_state=1,
163
    )
164
    hyper0.run()
165
166
    hyper1 = Hyperactive()
167
    hyper1.add_search(
168
        objective_function,
169
        search_space,
170
        n_iter=10,
171
        initialize={"random": 1},
172
        random_state=10,
173
    )
174
    hyper1.run()
175
176
    best_score0 = hyper0.best_score(objective_function)
177
    best_score1 = hyper1.best_score(objective_function)
178
179
    assert abs(best_score0 - best_score1) > err
180
181
182
def test_no_random_state_0():
183
    """Test non-reproducibility without fixed random state."""
184
    hyper0 = Hyperactive()
185
    hyper0.add_search(
186
        objective_function,
187
        search_space,
188
        n_iter=10,
189
        initialize={"random": 1},
190
    )
191
    hyper0.run()
192
193
    hyper1 = Hyperactive()
194
    hyper1.add_search(
195
        objective_function,
196
        search_space,
197
        n_iter=10,
198
        initialize={"random": 1},
199
    )
200
    hyper1.run()
201
202
    best_score0 = hyper0.best_score(objective_function)
203
    best_score1 = hyper1.best_score(objective_function)
204
205
    assert abs(best_score0 - best_score1) > err
206