Coverage for mlos_bench/mlos_bench/tests/tunables/test_tunable_categoricals.py: 100%

17 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"""Unit tests for deep copy of tunable objects and groups.""" 

6 

7from mlos_bench.tunables.tunable_groups import TunableGroups 

8 

9 

10def test_tunable_categorical_types() -> None: 

11 """Check if we accept tunable categoricals as ints as well as strings and convert 

12 both to strings. 

13 """ 

14 tunable_params = { 

15 "test-group": { 

16 "cost": 1, 

17 "params": { 

18 "int-cat": { 

19 "type": "categorical", 

20 "values": [1, 2, 3], 

21 "default": 1, 

22 }, 

23 "bool-cat": { 

24 "type": "categorical", 

25 "values": [True, False], 

26 "default": True, 

27 }, 

28 "false-bool-cat": { 

29 "type": "categorical", 

30 "values": [True, False], 

31 "default": False, 

32 }, 

33 "str-cat": { 

34 "type": "categorical", 

35 "values": ["a", "b", "c"], 

36 "default": "a", 

37 }, 

38 }, 

39 } 

40 } 

41 tunable_groups = TunableGroups(tunable_params) 

42 tunable_groups.reset() 

43 

44 int_cat, _ = tunable_groups.get_tunable("int-cat") 

45 assert isinstance(int_cat.value, str) 

46 assert int_cat.value == "1" 

47 

48 bool_cat, _ = tunable_groups.get_tunable("bool-cat") 

49 assert isinstance(bool_cat.value, str) 

50 assert bool_cat.value == "True" 

51 

52 false_bool_cat, _ = tunable_groups.get_tunable("false-bool-cat") 

53 assert isinstance(false_bool_cat.value, str) 

54 assert false_bool_cat.value == "False" 

55 

56 str_cat, _ = tunable_groups.get_tunable("str-cat") 

57 assert isinstance(str_cat.value, str) 

58 assert str_cat.value == "a"