Coverage for mlos_bench/mlos_bench/tests/storage/tunable_config_data_test.py: 100%

26 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"""Unit tests for loading the TunableConfigData.""" 

6 

7from math import ceil 

8 

9from mlos_bench.storage.base_experiment_data import ExperimentData 

10from mlos_bench.tests.storage import CONFIG_TRIAL_REPEAT_COUNT 

11from mlos_bench.tunables.tunable_groups import TunableGroups 

12 

13 

14def test_trial_data_tunable_config_data( 

15 exp_data: ExperimentData, 

16 tunable_groups: TunableGroups, 

17) -> None: 

18 """Check expected return values for TunableConfigData.""" 

19 trial_id = 1 

20 expected_config_id = 1 

21 trial = exp_data.trials[trial_id] 

22 tunable_config = trial.tunable_config 

23 assert tunable_config.tunable_config_id == expected_config_id 

24 # The first should be the defaults. 

25 assert tunable_config.config_dict == tunable_groups.get_param_values() 

26 assert trial.tunable_config_trial_group.tunable_config == tunable_config 

27 

28 

29def test_trial_metadata(exp_data: ExperimentData) -> None: 

30 """Check expected return values for TunableConfigData metadata.""" 

31 assert exp_data.objectives == {"score": "min"} 

32 for trial_id, trial in exp_data.trials.items(): 

33 assert trial.tunable_config_id == ceil(trial_id / CONFIG_TRIAL_REPEAT_COUNT) 

34 assert trial.metadata_dict == { 

35 # Only the first CONFIG_TRIAL_REPEAT_COUNT set should be the defaults. 

36 "is_defaults": str(trial_id <= CONFIG_TRIAL_REPEAT_COUNT), 

37 "opt_target_0": "score", 

38 "opt_direction_0": "min", 

39 "optimizer": "MockOptimizer", 

40 "repeat_i": ((trial_id - 1) % CONFIG_TRIAL_REPEAT_COUNT) + 1, 

41 } 

42 

43 

44def test_trial_data_no_tunables_config_data(exp_no_tunables_data: ExperimentData) -> None: 

45 """Check expected return values for TunableConfigData.""" 

46 empty_config: dict = {} 

47 for _trial_id, trial in exp_no_tunables_data.trials.items(): 

48 assert trial.tunable_config.config_dict == empty_config 

49 

50 

51def test_mixed_numerics_exp_trial_data( 

52 mixed_numerics_exp_data: ExperimentData, 

53 mixed_numerics_tunable_groups: TunableGroups, 

54) -> None: 

55 """Tests that data type conversions are retained when loading experiment data with 

56 mixed numeric tunable types. 

57 """ 

58 trial = next(iter(mixed_numerics_exp_data.trials.values())) 

59 config = trial.tunable_config.config_dict 

60 for tunable, _group in mixed_numerics_tunable_groups: 

61 assert isinstance(config[tunable.name], tunable.dtype)