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

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 

8Note: a configuration in this context is the set of tunable parameter values and can 

9be used by one or more trials. 

10 

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 

19 

20import pandas 

21 

22from mlos_bench.storage.util import kv_df_to_dict 

23from mlos_bench.tunables.tunable_types import TunableValue 

24 

25 

26class TunableConfigData(metaclass=ABCMeta): 

27 """ 

28 Base interface for accessing the stored experiment benchmark (tunable) config data. 

29 

30 A configuration in this context is the set of tunable parameter values. 

31 """ 

32 

33 def __init__(self, *, tunable_config_id: int): 

34 self._tunable_config_id = tunable_config_id 

35 

36 def __repr__(self) -> str: 

37 return f"TunableConfig :: {self._tunable_config_id}: {self.config_dict}" 

38 

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 

43 

44 @property 

45 def tunable_config_id(self) -> int: 

46 """Unique ID of the (tunable) configuration.""" 

47 return self._tunable_config_id 

48 

49 @property 

50 @abstractmethod 

51 def config_df(self) -> pandas.DataFrame: 

52 """ 

53 Retrieve the trials' tunable configuration from the storage. 

54 

55 Note: this corresponds to the Trial object's "tunables" property. 

56 

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 """ 

63 

64 @property 

65 def config_dict(self) -> dict[str, TunableValue | None]: 

66 """ 

67 Retrieve the trials' tunable configuration from the storage as a dict. 

68 

69 Note: this corresponds to the Trial object's "tunables" property. 

70 

71 Returns 

72 ------- 

73 config : dict 

74 """ 

75 return kv_df_to_dict(self.config_df) 

76 

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)