-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Pull Request resolved: #3363 Add analysis that summarizes metrics and how they're being used inside the experiment (i.e., tracking, optimization, constraint) Reviewed By: lena-kashtelyan Differential Revision: D69215346
- Loading branch information
1 parent
af616f0
commit 3be9dc2
Showing
5 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# 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 ax.analysis.analysis import Analysis, AnalysisCard, AnalysisCardLevel | ||
from ax.core.experiment import Experiment | ||
from ax.exceptions.core import UserInputError | ||
from ax.generation_strategy.generation_strategy import GenerationStrategy | ||
|
||
|
||
class MetricSummary(Analysis): | ||
""" | ||
Creates a dataframe with information about each metric in the | ||
experiment. The resulting dataframe has one row per metric, and the | ||
following columns: | ||
- Name: the name of the metric. | ||
- Type: the metric subclass (e.g., Metric, BraninMetric). | ||
- Goal: the goal for this for this metric, based on the optimization | ||
config (minimize, maximize, constraint or track). | ||
- Bound: the bound of this metric (e.g., "<=10.0") if it is being used | ||
as part of an ObjectiveThreshold or OutcomeConstraint. | ||
- Lower is Better: whether the user prefers this metric to be lower, | ||
if provided. | ||
""" | ||
|
||
def compute( | ||
self, | ||
experiment: Experiment | None = None, | ||
generation_strategy: GenerationStrategy | None = None, | ||
) -> AnalysisCard: | ||
if experiment is None: | ||
raise UserInputError( | ||
"`MetricSummary` analysis requires an `Experiment` input" | ||
) | ||
return self._create_analysis_card( | ||
title=f"MetricSummary for `{experiment.name}`", | ||
subtitle="High-level summary of the `Metric`-s in this `Experiment`", | ||
level=AnalysisCardLevel.MID, | ||
df=experiment.metric_config_summary_df, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# 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 | ||
|
||
import pandas as pd | ||
from ax.analysis.analysis import AnalysisCardLevel | ||
from ax.analysis.metric_summary import MetricSummary | ||
from ax.core.metric import Metric | ||
from ax.exceptions.core import UserInputError | ||
from ax.preview.api.client import Client | ||
from ax.preview.api.configs import ExperimentConfig | ||
from ax.utils.common.testutils import TestCase | ||
|
||
|
||
class TestMetricSummary(TestCase): | ||
def test_compute(self) -> None: | ||
client = Client() | ||
client.configure_experiment( | ||
experiment_config=ExperimentConfig( | ||
name="test_experiment", | ||
parameters=[], | ||
) | ||
) | ||
client.configure_optimization( | ||
objective="foo, bar", outcome_constraints=["baz <= 0.0", "foo >= 1.0"] | ||
) | ||
# TODO: Debug error raised by | ||
# client.configure_metrics(metrics=[IMetric(name="qux")]) | ||
|
||
client._experiment._tracking_metrics = {"qux": Metric(name="qux")} | ||
|
||
analysis = MetricSummary() | ||
|
||
with self.assertRaisesRegex(UserInputError, "requires an `Experiment`"): | ||
analysis.compute() | ||
|
||
experiment = client._experiment | ||
card = analysis.compute(experiment=experiment) | ||
|
||
# Test metadata | ||
self.assertEqual(card.name, "MetricSummary") | ||
self.assertEqual(card.title, "MetricSummary for `test_experiment`") | ||
self.assertEqual( | ||
card.subtitle, | ||
"High-level summary of the `Metric`-s in this `Experiment`", | ||
) | ||
self.assertEqual(card.level, AnalysisCardLevel.MID) | ||
self.assertIsNotNone(card.blob) | ||
self.assertEqual(card.blob_annotation, "dataframe") | ||
|
||
# Test dataframe for accuracy | ||
self.assertEqual( | ||
{*card.df.columns}, | ||
{ | ||
"Goal", | ||
"Name", | ||
"Type", | ||
"Lower is Better", | ||
"Bound", | ||
}, | ||
) | ||
expected = pd.DataFrame( | ||
{ | ||
"Name": ["bar", "foo", "baz", "qux"], | ||
"Type": ["MapMetric", "MapMetric", "MapMetric", "Metric"], | ||
"Goal": pd.Series( | ||
["maximize", "maximize", "constrain", "track"], | ||
dtype=pd.CategoricalDtype( | ||
categories=[ | ||
"minimize", | ||
"maximize", | ||
"constrain", | ||
"track", | ||
"None", | ||
], | ||
ordered=True, | ||
), | ||
), | ||
"Bound": ["None", ">= 1.0", "<= 0.0", "None"], | ||
"Lower is Better": ["None", "None", "None", "None"], | ||
} | ||
) | ||
pd.testing.assert_frame_equal(card.df, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters