Coverage for mlos_core/mlos_core/spaces/adapters/adapter.py: 100%
17 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 01:18 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 01:18 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""
6Contains the BaseSpaceAdapter abstract class.
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.
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"""
18from abc import ABCMeta, abstractmethod
20import ConfigSpace
21import pandas as pd
24class BaseSpaceAdapter(metaclass=ABCMeta):
25 """
26 SpaceAdapter abstract class defining the basic interface.
28 Parameters
29 ----------
30 orig_parameter_space : ConfigSpace.ConfigurationSpace
31 The original parameter space to explore.
32 """
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
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 )
46 @property
47 def orig_parameter_space(self) -> ConfigSpace.ConfigurationSpace:
48 """Original (user-provided) parameter space to explore."""
49 return self._orig_parameter_space
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
57 @abstractmethod
58 def transform(self, configuration: pd.DataFrame) -> pd.DataFrame:
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.
65 Parameters
66 ----------
67 configuration : pandas.DataFrame
68 Pandas dataframe with a single row. Column names are the parameter names
69 of the target parameter space.
71 Returns
72 -------
73 configuration : pandas.DataFrame
74 Pandas dataframe with a single row, 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
79 @abstractmethod
80 def inverse_transform(self, configurations: pd.DataFrame) -> pd.DataFrame:
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.
88 Parameters
89 ----------
90 configurations : pandas.DataFrame
91 Dataframe of configurations / parameters, which belong to the original parameter space.
92 The columns are the parameter names the original parameter space and the
93 rows are the configurations.
95 Returns
96 -------
97 configurations : pandas.DataFrame
98 Dataframe of the translated configurations / parameters.
99 The columns 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