Coverage for mlos_core/mlos_core/tests/spaces/adapters/identity_adapter_test.py: 100%

20 statements  

« 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"""Tests for Identity space adapter.""" 

6 

7# pylint: disable=missing-function-docstring 

8 

9import ConfigSpace as CS 

10import pandas as pd 

11 

12from mlos_core.spaces.adapters import IdentityAdapter 

13 

14 

15def test_identity_adapter() -> None: 

16 """Tests identity adapter.""" 

17 input_space = CS.ConfigurationSpace(seed=1234) 

18 input_space.add(CS.UniformIntegerHyperparameter(name="int_1", lower=0, upper=100)) 

19 input_space.add(CS.UniformFloatHyperparameter(name="float_1", lower=0, upper=100)) 

20 input_space.add(CS.CategoricalHyperparameter(name="str_1", choices=["on", "off"])) 

21 

22 adapter = IdentityAdapter(orig_parameter_space=input_space) 

23 

24 num_configs = 10 

25 for sampled_config in input_space.sample_configuration( 

26 size=num_configs 

27 ): # pylint: disable=not-an-iterable # (false positive) 

28 sampled_config_df = pd.DataFrame( 

29 [sampled_config.values()], columns=list(sampled_config.keys()) 

30 ) 

31 target_config_df = adapter.inverse_transform(sampled_config_df) 

32 assert target_config_df.equals(sampled_config_df) 

33 target_config = CS.Configuration( 

34 adapter.target_parameter_space, values=target_config_df.iloc[0].to_dict() 

35 ) 

36 assert target_config == sampled_config 

37 orig_config_df = adapter.transform(target_config_df) 

38 assert orig_config_df.equals(sampled_config_df) 

39 orig_config = CS.Configuration( 

40 adapter.orig_parameter_space, values=orig_config_df.iloc[0].to_dict() 

41 ) 

42 assert orig_config == sampled_config