Skip to content

Commit

Permalink
test: updates for scipy convergence
Browse files Browse the repository at this point in the history
  • Loading branch information
BradyPlanden committed Oct 4, 2024
1 parent 929f72d commit a966997
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pybop/parameters/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ def get_bounds_for_plotly(self, apply_transform: bool = False) -> np.ndarray:
bounds = self.get_bounds(apply_transform=apply_transform)

# Validate that all parameters have bounds
if bounds is None:
if bounds is None or not np.isfinite(list(bounds.values())).all():
raise ValueError("All parameters require bounds for plotting.")

return np.asarray(list(bounds.values())).T
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/test_optimisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def dataset(self):
return pybop.Dataset(
{
"Time [s]": np.linspace(0, 360, 10),
"Current function [A]": 0.01 * np.ones(10),
"Current function [A]": 1e-3 * np.ones(10),
"Voltage [V]": np.ones(10),
}
)
Expand Down Expand Up @@ -134,12 +134,12 @@ def test_no_optimisation_parameters(self, model, dataset):
)
@pytest.mark.unit
def test_optimiser_kwargs(self, cost, optimiser):
optim = optimiser(cost=cost, maxiter=3, tol=1e-6)
optim = optimiser(cost=cost, maxiter=1, tol=1e-6)
cost_bounds = cost.parameters.get_bounds()

# Check maximum iterations
results = optim.run()
assert results.n_iterations == 3
assert results.n_iterations == 1

if optimiser in [pybop.GradientDescent, pybop.Adam, pybop.NelderMead]:
# Ignored bounds
Expand Down Expand Up @@ -552,11 +552,11 @@ def test_unphysical_result(self, cost):
@pytest.mark.unit
def test_optimsation_results(self, cost):
# Construct OptimisationResult
results = pybop.OptimisationResult(x=[0.55], cost=cost, n_iterations=1)
results = pybop.OptimisationResult(x=[1e-3], cost=cost, n_iterations=1)

# Asserts
assert results.x[0] == 0.55
assert results.final_cost == cost([0.55])
assert results.x[0] == 1e-3
assert results.final_cost == cost([1e-3])
assert results.n_iterations == 1

# Test non-finite results
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ def test_cost_plots(self, cost):
# Test without bounds
for param in cost.parameters:
param.bounds = None
pybop.plot2d(cost, steps=5)
with pytest.raises(
ValueError, match="All parameters require bounds for plotting."
):
pybop.plot2d(cost, steps=5)

# Test with bounds
pybop.plot2d(cost, bounds=np.array([[0.5, 0.8], [0.4, 0.7]]), steps=5)
Expand Down

0 comments on commit a966997

Please sign in to comment.