From f24100e08be53365d042125457e8ebe44305370c Mon Sep 17 00:00:00 2001 From: Janos Gabler Date: Thu, 18 Jul 2024 10:16:54 +0200 Subject: [PATCH] Add error handling for non-supported and not-yet supported scipy arguments. --- src/optimagic/optimization/optimize.py | 84 +++++++++++++++++++ .../optimization/test_scipy_aliases.py | 55 ++++++++++++ 2 files changed, 139 insertions(+) diff --git a/src/optimagic/optimization/optimize.py b/src/optimagic/optimization/optimize.py index d2ba64662..2cc45bc5c 100644 --- a/src/optimagic/optimization/optimize.py +++ b/src/optimagic/optimization/optimize.py @@ -68,6 +68,13 @@ def maximize( # scipy aliases x0=None, method=None, + # scipy arguments that are not yet supported + hess=None, + hessp=None, + callback=None, + # scipy arguments that will never be supported + options=None, + tol=None, # deprecated arguments criterion=None, criterion_kwargs=None, @@ -107,6 +114,13 @@ def maximize( # scipy aliases x0=x0, method=method, + # scipy arguments that are not yet supported + hess=hess, + hessp=hessp, + callback=callback, + # scipy arguments that will never be supported + options=options, + tol=tol, # deprecated arguments criterion=criterion, criterion_kwargs=criterion_kwargs, @@ -147,6 +161,13 @@ def minimize( # scipy aliases x0=None, method=None, + # scipy arguments that are not yet supported + hess=None, + hessp=None, + callback=None, + # scipy arguments that will never be supported + options=None, + tol=None, # deprecated arguments criterion=None, criterion_kwargs=None, @@ -187,6 +208,13 @@ def minimize( # scipy aliases x0=x0, method=method, + # scipy arguments that are not yet supported + hess=hess, + hessp=hessp, + callback=callback, + # scipy arguments that will never be supported + options=options, + tol=tol, # deprecated arguments criterion=criterion, criterion_kwargs=criterion_kwargs, @@ -228,6 +256,13 @@ def _optimize( # scipy aliases x0, method, + # scipy arguments that are not yet supported + hess, + hessp, + callback, + # scipy arguments that will never be supported + options, + tol, # deprecated arguments criterion, criterion_kwargs, @@ -369,6 +404,55 @@ def _optimize( else: algorithm = map_method_to_algorithm(method) + # ================================================================================== + # Handle scipy arguments that are not yet implemented + # ================================================================================== + + if hess is not None: + msg = ( + "The hess argument is not yet supported in optimagic. Creat an issue on " + "https://github.com/OpenSourceEconomics/optimagic/ if you have urgent need " + "for this feature." + ) + raise NotImplementedError(msg) + + if hessp is not None: + msg = ( + "The hessp argument is not yet supported in optimagic. Creat an issue on " + "https://github.com/OpenSourceEconomics/optimagic/ if you have urgent need " + "for this feature." + ) + raise NotImplementedError(msg) + + if callback is not None: + msg = ( + "The callback argument is not yet supported in optimagic. Creat an issue " + "on https://github.com/OpenSourceEconomics/optimagic/ if you have urgent " + "need for this feature." + ) + raise NotImplementedError(msg) + + # ================================================================================== + # Handle scipy arguments that will never be supported + # ================================================================================== + + if options is not None: + # TODO: Add link to a how-to guide or tutorial for this + msg = ( + "The options argument is not supported in optimagic. Please use the " + "algo_options argument instead." + ) + raise NotImplementedError(msg) + + if tol is not None: + # TODO: Add link to a how-to guide or tutorial for this + msg = ( + "The tol argument is not supported in optimagic. Please use " + "algo_options or configured algorithms instead to set convergence criteria " + "for your optimizer." + ) + raise NotImplementedError(msg) + # ================================================================================== # Set default values and check options # ================================================================================== diff --git a/tests/optimagic/optimization/test_scipy_aliases.py b/tests/optimagic/optimization/test_scipy_aliases.py index 99dea7f85..86cdd0ed3 100644 --- a/tests/optimagic/optimization/test_scipy_aliases.py +++ b/tests/optimagic/optimization/test_scipy_aliases.py @@ -95,3 +95,58 @@ def test_method_and_algorithm_do_not_work_together_in_maximize(): algorithm="scipy_lbfgsb", method="L-BFGS-B", ) + + +def test_exception_for_hess(): + msg = "The hess argument is not yet supported" + with pytest.raises(NotImplementedError, match=msg): + om.minimize( + fun=lambda x: x @ x, + x0=np.arange(3), + algorithm="scipy_lbfgsb", + hess=lambda x: np.eye(len(x)), + ) + + +def test_exception_for_hessp(): + msg = "The hessp argument is not yet supported" + with pytest.raises(NotImplementedError, match=msg): + om.minimize( + fun=lambda x: x @ x, + x0=np.arange(3), + algorithm="scipy_lbfgsb", + hessp=lambda x, p: np.eye(len(x)) @ p, + ) + + +def test_exception_for_callback(): + msg = "The callback argument is not yet supported" + with pytest.raises(NotImplementedError, match=msg): + om.minimize( + fun=lambda x: x @ x, + x0=np.arange(3), + algorithm="scipy_lbfgsb", + callback=lambda x: print(x), + ) + + +def test_exception_for_options(): + msg = "The options argument is not supported" + with pytest.raises(NotImplementedError, match=msg): + om.minimize( + fun=lambda x: x @ x, + x0=np.arange(3), + algorithm="scipy_lbfgsb", + options={"maxiter": 100}, + ) + + +def test_exception_for_tol(): + msg = "The tol argument is not supported" + with pytest.raises(NotImplementedError, match=msg): + om.minimize( + fun=lambda x: x @ x, + x0=np.arange(3), + algorithm="scipy_lbfgsb", + tol=1e-6, + )