Skip to content

Commit

Permalink
#251 increment pybamm>23.5, fix examples incorrect model, remove non-…
Browse files Browse the repository at this point in the history
…integration tests in parameterisation tests
  • Loading branch information
BradyPlanden committed Apr 8, 2024
1 parent 5c334aa commit c7ef869
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/scripts/spm_adam.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Parameter set and model definition
parameter_set = pybop.ParameterSet.pybamm("Chen2020")
model = pybop.lithium_ion.SPMe(parameter_set=parameter_set)
model = pybop.lithium_ion.SPM(parameter_set=parameter_set)

# Fitting parameters
parameters = [
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/spm_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Parameter set and model definition
parameter_set = pybop.ParameterSet.pybamm("Chen2020")
model = pybop.lithium_ion.SPMe(parameter_set=parameter_set)
model = pybop.lithium_ion.SPM(parameter_set=parameter_set)

# Fitting parameters
parameters = [
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ classifiers = [
]
requires-python = ">=3.8, <3.13"
dependencies = [
"pybamm>=23.5",
"pybamm>23.5",
"numpy>=1.16",
"scipy>=1.3",
"pints>=0.5",
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci/build_matrix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
python_version=("3.8" "3.9" "3.10" "3.11" "3.12")
os=("ubuntu-latest" "windows-latest" "macos-latest")
# This command fetches the last three PyBaMM versions excluding release candidates from PyPI
pybamm_version=($(curl -s https://pypi.org/pypi/pybamm/json | jq -r '.releases | keys[]' | grep -v rc | tail -n 3 | paste -sd " " -))
pybamm_version=($(curl -s https://pypi.org/pypi/pybamm/json | jq -r '.releases | keys[]' | grep -v rc | tail -n 2 | paste -sd " " -))

# open dict
json='{
Expand Down
45 changes: 10 additions & 35 deletions tests/integration/test_parameterisations.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ def spm_costs(self, model, parameters, cost_class, init_soc):
def test_spm_optimisers(self, optimiser, spm_costs):
# Some optimisers require a complete set of bounds
if optimiser in [pybop.SciPyDifferentialEvolution, pybop.PSO]:
spm_costs.problem.parameters[1].set_bounds([0.375, 0.75])
spm_costs.problem.parameters[1].set_bounds(
[0.3, 0.8]
) # Large range to ensure IC within bounds
bounds = {"lower": [], "upper": []}
for param in spm_costs.problem.parameters:
bounds["lower"].append(param.bounds[0])
Expand All @@ -106,38 +108,16 @@ def test_spm_optimisers(self, optimiser, spm_costs):
parameterisation = pybop.Optimisation(
cost=spm_costs, optimiser=optimiser, sigma0=0.05
)
parameterisation.set_max_unchanged_iterations(iterations=35, threshold=5e-4)
parameterisation.set_max_unchanged_iterations(iterations=35, threshold=1e-5)
parameterisation.set_max_iterations(125)

initial_cost = parameterisation.cost(spm_costs.x0)

if optimiser in [pybop.CMAES]:
parameterisation.set_f_guessed_tracking(True)
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()

parameterisation.set_f_guessed_tracking(False)
parameterisation.set_max_iterations(125)

x, final_cost = parameterisation.run()
assert parameterisation._max_iterations == 125

elif optimiser in [pybop.GradientDescent]:
if optimiser in [pybop.GradientDescent]:
if isinstance(spm_costs, pybop.GaussianLogLikelihoodKnownSigma):
parameterisation.optimiser.set_learning_rate(1.8e-5)
parameterisation.set_min_iterations(150)
else:
parameterisation.optimiser.set_learning_rate(0.02)
parameterisation.set_max_iterations(150)
x, final_cost = parameterisation.run()

elif optimiser in [pybop.SciPyDifferentialEvolution]:
with pytest.raises(ValueError):
parameterisation.optimiser.set_population_size(-5)

parameterisation.optimiser.set_population_size(5)
x, final_cost = parameterisation.run()

elif optimiser in [pybop.SciPyMinimize]:
Expand Down Expand Up @@ -192,7 +172,9 @@ def spm_two_signal_cost(self, parameters, model, cost_class):
def test_multiple_signals(self, multi_optimiser, spm_two_signal_cost):
# Some optimisers require a complete set of bounds
if multi_optimiser in [pybop.SciPyDifferentialEvolution]:
spm_two_signal_cost.problem.parameters[1].set_bounds([0.375, 0.75])
spm_two_signal_cost.problem.parameters[1].set_bounds(
[0.3, 0.8]
) # Large range to ensure IC within bounds
bounds = {"lower": [], "upper": []}
for param in spm_two_signal_cost.problem.parameters:
bounds["lower"].append(param.bounds[0])
Expand All @@ -208,10 +190,6 @@ def test_multiple_signals(self, multi_optimiser, spm_two_signal_cost):
parameterisation.set_max_iterations(125)

initial_cost = parameterisation.cost(spm_two_signal_cost.x0)

if multi_optimiser in [pybop.SciPyDifferentialEvolution]:
parameterisation.optimiser.set_population_size(5)

x, final_cost = parameterisation.run()

# Assertions
Expand All @@ -224,7 +202,7 @@ def test_model_misparameterisation(self, parameters, model, init_soc):
# Define two different models with different parameter sets
# The optimisation should fail as the models are not the same
second_parameter_set = pybop.ParameterSet.pybamm("Ecker2015")
second_model = pybop.lithium_ion.SPM(parameter_set=second_parameter_set)
second_model = pybop.lithium_ion.SPMe(parameter_set=second_parameter_set)

# Form dataset
solution = self.getdata(second_model, self.ground_truth, init_soc)
Expand All @@ -237,10 +215,7 @@ def test_model_misparameterisation(self, parameters, model, init_soc):
)

# Define the cost to optimise
signal = ["Voltage [V]"]
problem = pybop.FittingProblem(
model, parameters, dataset, signal=signal, init_soc=init_soc
)
problem = pybop.FittingProblem(model, parameters, dataset, init_soc=init_soc)
cost = pybop.RootMeanSquaredError(problem)

# Select optimiser
Expand Down

0 comments on commit c7ef869

Please sign in to comment.