Skip to content

Commit d5126dc

Browse files
authored
Remove stale ndarray sorting warning (#603)
1 parent 6868d3a commit d5126dc

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

bayes_opt/bayesian_optimization.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from os import PathLike
1313
from pathlib import Path
1414
from typing import TYPE_CHECKING, Any
15-
from warnings import warn
1615

1716
import numpy as np
1817
from scipy.optimize import NonlinearConstraint
@@ -138,8 +137,6 @@ def __init__(
138137
raise TypeError(msg)
139138
self._bounds_transformer.initialize(self._space)
140139

141-
self._sorting_warning_already_shown = False # TODO: remove in future version
142-
143140
# Initialize logger
144141
self.logger = ScreenLogger(verbose=self._verbose, is_constrained=self.is_constrained)
145142

@@ -278,17 +275,6 @@ def register(
278275
constraint_value: float or None
279276
Value of the constraint function at the observation, if any.
280277
"""
281-
# TODO: remove in future version
282-
if isinstance(params, np.ndarray) and not self._sorting_warning_already_shown:
283-
msg = (
284-
"You're attempting to register an np.ndarray. In previous versions, the optimizer internally"
285-
" sorted parameters by key and expected any registered array to respect this order."
286-
" In the current and any future version the order as given by the pbounds dictionary will be"
287-
" used. If you wish to retain sorted parameters, please manually sort your pbounds"
288-
" dictionary before constructing the optimizer."
289-
)
290-
warn(msg, stacklevel=1)
291-
self._sorting_warning_already_shown = True
292278
self._space.register(params, target, constraint_value)
293279
self.logger.log_optimization_step(
294280
self._space.keys, self._space.res()[-1], self._space.params_config, self.max
@@ -308,18 +294,6 @@ def probe(self, params: ParamsType, lazy: bool = True) -> None:
308294
If True, the optimizer will evaluate the points when calling
309295
maximize(). Otherwise it will evaluate it at the moment.
310296
"""
311-
# TODO: remove in future version
312-
if isinstance(params, np.ndarray) and not self._sorting_warning_already_shown:
313-
msg = (
314-
"You're attempting to register an np.ndarray. In previous versions, the optimizer internally"
315-
" sorted parameters by key and expected any registered array to respect this order."
316-
" In the current and any future version the order as given by the pbounds dictionary will be"
317-
" used. If you wish to retain sorted parameters, please manually sort your pbounds"
318-
" dictionary before constructing the optimizer."
319-
)
320-
warn(msg, stacklevel=1)
321-
self._sorting_warning_already_shown = True
322-
params = self._space.array_to_params(params)
323297
if lazy:
324298
self._queue.append(params)
325299
else:

tests/test_bayesian_optimization.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import pickle
4+
import warnings
45
from pathlib import Path
56

67
import numpy as np
@@ -97,6 +98,28 @@ def test_register():
9798
optimizer.register(params={"p1": 5, "p2": 4}, target=9)
9899

99100

101+
def test_register_array_uses_pbounds_order_without_warning():
102+
optimizer = BayesianOptimization(target_func, {"p1": (0, 10), "p2": (0, 10)}, random_state=1)
103+
104+
with warnings.catch_warnings(record=True) as caught:
105+
warnings.simplefilter("always")
106+
optimizer.register(params=np.array([1, 2]), target=3)
107+
108+
assert caught == []
109+
assert optimizer.space.array_to_params(optimizer.space.params[0]) == {"p1": 1.0, "p2": 2.0}
110+
111+
112+
def test_probe_array_uses_pbounds_order_without_warning():
113+
optimizer = BayesianOptimization(target_func, {"p1": (0, 10), "p2": (0, 10)}, random_state=1)
114+
115+
with warnings.catch_warnings(record=True) as caught:
116+
warnings.simplefilter("always")
117+
optimizer.probe(params=np.array([1, 2]), lazy=False)
118+
119+
assert caught == []
120+
assert optimizer.space.array_to_params(optimizer.space.params[0]) == {"p1": 1.0, "p2": 2.0}
121+
122+
100123
def test_probe_lazy():
101124
optimizer = BayesianOptimization(target_func, PBOUNDS, random_state=1)
102125

0 commit comments

Comments
 (0)