Skip to content

Commit

Permalink
Updt. cost2d/optim2d x0 shape/colour, revert conftest win platform un…
Browse files Browse the repository at this point in the history
…icode, add infeasible unit test
  • Loading branch information
BradyPlanden committed Mar 13, 2024
1 parent 66efaba commit 9b03734
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
5 changes: 0 additions & 5 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest
import matplotlib
import plotly
import sys

plotly.io.renderers.default = None
matplotlib.use("Template")
Expand Down Expand Up @@ -43,10 +42,6 @@ def pytest_configure(config):
config.addinivalue_line("markers", "plots: mark test as a plot test")
config.addinivalue_line("markers", "notebook: mark test as a notebook test")

if sys.platform.startswith("win"):
# Set the output encoding to UTF-8 on Windows
sys.stdout = open(sys.stdout.fileno(), mode="w", encoding="utf8", buffering=1)


def pytest_collection_modifyitems(config, items):
options = {
Expand Down
30 changes: 15 additions & 15 deletions pybop/plotting/plot_convergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,35 +98,35 @@ def plot_optim2d(optim, bounds=None, steps=10, show=True, **layout_kwargs):
# Import plotly only when needed
go = pybop.PlotlyManager().go

# Plot the initial guess
# Plot the optimisation trace
optim_trace = np.array([item for sublist in optim.log for item in sublist])
optim_trace = optim_trace.reshape(-1, 2)
fig.add_trace(
go.Scatter(
x=[optim.x0[0]],
y=[optim.x0[1]],
x=optim_trace[:, 0],
y=optim_trace[:, 1],
mode="markers",
marker_symbol="x",
marker=dict(
color="red",
line_color="midnightblue",
line_width=1,
size=12,
color=[i / len(optim_trace) for i in range(len(optim_trace))],
colorscale="YlOrBr",
showscale=False,
),
showlegend=False,
)
)

# Plot the optimisation trace
optim_trace = np.array([item for sublist in optim.log for item in sublist])
optim_trace = optim_trace.reshape(-1, 2)
# Plot the initial guess
fig.add_trace(
go.Scatter(
x=optim_trace[:, 0],
y=optim_trace[:, 1],
x=[optim.x0[0]],
y=[optim.x0[1]],
mode="markers",
marker_symbol="circle",
marker=dict(
color=[i / len(optim_trace) for i in range(len(optim_trace))],
colorscale="YlOrBr",
color="mediumspringgreen",
line_color="mediumspringgreen",
line_width=1,
size=14,
showscale=False,
),
showlegend=False,
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_parameterisations.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_spm_optimisers(self, optimiser, spm_costs):

if optimiser in [pybop.CMAES]:
parameterisation.set_f_guessed_tracking(True)
parameterisation.cost.problem._model.allow_infeasible_solutions = False
parameterisation.cost.problem.model.allow_infeasible_solutions = False
assert parameterisation._use_f_guessed is True
parameterisation.set_max_iterations(1)
x, final_cost = parameterisation.run()
Expand All @@ -115,7 +115,7 @@ def test_spm_optimisers(self, optimiser, spm_costs):
x, final_cost = parameterisation.run()

elif optimiser in [pybop.SciPyMinimize]:
parameterisation.cost.problem._model.allow_infeasible_solutions = False
parameterisation.cost.problem.model.allow_infeasible_solutions = False
x, final_cost = parameterisation.run()

else:
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_optimisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ def test_halting(self, cost):
with pytest.raises(ValueError):
optim.set_max_unchanged_iterations(1, threshold=-1)

@pytest.mark.unit
def test_infeasible_solutions(self, cost):
# Test infeasible solutions
for optimiser in [pybop.SciPyMinimize, pybop.GradientDescent]:
optim = pybop.Optimisation(
cost=cost, optimiser=optimiser, allow_infeasible_solutions=False
)
optim.set_max_iterations(1)
optim.run()
assert optim._iterations == 1

@pytest.mark.unit
def test_unphysical_result(self, cost):
# Trigger parameters not physically viable warning
Expand Down

0 comments on commit 9b03734

Please sign in to comment.