-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconftest.py
351 lines (281 loc) · 11 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from botorch.models import SingleTaskGP
import numpy as np
import pytest
import random
import torch
from gpytorch.mlls import ExactMarginalLogLikelihood
from greattunes._initializers import Initializers
from greattunes._validators import Validators
### Parsing of keywords: allow for specialized tests for different python versions
def pytest_addoption(parser):
parser.addoption("--pythontestvers", action="store", default="3.8")
@pytest.fixture(autouse=True)
def pythontestvers(request):
return request.config.option.pythontestvers
### Fixing state of random number generators for test reproducibility
@pytest.fixture(autouse=True)
def rng_state_tests():
torch.manual_seed(0)
np.random.seed(0)
random.seed(0)
### Simple training data
@pytest.fixture(scope="class")
def custom_models_simple_training_data():
"""
defines very simple dataset for training of custom GP models. Defined in torch.tensor format
:return: train_X (torch.tensor)
:return: train_Y (torch.tensor)
"""
train_X = torch.tensor([[-1.0]], dtype=torch.double)
train_Y = torch.tensor([[0.2]], dtype=torch.double)
return train_X, train_Y
@pytest.fixture(scope="class")
def custom_models_simple_training_data_4elements():
"""
defines very simple dataset for training of custom GP models. Defined in torch.tensor format
:return: train_X (torch.tensor)
:return: train_Y (torch.tensor)
"""
train_X = torch.tensor([[-1.0], [-1.1], [-0.5], [1.0]], dtype=torch.double)
train_Y = torch.tensor([[0.2], [0.15], [0.5], [2.0]], dtype=torch.double)
return train_X, train_Y
@pytest.fixture(scope="class")
def custom_models_simple_training_data_4elements_covar_details():
"""
defines covar_details and covar_mapped_names that work with custom_models_simple_training_data_4elements
"""
covar_details = {"covar0": {"guess": 0.0, "min": -2.0, "max": 2.0, "type": float, "columns": 0}}
covar_mapped_names = ["covar0"]
GP_kernel_mapping_covar_identification = [{"type": float, "column": [0]}]
return covar_details, covar_mapped_names, GP_kernel_mapping_covar_identification
@pytest.fixture(scope="class")
def covars_for_custom_models_simple_training_data_4elements():
"""
defines initial covars compatible with custom_models_simple_training_data_4elements above
:return: covars (list of tuple)
"""
covars = [(0.0, -2.0, 2.0)]
return covars
@pytest.fixture(scope="class")
def covar_details_covars_for_custom_models_simple_training_data_4elements():
"""
covar_details corresponding to the covars in covars_for_custom_models_simple_training_data_4elements
"""
covar_details = {"covar0": {"guess": 0.0, "min": -2.0, "max": 2.0, "type": int, "columns": 0, "pandas_column": 0}}
covar_mapped_names = ["covar0"]
return covar_details, covar_mapped_names
@pytest.fixture(scope="class")
def covars_initialization_data():
"""
defines simple and more complex initial covariate datasets to test initialization method
(._initializers.Initializers__initialize_from_covars)
:return: covar_simple, covar_complex (lists of tuples of doubles)
"""
covar_simple = [(0.5, 0, 1)]
covar_complex = [(0.5, 0, 1), (12.5, 8, 15), (-2, -4, 1.1)]
return covar_simple, covar_complex
@pytest.fixture(scope="class")
def training_data_covar_complex(covars_initialization_data):
"""
defines simple training data that corresponds to covar_complex (covars_initialization_data[1]), where covar_complex
is the right format for initialization of the full user-facing class TuneSession
(greattunes.TuneSession)
"""
covars = covars_initialization_data[1]
# the covar training data: building it by taking the covars and in each row adding the factor from the y vector
train_X = torch.tensor([[x[0]+y for x in covars] for y in [0, -0.5, 1.2]], dtype=torch.double)
train_Y = torch.tensor([[1.1], [5.5], [0.1]], dtype=torch.double)
# the covars initialization data
covar_details = {}
covar_mapped_names = []
GP_kernel_mapping_covar_identification = []
for i in range(len(covars)):
name = "covar" + str(i)
covar_details["name"] = {"guess": covars[i][0], "min": covars[i][1], "max": covars[i][2], "type": float, "columns": i}
covar_mapped_names += [name]
GP_kernel_mapping_covar_identification += [{"type": float, "column": [i]}]
return covars, train_X, train_Y, covar_details, covar_mapped_names, GP_kernel_mapping_covar_identification
### Trained GP model
@pytest.fixture(scope="class")
def ref_model_and_training_data(custom_models_simple_training_data_4elements):
"""
defines a simple, univariate GP model and the data it is defined by
:return: train_X, train_Y (training data, from custom_models_simple_training_data_4elements above)
:return: model_obj (model object, SingleTaskGP)
:return: lh, ll (model likelihood and marginal log-likelihood)
"""
train_X = custom_models_simple_training_data_4elements[0]
train_Y = custom_models_simple_training_data_4elements[1]
# set up the model
model_obj = SingleTaskGP(train_X, train_Y)
# the likelihood
lh = model_obj.likelihood
# define the "loss" function
ll = ExactMarginalLogLikelihood(lh, model_obj)
return train_X, train_Y, model_obj, lh, ll
@pytest.fixture(scope="class")
def ref_model_and_multivariate_training_data(training_data_covar_complex):
"""
defines a multivariate GP model and the data it is defined by
:return: covars
:return: train_X, train_Y (training data, from custom_models_simple_training_data_4elements above)
:return: model_obj (model object, SingleTaskGP)
:return: lh, ll (model likelihood and marginal log-likelihood)
"""
covars = training_data_covar_complex[0]
train_X = training_data_covar_complex[1]
train_Y = training_data_covar_complex[2]
# set up the model
model_obj = SingleTaskGP(train_X, train_Y)
# the likelihood
lh = model_obj.likelihood
# define the "loss" function
ll = ExactMarginalLogLikelihood(lh, model_obj)
return covars, train_X, train_Y, model_obj, lh, ll
### initiated classes for testing
@pytest.fixture(scope="module")
def tmp_observe_class():
"""
temporary class to allow testing of methods from greattunes._observe
"""
# define class
class TmpClass(Validators):
def __init__(self):
self.sampling = {"method": None,
"response_func": None}
self.dtype = torch.double
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
from greattunes._observe import _get_and_verify_response_input, _get_response_function_input, \
_read_response_manual_input, _print_candidate_to_prompt, _read_covars_manual_input, \
_get_and_verify_covars_input, _get_covars_datapoint, _get_response_datapoint
cls = TmpClass()
return cls
@pytest.fixture(scope="module")
def tmp_modeling_class():
"""
temporary class to allow testing of methods from greattunes._modeling
"""
class TmpClass:
def __init__(self):
self.train_X = None
self.proposed_X = None
self.train_Y = None
self.x_data = None
self.y_data = None
self.model = {"model_type": None,
"likelihood": None,
"loglikelihood": None,
"response_sampled_iter": 0
}
# import method
from greattunes._modeling import _set_GP_model
# initialize class
cls = TmpClass()
return cls
@pytest.fixture(scope="function")
def tmp_best_response_class():
"""
temporary class to test methods stored in _best_response.py
"""
# test class
class TmpClass:
def __init__(self):
self.train_X = None
self.train_Y = None
self.proposed_X = None
self.covars_best_response_value = None
self.best_response_value = None
self.covars_best_response = None
self.best_response = None
# import methods
from greattunes._best_response import _find_max_response_value, _update_max_response_value, \
current_best, _update_proposed_data, _find_best_predicted, _evaluate_model, best_predicted
cls = TmpClass()
return cls
@pytest.fixture(scope="module")
def tmp_Initializers_with_find_max_response_value_class():
"""
test version of Initializers to endow it with the property from _find_max_response_value, which is
otherwise defined as a static method in ._best_response
"""
class TmpClass(Initializers):
from greattunes._best_response import _find_max_response_value
cls = TmpClass()
return cls
@pytest.fixture(scope="module")
def covar_details_covar_mapped_names():
"""
examples of matching 'covar_details' and 'covar_mapped_names' for a case of the following variables
- a: int
- b: float
- c: categorical (str), with options "red", "blue" and "green"
"""
covar_details = \
{
'a': {
'guess': 1,
'min': -1,
'max': 3,
'type': int,
'columns': 0,
},
'b': {
'guess': 2.2,
'min': -1.7,
'max': 4.2,
'type': float,
'columns': 1,
},
'c': {
'guess': 'red',
'options': {'red', 'green', 'blue'},
'type': str,
'columns': [2, 3, 4],
'opt_names': ['c_red', 'c_green', 'c_blue'],
}
}
covar_mapped_names = ['a', 'b', 'c_red', 'c_green', 'c_blue']
return covar_details, covar_mapped_names
@pytest.fixture(scope="module")
def covar_details_mapped_covar_mapped_names_tmp_observe_class():
"""
covar_details and covar_mapped_names corresponding to the sample problems being tested by tmp_observe_class in
tests/unit/test_observe_unit.py
"""
covar_details = {
"covar0": {
"guess": 0.1,
"min": -1.0,
"max": 2.0,
"type": float,
"columns": 0,
"pandas_column": 0,
},
"covar1": {
"guess": 2.5,
"min": -1.0,
"max": 3.0,
"type": float,
"columns": 1,
"pandas_column": 1,
},
"covar2": {
"guess": 12,
"min": 0,
"max": 250,
"type": float,
"columns": 2,
"pandas_column": 2,
},
"covar3": {
"guess": 0.22,
"min": -2.0,
"max": 1.0,
"type": float,
"columns": 3,
"pandas_column": 3,
},
}
covar_mapped_names = ["covar0", "covar1", "covar2", "covar3"]
sorted_pandas_columns = ["covar0", "covar1", "covar2", "covar3"]
return covar_details, covar_mapped_names, sorted_pandas_columns