1
|
|
|
"""Test module for results methods functionality.""" |
2
|
|
|
|
3
|
|
|
import numbers |
4
|
|
|
|
5
|
|
|
import numpy as np |
6
|
|
|
import pandas as pd |
7
|
|
|
import pytest |
8
|
|
|
|
9
|
|
|
from hyperactive import Hyperactive |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def objective_function(opt): |
13
|
|
|
"""Primary objective function for results testing.""" |
14
|
|
|
score = -opt["x1"] * opt["x1"] |
15
|
|
|
return score |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def objective_function1(opt): |
19
|
|
|
"""Secondary objective function for results testing.""" |
20
|
|
|
score = -opt["x1"] * opt["x1"] |
21
|
|
|
return score |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
search_space = { |
25
|
|
|
"x1": list(np.arange(0, 100, 1)), |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def test_attributes_best_score_objective_function_0(): |
30
|
|
|
"""Test best score returns numeric value.""" |
31
|
|
|
hyper = Hyperactive() |
32
|
|
|
hyper.add_search( |
33
|
|
|
objective_function, |
34
|
|
|
search_space, |
35
|
|
|
n_iter=15, |
36
|
|
|
) |
37
|
|
|
hyper.run() |
38
|
|
|
|
39
|
|
|
assert isinstance(hyper.best_score(objective_function), numbers.Number) |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def test_attributes_best_score_objective_function_1(): |
43
|
|
|
"""Test best score with multiple objective functions.""" |
44
|
|
|
hyper = Hyperactive() |
45
|
|
|
hyper.add_search( |
46
|
|
|
objective_function, |
47
|
|
|
search_space, |
48
|
|
|
n_iter=15, |
49
|
|
|
) |
50
|
|
|
hyper.add_search( |
51
|
|
|
objective_function1, |
52
|
|
|
search_space, |
53
|
|
|
n_iter=15, |
54
|
|
|
) |
55
|
|
|
hyper.run() |
56
|
|
|
|
57
|
|
|
assert isinstance(hyper.best_score(objective_function), numbers.Number) |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
""" |
61
|
|
|
def test_attributes_best_score_search_id_0(): |
62
|
|
|
# Test best score with search ID. |
63
|
|
|
hyper = Hyperactive() |
64
|
|
|
hyper.add_search( |
65
|
|
|
objective_function, |
66
|
|
|
search_space, |
67
|
|
|
search_id="1", |
68
|
|
|
n_iter=15, |
69
|
|
|
) |
70
|
|
|
hyper.run() |
71
|
|
|
|
72
|
|
|
assert isinstance(hyper.best_score(objective_function), numbers.Number) |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def test_attributes_best_score_search_id_1(): |
76
|
|
|
# Test best score with multiple search IDs. |
77
|
|
|
hyper = Hyperactive() |
78
|
|
|
hyper.add_search( |
79
|
|
|
objective_function, |
80
|
|
|
search_space, |
81
|
|
|
search_id="1", |
82
|
|
|
n_iter=15, |
83
|
|
|
) |
84
|
|
|
hyper.add_search( |
85
|
|
|
objective_function1, |
86
|
|
|
search_space, |
87
|
|
|
search_id="2", |
88
|
|
|
n_iter=15, |
89
|
|
|
) |
90
|
|
|
hyper.run() |
91
|
|
|
|
92
|
|
|
assert isinstance(hyper.best_score(objective_function), numbers.Number) |
93
|
|
|
""" |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
def test_attributes_best_para_objective_function_0(): |
97
|
|
|
"""Test best parameters returns dictionary.""" |
98
|
|
|
hyper = Hyperactive() |
99
|
|
|
hyper.add_search( |
100
|
|
|
objective_function, |
101
|
|
|
search_space, |
102
|
|
|
n_iter=15, |
103
|
|
|
) |
104
|
|
|
hyper.run() |
105
|
|
|
|
106
|
|
|
assert isinstance(hyper.best_para(objective_function), dict) |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
def test_attributes_best_para_objective_function_1(): |
110
|
|
|
"""Test best parameters with multiple objective functions.""" |
111
|
|
|
hyper = Hyperactive() |
112
|
|
|
hyper.add_search( |
113
|
|
|
objective_function, |
114
|
|
|
search_space, |
115
|
|
|
n_iter=15, |
116
|
|
|
) |
117
|
|
|
hyper.add_search( |
118
|
|
|
objective_function1, |
119
|
|
|
search_space, |
120
|
|
|
n_iter=15, |
121
|
|
|
) |
122
|
|
|
hyper.run() |
123
|
|
|
|
124
|
|
|
assert isinstance(hyper.best_para(objective_function), dict) |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
""" |
128
|
|
|
def test_attributes_best_para_search_id_0(): |
129
|
|
|
# Test best parameters with search ID. |
130
|
|
|
hyper = Hyperactive() |
131
|
|
|
hyper.add_search( |
132
|
|
|
objective_function, |
133
|
|
|
search_space, |
134
|
|
|
search_id="1", |
135
|
|
|
n_iter=15, |
136
|
|
|
) |
137
|
|
|
hyper.run() |
138
|
|
|
|
139
|
|
|
assert isinstance(hyper.best_para("1"), dict) |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
def test_attributes_best_para_search_id_1(): |
143
|
|
|
# Test best parameters with multiple search IDs. |
144
|
|
|
hyper = Hyperactive() |
145
|
|
|
hyper.add_search( |
146
|
|
|
objective_function, |
147
|
|
|
search_space, |
148
|
|
|
search_id="1", |
149
|
|
|
n_iter=15, |
150
|
|
|
) |
151
|
|
|
hyper.add_search( |
152
|
|
|
objective_function1, |
153
|
|
|
search_space, |
154
|
|
|
search_id="2", |
155
|
|
|
n_iter=15, |
156
|
|
|
) |
157
|
|
|
hyper.run() |
158
|
|
|
|
159
|
|
|
assert isinstance(hyper.best_para("1"), dict) |
160
|
|
|
""" |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
def test_attributes_results_objective_function_0(): |
164
|
|
|
"""Test search results returns DataFrame.""" |
165
|
|
|
hyper = Hyperactive() |
166
|
|
|
hyper.add_search( |
167
|
|
|
objective_function, |
168
|
|
|
search_space, |
169
|
|
|
n_iter=15, |
170
|
|
|
) |
171
|
|
|
hyper.run() |
172
|
|
|
|
173
|
|
|
assert isinstance(hyper.search_data(objective_function), pd.DataFrame) |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
def test_attributes_results_objective_function_1(): |
177
|
|
|
"""Test search results with multiple objective functions.""" |
178
|
|
|
hyper = Hyperactive() |
179
|
|
|
hyper.add_search( |
180
|
|
|
objective_function, |
181
|
|
|
search_space, |
182
|
|
|
n_iter=15, |
183
|
|
|
) |
184
|
|
|
hyper.add_search( |
185
|
|
|
objective_function1, |
186
|
|
|
search_space, |
187
|
|
|
n_iter=15, |
188
|
|
|
) |
189
|
|
|
hyper.run() |
190
|
|
|
|
191
|
|
|
assert isinstance(hyper.search_data(objective_function), pd.DataFrame) |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
""" |
195
|
|
|
def test_attributes_results_search_id_0(): |
196
|
|
|
# Test search results with search ID. |
197
|
|
|
hyper = Hyperactive() |
198
|
|
|
hyper.add_search( |
199
|
|
|
objective_function, |
200
|
|
|
search_space, |
201
|
|
|
search_id="1", |
202
|
|
|
n_iter=15, |
203
|
|
|
) |
204
|
|
|
hyper.run() |
205
|
|
|
|
206
|
|
|
assert isinstance(hyper.search_data("1"), pd.DataFrame) |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
def test_attributes_results_search_id_1(): |
210
|
|
|
# Test search results with multiple search IDs. |
211
|
|
|
hyper = Hyperactive() |
212
|
|
|
hyper.add_search( |
213
|
|
|
objective_function, |
214
|
|
|
search_space, |
215
|
|
|
search_id="1", |
216
|
|
|
n_iter=15, |
217
|
|
|
) |
218
|
|
|
hyper.add_search( |
219
|
|
|
objective_function1, |
220
|
|
|
search_space, |
221
|
|
|
search_id="2", |
222
|
|
|
n_iter=15, |
223
|
|
|
) |
224
|
|
|
hyper.run() |
225
|
|
|
|
226
|
|
|
assert isinstance(hyper.search_data("1"), pd.DataFrame) |
227
|
|
|
""" |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
def test_attributes_result_errors_0(): |
231
|
|
|
"""Test error handling with no search runs.""" |
232
|
|
|
with pytest.raises(ValueError): |
233
|
|
|
hyper = Hyperactive() |
234
|
|
|
hyper.add_search(objective_function, search_space, n_iter=15) |
235
|
|
|
hyper.run() |
236
|
|
|
|
237
|
|
|
hyper.best_para(objective_function1) |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
def test_attributes_result_errors_1(): |
241
|
|
|
"""Test error handling with unknown objective function.""" |
242
|
|
|
with pytest.raises(ValueError): |
243
|
|
|
hyper = Hyperactive() |
244
|
|
|
hyper.add_search(objective_function, search_space, n_iter=15) |
245
|
|
|
hyper.run() |
246
|
|
|
|
247
|
|
|
hyper.best_score(objective_function1) |
248
|
|
|
|
249
|
|
|
|
250
|
|
|
def test_attributes_result_errors_2(): |
251
|
|
|
"""Test error handling with unknown search ID.""" |
252
|
|
|
with pytest.raises(ValueError): |
253
|
|
|
hyper = Hyperactive() |
254
|
|
|
hyper.add_search(objective_function, search_space, n_iter=15) |
255
|
|
|
hyper.run() |
256
|
|
|
|
257
|
|
|
hyper.search_data(objective_function1) |
258
|
|
|
|