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

17 statements  

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

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 self._random_state = orig_parameter_space.random 

37 

38 def __repr__(self) -> str: 

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

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

41 self.__class__.__name__, 

42 self.orig_parameter_space, 

43 self.target_parameter_space, 

44 ) 

45 

46 @property 

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

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

49 return self._orig_parameter_space 

50 

51 @property 

52 @abstractmethod 

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

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

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

56 

57 @abstractmethod 

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

59 """ 

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

61 original parameter space. This method is called by the 

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

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

64 

65 Parameters 

66 ---------- 

67 configuration : pandas.Series 

68 Pandas series. Column names are the parameter names 

69 of the target parameter space. 

70 

71 Returns 

72 ------- 

73 configuration : pandas.Series 

74 Pandas series, containing the translated configuration. 

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

76 """ 

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

78 

79 @abstractmethod 

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

81 """ 

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

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

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

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

86 method. 

87 

88 Parameters 

89 ---------- 

90 configuration : pandas.Series 

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

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

93 rows are the configurations. 

94 

95 Returns 

96 ------- 

97 configuration : pandas.Series 

98 Series of the translated configurations / parameters. 

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

100 the rows are the configurations. 

101 """ 

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