Coverage for mlos_bench/mlos_bench/tests/tunables/test_tunables_size_props.py: 100%
39 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"""
6Unit tests for checking tunable size properties.
7"""
9import numpy as np
10import pytest
12from mlos_bench.tunables.tunable import Tunable
15# Note: these test do *not* check the ConfigSpace conversions for those same Tunables.
16# That is checked indirectly via grid_search_optimizer_test.py
18def test_tunable_int_size_props() -> None:
19 """Test tunable int size properties"""
20 tunable = Tunable(
21 name="test",
22 config={
23 "type": "int",
24 "range": [1, 5],
25 "default": 3,
26 })
27 assert tunable.span == 4
28 assert tunable.cardinality == 5
29 expected = [1, 2, 3, 4, 5]
30 assert list(tunable.quantized_values or []) == expected
31 assert list(tunable.values or []) == expected
34def test_tunable_float_size_props() -> None:
35 """Test tunable float size properties"""
36 tunable = Tunable(
37 name="test",
38 config={
39 "type": "float",
40 "range": [1.5, 5],
41 "default": 3,
42 })
43 assert tunable.span == 3.5
44 assert tunable.cardinality == np.inf
45 assert tunable.quantized_values is None
46 assert tunable.values is None
49def test_tunable_categorical_size_props() -> None:
50 """Test tunable categorical size properties"""
51 tunable = Tunable(
52 name="test",
53 config={
54 "type": "categorical",
55 "values": ["a", "b", "c"],
56 "default": "a",
57 })
58 with pytest.raises(AssertionError):
59 _ = tunable.span
60 assert tunable.cardinality == 3
61 assert tunable.values == ["a", "b", "c"]
62 with pytest.raises(AssertionError):
63 _ = tunable.quantized_values
66def test_tunable_quantized_int_size_props() -> None:
67 """Test quantized tunable int size properties"""
68 tunable = Tunable(
69 name="test",
70 config={
71 "type": "int",
72 "range": [100, 1000],
73 "default": 100,
74 "quantization": 100
75 })
76 assert tunable.span == 900
77 assert tunable.cardinality == 10
78 expected = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
79 assert list(tunable.quantized_values or []) == expected
80 assert list(tunable.values or []) == expected
83def test_tunable_quantized_float_size_props() -> None:
84 """Test quantized tunable float size properties"""
85 tunable = Tunable(
86 name="test",
87 config={
88 "type": "float",
89 "range": [0, 1],
90 "default": 0,
91 "quantization": .1
92 })
93 assert tunable.span == 1
94 assert tunable.cardinality == 11
95 expected = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
96 assert pytest.approx(list(tunable.quantized_values or []), 0.0001) == expected
97 assert pytest.approx(list(tunable.values or []), 0.0001) == expected