Skip to content

Commit 2bc2e50

Browse files
committed
[Test] Clean optimizer_test to include seed and checks
1 parent 423d582 commit 2bc2e50

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

mlos_core/mlos_core/tests/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
from pkgutil import walk_packages
1313
from typing import List, Optional, Set, Type, TypeVar
1414

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

1619
if sys.version_info >= (3, 10):
1720
from typing import TypeAlias

mlos_core/mlos_core/tests/optimizers/optimizer_test.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from mlos_core.optimizers.bayesian_optimizers import BaseBayesianOptimizer, SmacOptimizer
2424
from mlos_core.spaces.adapters import SpaceAdapterType
2525

26-
from mlos_core.tests import get_all_concrete_subclasses
26+
from mlos_core.tests import get_all_concrete_subclasses, SEED
2727

2828

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

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

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

0 commit comments

Comments
 (0)