Skip to content

Commit

Permalink
Merge pull request #4 from jungtaekkim/cleanse-tests
Browse files Browse the repository at this point in the history
Cleanse tests
  • Loading branch information
jungtaekkim committed Jul 5, 2018
2 parents 3b610fe + 206704f commit c5bf48f
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bayeso/bo.py
Expand Up @@ -137,7 +137,7 @@ def _optimize(self, fun_negative_acquisition, str_initial_method, int_samples):
return next_point.flatten(), next_points

def optimize(self, X_train, Y_train,
str_initial_method=constants.STR_OPTIMIZER_INITIALIZATION,
str_initial_method=constants.STR_AO_INITIALIZATION,
int_samples=constants.NUM_ACQ_SAMPLES,
is_normalized=True,
):
Expand Down
4 changes: 2 additions & 2 deletions bayeso/constants.py
Expand Up @@ -12,8 +12,8 @@
STR_OPTIMIZER_METHOD_GP = 'BFGS'
STR_GP_COV = 'se'
STR_BO_ACQ = 'ei'
STR_BO_INITIALIZATION = 'sobol'
STR_OPTIMIZER_INITIALIZATION = 'sobol'
STR_BO_INITIALIZATION = 'uniform'
STR_AO_INITIALIZATION = 'uniform'

NUM_BO_GRID = 50
NUM_BO_RANDOM = 1000
Expand Down
9 changes: 5 additions & 4 deletions bayeso/utils/utils_bo.py
Expand Up @@ -12,8 +12,9 @@
def get_grid(arr_ranges, int_grid):
assert isinstance(arr_ranges, np.ndarray)
assert isinstance(int_grid, int)
assert len(arr_ranges.shape)
assert len(arr_ranges.shape) == 2
assert arr_ranges.shape[1] == 2
assert (arr_ranges[:, 0] <= arr_ranges[:, 1]).all()

list_grid = []
for range_ in arr_ranges:
Expand Down Expand Up @@ -67,7 +68,7 @@ def get_next_best_acquisition(arr_points, arr_acquisitions, cur_points):
return next_point

