Skip to content

Commit b0c433d

Browse files
andycylmetafacebook-github-bot
authored andcommitted
Add add_tracking_metrics method to Ax API
Summary: Add add_tracking_metrics method to Ax API. Reviewed By: mpolson64 Differential Revision: D92087975
1 parent 8812057 commit b0c433d

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

ax/api/client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,33 @@ def configure_metrics(self, metrics: Sequence[IMetric]) -> None:
280280
"""
281281
self._set_metrics(metrics=metrics)
282282

283+
def configure_tracking_metrics(self, metric_names: Sequence[str]) -> None:
284+
"""
285+
Add tracking metrics to the ``Experiment`` by name.
286+
287+
Tracking metrics are metrics that are recorded during the experiment but
288+
are not used as part of the ``OptimizationConfig`` (i.e., they are not
289+
objectives or outcome constraints). Use this method to declare metrics
290+
that you want to track alongside your optimization objectives.
291+
292+
If any of the metrics are already defined on the experiment, they will be
293+
skipped with a warning.
294+
295+
Args:
296+
metric_names: Names of metrics to be added as tracking metrics.
297+
298+
Saves to database on completion if ``storage_config`` is present.
299+
"""
300+
for metric_name in metric_names:
301+
if metric_name in self._experiment.metrics:
302+
logger.warning(
303+
f"Metric {metric_name} already exists on experiment, skipping."
304+
)
305+
continue
306+
self._experiment.add_tracking_metric(metric=Metric(name=metric_name))
307+
308+
self._save_experiment_to_db_if_possible(experiment=self._experiment)
309+
283310
# -------------------- Section 1.2: Set (not API) -------------------------------
284311
def set_experiment(self, experiment: Experiment) -> None:
285312
"""

ax/api/tests/test_client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,35 @@ def test_configure_metric(self) -> None:
421421
client._experiment.tracking_metrics[0],
422422
)
423423

424+
def test_configure_tracking_metrics(self) -> None:
425+
"""Test adding tracking metrics to an experiment."""
426+
client = Client()
427+
428+
with self.assertRaisesRegex(AssertionError, "Experiment not set"):
429+
client.configure_tracking_metrics(metric_names=["tracking1"])
430+
431+
client.configure_experiment(
432+
parameters=[
433+
RangeParameterConfig(name="x1", parameter_type="float", bounds=(0, 1))
434+
],
435+
name="test_tracking_metrics",
436+
)
437+
client.configure_optimization(objective="objective")
438+
439+
# Test adding tracking metrics
440+
client.configure_tracking_metrics(metric_names=["tracking1", "tracking2"])
441+
442+
# Verify tracking metrics were added
443+
self.assertIn("tracking1", client._experiment.metrics)
444+
self.assertIn("tracking2", client._experiment.metrics)
445+
self.assertEqual(len(client._experiment.tracking_metrics), 2)
446+
447+
# Test that adding an existing metric logs a warning and skips it
448+
with mock.patch("ax.api.client.logger.warning") as mock_logger:
449+
client.configure_tracking_metrics(metric_names=["tracking1"])
450+
mock_logger.assert_called_once()
451+
self.assertIn("already exists", mock_logger.call_args[0][0])
452+
424453
def test_set_experiment(self) -> None:
425454
client = Client()
426455
experiment = get_branin_experiment()

0 commit comments

Comments
 (0)