Coverage for mlos_core/mlos_core/util.py: 100%
9 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-06 00:35 +0000
« 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"""
6Internal helper functions for mlos_core package.
7"""
9from typing import Union
11from ConfigSpace import Configuration, ConfigurationSpace
12import pandas as pd
15def config_to_dataframe(config: Configuration) -> pd.DataFrame:
16 """Converts a ConfigSpace config to a DataFrame
18 Parameters
19 ----------
20 config : ConfigSpace.Configuration
21 The config to convert.
23 Returns
24 -------
25 pd.DataFrame
26 A DataFrame with a single row, containing the config's parameters.
27 """
28 return pd.DataFrame([dict(config)])
31def normalize_config(config_space: ConfigurationSpace, config: Union[Configuration, dict]) -> Configuration:
32 """
33 Convert a dictionary to a valid ConfigSpace configuration.
35 Some optimizers and adapters ignore ConfigSpace conditionals when proposing new
36 configurations. We have to manually remove inactive hyperparameters such suggestions.
38 Parameters
39 ----------
40 config_space : ConfigurationSpace
41 The parameter space to use.
42 config : dict
43 The configuration to convert.
45 Returns
46 -------
47 cs_config: Configuration
48 A valid ConfigSpace configuration with inactive parameters removed.
49 """
50 cs_config = Configuration(config_space, values=config, allow_inactive_with_values=True)
51 return Configuration(
52 config_space, values={
53 key: cs_config[key]
54 for key in config_space.get_active_hyperparameters(cs_config)
55 }
56 )