Skip to content

Commit

Permalink
Update tests and improve pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
jungtaekkim committed May 20, 2024
1 parent 5e2ebc5 commit 2775c96
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 99 deletions.
11 changes: 6 additions & 5 deletions bayeso/bo/bo_w_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def get_trees(
assert isinstance(num_trees, int)
assert isinstance(depth_max, int)
assert isinstance(size_min_leaf, int)
assert len(X_train.shape) == 2
assert len(Y_train.shape) == 2
assert X_train.ndim == 2
assert Y_train.ndim == 2
assert X_train.shape[0] == Y_train.shape[0]
assert Y_train.shape[1] == 1

Expand Down Expand Up @@ -282,9 +282,10 @@ def optimize(
next_points, np.array(self._get_bounds())
)

fun_negative_acquisition = lambda X_test: -1.0 * self.compute_acquisitions(
X_test, X_train, Y_train, trees
)
def fun_negative_acquisition(X_test):
return -1.0 * self.compute_acquisitions(
X_test, X_train, Y_train, trees
)
acquisitions = fun_negative_acquisition(next_points)
ind_next_point = np.argmin(acquisitions)
next_point = next_points[ind_next_point]
Expand Down
2 changes: 2 additions & 0 deletions bayeso/gp/gp_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ def get_optimized_kernel(
elif str_cov in constants.ALLOWED_COV_SET:
num_dim = X_train.shape[2]
use_gradient = False
else:
raise ValueError

if str_modelselection_method == "ml":
def neg_log_ml_(hyps):
Expand Down
2 changes: 2 additions & 0 deletions bayeso/tp/tp_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def get_optimized_kernel(
elif str_cov in constants.ALLOWED_COV_SET:
num_dim = X_train.shape[2]
use_gradient = False
else:
raise ValueError

def neg_log_ml_(hyps):
return tp_likelihood.neg_log_ml(
Expand Down
23 changes: 14 additions & 9 deletions bayeso/utils/utils_bo.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,22 @@ def choose_fun_acquisition(
elif str_acq == "aei":
assert noise is not None

fun_acquisition = lambda pred_mean, pred_std, Y_train: acquisition.aei(
pred_mean, pred_std, Y_train, noise
)
def fun_acquisition(pred_mean, pred_std, Y_train):
return acquisition.aei(
pred_mean, pred_std, Y_train, noise
)
elif str_acq == "pure_exploit":
fun_acquisition = lambda pred_mean, pred_std, Y_train: acquisition.pure_exploit(
pred_mean
)
def fun_acquisition(pred_mean, pred_std, Y_train):
_, _ = pred_std, Y_train
return acquisition.pure_exploit(
pred_mean
)
elif str_acq == "pure_explore":
fun_acquisition = lambda pred_mean, pred_std, Y_train: acquisition.pure_explore(
pred_std
)
def fun_acquisition(pred_mean, pred_std, Y_train):
_, _ = pred_mean, Y_train
return acquisition.pure_explore(
pred_std
)
else:
raise NotImplementedError(
"_choose_fun_acquisition: allowed str_acq,\
Expand Down
58 changes: 28 additions & 30 deletions tests/common/test_bo_bo_w_tp.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ def test_get_samples():
)
assert (np.abs(arr_initials - truth_arr_initials) < TEST_EPSILON).all()

arr_initials_ = model_bo.get_samples("sobol", num_samples=3)
arr_initials = model_bo.get_samples("sobol", num_samples=3, seed=42)
arr_initials_ = model_bo.get_samples("sobol", num_samples=4)
arr_initials = model_bo.get_samples("sobol", num_samples=4, seed=42)

print("sobol")
for elem_1 in arr_initials:
Expand All @@ -177,14 +177,19 @@ def test_get_samples():
3.06412766687572,
],
[
8.698135614395142,
-0.250022292137146,
-0.012653172016143799,
8.69813535362482,
-0.2500223182141781,
-0.012653125450015068,
],
[
5.779154300689697,
0.04064440727233887,
2.2647011280059814,
5.779154505580664,
0.04064444452524185,
2.2647008765488863,
],
[
1.3686652854084969,
-1.0451578684151173,
-4.681709306314588,
],
]
)
Expand Down Expand Up @@ -291,8 +296,8 @@ def test_get_initials():
with pytest.raises(AssertionError) as error:
model_bo.get_initials("abc", 10)

arr_initials = model_bo.get_initials("sobol", 3)
arr_initials = model_bo.get_initials("sobol", 3, seed=42)
arr_initials = model_bo.get_initials("sobol", 4)
arr_initials = model_bo.get_initials("sobol", 4, seed=42)

print("sobol")
for elem_1 in arr_initials:
Expand All @@ -307,14 +312,19 @@ def test_get_initials():
3.06412766687572,
],
[
8.698135614395142,
-0.250022292137146,
-0.012653172016143799,
8.69813535362482,
-0.2500223182141781,
-0.012653125450015068,
],
[
5.779154505580664,
0.04064444452524185,
2.2647008765488863,
],
[
5.779154300689697,
0.04064440727233887,
2.2647011280059814,
1.3686652854084969,
-1.0451578684151173,
-4.681709306314588,
],
]
)
Expand Down Expand Up @@ -761,7 +771,7 @@ def test_compute_posteriors():

cov_X_X, inv_cov_X_X, _ = covariance.get_kernel_inverse(X, hyps, model_bo.str_cov)

X_test = model_bo.get_samples("sobol", num_samples=10, seed=111)
X_test = model_bo.get_samples("sobol", num_samples=16, seed=111)

with pytest.raises(AssertionError) as error:
model_bo.compute_posteriors(1, Y, X_test, cov_X_X, inv_cov_X_X, hyps)
Expand Down Expand Up @@ -875,7 +885,7 @@ def test_compute_acquisitions():

cov_X_X, inv_cov_X_X, _ = covariance.get_kernel_inverse(X, hyps, model_bo.str_cov)

X_test = model_bo.get_samples("sobol", num_samples=10, seed=111)
X_test = model_bo.get_samples("sobol", num_samples=8, seed=111)

truth_X_test = np.array(
[
Expand Down Expand Up @@ -919,16 +929,6 @@ def test_compute_acquisitions():
0.5484802722930908,
-0.9542649984359741,
],
[
4.554623663425446,
-1.7126559913158417,
1.575535535812378,
],
[
6.998107433319092,
1.1039907932281494,
-0.24655908346176147,
],
]
)

Expand Down Expand Up @@ -969,8 +969,6 @@ def test_compute_acquisitions():
0.634886228604709,
0.6350134713825293,
0.6263764526122838,
0.6444373090088434,
0.6348254001303183,
]
)

Expand Down
Loading

0 comments on commit 2775c96

Please sign in to comment.