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

22 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-22 01:18 +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, _metadata = optimizer.suggest() 

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

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

42 

43 with pytest.raises(UserWarning): 

44 optimizer.register(configs=suggestion, scores=scores, context=context) 

45 

46 with pytest.raises(UserWarning): 

47 optimizer.suggest(context=context) 

48 

49 if isinstance(optimizer, BaseBayesianOptimizer): 

50 with pytest.raises(UserWarning): 

51 optimizer.surrogate_predict(configs=suggestion, context=context)