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

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

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 

11See Also 

12-------- 

13:py:mod:`mlos_bench.storage` : The base storage module for mlos_bench, which 

14 includes some basic examples in the documentation. 

15""" 

16from abc import ABCMeta, abstractmethod 

17from typing import Any, Dict, Optional 

18 

19import pandas 

20 

21from mlos_bench.storage.util import kv_df_to_dict 

22from mlos_bench.tunables.tunable import TunableValue 

23 

24 

25class TunableConfigData(metaclass=ABCMeta): 

26 """ 

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

28 

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

30 """ 

31 

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

33 self._tunable_config_id = tunable_config_id 

34 

35 def __repr__(self) -> str: 

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

37 

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

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

40 return False 

41 return self._tunable_config_id == other._tunable_config_id 

42 

43 @property 

44 def tunable_config_id(self) -> int: 

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

46 return self._tunable_config_id 

47 

48 @property 

49 @abstractmethod 

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

51 """ 

52 Retrieve the trials' tunable configuration from the storage. 

53 

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

55 

56 Returns 

57 ------- 

58 config : pandas.DataFrame 

59 A dataframe with the tunable configuration of the trial. 

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

61 """ 

62 

63 @property 

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

65 """ 

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

67 

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

69 

70 Returns 

71 ------- 

72 config : dict 

73 """ 

74 return kv_df_to_dict(self.config_df) 

75 

76 # TODO: add methods for retrieving 

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

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