From c7ef869980b84f01ffdb912a65b187403ce4933b Mon Sep 17 00:00:00 2001 From: Brady Planden Date: Mon, 8 Apr 2024 17:45:00 +0100 Subject: [PATCH] #251 increment pybamm>23.5, fix examples incorrect model, remove non-integration tests in parameterisation tests --- examples/scripts/spm_adam.py | 2 +- examples/scripts/spm_descent.py | 2 +- pyproject.toml | 2 +- scripts/ci/build_matrix.sh | 2 +- tests/integration/test_parameterisations.py | 45 +++++---------------- 5 files changed, 14 insertions(+), 39 deletions(-) diff --git a/examples/scripts/spm_adam.py b/examples/scripts/spm_adam.py index e0796744c..f6a75354b 100644 --- a/examples/scripts/spm_adam.py +++ b/examples/scripts/spm_adam.py @@ -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 = [ diff --git a/examples/scripts/spm_descent.py b/examples/scripts/spm_descent.py index 3fe078689..5a4568f00 100644 --- a/examples/scripts/spm_descent.py +++ b/examples/scripts/spm_descent.py @@ -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 = [ diff --git a/pyproject.toml b/pyproject.toml index 3663ba670..be1d6d938 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", diff --git a/scripts/ci/build_matrix.sh b/scripts/ci/build_matrix.sh index 5e580303d..acc9578f2 100755 --- a/scripts/ci/build_matrix.sh +++ b/scripts/ci/build_matrix.sh @@ -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='{ diff --git a/tests/integration/test_parameterisations.py b/tests/integration/test_parameterisations.py index 4da45fd4d..ae5748235 100644 --- a/tests/integration/test_parameterisations.py +++ b/tests/integration/test_parameterisations.py @@ -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]) @@ -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]: @@ -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]) @@ -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 @@ -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) @@ -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