Coverage for mlos_bench/mlos_bench/storage/base_tunable_config_data.py: 95%
21 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-01 00:52 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-01 00:52 +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` :
14 The base storage module for mlos_bench, which includes some basic examples in
15 the documentation.
16"""
17from abc import ABCMeta, abstractmethod
18from typing import Any
20import pandas
22from mlos_bench.storage.util import kv_df_to_dict
23from mlos_bench.tunables.tunable_types import TunableValue
26class TunableConfigData(metaclass=ABCMeta):
27 """
28 Base interface for accessing the stored experiment benchmark (tunable) config data.
30 A configuration in this context is the set of tunable parameter values.
31 """
33 def __init__(self, *, tunable_config_id: int):
34 self._tunable_config_id = tunable_config_id
36 def __repr__(self) -> str:
37 return f"TunableConfig :: {self._tunable_config_id}: {self.config_dict}"
39 def __eq__(self, other: Any) -> bool:
40 if not isinstance(other, self.__class__):
41 return False
42 return self._tunable_config_id == other._tunable_config_id
44 @property
45 def tunable_config_id(self) -> int:
46 """Unique ID of the (tunable) configuration."""
47 return self._tunable_config_id
49 @property
50 @abstractmethod
51 def config_df(self) -> pandas.DataFrame:
52 """
53 Retrieve the trials' tunable configuration from the storage.
55 Note: this corresponds to the Trial object's "tunables" property.
57 Returns
58 -------
59 config : pandas.DataFrame
60 A dataframe with the tunable configuration of the trial.
61 It has two `str` columns, "parameter" and "value".
62 """
64 @property
65 def config_dict(self) -> dict[str, TunableValue | None]:
66 """
67 Retrieve the trials' tunable configuration from the storage as a dict.
69 Note: this corresponds to the Trial object's "tunables" property.
71 Returns
72 -------
73 config : dict
74 """
75 return kv_df_to_dict(self.config_df)
77 # TODO: add methods for retrieving
78 # - trials by tunable config, even across experiments (e.g., for merging)
79 # - trial config groups (i.e., all experiments' trials with the same tunable config)