Coverage for mlos_core/mlos_core/tests/optimizers/bayesian_optimizers_test.py: 96%

23 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-20 00:44 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Tests for Bayesian Optimizers.""" 

6 

7from typing import Optional, Type 

8 

9import ConfigSpace as CS 

10import pandas as pd 

11import pytest 

12 

13from mlos_core.optimizers import BaseOptimizer, OptimizerType 

14from mlos_core.optimizers.bayesian_optimizers import BaseBayesianOptimizer 

15 

16 

17@pytest.mark.filterwarnings("error:Not Implemented") 

18@pytest.mark.parametrize( 

19 ("optimizer_class", "kwargs"), 

20 [ 

21 *[(member.value, {}) for member in OptimizerType], 

22 ], 

23) 

24def test_context_not_implemented_warning( 

25 configuration_space: CS.ConfigurationSpace, 

26 optimizer_class: Type[BaseOptimizer], 

27 kwargs: Optional[dict], 

28) -> None: 

29 """Make sure we raise warnings for the functionality that has not been implemented 

30 yet. 

31 """ 

32 if kwargs is None: 

33 kwargs = {} 

34 optimizer = optimizer_class( 

35 parameter_space=configuration_space, 

36 optimization_targets=["score"], 

37 **kwargs, 

38 ) 

39 suggestion = optimizer.suggest() 

40 scores = pd.Series({"score": [1]}) 

41 context = pd.Series([["something"]]) 

42 

43 suggestion._context = context # pylint: disable=protected-access 

44 with pytest.raises(UserWarning): 

45 optimizer.register(observations=suggestion.complete(scores)) 

46 

47 with pytest.raises(UserWarning): 

48 optimizer.suggest(context=context) 

49 

50 if isinstance(optimizer, BaseBayesianOptimizer): 

51 with pytest.raises(UserWarning): 

52 optimizer.surrogate_predict(suggestion=suggestion)