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
« 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.
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
14import pandas
16from mlos_bench.storage.util import kv_df_to_dict
17from mlos_bench.tunables.tunable import TunableValue
20class TunableConfigData(metaclass=ABCMeta):
21 """
22 Base interface for accessing the stored experiment benchmark (tunable) config data.
24 A configuration in this context is the set of tunable parameter values.
25 """
27 def __init__(self, *, tunable_config_id: int):
28 self._tunable_config_id = tunable_config_id
30 def __repr__(self) -> str:
31 return f"TunableConfig :: {self._tunable_config_id}: {self.config_dict}"
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
38 @property
39 def tunable_config_id(self) -> int:
40 """Unique ID of the (tunable) configuration."""
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)