Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors BaseCost.evaluate/S1 into BaseCost.compute #455

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
993e576
feat: refactor cost.compute/S1 into single method, refactor cost.eval…
BradyPlanden Aug 8, 2024
dac1e08
fix: update tests, correct cost call across codebase
BradyPlanden Aug 8, 2024
dca3984
tests: align multi-signal spm parameterisation, fix incorrect MLE exa…
BradyPlanden Aug 8, 2024
10b24d7
fix: Incorrect likelihood calculation, add dynamic bounds to sigma, r…
BradyPlanden Aug 9, 2024
95cea43
feat: updated cost.compute method for arg only calc (no-longer via se…
BradyPlanden Aug 9, 2024
73b38b7
refactor: construct fail return attrs in BaseCost, fix unit test, upd…
BradyPlanden Aug 9, 2024
ace4846
Add changelog entry
BradyPlanden Aug 9, 2024
61bf719
fix: tidy BaseCost.__call__
BradyPlanden Aug 9, 2024
47d8e59
Merge branch 'refs/heads/develop' into 436-refactor-cost_evaluate-met…
BradyPlanden Aug 9, 2024
ef37872
tests: increase bounds for better stability for initial samples with …
BradyPlanden Aug 12, 2024
5303d45
tests: Add test for grad compute w/o dy
BradyPlanden Aug 12, 2024
7156181
Merge branch 'refs/heads/develop' into 436-refactor-cost_evaluate-met…
BradyPlanden Aug 12, 2024
0999f61
Merge branch 'refs/heads/develop' into 436-refactor-cost_evaluate-met…
BradyPlanden Aug 14, 2024
44f50f7
Remove inputs from compute()
NicolaCourtier Aug 20, 2024
ed51bd8
Merge branch 'develop' into 436-refactor-cost_evaluate-methods-to-acc…
NicolaCourtier Aug 20, 2024
61b6f72
apply changes from review
BradyPlanden Aug 20, 2024
ebc0446
Fix typo
NicolaCourtier Aug 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

## Breaking Changes

- [#436](https://github.com/pybop-team/PyBOP/pull/436) - **API Change:** The functionality from `BaseCost.evaluate/S1` & `BaseCost._evaluate/S1` is represented in `BaseCost.__call__` & `BaseCost.compute`. `BaseCost.compute` directly acts on the predictions, while `BaseCost.__call__` calls `BaseProblem.evaluate/S1` before `BaseCost.compute`. `compute` has optional args for gradient cost calculations.
- [#424](https://github.com/pybop-team/PyBOP/issues/424) - Replaces the `init_soc` input to `FittingProblem` with the option to pass an initial OCV value, updates `BaseModel` and fixes `multi_model_identification.ipynb` and `spm_electrode_design.ipynb`.

# [v24.6.1](https://github.com/pybop-team/PyBOP/tree/v24.6.1) - 2024-07-31
Expand Down
46 changes: 30 additions & 16 deletions examples/scripts/spm_MLE.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import pybop

# Define model
# Define model and set initial parameter values
parameter_set = pybop.ParameterSet.pybamm("Chen2020")
parameter_set.update(
{
"Negative electrode active material volume fraction": 0.63,
"Positive electrode active material volume fraction": 0.51,
}
)
model = pybop.lithium_ion.SPM(parameter_set=parameter_set)

# Fitting parameters
Expand All @@ -19,31 +25,39 @@
),
)

# Set initial parameter values
parameter_set.update(
{
"Negative electrode active material volume fraction": 0.63,
"Positive electrode active material volume fraction": 0.51,
}
)
# Generate data
sigma = 0.005
t_eval = np.arange(0, 900, 3)
values = model.predict(t_eval=t_eval)
corrupt_values = values["Voltage [V]"].data + np.random.normal(0, sigma, len(t_eval))
sigma = 0.002
experiment = pybop.Experiment(
[
(
"Discharge at 0.5C for 3 minutes (3 second period)",
"Charge at 0.5C for 3 minutes (3 second period)",
),
]
)
values = model.predict(initial_state={"Initial SoC": 0.5}, experiment=experiment)


def noise(sigma):
return np.random.normal(0, sigma, len(values["Voltage [V]"].data))


# Form dataset
dataset = pybop.Dataset(
{
"Time [s]": t_eval,
"Time [s]": values["Time [s]"].data,
"Current function [A]": values["Current [A]"].data,
"Voltage [V]": corrupt_values,
"Voltage [V]": values["Voltage [V]"].data + noise(sigma),
"Bulk open-circuit voltage [V]": values["Bulk open-circuit voltage [V]"].data
+ noise(sigma),
}
)


signal = ["Voltage [V]", "Bulk open-circuit voltage [V]"]
# Generate problem, cost function, and optimisation class
problem = pybop.FittingProblem(model, parameters, dataset)
likelihood = pybop.GaussianLogLikelihoodKnownSigma(problem, sigma0=sigma)
problem = pybop.FittingProblem(model, parameters, dataset, signal=signal)
likelihood = pybop.GaussianLogLikelihood(problem, sigma0=sigma * 4)
optim = pybop.IRPropMin(
likelihood,
max_unchanged_iterations=20,
Expand Down
14 changes: 6 additions & 8 deletions examples/standalone/cost.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import numpy as np

import pybop


Expand Down Expand Up @@ -43,22 +45,18 @@ def __init__(self, problem=None):
)
self.x0 = self.parameters.initial_value()

def compute(self, inputs):
def compute(
self, y: dict = None, dy: np.ndarray = None, calculate_grad: bool = False
):
"""
Compute the cost for a given parameter value.

The cost function is defined as cost(x) = x^2 + 42, where x is the
parameter value.

Parameters
----------
inputs : Dict
The parameters for which to evaluate the cost.

Returns
-------
float
The calculated cost value for the given parameter.
"""

return inputs["x"] ** 2 + 42
return self.parameters["x"].value ** 2 + 42
Loading
Loading