Skip to content

Commit

Permalink
Merge pull request #457 from pybop-team/454-bug-fix-failing-benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
BradyPlanden authored Aug 16, 2024
2 parents d519429 + 6479a18 commit a5ce284
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,37 @@ jobs:
run: |
nox -s examples
# Quick benchmarks on macos-14
benchmarks:
needs: style
runs-on: macos-14
strategy:
fail-fast: false
name: Benchmarks

steps:
- name: Check out PyBOP repository
uses: actions/checkout@v4
with:
fetch-depth: 2

- name: Set up Python 3.12
id: setup-python
uses: actions/setup-python@v4
with:
python-version: 3.12

- name: Install dependencies
shell: bash
run: |
python -m pip install --upgrade pip asv[virtualenv]
- name: Run quick benchmarks
shell: bash
run: |
asv machine --machine "GitHubRunner"
asv run --machine "GitHubRunner" --quick --show-stderr
# Runs only on macos-14 with Python 3.12
check_coverage:
needs: style
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

## Bug Fixes

- [#454](https://github.com/pybop-team/PyBOP/issue/454) - Fixes benchmarking suite.
- [#421](https://github.com/pybop-team/PyBOP/issues/421) - Adds a default value for the initial SOC for design problems.

## Breaking Changes
Expand Down
2 changes: 1 addition & 1 deletion asv.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"python -m build --wheel -o {build_cache_dir} {build_dir}"
],
"default_benchmark_timeout": 180,
"branches": ["develop"],
"branches": ["HEAD"],
"environment_type": "virtualenv",
"matrix": {
"req":{
Expand Down
13 changes: 8 additions & 5 deletions benchmarks/benchmark_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ def setup(self, model, parameter_set):
self.model = model(parameter_set=pybop.ParameterSet.pybamm(parameter_set))

# Define fitting parameters
parameters = [
parameters = pybop.Parameters(
pybop.Parameter(
"Current function [A]",
prior=pybop.Gaussian(0.4, 0.02),
bounds=[0.2, 0.7],
initial_value=0.4,
)
]
)

# Generate synthetic data
sigma = 0.001
self.t_eval = np.arange(0, 900, 2)
values = self.model.predict(t_eval=self.t_eval)
self.init_state = {"Initial SoC": 0.5}
values = self.model.predict(t_eval=self.t_eval, initial_state=self.init_state)
corrupt_values = values["Voltage [V]"].data + np.random.normal(
0, sigma, len(self.t_eval)
)
Expand All @@ -58,7 +59,7 @@ def setup(self, model, parameter_set):

# Create fitting problem
self.problem = pybop.FittingProblem(
model=self.model, dataset=dataset, parameters=parameters, init_soc=0.5
model=self.model, dataset=dataset, parameters=parameters
)

def time_model_predict(self, model, parameter_set):
Expand All @@ -69,7 +70,9 @@ def time_model_predict(self, model, parameter_set):
model (pybop.Model): The model class being benchmarked.
parameter_set (str): The name of the parameter set being used.
"""
self.model.predict(inputs=self.inputs, t_eval=self.t_eval)
self.model.predict(
inputs=self.inputs, t_eval=self.t_eval, initial_state=self.init_state
)

def time_model_simulate(self, model, parameter_set):
"""
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/benchmark_optim_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setup(self, model, parameter_set, optimiser):
model_instance = model(parameter_set=pybop.ParameterSet.pybamm(parameter_set))

# Define fitting parameters
parameters = [
parameters = pybop.Parameters(
pybop.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Gaussian(0.6, 0.02),
Expand All @@ -41,7 +41,7 @@ def setup(self, model, parameter_set, optimiser):
bounds=[0.375, 0.625],
initial_value=0.51,
),
]
)

# Generate synthetic data
sigma = 0.001
Expand Down Expand Up @@ -75,7 +75,7 @@ def time_optimisation_construction(self, model, parameter_set, optimiser):
Args:
model (pybop.Model): The model class being benchmarked.
parameter_set (str): The name of the parameter set being used.
optimiser (pybop.Optimiser): The optimizer class being used.
optimiser (pybop.Optimiser): The optimiser class being used.
"""
self.optim = pybop.Optimisation(self.cost, optimiser=optimiser)

Expand All @@ -86,6 +86,6 @@ def time_cost_evaluate(self, model, parameter_set, optimiser):
Args:
model (pybop.Model): The model class being benchmarked.
parameter_set (str): The name of the parameter set being used.
optimiser (pybop.Optimiser): The optimizer class being used.
optimiser (pybop.Optimiser): The optimiser class being used.
"""
self.cost([0.63, 0.51])
8 changes: 4 additions & 4 deletions benchmarks/benchmark_parameterisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def setup(self, model, parameter_set, optimiser):
Args:
model (pybop.Model): The model class to be benchmarked.
parameter_set (str): The name of the parameter set to be used.
optimiser (pybop.Optimiser): The optimizer class to be used.
optimiser (pybop.Optimiser): The optimiser class to be used.
"""
# Set random seed
set_random_seed()
Expand All @@ -45,7 +45,7 @@ def setup(self, model, parameter_set, optimiser):
model_instance = model(parameter_set=params)

# Define fitting parameters
parameters = [
parameters = pybop.Parameters(
pybop.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Gaussian(0.55, 0.03),
Expand All @@ -56,7 +56,7 @@ def setup(self, model, parameter_set, optimiser):
prior=pybop.Gaussian(0.55, 0.03),
bounds=[0.375, 0.7],
),
]
)

# Generate synthetic data
sigma = 0.003
Expand Down Expand Up @@ -110,7 +110,7 @@ def time_parameterisation(self, model, parameter_set, optimiser):
Args:
model (pybop.Model): The model class being benchmarked (unused).
parameter_set (str): The name of the parameter set being used (unused).
optimiser (pybop.Optimiser): The optimizer class being used (unused).
optimiser (pybop.Optimiser): The optimiser class being used (unused).
"""
self.optim.run()

Expand Down
4 changes: 2 additions & 2 deletions benchmarks/benchmark_track_parameterisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def setup(self, model, parameter_set, optimiser):
model_instance = model(parameter_set=params)

# Define fitting parameters
parameters = [
parameters = pybop.Parameters(
pybop.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Gaussian(0.55, 0.03),
Expand All @@ -56,7 +56,7 @@ def setup(self, model, parameter_set, optimiser):
prior=pybop.Gaussian(0.55, 0.03),
bounds=[0.375, 0.7],
),
]
)

# Generate synthetic data
sigma = 0.003
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ def test_predict_without_pybamm(self, model):
):
model.predict(None, None)

# Test new_copy() without pybamm_model
if not isinstance(model, pybop.lithium_ion.MSMR):
new_model = model.new_copy()
assert new_model.pybamm_model is not None
assert new_model.parameter_set is not None

@pytest.mark.unit
def test_predict_with_inputs(self, model):
# Define inputs
Expand Down

0 comments on commit a5ce284

Please sign in to comment.