forked from facebook/Ax
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_prediction_utils.py
More file actions
163 lines (139 loc) · 5.98 KB
/
test_prediction_utils.py
File metadata and controls
163 lines (139 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# pyre-strict
from unittest import mock
import numpy as np
from ax.adapter.prediction_utils import predict_at_point, predict_by_features
from ax.core.observation import ObservationFeatures
from ax.core.types import TEvaluationOutcome, TParameterization
from ax.service.ax_client import AxClient
from ax.service.utils.instantiation import ObjectiveProperties
from ax.utils.common.testutils import TestCase
from pyre_extensions import none_throws
class TestPredictionUtils(TestCase):
"""Tests prediction utilities."""
def test_predict_at_point(self) -> None:
ax_client = _set_up_client_for_get_model_predictions_no_next_trial()
_attach_completed_trials(ax_client)
ax_client.get_model_predictions() # Ensures model is instantiated
observation_features = ObservationFeatures(parameters={"x1": 0.3, "x2": 0.5})
y_hat, se_hat = predict_at_point(
model=none_throws(ax_client.generation_strategy.adapter),
obsf=observation_features,
metric_names={"test_metric1"},
)
self.assertEqual(len(y_hat), 1)
self.assertEqual(len(se_hat), 1)
y_hat, se_hat = predict_at_point(
model=none_throws(ax_client.generation_strategy.adapter),
obsf=observation_features,
metric_names={"test_metric1", "test_metric2", "test_metric:agg"},
scalarized_metric_config=[
{
"name": "test_metric:agg",
"weight": {"test_metric1": 0.5, "test_metric2": 0.5},
},
],
)
self.assertEqual(len(y_hat), 3)
self.assertEqual(len(se_hat), 3)
y_hat, se_hat = predict_at_point(
model=none_throws(ax_client.generation_strategy.adapter),
obsf=observation_features,
metric_names={"test_metric1"},
scalarized_metric_config=[
{
"name": "test_metric:agg",
"weight": {"test_metric1": 0.5, "test_metric2": 0.5},
},
],
)
self.assertEqual(len(y_hat), 1)
self.assertEqual(len(se_hat), 1)
def test_predict_by_features(self) -> None:
ax_client = _set_up_client_for_get_model_predictions_no_next_trial()
_attach_completed_trials(ax_client)
ax_client.get_model_predictions() # Ensures model is instantiated
observation_features_dict = {
18: ObservationFeatures(parameters={"x1": 0.3, "x2": 0.5}),
19: ObservationFeatures(parameters={"x1": 0.4, "x2": 0.5}),
20: ObservationFeatures(parameters={"x1": 0.8, "x2": 0.5}),
}
predictions_map = predict_by_features(
model=none_throws(ax_client.generation_strategy.adapter),
label_to_feature_dict=observation_features_dict,
metric_names={"test_metric1"},
)
self.assertEqual(len(predictions_map), 3)
@mock.patch("ax.adapter.random.RandomAdapter.predict")
@mock.patch("ax.adapter.random.RandomAdapter")
def test_predict_by_features_with_non_predicting_model(
self,
adapter_mock: mock.MagicMock,
predict_mock: mock.MagicMock,
) -> None:
ax_client = _set_up_client_for_get_model_predictions_no_next_trial()
_attach_completed_trials(ax_client)
# Do not call get_next_trial or get_model_predictions.
# This test is for handling the use case where no model
# is instantiated.
observation_features_dict = {
18: ObservationFeatures(parameters={"x1": 0.3, "x2": 0.5}),
19: ObservationFeatures(parameters={"x1": 0.4, "x2": 0.5}),
20: ObservationFeatures(parameters={"x1": 0.8, "x2": 0.5}),
}
predict_mock.side_effect = NotImplementedError()
self.assertRaises(
NotImplementedError,
predict_by_features,
**{
"model": adapter_mock,
"label_to_feature_dict": observation_features_dict,
"metric_names": ["test_metric1"],
},
)
# Utility functions for testing get_model_predictions without calling
# get_next_trial. Create Ax Client with an experiment where
# num_initial_trials kwarg is zero. Note that this kwarg is
# needed to be able to instantiate the model for the first time
# without calling get_next_trial().
def _set_up_client_for_get_model_predictions_no_next_trial() -> AxClient:
ax_client = AxClient()
ax_client.create_experiment(
name="test_experiment",
choose_generation_strategy_kwargs={"num_initialization_trials": 0},
parameters=[
{
"name": "x1",
"type": "range",
"bounds": [0.0, 1.0],
},
{
"name": "x2",
"type": "range",
"bounds": [0.1, 1.0],
},
],
objectives={"test_metric1": ObjectiveProperties(minimize=False)},
outcome_constraints=["test_metric2 <= 1.5"],
)
return ax_client
def _attach_completed_trials(ax_client: AxClient) -> None:
# Attach completed trials
trial1: TParameterization = {"x1": 0.1, "x2": 0.1}
parameters, trial_index = ax_client.attach_trial(trial1)
ax_client.complete_trial(
trial_index=trial_index, raw_data=_evaluate_test_metrics(parameters)
)
trial2: TParameterization = {"x1": 0.2, "x2": 0.1}
parameters, trial_index = ax_client.attach_trial(trial2)
ax_client.complete_trial(
trial_index=trial_index, raw_data=_evaluate_test_metrics(parameters)
)
# Test metric evaluation method
def _evaluate_test_metrics(parameters: TParameterization) -> TEvaluationOutcome:
x = np.array([parameters.get(f"x{i + 1}") for i in range(2)])
return {"test_metric1": (x[0] / x[1], 0.0), "test_metric2": (x[0] + x[1], 0.0)}