Skip to content

Commit f4016e0

Browse files
authored
Fix array warning (#535)
1 parent d8ccc69 commit f4016e0

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

bayes_opt/bayesian_optimization.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
from collections import deque
1010
from typing import TYPE_CHECKING, Any
11+
from warnings import warn
1112

13+
import numpy as np
1214
from sklearn.gaussian_process import GaussianProcessRegressor
1315
from sklearn.gaussian_process.kernels import Matern
1416

@@ -22,7 +24,6 @@
2224
if TYPE_CHECKING:
2325
from collections.abc import Callable, Iterable, Mapping, Sequence
2426

25-
import numpy as np
2627
from numpy.random import RandomState
2728
from numpy.typing import NDArray
2829
from scipy.optimize import NonlinearConstraint
@@ -166,6 +167,7 @@ def __init__(
166167
error_msg = "The transformer must be an instance of DomainTransformer"
167168
raise TypeError(error_msg) from exc
168169

170+
self._sorting_warning_already_shown = False # TODO: remove in future version
169171
super().__init__(events=DEFAULT_EVENTS)
170172

171173
@property
@@ -220,6 +222,17 @@ def register(
220222
constraint_value: float or None
221223
Value of the constraint function at the observation, if any.
222224
"""
225+
# TODO: remove in future version
226+
if isinstance(params, np.ndarray) and not self._sorting_warning_already_shown:
227+
msg = (
228+
"You're attempting to register an np.ndarray. Currently, the optimizer internally sorts"
229+
" parameters by key and expects any registered array to respect this order. In future"
230+
" versions this behaviour will change and the order as given by the pbounds dictionary"
231+
" will be used. If you wish to retain sorted parameters, please manually sort your pbounds"
232+
" dictionary before constructing the optimizer."
233+
)
234+
warn(msg, stacklevel=1)
235+
self._sorting_warning_already_shown = True
223236
self._space.register(params, target, constraint_value)
224237
self.dispatch(Events.OPTIMIZATION_STEP)
225238

@@ -239,6 +252,18 @@ def probe(
239252
If True, the optimizer will evaluate the points when calling
240253
maximize(). Otherwise it will evaluate it at the moment.
241254
"""
255+
# TODO: remove in future version
256+
if isinstance(params, np.ndarray) and not self._sorting_warning_already_shown:
257+
msg = (
258+
"You're attempting to register an np.ndarray. Currently, the optimizer internally sorts"
259+
" parameters by key and expects any registered array to respect this order. In future"
260+
" versions this behaviour will change and the order as given by the pbounds dictionary"
261+
" will be used. If you wish to retain sorted parameters, please manually sort your pbounds"
262+
" dictionary before constructing the optimizer."
263+
)
264+
warn(msg, stacklevel=1)
265+
self._sorting_warning_already_shown = True
266+
params = self._space.array_to_params(params)
242267
if lazy:
243268
self._queue.append(params)
244269
else:
@@ -267,7 +292,8 @@ def _prime_queue(self, init_points: int) -> None:
267292
init_points = max(init_points, 1)
268293

269294
for _ in range(init_points):
270-
self._queue.append(self._space.random_sample())
295+
sample = self._space.random_sample()
296+
self._queue.append(self._space.array_to_params(sample))
271297

272298
def _prime_subscriptions(self) -> None:
273299
if not any([len(subs) for subs in self._events.values()]):

bayes_opt/target_space.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ def __init__(
102102
else:
103103
self._constraint_values = np.empty(shape=(0, constraint.lb.size), dtype=float)
104104

105-
self._sorting_warning_already_shown = False # TODO: remove in future version
106-
107105
def __contains__(self, x: NDArray[Float]) -> bool:
108106
"""Check if this parameter has already been registered.
109107
@@ -332,17 +330,6 @@ def register(
332330
>>> len(space)
333331
1
334332
"""
335-
# TODO: remove in future version
336-
if isinstance(params, np.ndarray) and not self._sorting_warning_already_shown:
337-
msg = (
338-
"You're attempting to register an np.ndarray. Currently, the optimizer internally sorts"
339-
" parameters by key and expects any registered array to respect this order. In future"
340-
" versions this behaviour will change and the order as given by the pbounds dictionary"
341-
" will be used. If you wish to retain sorted parameters, please manually sort your pbounds"
342-
" dictionary before constructing the optimizer."
343-
)
344-
warn(msg, stacklevel=1)
345-
self._sorting_warning_already_shown = True
346333
x = self._as_array(params)
347334
if x in self:
348335
if self._allow_duplicate_points:

0 commit comments

Comments
 (0)