Skip to content

Commit 928fe65

Browse files
committed
Added basic tests
1 parent 84839bd commit 928fe65

10 files changed

+933
-284
lines changed

tests/1d_test_RF.py

Lines changed: 0 additions & 210 deletions
This file was deleted.

tests/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# edbo
22

3-
Experimental Design via Bayesian Optimization
4-
53
## Testing
64

75
This directory contains a number of tests of the basic functionality of *edbo*. Some of the functions test *edbo*'s plotting functions so you will need to close the plot windows as they pop up for tests to proceed.

tests/acquisition_functions_gp.py

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
############################################################################## Setup
2+
"""
3+
Acquisition Functions Test Parameters:
4+
(0) Gaussian Process model.
5+
(1) 1D objective.
6+
(2) Initialize with random data.
7+
(3) Test predictions, variance estimation, and sampling.
8+
(4) Run single iteration of each acquisition function.
9+
(5) Test gpytroch fast computation features.
10+
"""
11+
12+
# Imports
13+
14+
import numpy as np
15+
import pandas as pd
16+
from gpytorch.priors import GammaPrior
17+
from edbo.bro import BO
18+
from edbo.pd_utils import to_torch
19+
import random
20+
21+
############################################################################## Test Functions
22+
23+
# Objective
24+
25+
def f(x):
26+
"""Noise free objective."""
27+
28+
return np.sin(10 * x) * x * 100
29+
30+
# Test a precomputed objective
31+
32+
def BO_pred(acq_func, plot=False, return_='pred', append=False, init='external', fast_comp=True):
33+
34+
# Experiment index
35+
X = np.linspace(0,1,1000)
36+
exindex = pd.DataFrame([[x, f(x)] for x in X], columns=['x', 'f(x)'])
37+
training_points = [50, 300, 500, 900]
38+
39+
# Instatiate BO class
40+
bo = BO(exindex=exindex,
41+
domain=exindex.drop('f(x)', axis=1),
42+
results=exindex.iloc[training_points],
43+
acquisition_function=acq_func,
44+
init_method=init,
45+
lengthscale_prior=[GammaPrior(1.2,1.1), 0.2],
46+
noise_prior=None,
47+
batch_size=random.sample([1,2,3,4,5,6,7,8,9,10],1)[0],
48+
fast_comp=fast_comp)
49+
50+
bo.run(append=append)
51+
52+
# Check prediction
53+
if return_ == 'pred':
54+
55+
try:
56+
bo.model.predict(to_torch(bo.obj.domain)) # torch.tensor
57+
bo.model.predict(bo.obj.domain.values) # numpy.array
58+
bo.model.predict(list(bo.obj.domain.values)) # list
59+
bo.model.predict(exindex.drop('f(x)', axis=1)) # pandas.DataFrame
60+
except:
61+
return False
62+
63+
pred = bo.model.predict(bo.obj.domain.iloc[[32]])
64+
pred = bo.obj.scaler.unstandardize(pred)
65+
return (pred[0] - 1.33) < 0.1
66+
67+
# Check predictive postrior variance
68+
elif return_ == 'var':
69+
70+
try:
71+
bo.model.predict(to_torch(bo.obj.domain)) # torch.tensor
72+
bo.model.predict(bo.obj.domain.values) # numpy.array
73+
bo.model.predict(list(bo.obj.domain.values)) # list
74+
bo.model.predict(exindex.drop('f(x)', axis=1)) # pandas.DataFrame
75+
except:
76+
return False
77+
78+
var = bo.model.variance(bo.obj.domain.iloc[[32]])
79+
return (var[0] - 0.04) < 0.1
80+
81+
# Make sure sampling works with tensors, arrays, lists, and DataFrames
82+
elif return_ == 'sample':
83+
try:
84+
bo.model.sample_posterior(to_torch(bo.obj.domain)) # torch.tensor
85+
bo.model.sample_posterior(bo.obj.domain.values) # numpy.array
86+
bo.model.sample_posterior(list(bo.obj.domain.values)) # list
87+
bo.model.sample_posterior(exindex.drop('f(x)', axis=1)) # pandas.DataFrame
88+
return True
89+
except:
90+
return False
91+
92+
elif return_ == 'simulate':
93+
94+
if init != 'external':
95+
bo.init_seq.batch_size = random.sample([2,3,4,5,6,7,8,9,10],1)[0]
96+
97+
bo.simulate(iterations=5)
98+
bo.plot_convergence()
99+
bo.model.regression()
100+
101+
return True
102+
103+
elif return_ == 'none':
104+
return True
105+
106+
############################################################################## Tests
107+
108+
# Test predicted mean, variance, and sampling
109+
110+
def test_BO_pred_mean():
111+
assert BO_pred('TS', return_='pred')
112+
113+
def test_BO_pred_var():
114+
assert BO_pred('TS', return_='var')
115+
116+
def test_BO_sample():
117+
assert BO_pred('TS', return_='sample')
118+
119+
# Test different acquisition functions
120+
121+
def test_BO_EI():
122+
assert BO_pred('EI', return_='none')
123+
124+
def test_BO_PI():
125+
assert BO_pred('PI', return_='none')
126+
127+
def test_BO_UCB():
128+
assert BO_pred('UCB', return_='none')
129+
130+
def test_BO_rand():
131+
assert BO_pred('rand', return_='none')
132+
133+
def test_BO_MeanMax():
134+
assert BO_pred('MeanMax', return_='none')
135+
136+
def test_BO_VarMax():
137+
assert BO_pred('VarMax', return_='none')
138+
139+
def test_BO_EI_TS():
140+
assert BO_pred('EI-TS', return_='none')
141+
142+
def test_BO_PI_TS():
143+
assert BO_pred('PI-TS', return_='none')
144+
145+
def test_BO_UCB_TS():
146+
assert BO_pred('UCB-TS', return_='none')
147+
148+
def test_BO_rand_TS():
149+
assert BO_pred('rand-TS', return_='none')
150+
151+
def test_BO_MeanMax_TS():
152+
assert BO_pred('MeanMax-TS', return_='none')
153+
154+
def test_BO_VarMax_TS():
155+
assert BO_pred('VarMax-TS', return_='none')
156+
157+
def test_BO_eps_greedy():
158+
assert BO_pred('eps-greedy', return_='none')
159+
160+
# Turn fast computation off
161+
162+
def test_fast_comp_off():
163+
assert BO_pred('TS', fast_comp=False)

0 commit comments

Comments
 (0)