Skip to content

Commit c02b9a5

Browse files
committed
update tests for new logging system
1 parent 79cf21e commit c02b9a5

File tree

8 files changed

+23
-308
lines changed

8 files changed

+23
-308
lines changed

tests/test_bayesian_optimization.py

Lines changed: 6 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from bayes_opt import BayesianOptimization, acquisition
1111
from bayes_opt.acquisition import AcquisitionFunction
1212
from bayes_opt.domain_reduction import SequentialDomainReductionTransformer
13-
from bayes_opt.event import DEFAULT_EVENTS, Events
1413
from bayes_opt.exception import NotUniqueError
1514
from bayes_opt.logger import ScreenLogger
1615
from bayes_opt.parameter import BayesParameter
@@ -161,38 +160,6 @@ def test_prime_queue_with_register_and_init():
161160
assert len(optimizer.space) == 1
162161

163162

164-
def test_prime_subscriptions():
165-
optimizer = BayesianOptimization(target_func, PBOUNDS, random_state=1)
166-
optimizer._prime_subscriptions()
167-
168-
# Test that the default observer is correctly subscribed
169-
for event in DEFAULT_EVENTS:
170-
assert all([isinstance(k, ScreenLogger) for k in optimizer._events[event]])
171-
assert all([hasattr(k, "update") for k in optimizer._events[event]])
172-
173-
test_subscriber = "test_subscriber"
174-
175-
def test_callback(event, instance):
176-
pass
177-
178-
optimizer = BayesianOptimization(target_func, PBOUNDS, random_state=1)
179-
optimizer.subscribe(event=Events.OPTIMIZATION_START, subscriber=test_subscriber, callback=test_callback)
180-
# Test that the desired observer is subscribed
181-
assert all([k == test_subscriber for k in optimizer._events[Events.OPTIMIZATION_START]])
182-
assert all([v == test_callback for v in optimizer._events[Events.OPTIMIZATION_START].values()])
183-
184-
# Check that prime subscriptions won't overwrite manual subscriptions
185-
optimizer._prime_subscriptions()
186-
assert all([k == test_subscriber for k in optimizer._events[Events.OPTIMIZATION_START]])
187-
assert all([v == test_callback for v in optimizer._events[Events.OPTIMIZATION_START].values()])
188-
189-
assert optimizer._events[Events.OPTIMIZATION_STEP] == {}
190-
assert optimizer._events[Events.OPTIMIZATION_END] == {}
191-
192-
with pytest.raises(KeyError):
193-
optimizer._events["other"]
194-
195-
196163
def test_set_bounds():
197164
pbounds = {"p1": (0, 1), "p3": (0, 3), "p2": (0, 2), "p4": (0, 4)}
198165
optimizer = BayesianOptimization(target_func, pbounds, random_state=1)
@@ -223,56 +190,27 @@ def test_set_gp_params():
223190

224191

225192
def test_maximize():
226-
class Tracker:
227-
def __init__(self):
228-
self.start_count = 0
229-
self.step_count = 0
230-
self.end_count = 0
231-
232-
def update_start(self, event, instance):
233-
self.start_count += 1
234-
235-
def update_step(self, event, instance):
236-
self.step_count += 1
237-
238-
def update_end(self, event, instance):
239-
self.end_count += 1
240-
241-
def reset(self):
242-
self.__init__()
243-
244193
acq = acquisition.UpperConfidenceBound()
245194
optimizer = BayesianOptimization(
246195
target_func, PBOUNDS, acq, random_state=np.random.RandomState(1), allow_duplicate_points=True
247196
)
248197

249-
tracker = Tracker()
250-
optimizer.subscribe(event=Events.OPTIMIZATION_START, subscriber=tracker, callback=tracker.update_start)
251-
optimizer.subscribe(event=Events.OPTIMIZATION_STEP, subscriber=tracker, callback=tracker.update_step)
252-
optimizer.subscribe(event=Events.OPTIMIZATION_END, subscriber=tracker, callback=tracker.update_end)
253-
198+
# Test initial maximize with no init_points and n_iter
254199
optimizer.maximize(init_points=0, n_iter=0)
255200
assert not optimizer._queue
256-
assert len(optimizer.space) == 1
257-
assert tracker.start_count == 1
258-
assert tracker.step_count == 1
259-
assert tracker.end_count == 1
201+
assert len(optimizer.space) == 1 # Even with no init_points, we should have at least one point
260202

203+
# Test after setting GP parameters
261204
optimizer.set_gp_params(alpha=1e-2)
262205
optimizer.maximize(init_points=2, n_iter=0)
263206
assert not optimizer._queue
264-
assert len(optimizer.space) == 3
207+
assert len(optimizer.space) == 3 # Previously had 1, add 2 more from init_points
265208
assert optimizer._gp.alpha == 1e-2
266-
assert tracker.start_count == 2
267-
assert tracker.step_count == 3
268-
assert tracker.end_count == 2
269209

210+
# Test with additional iterations
270211
optimizer.maximize(init_points=0, n_iter=2)
271212
assert not optimizer._queue
272-
assert len(optimizer.space) == 5
273-
assert tracker.start_count == 3
274-
assert tracker.step_count == 5
275-
assert tracker.end_count == 3
213+
assert len(optimizer.space) == 5 # Previously had 3, add 2 more from n_iter
276214

