Coverage for mlos_core/mlos_core/util.py: 94%
18 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-21 01:50 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-21 01:50 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""Internal helper functions for mlos_core package."""
8import pandas as pd
9from ConfigSpace import Configuration, ConfigurationSpace
12def compare_optional_series(left: pd.Series | None, right: pd.Series | None) -> bool:
13 """
14 Compare Series that may also be None.
16 Parameters
17 ----------
18 left : pandas.Series | None
19 The left Series to compare
20 right : pandas.Series | None
21 The right Series to compare
23 Returns
24 -------
25 bool
26 Compare the equality of two pd.Series | None objects
27 """
28 if isinstance(left, pd.Series) and isinstance(right, pd.Series):
29 return left.equals(right)
30 return left is None and right is None
33def compare_optional_dataframe(
34 left: pd.DataFrame | None,
35 right: pd.DataFrame | None,
36) -> bool:
37 """
38 Compare DataFrames that may also be None.
40 Parameters
41 ----------
42 left : pandas.DataFrame | None
43 The left DataFrame to compare
44 right : pandas.DataFrame | None
45 The right DataFrame to compare
47 Returns
48 -------
49 bool
50 Compare the equality of two pd.DataFrame | None objects
51 """
52 if isinstance(left, pd.DataFrame) and isinstance(right, pd.DataFrame):
53 return left.equals(right)
54 return left is None and right is None
57def config_to_series(config: Configuration) -> pd.Series:
58 """
59 Converts a ConfigSpace config to a Series.
61 Parameters
62 ----------
63 config : ConfigSpace.Configuration
64 The config to convert.
66 Returns
67 -------
68 pandas.Series
69 A Series, containing the config's parameters.
70 """
71 series: pd.Series = pd.Series(dict(config)) # needed for type hinting
72 return series
75def drop_nulls(d: dict) -> dict:
76 """
77 Remove all key-value pairs where the value is None.
79 Parameters
80 ----------
81 d : dict
82 The dictionary to clean.
84 Returns
85 -------
86 dict
87 The cleaned dictionary.
88 """
89 return {k: v for k, v in d.items() if v is not None}
92def normalize_config(
93 config_space: ConfigurationSpace,
94 config: Configuration | dict,
95) -> Configuration:
96 """
97 Convert a dictionary to a valid ConfigSpace configuration.
99 Some optimizers and adapters ignore ConfigSpace conditionals when proposing new
100 configurations. We have to manually remove inactive hyperparameters such suggestions.
102 Parameters
103 ----------
104 config_space : ConfigSpace.ConfigurationSpace
105 The parameter space to use.
106 config : dict
107 The configuration to convert.
109 Returns
110 -------
111 cs_config: ConfigSpace.Configuration
112 A valid ConfigSpace configuration with inactive parameters removed.
113 """
114 cs_config = Configuration(config_space, values=config, allow_inactive_with_values=True)
115 return Configuration(
116 config_space,
117 values={key: cs_config[key] for key in config_space.get_active_hyperparameters(cs_config)},
118 )