From 39ca7fe6f70719a778706bb595581243c1670621 Mon Sep 17 00:00:00 2001 From: Sait Cakmak Date: Tue, 29 Oct 2024 08:14:55 -0700 Subject: [PATCH] Rename `fast_optimize` to `mock_optimize` Summary: Updates the name to clarify that this is just a mock and does not magically speed up optimization without consequences. Differential Revision: D65146794 --- botorch/test_utils/__init__.py | 4 ++-- botorch/test_utils/mock.py | 16 ++++++++-------- test/acquisition/test_input_constructors.py | 4 ++-- test/test_utils/test_mock.py | 20 ++++++++++---------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/botorch/test_utils/__init__.py b/botorch/test_utils/__init__.py index 3a89f80f66..aa035bf7eb 100644 --- a/botorch/test_utils/__init__.py +++ b/botorch/test_utils/__init__.py @@ -11,6 +11,6 @@ 'botorch/'. """ -from botorch.test_utils.mock import fast_optimize +from botorch.test_utils.mock import mock_optimize -__all__ = ["fast_optimize"] +__all__ = ["mock_optimize"] diff --git a/botorch/test_utils/mock.py b/botorch/test_utils/mock.py index f39e0a8ed3..5bd8e5a0ec 100644 --- a/botorch/test_utils/mock.py +++ b/botorch/test_utils/mock.py @@ -27,12 +27,12 @@ @contextmanager -def fast_optimize_context_manager( +def mock_optimize_context_manager( force: bool = False, ) -> Generator[None, None, None]: - """A context manager to force botorch to speed up optimization. Currently, the - primary tactic is to force the underlying scipy methods to stop after just one - iteration. + """A context manager that uses mocks to speed up optimization for testing. + Currently, the primary tactic is to force the underlying scipy methods to stop + after just one iteration. force: If True will not raise an AssertionError if no mocks are called. USE RESPONSIBLY. @@ -118,17 +118,17 @@ def minimal_gen_os_ics(*args: Any, **kwargs: Any) -> Tensor | None: ): raise AssertionError( "No mocks were called in the context manager. Please remove unused " - "fast_optimize_context_manager()." + "mock_optimize_context_manager()." ) -def fast_optimize(f: Callable) -> Callable: - """Wraps f in the fast_botorch_optimize_context_manager for use as a decorator.""" +def mock_optimize(f: Callable) -> Callable: + """Wraps `f` in `mock_optimize_context_manager` for use as a decorator.""" @wraps(f) # pyre-fixme[3]: Return type must be annotated. def inner(*args: Any, **kwargs: Any): - with fast_optimize_context_manager(): + with mock_optimize_context_manager(): return f(*args, **kwargs) return inner diff --git a/test/acquisition/test_input_constructors.py b/test/acquisition/test_input_constructors.py index 90dc93bc65..53120d1325 100644 --- a/test/acquisition/test_input_constructors.py +++ b/test/acquisition/test_input_constructors.py @@ -109,7 +109,7 @@ from botorch.models.deterministic import FixedSingleSampleModel from botorch.models.model_list_gp_regression import ModelListGP from botorch.sampling.normal import IIDNormalSampler, SobolQMCNormalSampler -from botorch.test_utils.mock import fast_optimize +from botorch.test_utils.mock import mock_optimize from botorch.utils.constraints import get_outcome_constraint_transforms from botorch.utils.datasets import SupervisedDataset from botorch.utils.multi_objective.box_decompositions.non_dominated import ( @@ -1841,7 +1841,7 @@ def setUp(self, suppress_input_warnings: bool = True) -> None: }, ) - @fast_optimize + @mock_optimize def test_constructors_can_instantiate(self) -> None: for key, (classes, input_constructor_kwargs) in self.cases.items(): with self.subTest( diff --git a/test/test_utils/test_mock.py b/test/test_utils/test_mock.py index 376839985c..8864ab856d 100644 --- a/test/test_utils/test_mock.py +++ b/test/test_utils/test_mock.py @@ -23,7 +23,7 @@ get_nearest_neighbors, optimize_acqf_mixed_alternating, ) -from botorch.test_utils.mock import fast_optimize, fast_optimize_context_manager +from botorch.test_utils.mock import mock_optimize, mock_optimize_context_manager from botorch.utils.testing import BotorchTestCase, MockAcquisitionFunction @@ -38,14 +38,14 @@ def __call__(self, X): class TestMock(BotorchTestCase): - def test_fast_optimize_context_manager(self) -> None: + def test_mock_optimize_context_manager(self) -> None: with self.subTest("gen_candidates_scipy"): - with fast_optimize_context_manager(): + with mock_optimize_context_manager(): cand, value = gen_candidates_scipy( initial_conditions=torch.tensor([[0.0]]), acquisition_function=SinAcqusitionFunction(), ) - # When not using `fast_optimize`, the value is 1.0. With it, the value is + # When not using `mock_optimize`, the value is 1.0. With it, the value is # around 0.84 self.assertLess(value.item(), 0.99) @@ -55,14 +55,14 @@ def test_fast_optimize_context_manager(self) -> None: def closure(): return torch.sin(x), [torch.cos(x)] - with fast_optimize_context_manager(): + with mock_optimize_context_manager(): result = scipy_minimize(closure=closure, parameters={"x": x}) self.assertEqual( result.message, "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT" ) with self.subTest("optimize_acqf"): - with fast_optimize_context_manager(): + with mock_optimize_context_manager(): cand, value = optimize_acqf( acq_function=SinAcqusitionFunction(), bounds=torch.tensor([[-2.0], [2.0]]), @@ -73,7 +73,7 @@ def closure(): self.assertLess(value.item(), 0.99) with self.subTest("gen_batch_initial_conditions"): - with fast_optimize_context_manager(), patch( + with mock_optimize_context_manager(), patch( "botorch.optim.initializers.initialize_q_batch", wraps=initialize_q_batch, ) as mock_init_q_batch: @@ -86,7 +86,7 @@ def closure(): ) self.assertEqual(mock_init_q_batch.call_args[1]["n"], 2) - def test_fast_optimize_mixed_alternating(self) -> None: + def test_mock_optimize_mixed_alternating(self) -> None: with mock.patch( "botorch.optim.optimize_acqf_mixed.discrete_step", wraps=discrete_step, @@ -114,7 +114,7 @@ def test_fast_optimize_mixed_alternating(self) -> None: # `mock_discrete`, which should total to 1. self.assertLessEqual(mock_neighbors.call_count, 4) - @fast_optimize + @mock_optimize def test_decorator(self) -> None: model = SingleTaskGP( train_X=torch.tensor([[0.0]], dtype=torch.double), @@ -141,5 +141,5 @@ def test_decorator(self) -> None: def test_raises_when_unused(self) -> None: with self.assertRaisesRegex(AssertionError, "No mocks were called"): - with fast_optimize_context_manager(): + with mock_optimize_context_manager(): pass