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
« 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
11import pandas
13from mlos_bench.storage.util import kv_df_to_dict
14from mlos_bench.tunables.tunable import TunableValue
17class TunableConfigData(metaclass=ABCMeta):
18 """
19 Base interface for accessing the stored experiment benchmark (tunable) config data.
21 A configuration in this context is the set of tunable parameter values.
22 """
24 def __init__(self, *,
25 tunable_config_id: int):
26 self._tunable_config_id = tunable_config_id
28 def __repr__(self) -> str:
29 return f"TunableConfig :: {self._tunable_config_id}: {self.config_dict}"
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
36 @property
37 def tunable_config_id(self) -> int:
38 """
39 Unique ID of the (tunable) configuration.
40 """
41 return self._tunable_config_id
43 @property
44 @abstractmethod
45 def config_df(self) -> pandas.DataFrame:
46 """
47 Retrieve the trials' tunable configuration from the storage.
49 Note: this corresponds to the Trial object's "tunables" property.
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 """
58 @property
59 def config_dict(self) -> Dict[str, Optional[TunableValue]]:
60 """
61 Retrieve the trials' tunable configuration from the storage as a dict.
63 Note: this corresponds to the Trial object's "tunables" property.
65 Returns
66 -------
67 config : dict
68 """
69 return kv_df_to_dict(self.config_df)
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)