Skip to content

Commit

Permalink
Fix array warning (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
till-m authored Dec 18, 2024
1 parent d8ccc69 commit f4016e0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
30 changes: 28 additions & 2 deletions bayes_opt/bayesian_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

from collections import deque
from typing import TYPE_CHECKING, Any
from warnings import warn

import numpy as np
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import Matern

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

import numpy as np
from numpy.random import RandomState
from numpy.typing import NDArray
from scipy.optimize import NonlinearConstraint
Expand Down Expand Up @@ -166,6 +167,7 @@ def __init__(
error_msg = "The transformer must be an instance of DomainTransformer"
raise TypeError(error_msg) from exc

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

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

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

for _ in range(init_points):
self._queue.append(self._space.random_sample())
sample = self._space.random_sample()
self._queue.append(self._space.array_to_params(sample))

def _prime_subscriptions(self) -> None:
if not any([len(subs) for subs in self._events.values()]):
Expand Down
13 changes: 0 additions & 13 deletions bayes_opt/target_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ def __init__(
else:
self._constraint_values = np.empty(shape=(0, constraint.lb.size), dtype=float)

self._sorting_warning_already_shown = False # TODO: remove in future version

def __contains__(self, x: NDArray[Float]) -> bool:
"""Check if this parameter has already been registered.
Expand Down Expand Up @@ -332,17 +330,6 @@ def register(
>>> len(space)
1
"""
# TODO: remove in future version
if isinstance(params, np.ndarray) and not self._sorting_warning_already_shown:
msg = (
"You're attempting to register an np.ndarray. Currently, the optimizer internally sorts"
" parameters by key and expects any registered array to respect this order. In future"
" versions this behaviour will change and the order as given by the pbounds dictionary"
" will be used. If you wish to retain sorted parameters, please manually sort your pbounds"
" dictionary before constructing the optimizer."
)
warn(msg, stacklevel=1)
self._sorting_warning_already_shown = True
x = self._as_array(params)
if x in self:
if self._allow_duplicate_points:
Expand Down

0 comments on commit f4016e0

Please sign in to comment.