def optimize_many_(model_bo, fun_target, X_train, Y_train, int_iter,
str_initial_method_ao=constants.STR_OPTIMIZER_INITIALIZATION,
str_initial_method_ao=constants.STR_AO_INITIALIZATION,
int_samples_ao=constants.NUM_ACQ_SAMPLES,
):
assert isinstance(model_bo, bo.BO)
Expand Down Expand Up @@ -113,7 +114,7 @@ def optimize_many_(model_bo, fun_target, X_train, Y_train, int_iter,
return X_final, Y_final

def optimize_many(model_bo, fun_target, X_train, int_iter,
str_initial_method_ao=constants.STR_OPTIMIZER_INITIALIZATION,
str_initial_method_ao=constants.STR_AO_INITIALIZATION,
int_samples_ao=constants.NUM_ACQ_SAMPLES,
):
assert isinstance(model_bo, bo.BO)
Expand Down Expand Up @@ -142,7 +143,7 @@ def optimize_many(model_bo, fun_target, X_train, int_iter,

def optimize_many_with_random_init(model_bo, fun_target, int_init, int_iter,
str_initial_method_bo=constants.STR_BO_INITIALIZATION,
str_initial_method_ao=constants.STR_OPTIMIZER_INITIALIZATION,
str_initial_method_ao=constants.STR_AO_INITIALIZATION,
int_samples_ao=constants.NUM_ACQ_SAMPLES,
int_seed=None,
):
Expand Down
2 changes: 2 additions & 0 deletions docs/index.rst
Expand Up @@ -10,6 +10,8 @@ Welcome to bayeso's documentation!

The code can be found in `our GitHub repository <https://github.com/jungtaekkim/bayeso>`_.

It is still under construction. We will update soon.

.. toctree::
:maxdepth: 2
:caption: Contents:
Expand Down
1 change: 0 additions & 1 deletion tests/test_benchmarks.py
Expand Up @@ -37,4 +37,3 @@ def test_branin():
val_fun = benchmarks.branin(X)
assert len(val_fun.shape) == 1
assert val_fun.shape[0] == X.shape[0]

8 changes: 8 additions & 0 deletions tests/test_bo.py
Expand Up @@ -124,11 +124,19 @@ def test_optimize():
model_bo.optimize(X, Y, str_initial_method='abc')
with pytest.raises(AssertionError) as error:
model_bo.optimize(X, Y, int_samples='abc')
with pytest.raises(AssertionError) as error:
model_bo.optimize(X, Y, is_normalized='abc')

next_point, next_points, acquisitions, cov_X_X, inv_cov_X_X, hyps = model_bo.optimize(X, Y)
assert isinstance(next_point, np.ndarray)
assert isinstance(next_points, np.ndarray)
assert isinstance(acquisitions, np.ndarray)
assert isinstance(cov_X_X, np.ndarray)
assert isinstance(inv_cov_X_X, np.ndarray)
assert isinstance(hyps, dict)
assert len(next_point.shape) == 1
assert len(next_points.shape) == 2
assert len(acquisitions.shape) == 1
assert next_point.shape[0] == dim_X
assert next_points.shape[1] == dim_X
assert next_points.shape[0] == acquisitions.shape[0]
14 changes: 14 additions & 0 deletions tests/test_gp.py
Expand Up @@ -45,6 +45,8 @@ def test_get_kernels():
gp.get_kernels(X, hyps, 1)
with pytest.raises(ValueError) as error:
gp.get_kernels(X, hyps, 'abc')
with pytest.raises(AssertionError) as error:
gp.get_kernels(X, hyps, 'se', debug=1)

cov_X_X, inv_cov_X_X = gp.get_kernels(X, hyps, 'se')
print(cov_X_X)
Expand Down Expand Up @@ -89,6 +91,10 @@ def test_log_ml():
gp.log_ml(X, np.expand_dims(np.arange(0, 4), axis=1), arr_hyps, str_cov, prior_mu_X)
with pytest.raises(AssertionError) as error:
gp.log_ml(X, Y, arr_hyps, str_cov, np.expand_dims(np.arange(0, 4), axis=1))
with pytest.raises(AssertionError) as error:
gp.log_ml(X, Y, arr_hyps, str_cov, prior_mu_X, is_fixed_noise=1)
with pytest.raises(AssertionError) as error:
gp.log_ml(X, Y, arr_hyps, str_cov, prior_mu_X, debug=1)

log_ml = gp.log_ml(X, Y, arr_hyps, str_cov, prior_mu_X)
print(log_ml)
Expand Down Expand Up @@ -123,6 +129,10 @@ def test_get_optimized_kernel():
gp.get_optimized_kernel(X, Y, prior_mu, 'abc')
with pytest.raises(AssertionError) as error:
gp.get_optimized_kernel(X, Y, prior_mu, 'se', str_optimizer_method=1)
with pytest.raises(AssertionError) as error:
gp.get_optimized_kernel(X, Y, prior_mu, 'se', is_fixed_noise=1)
with pytest.raises(AssertionError) as error:
gp.get_optimized_kernel(X, Y, prior_mu, 'se', debug=1)

def test_predict_test_():
np.random.seed(42)
Expand Down Expand Up @@ -222,3 +232,7 @@ def test_predict_optimized():
gp.predict_optimized(np.random.randn(10, dim_X), Y, X_test, str_cov='se', prior_mu=prior_mu)
with pytest.raises(AssertionError) as error:
gp.predict_optimized(X, np.random.randn(10, 1), X_test, str_cov='se', prior_mu=prior_mu)
with pytest.raises(AssertionError) as error:
gp.predict_optimized(X, Y, X_test, is_fixed_noise=1)
with pytest.raises(AssertionError) as error:
gp.predict_optimized(X, Y, X_test, debug=1)
45 changes: 45 additions & 0 deletions tests/test_utils_bo.py
Expand Up @@ -83,6 +83,17 @@ def test_get_grid():
[10., 2., 5.],
])

with pytest.raises(AssertionError) as error:
utils_bo.get_grid('abc', 3)
with pytest.raises(AssertionError) as error:
utils_bo.get_grid(arr_range_1, 'abc')
with pytest.raises(AssertionError) as error:
utils_bo.get_grid(np.arange(0, 10), 3)
with pytest.raises(AssertionError) as error:
utils_bo.get_grid(np.ones((3, 3)), 3)
with pytest.raises(AssertionError) as error:
utils_bo.get_grid(np.array([[0.0, -2.0], [10.0, 20.0]]), 3)

