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

18 statements  

« 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""" 

6Unit tests for deep copy of tunable objects and groups. 

7""" 

8 

9from mlos_bench.tunables.tunable_groups import TunableGroups 

10 

11 

12def test_tunable_categorical_types() -> None: 

13 """ 

14 Check if we accept tunable categoricals as ints as well as strings and 

15 convert both to strings. 

16 """ 

17 tunable_params = { 

18 "test-group": { 

19 "cost": 1, 

20 "params": { 

21 "int-cat": { 

22 "type": "categorical", 

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

24 "default": 1, 

25 }, 

26 "bool-cat": { 

27 "type": "categorical", 

28 "values": [True, False], 

29 "default": True, 

30 }, 

31 "false-bool-cat": { 

32 "type": "categorical", 

33 "values": [True, False], 

34 "default": False, 

35 }, 

36 "str-cat": { 

37 "type": "categorical", 

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

39 "default": "a", 

40 }, 

41 } 

42 } 

43 } 

44 tunable_groups = TunableGroups(tunable_params) 

45 tunable_groups.reset() 

46 

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

48 assert isinstance(int_cat.value, str) 

49 assert int_cat.value == "1" 

50 

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

52 assert isinstance(bool_cat.value, str) 

53 assert bool_cat.value == "True" 

54 

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

56 assert isinstance(false_bool_cat.value, str) 

57 assert false_bool_cat.value == "False" 

58 

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

60 assert isinstance(str_cat.value, str) 

61 assert str_cat.value == "a"