Coverage for mlos_core/mlos_core/spaces/adapters/adapter.py: 100%

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

6Contains the BaseSpaceAdapter abstract class. 

7 

8As mentioned in :py:mod:`mlos_core.spaces.adapters`, the space adapters provide a 

9mechanism for automatic transformation of the original 

10:py:class:`ConfigSpace.ConfigurationSpace` provided to the Optimizer into a new 

11space for the Optimizer to search over. 

12 

13It's main APIs are the :py:meth:`~.BaseSpaceAdapter.transform` and 

14:py:meth:`~.BaseSpaceAdapter.inverse_transform` methods, which are used to translate 

15configurations from one space to another. 

16""" 

17 

18from abc import ABCMeta, abstractmethod 

19 

20import ConfigSpace 

21import pandas as pd 

22 

23 

24class BaseSpaceAdapter(metaclass=ABCMeta): 

25 """ 

26 SpaceAdapter abstract class defining the basic interface. 

27 

28 Parameters 

29 ---------- 

30 orig_parameter_space : ConfigSpace.ConfigurationSpace 

31 The original parameter space to explore. 

32 """ 

33 

34 def __init__(self, *, orig_parameter_space: ConfigSpace.ConfigurationSpace): 

35 self._orig_parameter_space: ConfigSpace.ConfigurationSpace = orig_parameter_space 

36 # Use the same random state as the original parameter space. 

37 self._random_state = orig_parameter_space.random 

38 

39 def __repr__(self) -> str: 

40 # pylint: disable=consider-using-f-string 

41 return "{}(original_parameter_space={}, target_parameter_space={})".format( 

42 self.__class__.__name__, 

43 self.orig_parameter_space, 

44 self.target_parameter_space, 

45 ) 

46 

47 @property 

48 def orig_parameter_space(self) -> ConfigSpace.ConfigurationSpace: 

49 """Original (user-provided) parameter space to explore.""" 

50 return self._orig_parameter_space 

51 

52 @property 

53 @abstractmethod 

54 def target_parameter_space(self) -> ConfigSpace.ConfigurationSpace: 

55 """Target parameter space that is fed to the underlying optimizer.""" 

56 pass # pylint: disable=unnecessary-pass # pragma: no cover 

57 

58 @abstractmethod 

59 def transform(self, configuration: pd.Series) -> pd.Series: 

60 """ 

61 Translates a configuration, which belongs to the target parameter space, to the 

62 original parameter space. This method is called by the 

63 :py:meth:`~mlos_core.optimizers.optimizer.BaseOptimizer.suggest` method of the 

64 :py:class:`~mlos_core.optimizers.optimizer.BaseOptimizer` class. 

65 

66 Parameters 

67 ---------- 

68 configuration : pandas.Series 

69 Pandas series. Column names are the parameter names 

70 of the target parameter space. 

71 

72 Returns 

73 ------- 

74 configuration : pandas.Series 

75 Pandas series, containing the translated configuration. 

76 Column names are the parameter names of the original parameter space. 

77 """ 

78 pass # pylint: disable=unnecessary-pass # pragma: no cover 

79 

80 @abstractmethod 

81 def inverse_transform(self, configuration: pd.Series) -> pd.Series: 

82 """ 

83 Translates a configuration, which belongs to the original parameter space, to 

84 the target parameter space. This method is called by the `register` method of 

85 the :py:class:`~mlos_core.optimizers.optimizer.BaseOptimizer` class, and 

86 performs the inverse operation of :py:meth:`~.BaseSpaceAdapter.transform` 

87 method. 

88 

89 Parameters 

90 ---------- 

91 configuration : pandas.Series 

92 A Series of configuration parameters, which belong to the original parameter space. 

93 The indices are the parameter names the original parameter space and the 

94 rows are the configurations. 

95 

96 Returns 

97 ------- 

98 configuration : pandas.Series 

99 Series of the translated configurations / parameters. 

100 The indices are the parameter names of the target parameter space and 

101 the rows are the configurations. 

102 """ 

103 pass # pylint: disable=unnecessary-pass # pragma: no cover