277215

278216
def test_define_wrong_transformer():

tests/test_logs.log

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

tests/test_logs_bounds.log

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

tests/test_logs_constrained.log

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

tests/test_logs_duplicates.log

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

tests/test_logs_multiple_constraints.log

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

tests/test_observer.py

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

tests/test_util.py

Lines changed: 17 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3,98 +3,26 @@
33
from pathlib import Path
44

55
import numpy as np
6-
import pytest
7-
from scipy.optimize import NonlinearConstraint
6+
from bayes_opt.util import ensure_rng
87

9-
from bayes_opt import BayesianOptimization
10-
from bayes_opt.util import load_logs
118

12-
test_dir = Path(__file__).parent.resolve()
9+
def test_ensure_rng():
10+
"""Test that ensure_rng properly handles different inputs."""
1311

12+
# Test with None (should return a new RandomState)
13+
rng1 = ensure_rng(None)
14+
assert isinstance(rng1, np.random.RandomState)
1415

15-
def test_logs():
16-
def f(x, y):
17-
return -(x**2) - (y - 1) ** 2 + 1
16+
# Test with int (should return a new RandomState seeded with that int)
17+
rng2 = ensure_rng(123)
18+
assert isinstance(rng2, np.random.RandomState)
1819

19-
optimizer = BayesianOptimization(f=f, pbounds={"x": (-200, 200), "y": (-200, 200)})
20-
assert len(optimizer.space) == 0
20+
# Test with RandomState (should return the same RandomState)
21+
rng3 = np.random.RandomState(456)
22+
rng4 = ensure_rng(rng3)
23+
assert rng3 is rng4
2124

22-
load_logs(optimizer, [str(test_dir / "test_logs.log")])
23-
assert len(optimizer.space) == 5
24-
25-
load_logs(optimizer, [str(test_dir / "test_logs.log")])
26-
assert len(optimizer.space) == 5
27-
28-
other_optimizer = BayesianOptimization(f=lambda x: -(x**2), pbounds={"x": (-2, 2)})
29-
with pytest.raises(ValueError):
30-
load_logs(other_optimizer, [str(test_dir / "test_logs.log")])
31-
32-
33-
def test_logs_str():
34-
def f(x, y):
35-
return -(x**2) - (y - 1) ** 2 + 1
36-
37-
optimizer = BayesianOptimization(f=f, pbounds={"x": (-200, 200), "y": (-200, 200)})
38-
assert len(optimizer.space) == 0
39-
40-
load_logs(optimizer, str(test_dir / "test_logs.log"))
41-
assert len(optimizer.space) == 5
42-
43-
44-
def test_logs_bounds():
45-
def f(x, y):
46-
return x + y
47-
48-
optimizer = BayesianOptimization(f=f, pbounds={"x": (-2, 2), "y": (-2, 2)})
49-
50-
with pytest.warns(UserWarning):
51-
load_logs(optimizer, [str(test_dir / "test_logs_bounds.log")])
52-
53-
assert len(optimizer.space) == 5
54-
55-
56-
def test_logs_constraint():
57-
def f(x, y):
58-
return -(x**2) - (y - 1) ** 2 + 1
59-
60-
def c(x, y):
61-
return x**2 + y**2
62-
63-
constraint = NonlinearConstraint(c, -np.inf, 3)
64-
65-
optimizer = BayesianOptimization(f=f, pbounds={"x": (-200, 200), "y": (-200, 200)}, constraint=constraint)
66-
67-
with pytest.raises(KeyError):
68-
load_logs(optimizer, [str(test_dir / "test_logs.log")])
69-
70-
load_logs(optimizer, [str(test_dir / "test_logs_constrained.log")])
71-
72-
assert len(optimizer.space) == 7
73-
74-
75-
def test_logs_constraint_new_array():
76-
def f(x, y):
77-
return -(x**2) - (y - 1) ** 2 + 1
78-
79-
def c(x, y):
80-
return np.array(
81-
[-np.cos(x) * np.cos(y) + np.sin(x) * np.sin(y), -np.cos(x) * np.cos(-y) + np.sin(x) * np.sin(-y)]
82-
)
83-
84-
constraint_lower = np.array([-np.inf, -np.inf])
85-
constraint_upper = np.array([0.6, 0.6])
86-
87-
constraint = NonlinearConstraint(c, constraint_lower, constraint_upper)
88-
89-
optimizer = BayesianOptimization(f=f, pbounds={"x": (-200, 200), "y": (-200, 200)}, constraint=constraint)
90-
91-
with pytest.raises(KeyError):
92-
load_logs(optimizer, [str(test_dir / "test_logs.log")])
93-
94-
with pytest.raises(ValueError):
95-
load_logs(optimizer, [str(test_dir / "test_logs_constrained.log")])
96-
97-
load_logs(optimizer, [str(test_dir / "test_logs_multiple_constraints.log")])
98-
99-
print(optimizer.space)
100-
assert len(optimizer.space) == 12
25+
# Test that different seeds produce different random numbers
26+
rng5 = ensure_rng(1)
27+
rng6 = ensure_rng(2)
28+
assert rng5.random() != rng6.random()

0 commit comments

Comments
 (0)