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

22 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-06 00:35 +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""" 

8from abc import ABCMeta, abstractmethod 

9from typing import Any, Dict, Optional 

10 

11import pandas 

12 

13from mlos_bench.storage.util import kv_df_to_dict 

14from mlos_bench.tunables.tunable import TunableValue 

15 

16 

17class TunableConfigData(metaclass=ABCMeta): 

18 """ 

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

20 

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

22 """ 

23 

24 def __init__(self, *, 

25 tunable_config_id: int): 

26 self._tunable_config_id = tunable_config_id 

27 

28 def __repr__(self) -> str: 

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

30 

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

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

33 return False 

34 return self._tunable_config_id == other._tunable_config_id 

35 

36 @property 

37 def tunable_config_id(self) -> int: 

38 """ 

39 Unique ID of the (tunable) configuration. 

40 """ 

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)