Skip to content

Commit

Permalink
test(integ): Adjusts sigma0, adds coverage for prior updt
Browse files Browse the repository at this point in the history
Add tests for the prior cls update and SciPyMinimize resampling.
Also updates sigma0 for GradientDescent and loosens assertion
  • Loading branch information
BradyPlanden committed May 13, 2024
1 parent 8576c8a commit 44c7bb8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pybop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
#
from .parameters.parameter import Parameter
from .parameters.parameter_set import ParameterSet
from .parameters.priors import Gaussian, Uniform, Exponential
from .parameters.priors import BasePrior, Gaussian, Uniform, Exponential


#
Expand Down
4 changes: 3 additions & 1 deletion pybop/optimisers/scipy_optimisers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def callback(x):
if not np.isinf(self.cost0):
break
if np.isinf(self.cost0):
raise Exception("The initial parameter values return an infinite cost.")
raise ValueError(
"The initial parameter values return an infinite cost."
)

# Scale the cost function and eliminate nan values
self.inf_count = 0
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 @@ -120,7 +120,7 @@ def test_spm_optimisers(self, optimiser, spm_costs):
)
else:
parameterisation = pybop.Optimisation(
cost=spm_costs, optimiser=optimiser, sigma0=0.02
cost=spm_costs, optimiser=optimiser, sigma0=0.035
)
elif optimiser in [pybop.SciPyMinimize]:
parameterisation = pybop.Optimisation(
Expand Down Expand Up @@ -148,7 +148,7 @@ def test_spm_optimisers(self, optimiser, spm_costs):
else:
np.testing.assert_allclose(x, self.ground_truth, atol=3.0e-2)
else:
if optimiser in [pybop.SciPyMinimize]:
if optimiser in [pybop.GradientDescent, pybop.SciPyMinimize]:
np.testing.assert_allclose(x, self.ground_truth, atol=2.5e-2)
else:
np.testing.assert_allclose(x, self.ground_truth, atol=1.75e-2)
Expand Down
37 changes: 35 additions & 2 deletions tests/unit/test_optimisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def cost(self, model, one_parameter, dataset):
model,
one_parameter,
dataset,
signal=["Voltage [V]"],
)
return pybop.SumSquaredError(problem)

Expand All @@ -64,7 +63,6 @@ def two_param_cost(self, model, two_parameters, dataset):
model,
two_parameters,
dataset,
signal=["Voltage [V]"],
)
return pybop.SumSquaredError(problem)

Expand Down Expand Up @@ -142,6 +140,41 @@ def test_prior_sampling(self, cost):

assert opt.x0 <= 0.62 and opt.x0 >= 0.58

@pytest.mark.unit
@pytest.mark.parametrize(
"mean, sigma, expect_exception",
[
(0.85, 0.2, False),
(0.85, 0.001, True),
],
)
def test_scipy_prior_resampling(
self, model, dataset, mean, sigma, expect_exception
):
# Set up the parameter with a Gaussian prior
parameter = pybop.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Gaussian(mean, sigma),
bounds=[0.55, 0.95],
)

# Define the problem and cost
problem = pybop.FittingProblem(model, [parameter], dataset)
cost = pybop.SumSquaredError(problem)

# Create the optimisation class with infeasible solutions disabled
opt = pybop.Optimisation(
cost=cost, optimiser=pybop.SciPyMinimize, allow_infeasible_solutions=False
)
opt.set_max_iterations(1)

# If small sigma, expect a ValueError due inability to resample a non np.inf cost
if expect_exception:
with pytest.raises(ValueError):
opt.run()
else:
opt.run()

@pytest.mark.unit
def test_halting(self, cost):
# Test max evalutions
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_priors.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def Uniform(self):
def Exponential(self):
return pybop.Exponential(scale=1)

@pytest.mark.unit
def test_base_prior(self):
base = pybop.BasePrior()
assert isinstance(base, pybop.BasePrior)

@pytest.mark.unit
def test_priors(self, Gaussian, Uniform, Exponential):
# Test pdf
Expand Down Expand Up @@ -49,6 +54,13 @@ def test_gaussian_rvs(self, Gaussian):
assert abs(mean - 0.5) < 0.2
assert abs(std - 1) < 0.2

@pytest.mark.unit
def test_incorrect_rvs(self, Gaussian):
with pytest.raises(ValueError):
Gaussian.rvs(size="a")
with pytest.raises(ValueError):
Gaussian.rvs(size=(1, 2, -1))

@pytest.mark.unit
def test_uniform_rvs(self, Uniform):
samples = Uniform.rvs(size=500)
Expand Down

0 comments on commit 44c7bb8

Please sign in to comment.