Skip to content

Commit

Permalink
Add error handling for non-supported and not-yet supported scipy argu…
Browse files Browse the repository at this point in the history
…ments.
  • Loading branch information
janosg committed Jul 18, 2024
1 parent 8c34407 commit f24100e
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/optimagic/optimization/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
# ==================================================================================
Expand Down
55 changes: 55 additions & 0 deletions tests/optimagic/optimization/test_scipy_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

0 comments on commit f24100e

Please sign in to comment.