Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
janosg committed Nov 12, 2024
1 parent 5d4f5e4 commit 09f4719
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/optimagic/visualization/history_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def _harmonize_inputs_to_dict(results, names):
# handle dict case
if isinstance(results, dict):
if names is not None:
results_dict = dict(zip(names, results, strict=False))
results_dict = dict(zip(names, list(results.values()), strict=False))
else:
results_dict = results

Expand Down
6 changes: 6 additions & 0 deletions tests/optimagic/test_algo_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ def test_scipy_cobyla_is_present():
assert hasattr(algos.NonlinearConstrained.GradientFree.Local, "scipy_cobyla")
assert hasattr(algos.NonlinearConstrained.Local.GradientFree, "scipy_cobyla")
assert hasattr(algos.Local.NonlinearConstrained.GradientFree, "scipy_cobyla")


def test_algorithm_lists():
assert len(algos.All) >= len(algos.Available)
assert len(algos.AllNames) == len(algos.All)
assert len(algos.AvailableNames) == len(algos.Available)
46 changes: 45 additions & 1 deletion tests/optimagic/visualization/test_history_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from optimagic.logging import SQLiteLogOptions
from optimagic.optimization.optimize import minimize
from optimagic.parameters.bounds import Bounds
from optimagic.visualization.history_plots import criterion_plot, params_plot
from optimagic.visualization.history_plots import (
_harmonize_inputs_to_dict,
criterion_plot,
params_plot,
)


@pytest.fixture()
Expand Down Expand Up @@ -130,3 +134,43 @@ def test_criterion_plot_wrong_inputs():

with pytest.raises(ValueError):
criterion_plot(["bla", "bla"], names="blub")


def test_harmonize_inputs_to_dict_single_result():
res = minimize(fun=lambda x: x @ x, params=np.arange(5), algorithm="scipy_lbfgsb")
assert _harmonize_inputs_to_dict(results=res, names=None) == {"0": res}


def test_harmonize_inputs_to_dict_single_result_with_name():
res = minimize(fun=lambda x: x @ x, params=np.arange(5), algorithm="scipy_lbfgsb")
assert _harmonize_inputs_to_dict(results=res, names="bla") == {"bla": res}


def test_harmonize_inputs_to_dict_list_results():
res = minimize(fun=lambda x: x @ x, params=np.arange(5), algorithm="scipy_lbfgsb")
results = [res, res]
assert _harmonize_inputs_to_dict(results=results, names=None) == {
"0": res,
"1": res,
}


def test_harmonize_inputs_to_dict_dict_input():
res = minimize(fun=lambda x: x @ x, params=np.arange(5), algorithm="scipy_lbfgsb")
results = {"bla": res, "blub": res}
assert _harmonize_inputs_to_dict(results=results, names=None) == results


def test_harmonize_inputs_to_dict_dict_input_with_names():
res = minimize(fun=lambda x: x @ x, params=np.arange(5), algorithm="scipy_lbfgsb")
results = {"bla": res, "blub": res}
got = _harmonize_inputs_to_dict(results=results, names=["a", "b"])
expected = {"a": res, "b": res}
assert got == expected


def test_harmonize_inputs_to_dict_invalid_names():
results = [None]
names = ["a", "b"]
with pytest.raises(ValueError):
_harmonize_inputs_to_dict(results=results, names=names)

0 comments on commit 09f4719

Please sign in to comment.