Skip to content

Commit

Permalink
[Test] Clean optimizer_test to include seed and checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ephoris committed Feb 7, 2024
1 parent 423d582 commit 2bc2e50
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
3 changes: 3 additions & 0 deletions mlos_core/mlos_core/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
from pkgutil import walk_packages
from typing import List, Optional, Set, Type, TypeVar

# A common seed to use to avoid tracking down race conditions and intermingling
# issues of seeds across tests that run in non-deterministic parallel orders.
SEED = 42

if sys.version_info >= (3, 10):
from typing import TypeAlias
Expand Down
19 changes: 9 additions & 10 deletions mlos_core/mlos_core/tests/optimizers/optimizer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from mlos_core.optimizers.bayesian_optimizers import BaseBayesianOptimizer, SmacOptimizer
from mlos_core.spaces.adapters import SpaceAdapterType

from mlos_core.tests import get_all_concrete_subclasses
from mlos_core.tests import get_all_concrete_subclasses, SEED


_LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -76,7 +76,7 @@ def objective(x: pd.Series) -> npt.ArrayLike: # pylint: disable=invalid-name
ret: npt.ArrayLike = (6 * x - 2)**2 * np.sin(12 * x - 4)
return ret
# Emukit doesn't allow specifying a random state, so we set the global seed.
np.random.seed(42)
np.random.seed(SEED)
optimizer = optimizer_class(parameter_space=configuration_space, **kwargs)

with pytest.raises(ValueError, match="No observations"):
Expand Down Expand Up @@ -320,7 +320,7 @@ def objective(point: pd.DataFrame) -> pd.Series:
ret: pd.Series = point["x"] + point["y"]
return ret

input_space = CS.ConfigurationSpace(seed=2169)
input_space = CS.ConfigurationSpace(seed=SEED)
# add a mix of numeric datatypes
input_space.add_hyperparameter(CS.UniformIntegerHyperparameter(name='x', lower=0, upper=5))
input_space.add_hyperparameter(CS.UniformFloatHyperparameter(name='y', lower=0.0, upper=5.0))
Expand All @@ -347,14 +347,13 @@ def objective(point: pd.DataFrame) -> pd.Series:
suggestion = optimizer.suggest()
assert isinstance(suggestion, pd.DataFrame)
assert (suggestion.columns == ['x', 'y']).all()
# Build suggestion mapping to cooperate with Configuration, note that
# doing a .iloc[0].to_dict() will cause pandas convert all numeric types
# to float64
tmp_suggest = {'x': suggestion['x'].values[0], 'y': suggestion['y'].values[0]}
# check that suggestion is in the space
configuration = CS.Configuration(optimizer.parameter_space, tmp_suggest)
# Check suggestion values are the expected dtype
assert isinstance(suggestion['x'].iloc[0], np.integer)
assert isinstance(suggestion['y'].iloc[0], np.floating)
# Check that suggestion is in the space
test_configuration = CS.Configuration(optimizer.parameter_space, suggestion.astype('O').iloc[0].to_dict())
# Raises an error if outside of configuration space
configuration.is_valid_configuration()
test_configuration.is_valid_configuration()
observation = objective(suggestion)
assert isinstance(observation, pd.Series)
optimizer.register(suggestion, observation)
Expand Down

0 comments on commit 2bc2e50

Please sign in to comment.