Coverage for mlos_bench/mlos_bench/tests/tunables/test_tunables_size_props.py: 100%
39 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 01:18 +0000
« 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 checking tunable size properties."""
7import pytest
9from mlos_bench.tunables.tunable import Tunable
11# Note: these test do *not* check the ConfigSpace conversions for those same Tunables.
12# That is checked indirectly via grid_search_optimizer_test.py
15def test_tunable_int_size_props() -> None:
16 """Test tunable int size properties."""
17 tunable = Tunable(
18 name="test",
19 config={
20 "type": "int",
21 "range": [1, 5],
22 "default": 3,
23 },
24 )
25 expected = [1, 2, 3, 4, 5]
26 assert tunable.span == 4
27 assert tunable.cardinality == len(expected)
28 assert list(tunable.quantized_values or []) == expected
29 assert list(tunable.values or []) == expected
32def test_tunable_float_size_props() -> None:
33 """Test tunable float size properties."""
34 tunable = Tunable(
35 name="test",
36 config={
37 "type": "float",
38 "range": [1.5, 5],
39 "default": 3,
40 },
41 )
42 assert tunable.span == 3.5
43 assert tunable.cardinality is None
44 assert tunable.quantized_values is None
45 assert tunable.values is None
48def test_tunable_categorical_size_props() -> None:
49 """Test tunable categorical size properties."""
50 tunable = Tunable(
51 name="test",
52 config={
53 "type": "categorical",
54 "values": ["a", "b", "c"],
55 "default": "a",
56 },
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_bins": 10,
75 },
76 )
77 expected = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
78 assert tunable.span == 900
79 assert tunable.cardinality == len(expected)
80 assert tunable.quantization_bins == len(expected)
81 assert list(tunable.quantized_values or []) == expected
82 assert list(tunable.values or []) == expected
85def test_tunable_quantized_float_size_props() -> None:
86 """Test quantized tunable float size properties."""
87 tunable = Tunable(
88 name="test",
89 config={
90 "type": "float",
91 "range": [0, 1],
92 "default": 0,
93 "quantization_bins": 11,
94 },
95 )
96 expected = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
97 assert tunable.span == 1
98 assert tunable.cardinality == len(expected)
99 assert tunable.quantization_bins == len(expected)
100 assert pytest.approx(list(tunable.quantized_values or []), 0.0001) == expected
101 assert pytest.approx(list(tunable.values or []), 0.0001) == expected