Skip to content

Commit 0644bbb

Browse files
sdaultonmeta-codesync[bot]
authored andcommitted
Create new target in ax/adapter for parameter utils to limit deps (#5149)
Summary: Pull Request resolved: #5149 Reviewed By: Balandat Differential Revision: D99688239 fbshipit-source-id: d30e8eee37e66840369aee8bbb471cb62902b662
1 parent fdaf7e9 commit 0644bbb

2 files changed

Lines changed: 70 additions & 54 deletions

File tree

ax/adapter/adapter_utils.py

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
import numpy as np
1818
import numpy.typing as npt
1919
import torch
20+
from ax.adapter.parameter_utils import ( # noqa: F401
21+
can_map_to_binary,
22+
is_unordered_choice,
23+
)
2024
from ax.adapter.transforms.base import Transform
2125
from ax.adapter.transforms.utils import (
2226
derelativize_optimization_config_with_raw_status_quo,
@@ -35,7 +39,7 @@
3539
OutcomeConstraint,
3640
ScalarizedOutcomeConstraint,
3741
)
38-
from ax.core.parameter import ChoiceParameter, Parameter, ParameterType, RangeParameter
42+
from ax.core.parameter import ChoiceParameter, ParameterType, RangeParameter
3943
from ax.core.parameter_constraint import ParameterConstraint
4044
from ax.core.search_space import SearchSpace, SearchSpaceDigest
4145
from ax.core.types import TBounds, TCandidateMetadata, TNumeric
@@ -1438,56 +1442,3 @@ def _consolidate_comparisons(X: Tensor, Y: Tensor) -> tuple[Tensor, Tensor]:
14381442

14391443
X, Y, _ = consolidate_duplicates(X, Y)
14401444
return X, Y
1441-
1442-
1443-
def is_unordered_choice(
1444-
p: Parameter, min_choices: int | None = None, max_choices: int | None = None
1445-
) -> bool:
1446-
"""Returns whether a parameter is an unordered choice (categorical) parameter.
1447-
1448-
You can also specify `min_choices` and `max_choices` to restrict how many
1449-
possible values the parameter can take on.
1450-
1451-
Args:
1452-
p: Parameter.
1453-
min_choices: The minimum number of possible values for the parameter.
1454-
max_choices: The maximum number of possible values for the parameter.
1455-
1456-
Returns:
1457-
A boolean indicating whether p is an unordered choice parameter or not.
1458-
"""
1459-
if min_choices is not None and min_choices < 0:
1460-
raise UserInputError("`min_choices` must be a non-negative integer.")
1461-
if max_choices is not None and max_choices < 0:
1462-
raise UserInputError("`max_choices` must be a non-negative integer.")
1463-
if (
1464-
min_choices is not None
1465-
and max_choices is not None
1466-
and min_choices > max_choices
1467-
):
1468-
raise UserInputError("`min_choices` cannot be larger than `max_choices`.")
1469-
return (
1470-
isinstance(p, ChoiceParameter)
1471-
and not p.is_ordered
1472-
and (min_choices is None or min_choices <= len(p.values))
1473-
and (max_choices is None or max_choices >= len(p.values))
1474-
)
1475-
1476-
1477-
def can_map_to_binary(p: Parameter) -> bool:
1478-
"""Returns whether a parameter can be transformed to a binary parameter.
1479-
1480-
Any choice/range parameters with exactly two values can be transformed to a
1481-
binary parameter.
1482-
1483-
Args:
1484-
p: Parameter.
1485-
1486-
Returns
1487-
A boolean indicating whether p can be transformed to a binary parameter.
1488-
"""
1489-
return (isinstance(p, ChoiceParameter) and len(p.values) == 2) or (
1490-
isinstance(p, RangeParameter)
1491-
and p.parameter_type == ParameterType.INT
1492-
and p.lower == p.upper - 1
1493-
)

ax/adapter/parameter_utils.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
#
4+
# This source code is licensed under the MIT license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# pyre-strict
8+
9+
from __future__ import annotations
10+
11+
from ax.core.parameter import ChoiceParameter, Parameter, ParameterType, RangeParameter
12+
from ax.exceptions.core import UserInputError
13+
14+
15+
def is_unordered_choice(
16+
p: Parameter, min_choices: int | None = None, max_choices: int | None = None
17+
) -> bool:
18+
"""Returns whether a parameter is an unordered choice (categorical) parameter.
19+
20+
You can also specify `min_choices` and `max_choices` to restrict how many
21+
possible values the parameter can take on.
22+
23+
Args:
24+
p: Parameter.
25+
min_choices: The minimum number of possible values for the parameter.
26+
max_choices: The maximum number of possible values for the parameter.
27+
28+
Returns:
29+
A boolean indicating whether p is an unordered choice parameter or not.
30+
"""
31+
if min_choices is not None and min_choices < 0:
32+
raise UserInputError("`min_choices` must be a non-negative integer.")
33+
if max_choices is not None and max_choices < 0:
34+
raise UserInputError("`max_choices` must be a non-negative integer.")
35+
if (
36+
min_choices is not None
37+
and max_choices is not None
38+
and min_choices > max_choices
39+
):
40+
raise UserInputError("`min_choices` cannot be larger than `max_choices`.")
41+
return (
42+
isinstance(p, ChoiceParameter)
43+
and not p.is_ordered
44+
and (min_choices is None or min_choices <= len(p.values))
45+
and (max_choices is None or max_choices >= len(p.values))
46+
)
47+
48+
49+
def can_map_to_binary(p: Parameter) -> bool:
50+
"""Returns whether a parameter can be transformed to a binary parameter.
51+
52+
Any choice/range parameters with exactly two values can be transformed to a
53+
binary parameter.
54+
55+
Args:
56+
p: Parameter.
57+
58+
Returns
59+
A boolean indicating whether p can be transformed to a binary parameter.
60+
"""
61+
return (isinstance(p, ChoiceParameter) and len(p.values) == 2) or (
62+
isinstance(p, RangeParameter)
63+
and p.parameter_type == ParameterType.INT
64+
and p.lower == p.upper - 1
65+
)

0 commit comments

Comments
 (0)