Skip to content

Commit

Permalink
Merge pull request #7 from jungtaekkim/cleanse-tests
Browse files Browse the repository at this point in the history
Cleanse tests
  • Loading branch information
jungtaekkim committed Jul 9, 2018
2 parents 9948ca5 + b7c8f00 commit 678db2f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
6 changes: 4 additions & 2 deletions bayeso/bo.py
@@ -1,6 +1,6 @@
# bo
# author: Jungtaek Kim (jtkim@postech.ac.kr)
# last updated: July 04, 2018
# last updated: July 09, 2018

import numpy as np
import time
Expand Down Expand Up @@ -34,6 +34,8 @@ def __init__(self, arr_range,
assert len(arr_range.shape) == 2
assert arr_range.shape[1] == 2
assert (arr_range[:, 0] <= arr_range[:, 1]).all()
assert str_cov in constants.ALLOWED_GP_COV
assert str_acq in constants.ALLOWED_BO_ACQ

self.arr_range = arr_range
self.num_dim = arr_range.shape[0]
Expand Down Expand Up @@ -115,7 +117,6 @@ def _optimize_objective(self, fun_acquisition, X_train, Y_train, X_test, cov_X_X
return acquisitions

def _optimize(self, fun_negative_acquisition, str_initial_method, int_samples):
assert str_initial_method in constants.ALLOWED_INITIALIZATIONS_OPTIMIZER
list_bounds = []
for elem in self.arr_range:
list_bounds.append(tuple(elem))
Expand Down Expand Up @@ -153,6 +154,7 @@ def optimize(self, X_train, Y_train,
assert X_train.shape[0] == Y_train.shape[0]
assert X_train.shape[1] == self.num_dim
assert int_samples > 0
assert str_initial_method in constants.ALLOWED_INITIALIZATIONS_AO

time_start = time.time()

Expand Down
6 changes: 4 additions & 2 deletions bayeso/constants.py
@@ -1,6 +1,6 @@
# constants
# author: Jungtaek Kim (jtkim@postech.ac.kr)
# last updated: June 23, 2018
# last updated: July 09, 2018

import numpy as np

Expand Down Expand Up @@ -31,8 +31,10 @@
TIME_PAUSE = 2.0
RANGE_SHADE = 1.96

ALLOWED_GP_COV = ['se', 'matern32', 'matern52']
ALLOWED_BO_ACQ = ['pi', 'ei', 'ucb']
ALLOWED_INITIALIZATIONS_BO = ['sobol', 'uniform', 'latin']
ALLOWED_INITIALIZATIONS_OPTIMIZER = ALLOWED_INITIALIZATIONS_BO + ['grid']
ALLOWED_INITIALIZATIONS_AO = ALLOWED_INITIALIZATIONS_BO + ['grid']

COLORS = [
'red',
Expand Down
3 changes: 2 additions & 1 deletion bayeso/covariance.py
@@ -1,6 +1,6 @@
# covariance
# author: Jungtaek Kim (jtkim@postech.ac.kr)
# last updated: June 23, 2018
# last updated: July 09, 2018

import numpy as np

Expand All @@ -26,6 +26,7 @@ def cov_main(str_cov, X, Xs, hyps, jitter=constants.JITTER_COV):
assert isinstance(jitter, float)
assert len(X.shape) == 2
assert len(Xs.shape) == 2
assert str_cov in constants.ALLOWED_GP_COV

num_X = X.shape[0]
num_d_X = X.shape[1]
Expand Down
4 changes: 4 additions & 0 deletions tests/test_bo.py
Expand Up @@ -44,8 +44,12 @@ def test_load_bo():
model_bo = bo.BO(arr_range_4)
with pytest.raises(AssertionError) as error:
model_bo = bo.BO(arr_range_1, str_cov=1)
with pytest.raises(AssertionError) as error:
model_bo = bo.BO(arr_range_1, str_cov='abc')
with pytest.raises(AssertionError) as error:
model_bo = bo.BO(arr_range_1, str_acq=1)
with pytest.raises(AssertionError) as error:
model_bo = bo.BO(arr_range_1, str_acq='abc')
with pytest.raises(AssertionError) as error:
model_bo = bo.BO(arr_range_1, is_ard=1)
with pytest.raises(AssertionError) as error:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_covariance.py
Expand Up @@ -49,7 +49,7 @@ def test_cov_main():
with pytest.raises(AssertionError) as error:
covariance.cov_main('se', np.zeros((10, 3)), np.zeros((20, 3)), cur_hyps, 1)

with pytest.raises(ValueError) as error:
with pytest.raises(AssertionError) as error:
covariance.cov_main('abc', np.zeros((10, 3)), np.zeros((20, 3)), cur_hyps, 0.001)

cur_hyps.pop('signal', None)
Expand Down
40 changes: 38 additions & 2 deletions tests/test_gp.py
@@ -1,6 +1,6 @@
# test_gp
# author: Jungtaek Kim (jtkim@postech.ac.kr)
# last updated: June 20, 2018
# last updated: July 09, 2018

import numpy as np
import pytest
Expand Down Expand Up @@ -43,7 +43,7 @@ def test_get_kernels():
gp.get_kernels(X, 1, 'se')
with pytest.raises(AssertionError) as error:
gp.get_kernels(X, hyps, 1)
with pytest.raises(ValueError) as error:
with pytest.raises(AssertionError) as error:
gp.get_kernels(X, hyps, 'abc')
with pytest.raises(AssertionError) as error:
gp.get_kernels(X, hyps, 'se', debug=1)
Expand All @@ -63,6 +63,42 @@ def test_get_kernels():
]
assert (cov_X_X - truth_cov_X_X < TEST_EPSILON).all()
assert (inv_cov_X_X - truth_inv_cov_X_X < TEST_EPSILON).all()
assert cov_X_X.shape == inv_cov_X_X.shape

def test_get_kernel_cholesky():
dim_X = 3
X = np.reshape(np.arange(0, 9), (3, dim_X))
hyps = utils_covariance.get_hyps('se', dim_X)

with pytest.raises(AssertionError) as error:
gp.get_kernel_cholesky(1, hyps, 'se')
with pytest.raises(AssertionError) as error:
gp.get_kernel_cholesky(np.arange(0, 10), hyps, 'se')
with pytest.raises(AssertionError) as error:
gp.get_kernel_cholesky(X, 1, 'se')
with pytest.raises(AssertionError) as error:
gp.get_kernel_cholesky(X, hyps, 1)
with pytest.raises(AssertionError) as error:
gp.get_kernel_cholesky(X, hyps, 'abc')
with pytest.raises(AssertionError) as error:
gp.get_kernel_cholesky(X, hyps, 'se', debug=1)

cov_X_X, lower = gp.get_kernel_cholesky(X, hyps, 'se')
print(cov_X_X)
print(lower)
truth_cov_X_X = [
[1.00011000e+00, 1.37095909e-06, 3.53262857e-24],
[1.37095909e-06, 1.00011000e+00, 1.37095909e-06],
[3.53262857e-24, 1.37095909e-06, 1.00011000e+00],
]
truth_lower = [
[1.00005500e+00, 0.00000000e+00, 0.00000000e+00],
[1.37088369e-06, 1.00005500e+00, 0.00000000e+00],
[3.53243429e-24, 1.37088369e-06, 1.00005500e+00],
]
assert (cov_X_X - truth_cov_X_X < TEST_EPSILON).all()
assert (lower - truth_lower < TEST_EPSILON).all()
assert cov_X_X.shape == lower.shape

def test_log_ml():
dim_X = 3
Expand Down

0 comments on commit 678db2f

Please sign in to comment.