arr_grid_1 = utils_bo.get_grid(arr_range_1, 3)
arr_grid_2 = utils_bo.get_grid(arr_range_2, 3)

Expand All @@ -106,6 +117,40 @@ def test_get_best_acquisition():
assert best_initial.shape[1] == arr_initials.shape[1]
assert best_initial == np.array([[1]])

def test_get_next_best_acquisition():
arr_points = np.array([
[0.0, 1.0],
[1.0, -3.0],
[-2.0, -4.0],
[1.0, 3.0],
])
arr_acquisitions = np.array([1.1, 0.2, 0.5, 0.6])
cur_points = np.array([
[-10.0, 1.0],
[1.0, -3.0],
[11.0, 2.0],
])

with pytest.raises(AssertionError) as error:
utils_bo.get_next_best_acquisition(1, arr_acquisitions, cur_points)
with pytest.raises(AssertionError) as error:
utils_bo.get_next_best_acquisition(np.arange(0, 4), arr_acquisitions, cur_points)
with pytest.raises(AssertionError) as error:
utils_bo.get_next_best_acquisition(arr_points, 1, cur_points)
with pytest.raises(AssertionError) as error:
utils_bo.get_next_best_acquisition(arr_points, np.ones((4, 2)), cur_points)
with pytest.raises(AssertionError) as error:
utils_bo.get_next_best_acquisition(arr_points, arr_acquisitions, 1)
with pytest.raises(AssertionError) as error:
utils_bo.get_next_best_acquisition(arr_points, arr_acquisitions, np.arange(0, 3))
with pytest.raises(AssertionError) as error:
utils_bo.get_next_best_acquisition(arr_points, np.arange(0, 10), cur_points)
with pytest.raises(AssertionError) as error:
utils_bo.get_next_best_acquisition(arr_points, arr_acquisitions, np.ones((3, 5)))

next_point = utils_bo.get_next_best_acquisition(arr_points, arr_acquisitions, cur_points)
assert (next_point == np.array([-2.0, -4.0])).all()

def test_optimize_many_():
np.random.seed(42)
arr_range = np.array([
Expand Down
11 changes: 11 additions & 0 deletions tests/test_utils_covariance.py
Expand Up @@ -34,6 +34,8 @@ def test_convert_hyps():
utils_covariance.convert_hyps('se', 2.1)
with pytest.raises(AssertionError) as error:
utils_covariance.convert_hyps('abc', 2.1)
with pytest.raises(AssertionError) as error:
utils_covariance.convert_hyps('se', dict(), is_fixed_noise=1)

cur_hyps = {'noise': 0.1, 'signal': 1.0, 'lengthscales': np.array([1.0, 1.0])}
converted_hyps = utils_covariance.convert_hyps('se', cur_hyps)
Expand All @@ -52,9 +54,18 @@ def test_restore_hyps():
utils_covariance.restore_hyps('abc', 2.1)
with pytest.raises(AssertionError) as error:
utils_covariance.restore_hyps('se', np.array([[1.0, 1.0], [1.0, 1.0]]))
with pytest.raises(AssertionError) as error:
utils_covariance.restore_hyps('se', np.array([1.0, 1.0, 1.0]), is_fixed_noise=1)
with pytest.raises(AssertionError) as error:
utils_covariance.restore_hyps('se', np.array([1.0, 1.0, 1.0]), fixed_noise='abc')

cur_hyps = np.array([0.1, 1.0, 1.0, 1.0, 1.0])
restored_hyps = utils_covariance.restore_hyps('se', cur_hyps)
assert restored_hyps['noise'] == cur_hyps[0]
assert restored_hyps['signal'] == cur_hyps[1]
assert (restored_hyps['lengthscales'] == cur_hyps[2:]).all()

restored_hyps = utils_covariance.restore_hyps('se', cur_hyps, is_fixed_noise=True)
assert restored_hyps['noise'] == constants.GP_NOISE
assert restored_hyps['signal'] == cur_hyps[0]
assert (restored_hyps['lengthscales'] == cur_hyps[1:]).all()

0 comments on commit c5bf48f

Please sign in to comment.