|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the MIT license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +# pyre-strict |
| 7 | + |
| 8 | +from unittest.mock import patch |
| 9 | + |
| 10 | +from ax.analysis.healthcheck.healthcheck_analysis import HealthcheckStatus |
| 11 | +from ax.analysis.healthcheck.transfer_learning_analysis import TransferLearningAnalysis |
| 12 | +from ax.core.auxiliary import TransferLearningMetadata |
| 13 | +from ax.core.experiment import Experiment |
| 14 | +from ax.core.parameter import ParameterType, RangeParameter |
| 15 | +from ax.core.search_space import SearchSpace |
| 16 | +from ax.exceptions.core import UserInputError |
| 17 | +from ax.utils.common.testutils import TestCase |
| 18 | + |
| 19 | + |
| 20 | +def _make_experiment( |
| 21 | + param_names: list[str], |
| 22 | + experiment_type: str | None = None, |
| 23 | +) -> Experiment: |
| 24 | + """Create a simple experiment with the given parameter names.""" |
| 25 | + return Experiment( |
| 26 | + search_space=SearchSpace( |
| 27 | + parameters=[ |
| 28 | + RangeParameter( |
| 29 | + name=name, |
| 30 | + parameter_type=ParameterType.FLOAT, |
| 31 | + lower=0.0, |
| 32 | + upper=1.0, |
| 33 | + ) |
| 34 | + for name in param_names |
| 35 | + ] |
| 36 | + ), |
| 37 | + name="test_experiment", |
| 38 | + experiment_type=experiment_type, |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +_MOCK_TARGET = "ax.storage.sqa_store.load.identify_transferable_experiments" |
| 43 | + |
| 44 | + |
| 45 | +class TestTransferLearningAnalysis(TestCase): |
| 46 | + def test_no_experiment_type_returns_pass(self) -> None: |
| 47 | + """When no experiment_type is set and no experiment_types provided, |
| 48 | + return PASS.""" |
| 49 | + experiment = _make_experiment(["x1", "x2"], experiment_type=None) |
| 50 | + analysis = TransferLearningAnalysis() |
| 51 | + card = analysis.compute(experiment=experiment) |
| 52 | + self.assertEqual(card.get_status(), HealthcheckStatus.PASS) |
| 53 | + self.assertTrue(card.is_passing()) |
| 54 | + self.assertIn("No experiment type set", card.subtitle) |
| 55 | + |
| 56 | + @patch(_MOCK_TARGET, return_value={}) |
| 57 | + def test_no_candidates_returns_pass(self, mock_identify: object) -> None: |
| 58 | + experiment = _make_experiment(["x1", "x2"], experiment_type="my_type") |
| 59 | + analysis = TransferLearningAnalysis() |
| 60 | + card = analysis.compute(experiment=experiment) |
| 61 | + self.assertEqual(card.get_status(), HealthcheckStatus.PASS) |
| 62 | + self.assertTrue(card.is_passing()) |
| 63 | + self.assertTrue(card.df.empty) |
| 64 | + |
| 65 | + @patch(_MOCK_TARGET) |
| 66 | + def test_single_candidate_returns_warning(self, mock_identify: object) -> None: |
| 67 | + experiment = _make_experiment( |
| 68 | + ["x1", "x2", "x3", "x4", "x5"], experiment_type="my_type" |
| 69 | + ) |
| 70 | + mock_identify.return_value = { # pyre-ignore[16] |
| 71 | + "source_exp": TransferLearningMetadata( |
| 72 | + overlap_parameters=["x1", "x2", "x3", "x4"], |
| 73 | + ), |
| 74 | + } |
| 75 | + analysis = TransferLearningAnalysis() |
| 76 | + card = analysis.compute(experiment=experiment) |
| 77 | + self.assertEqual(card.get_status(), HealthcheckStatus.WARNING) |
| 78 | + self.assertFalse(card.is_passing()) |
| 79 | + self.assertIn("source_exp", card.subtitle) |
| 80 | + self.assertIn("80.0%", card.subtitle) |
| 81 | + self.assertEqual(len(card.df), 1) |
| 82 | + self.assertEqual(card.df.iloc[0]["Experiment"], "source_exp") |
| 83 | + self.assertEqual(card.df.iloc[0]["Overlapping Parameters"], 4) |
| 84 | + self.assertEqual(card.df.iloc[0]["Overlap (%)"], 80.0) |
| 85 | + |
| 86 | + @patch(_MOCK_TARGET) |
| 87 | + def test_multiple_candidates_preserves_order(self, mock_identify: object) -> None: |
| 88 | + """Results should preserve the order from identify_transferable_experiments |
| 89 | + (sorted by overlap then recency).""" |
| 90 | + experiment = _make_experiment( |
| 91 | + ["x1", "x2", "x3", "x4", "x5"], experiment_type="my_type" |
| 92 | + ) |
| 93 | + # Mock returns already-sorted results (as identify_transferable_experiments |
| 94 | + # now handles sorting by overlap desc, then recency desc). |
| 95 | + mock_identify.return_value = { # pyre-ignore[16] |
| 96 | + "exp_high": TransferLearningMetadata( |
| 97 | + overlap_parameters=["x1", "x2", "x3", "x4"], |
| 98 | + ), |
| 99 | + "exp_mid": TransferLearningMetadata( |
| 100 | + overlap_parameters=["x1", "x2", "x3"], |
| 101 | + ), |
| 102 | + "exp_low": TransferLearningMetadata( |
| 103 | + overlap_parameters=["x1"], |
| 104 | + ), |
| 105 | + } |
| 106 | + analysis = TransferLearningAnalysis() |
| 107 | + card = analysis.compute(experiment=experiment) |
| 108 | + self.assertEqual(card.get_status(), HealthcheckStatus.WARNING) |
| 109 | + |
| 110 | + # Verify order is preserved from identify_transferable_experiments |
| 111 | + self.assertEqual(card.df.iloc[0]["Experiment"], "exp_high") |
| 112 | + self.assertEqual(card.df.iloc[0]["Overlapping Parameters"], 4) |
| 113 | + self.assertEqual(card.df.iloc[1]["Experiment"], "exp_mid") |
| 114 | + self.assertEqual(card.df.iloc[1]["Overlapping Parameters"], 3) |
| 115 | + self.assertEqual(card.df.iloc[2]["Experiment"], "exp_low") |
| 116 | + self.assertEqual(card.df.iloc[2]["Overlapping Parameters"], 1) |
| 117 | + |
| 118 | + # All experiments listed in subtitle |
| 119 | + self.assertIn("exp_high", card.subtitle) |
| 120 | + self.assertIn("exp_mid", card.subtitle) |
| 121 | + self.assertIn("exp_low", card.subtitle) |
| 122 | + self.assertIn("We found **3 eligible source experiment(s)**", card.subtitle) |
| 123 | + |
| 124 | + @patch(_MOCK_TARGET) |
| 125 | + def test_percentage_calculation(self, mock_identify: object) -> None: |
| 126 | + experiment = _make_experiment(["x1", "x2", "x3"], experiment_type="my_type") |
| 127 | + mock_identify.return_value = { # pyre-ignore[16] |
| 128 | + "exp_a": TransferLearningMetadata( |
| 129 | + overlap_parameters=["x1"], |
| 130 | + ), |
| 131 | + } |
| 132 | + analysis = TransferLearningAnalysis() |
| 133 | + card = analysis.compute(experiment=experiment) |
| 134 | + self.assertEqual(card.df.iloc[0]["Overlap (%)"], 33.3) |
| 135 | + |
| 136 | + @patch(_MOCK_TARGET) |
| 137 | + def test_parameters_listed_alphabetically(self, mock_identify: object) -> None: |
| 138 | + experiment = _make_experiment( |
| 139 | + ["alpha", "beta", "gamma", "delta"], experiment_type="my_type" |
| 140 | + ) |
| 141 | + mock_identify.return_value = { # pyre-ignore[16] |
| 142 | + "exp_a": TransferLearningMetadata( |
| 143 | + overlap_parameters=["gamma", "alpha", "delta"], |
| 144 | + ), |
| 145 | + } |
| 146 | + analysis = TransferLearningAnalysis() |
| 147 | + card = analysis.compute(experiment=experiment) |
| 148 | + self.assertEqual(card.df.iloc[0]["Parameters"], "alpha, delta, gamma") |
| 149 | + |
| 150 | + def test_requires_experiment(self) -> None: |
| 151 | + analysis = TransferLearningAnalysis() |
| 152 | + with self.assertRaises(UserInputError): |
| 153 | + analysis.compute(experiment=None) |
| 154 | + |
| 155 | + @patch(_MOCK_TARGET, return_value={}) |
| 156 | + def test_experiment_name_passed_to_identify(self, mock_identify: object) -> None: |
| 157 | + """Verify that experiment.name is forwarded to |
| 158 | + identify_transferable_experiments so it can filter the target out.""" |
| 159 | + experiment = _make_experiment(["x1", "x2", "x3"], experiment_type="my_type") |
| 160 | + analysis = TransferLearningAnalysis() |
| 161 | + analysis.compute(experiment=experiment) |
| 162 | + mock_identify.assert_called_once() # pyre-ignore[16] |
| 163 | + call_kwargs = mock_identify.call_args.kwargs # pyre-ignore[16] |
| 164 | + self.assertEqual(call_kwargs["experiment_name"], "test_experiment") |
0 commit comments