Coverage for mlos_bench/mlos_bench/storage/base_tunable_config_data.py: 95%
21 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-10-07 01:52 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-10-07 01:52 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""Base interface for accessing the stored benchmark (tunable) config data."""
6from abc import ABCMeta, abstractmethod
7from typing import Any, Dict, Optional
9import pandas
11from mlos_bench.storage.util import kv_df_to_dict
12from mlos_bench.tunables.tunable import TunableValue
15class TunableConfigData(metaclass=ABCMeta):
16 """
17 Base interface for accessing the stored experiment benchmark (tunable) config data.
19 A configuration in this context is the set of tunable parameter values.
20 """
22 def __init__(self, *, tunable_config_id: int):
23 self._tunable_config_id = tunable_config_id
25 def __repr__(self) -> str:
26 return f"TunableConfig :: {self._tunable_config_id}: {self.config_dict}"
28 def __eq__(self, other: Any) -> bool:
29 if not isinstance(other, self.__class__):
30 return False
31 return self._tunable_config_id == other._tunable_config_id
33 @property
34 def tunable_config_id(self) -> int:
35 """Unique ID of the (tunable) configuration."""
36 return self._tunable_config_id
38 @property
39 @abstractmethod
40 def config_df(self) -> pandas.DataFrame:
41 """
42 Retrieve the trials' tunable configuration from the storage.
44 Note: this corresponds to the Trial object's "tunables" property.
46 Returns
47 -------
48 config : pandas.DataFrame
49 A dataframe with the tunable configuration of the trial.
50 It has two `str` columns, "parameter" and "value".
51 """
53 @property
54 def config_dict(self) -> Dict[str, Optional[TunableValue]]:
55 """
56 Retrieve the trials' tunable configuration from the storage as a dict.
58 Note: this corresponds to the Trial object's "tunables" property.
60 Returns
61 -------
62 config : dict
63 """
64 return kv_df_to_dict(self.config_df)
66 # TODO: add methods for retrieving
67 # - trials by tunable config, even across experiments (e.g., for merging)
68 # - trial config groups (i.e., all experiments' trials with the same tunable config)