|
| 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 ax.core import Experiment |
| 10 | +from ax.core.experiment import ExperimentDesign |
| 11 | +from ax.utils.common.testutils import TestCase |
| 12 | +from ax.utils.testing.core_stubs import get_branin_search_space |
| 13 | + |
| 14 | + |
| 15 | +class ExperimentDesignTest(TestCase): |
| 16 | + """Tests for ExperimentDesign and related logic.""" |
| 17 | + |
| 18 | + def test_experiment_design_defaults(self) -> None: |
| 19 | + """Test that ExperimentDesign has expected defaults.""" |
| 20 | + design = ExperimentDesign() |
| 21 | + self.assertIsNone(design.concurrency_limit) |
| 22 | + |
| 23 | + def test_experiment_design_with_concurrency_limit(self) -> None: |
| 24 | + """Test ExperimentDesign with concurrency_limit set.""" |
| 25 | + design = ExperimentDesign(concurrency_limit=10) |
| 26 | + self.assertEqual(design.concurrency_limit, 10) |
| 27 | + |
| 28 | + design_zero = ExperimentDesign(concurrency_limit=0) |
| 29 | + self.assertEqual(design_zero.concurrency_limit, 0) |
| 30 | + |
| 31 | + def test_experiment_design_property(self) -> None: |
| 32 | + """Test that Experiment.design property returns ExperimentDesign instance.""" |
| 33 | + experiment = Experiment( |
| 34 | + name="test", |
| 35 | + search_space=get_branin_search_space(), |
| 36 | + ) |
| 37 | + self.assertIsInstance(experiment.design, ExperimentDesign) |
| 38 | + self.assertIsNone(experiment.design.concurrency_limit) |
| 39 | + |
| 40 | + def test_experiment_design_modification(self) -> None: |
| 41 | + """Test that ExperimentDesign can be modified after experiment creation.""" |
| 42 | + experiment = Experiment( |
| 43 | + name="test", |
| 44 | + search_space=get_branin_search_space(), |
| 45 | + ) |
| 46 | + # Modify concurrency_limit |
| 47 | + experiment.design.concurrency_limit = 5 |
| 48 | + self.assertEqual(experiment.design.concurrency_limit, 5) |
| 49 | + |
| 50 | + # Set to None |
| 51 | + experiment.design.concurrency_limit = None |
| 52 | + self.assertIsNone(experiment.design.concurrency_limit) |
| 53 | + |
| 54 | + def test_experiment_design_restore_from_properties(self) -> None: |
| 55 | + """Test that ExperimentDesign is restored from properties during init.""" |
| 56 | + # Simulate deserializing an experiment with design stored in properties |
| 57 | + experiment = Experiment( |
| 58 | + name="test", |
| 59 | + search_space=get_branin_search_space(), |
| 60 | + properties={"design": {"concurrency_limit": 25}}, |
| 61 | + ) |
| 62 | + # The design should be restored and the "design" key should be removed |
| 63 | + # from properties |
| 64 | + self.assertEqual(experiment.design.concurrency_limit, 25) |
| 65 | + self.assertNotIn("design", experiment._properties) |
| 66 | + |
| 67 | + def test_experiment_design_restore_from_properties_with_none(self) -> None: |
| 68 | + """Test that ExperimentDesign handles None concurrency_limit in properties.""" |
| 69 | + experiment = Experiment( |
| 70 | + name="test", |
| 71 | + search_space=get_branin_search_space(), |
| 72 | + properties={"design": {"concurrency_limit": None}}, |
| 73 | + ) |
| 74 | + self.assertIsNone(experiment.design.concurrency_limit) |
| 75 | + # The "design" key should be removed from properties once it's consumed |
| 76 | + # to recreate `ExperimentDesign`. |
| 77 | + self.assertNotIn("design", experiment._properties) |
| 78 | + |
| 79 | + def test_experiment_design_restore_from_properties_empty_dict(self) -> None: |
| 80 | + """Test that ExperimentDesign handles empty design dict in properties.""" |
| 81 | + experiment = Experiment( |
| 82 | + name="test", |
| 83 | + search_space=get_branin_search_space(), |
| 84 | + properties={"design": {}}, |
| 85 | + ) |
| 86 | + self.assertIsNone(experiment.design.concurrency_limit) |
| 87 | + # The "design" key should be removed from properties once it's consumed |
| 88 | + # to recreate `ExperimentDesign`. |
| 89 | + self.assertNotIn("design", experiment._properties) |
| 90 | + |
| 91 | + def test_experiment_design_not_affecting_other_properties(self) -> None: |
| 92 | + """Test that ExperimentDesign restoration doesn't affect other properties.""" |
| 93 | + experiment = Experiment( |
| 94 | + name="test", |
| 95 | + search_space=get_branin_search_space(), |
| 96 | + properties={ |
| 97 | + "design": {"concurrency_limit": 15}, |
| 98 | + "custom_property": "custom_value", |
| 99 | + "another_property": 42, |
| 100 | + }, |
| 101 | + ) |
| 102 | + self.assertEqual(experiment.design.concurrency_limit, 15) |
| 103 | + # The "design" key should be removed from properties once it's consumed |
| 104 | + # to recreate `ExperimentDesign`. |
| 105 | + self.assertNotIn("design", experiment._properties) |
| 106 | + self.assertEqual(experiment._properties["custom_property"], "custom_value") |
| 107 | + self.assertEqual(experiment._properties["another_property"], 42) |
0 commit comments