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

18 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-06 00:35 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5""" 

6Contains the BaseSpaceAdapter abstract class. 

7""" 

8 

9from abc import ABCMeta, abstractmethod 

10 

11import ConfigSpace 

12import pandas as pd 

13 

14 

15class BaseSpaceAdapter(metaclass=ABCMeta): 

16 """SpaceAdapter abstract class defining the basic interface. 

17 

18 Parameters 

19 ---------- 

20 orig_parameter_space : ConfigSpace.ConfigurationSpace 

21 The original parameter space to explore. 

22 """ 

23 

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

25 self._orig_parameter_space: ConfigSpace.ConfigurationSpace = orig_parameter_space 

26 self._random_state = orig_parameter_space.random 

27 

28 def __repr__(self) -> str: 

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

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

31 self.__class__.__name__, 

32 self.orig_parameter_space, 

33 self.target_parameter_space, 

34 ) 

35 

36 @property 

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

38 """ 

39 Original (user-provided) parameter space to explore. 

40 """ 

41 return self._orig_parameter_space 

42 

43 @property 

44 @abstractmethod 

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

46 """ 

47 Target parameter space that is fed to the underlying optimizer. 

48 """ 

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

50 

51 @abstractmethod 

52 def transform(self, configuration: pd.DataFrame) -> pd.DataFrame: 

53 """Translates a configuration, which belongs to the target parameter space, to the original parameter space. 

54 This method is called by the `suggest` method of the `BaseOptimizer` class. 

55 

56 Parameters 

57 ---------- 

58 configuration : pd.DataFrame 

59 Pandas dataframe with a single row. Column names are the parameter names of the target parameter space. 

60 

61 Returns 

62 ------- 

63 configuration : pd.DataFrame 

64 Pandas dataframe with a single row, containing the translated configuration. 

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

66 """ 

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

68 

69 @abstractmethod 

70 def inverse_transform(self, configurations: pd.DataFrame) -> pd.DataFrame: 

71 """Translates a configuration, which belongs to the original parameter space, to the target parameter space. 

72 This method is called by the `register` method of the `BaseOptimizer` class, and performs the inverse operation 

73 of `BaseSpaceAdapter.transform` method. 

74 

75 Parameters 

76 ---------- 

77 configurations : pd.DataFrame 

78 Dataframe of configurations / parameters, which belong to the original parameter space. 

79 The columns are the parameter names the original parameter space and the rows are the configurations. 

80 

81 Returns 

82 ------- 

83 configurations : pd.DataFrame 

84 Dataframe of the translated configurations / parameters. 

85 The columns are the parameter names of the target parameter space and the rows are the configurations. 

86 """ 

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