Skip to content

Commit

Permalink
[WIP] Change suggestion building in pytest example
Browse files Browse the repository at this point in the history
Note that if you have a dataframe will all numeric types, df.iloc[0] will convert all columns to float64 because Series cannot be mixed type.
  • Loading branch information
ephoris committed Feb 5, 2024
1 parent aae94e4 commit 6c3a482
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions mlos_core/mlos_core/tests/optimizers/optimizer_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
"""
Expand Down Expand Up @@ -301,14 +299,11 @@ def test_optimizer_type_defs(optimizer_class: Type[BaseOptimizer]) -> None:


@pytest.mark.parametrize(('optimizer_type', 'kwargs'), [
# Default optimizer
(None, {}),
# Enumerate all supported Optimizers
*[(member, {}) for member in OptimizerType],
# Optimizer with non-empty kwargs argument
(OptimizerType.SMAC, {
# Test with default config.
'use_default_config': True,
# 'n_random_init': 10,
}),
])
def test_mixed_input_space_types(optimizer_type: OptimizerType, kwargs: Optional[dict]) -> None:
"""
Expand All @@ -328,11 +323,17 @@ def objective(point: pd.DataFrame) -> pd.Series:
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))

optimizer: BaseOptimizer = OptimizerFactory.create(
parameter_space=input_space,
optimizer_type=optimizer_type,
optimizer_kwargs=kwargs,
)
if optimizer_type is None:
optimizer: BaseOptimizer = OptimizerFactory.create(
parameter_space=input_space,
optimizer_kwargs=kwargs,
)
else:
optimizer: BaseOptimizer = OptimizerFactory.create(
parameter_space=input_space,
optimizer_type=optimizer_type,
optimizer_kwargs=kwargs,
)

with pytest.raises(ValueError, match="No observations"):
optimizer.get_best_observation()
Expand All @@ -344,8 +345,12 @@ 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, suggestion.iloc[0].to_dict())
configuration = CS.Configuration(optimizer.parameter_space, tmp_suggest)
# Raises an error if outside of configuration space
configuration.is_valid_configuration()
observation = objective(suggestion)
Expand Down

0 comments on commit 6c3a482

Please sign in to comment.