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
« 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.
8Note: a configuration in this context is the set of tunable parameter values and can
9be used by one or more trials.
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
19import pandas
21from mlos_bench.storage.util import kv_df_to_dict
22from mlos_bench.tunables.tunable import TunableValue
25class TunableConfigData(metaclass=ABCMeta):
26 """
27 Base interface for accessing the stored experiment benchmark (tunable) config data.
29 A configuration in this context is the set of tunable parameter values.
30 """
32 def __init__(self, *, tunable_config_id: int):
33 self._tunable_config_id = tunable_config_id
35 def __repr__(self) -> str:
36 return f"TunableConfig :: {self._tunable_config_id}: {self.config_dict}"
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
43 @property
44 def tunable_config_id(self) -> int:
45 """Unique ID of the (tunable) configuration."""
46 return self._tunable_config_id
48 @property
49 @abstractmethod
50 def config_df(self) -> pandas.DataFrame:
51 """
52 Retrieve the trials' tunable configuration from the storage.
54 Note: this corresponds to the Trial object's "tunables" property.
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 """
63 @property
64 def config_dict(self) -> Dict[str, Optional[TunableValue]]:
65 """
66 Retrieve the trials' tunable configuration from the storage as a dict.
68 Note: this corresponds to the Trial object's "tunables" property.
70 Returns
71 -------
72 config : dict
73 """
74 return kv_df_to_dict(self.config_df)
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)