Coverage for mlos_bench/mlos_bench/storage/base_tunable_config_data.py: 95%

21 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""" 

6Base interface for accessing the stored benchmark (tunable) config data. 

7 

8Note: a configuration in this context is the set of tunable parameter values and can 

9be used by one or more trials. 

10""" 

11from abc import ABCMeta, abstractmethod 

12from typing import Any, Dict, Optional 

13 

14import pandas 

15 

16from mlos_bench.storage.util import kv_df_to_dict 

17from mlos_bench.tunables.tunable import TunableValue 

18 

19 

20class TunableConfigData(metaclass=ABCMeta): 

21 """ 

22 Base interface for accessing the stored experiment benchmark (tunable) config data. 

23 

24 A configuration in this context is the set of tunable parameter values. 

25 """ 

26 

27 def __init__(self, *, tunable_config_id: int): 

28 self._tunable_config_id = tunable_config_id 

29 

30 def __repr__(self) -> str: 

31 return f"TunableConfig :: {self._tunable_config_id}: {self.config_dict}" 

32 

33 def __eq__(self, other: Any) -> bool: 

34 if not isinstance(other, self.__class__): 

35 return False 

36 return self._tunable_config_id == other._tunable_config_id 

37 

38 @property 

39 def tunable_config_id(self) -> int: 

40 """Unique ID of the (tunable) configuration.""" 

41 return self._tunable_config_id 

42 

43 @property 

44 @abstractmethod 

45 def config_df(self) -> pandas.DataFrame: 

46 """ 

47 Retrieve the trials' tunable configuration from the storage. 

48 

49 Note: this corresponds to the Trial object's "tunables" property. 

50 

51 Returns 

52 ------- 

53 config : pandas.DataFrame 

54 A dataframe with the tunable configuration of the trial. 

55 It has two `str` columns, "parameter" and "value". 

56 """ 

57 

58 @property 

59 def config_dict(self) -> Dict[str, Optional[TunableValue]]: 

60 """ 

61 Retrieve the trials' tunable configuration from the storage as a dict. 

62 

63 Note: this corresponds to the Trial object's "tunables" property. 

64 

65 Returns 

66 ------- 

67 config : dict 

68 """ 

69 return kv_df_to_dict(self.config_df) 

70 

71 # TODO: add methods for retrieving 

72 # - trials by tunable config, even across experiments (e.g., for merging) 

73 # - trial config groups (i.e., all experiments' trials with the same tunable config)