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

Fix parameter set logic and allow FittingProblem to take init_ocv rather than init_soc #425

Merged
merged 105 commits into from
Aug 7, 2024
Merged
Changes from 1 commit
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
b66a52a
Update default init_soc
NicolaCourtier Jul 24, 2024
157c211
Update CHANGELOG.md
NicolaCourtier Jul 24, 2024
5e60cee
Update check_params
NicolaCourtier Jul 24, 2024
60ae64e
Add pybamm_model as default attribute
NicolaCourtier Jul 24, 2024
c682ace
Ensure predict uses unprocessed_model
NicolaCourtier Jul 25, 2024
914f307
Move rebuild check to model.simulate
NicolaCourtier Jul 25, 2024
367ab12
Align simulate output with predict
NicolaCourtier Jul 25, 2024
4e92192
Replace init_soc with init_ocv for FittingProblem
NicolaCourtier Jul 25, 2024
f799e06
Update notebooks
NicolaCourtier Jul 25, 2024
3f61322
Update test_observers.py
NicolaCourtier Jul 26, 2024
46f31fc
Update descriptions and simplify
NicolaCourtier Jul 26, 2024
28d56fb
Add test_set_initial_state
NicolaCourtier Jul 26, 2024
cba5c36
Update test_problem.py
NicolaCourtier Jul 26, 2024
6e6cb70
Break connection between parameter_sets
NicolaCourtier Jul 26, 2024
d17c728
Allow predict to update initial state
NicolaCourtier Jul 26, 2024
aeba8c4
Fix typo
NicolaCourtier Jul 26, 2024
5e65542
Add nbstripout pre-commit hook
NicolaCourtier Jul 28, 2024
c54ae5b
Add -q and re-run all notebooks
NicolaCourtier Jul 28, 2024
4cba081
Copy parameter sets and remove model.initial_state
NicolaCourtier Jul 28, 2024
01115df
Reset spm_NelderMead.py
NicolaCourtier Jul 28, 2024
08df450
Update CHANGELOG.md
NicolaCourtier Jul 28, 2024
0039d8a
Update CHANGELOG.md
NicolaCourtier Jul 29, 2024
e655ed7
Allow parameter_set is None
NicolaCourtier Jul 29, 2024
b2f389b
Merge branch '427-pre-commit-nbstripout' into 424-fitting-ocv
NicolaCourtier Jul 29, 2024
9a7282c
Re-run notebooks
NicolaCourtier Jul 29, 2024
6223251
Update bounds
NicolaCourtier Jul 30, 2024
a71350e
Update notebooks
NicolaCourtier Jul 30, 2024
37be820
Update notebooks
NicolaCourtier Jul 30, 2024
8591ba7
Set numpy random seed in notebooks
NicolaCourtier Jul 30, 2024
d2c7e68
Re-run with fixed seed
NicolaCourtier Jul 30, 2024
e2410cc
Merge branch '427-pre-commit-nbstripout' into 424-fitting-ocv
NicolaCourtier Jul 30, 2024
9b8d27a
Update bounds
NicolaCourtier Jul 30, 2024
2f73168
Update notebooks to initial_state
NicolaCourtier Jul 30, 2024
80df8fe
Add set_initial_state for ECMs
NicolaCourtier Jul 30, 2024
eda8096
Add init_ocv setter
NicolaCourtier Jul 30, 2024
285777b
Add init_ocv values
NicolaCourtier Jul 30, 2024
aebec3c
Re-run notebooks
NicolaCourtier Jul 30, 2024
8df8618
Add tests for ECM get_initial_state
NicolaCourtier Jul 30, 2024
6f05cda
Add ECM initial state error tests
NicolaCourtier Jul 30, 2024
d18b28f
Remove unused store_optimised_parameters
NicolaCourtier Jul 31, 2024
5d4f2ea
Update parameters.initial_value
NicolaCourtier Jul 31, 2024
eac17cc
Use any Initial SoC from parameter_set
NicolaCourtier Jul 31, 2024
24e40a0
Merge branch 'develop' into 424-fitting-ocv
NicolaCourtier Jul 31, 2024
b9650c3
Update bounds again
NicolaCourtier Jul 31, 2024
7a7d58a
Update init_soc in notebooks
NicolaCourtier Jul 31, 2024
d993632
Move dataset check within unscented_kalman
NicolaCourtier Jul 31, 2024
5059f71
Remove unnecessary lines from spm_UKF
NicolaCourtier Jul 31, 2024
9bc0c0e
Update all parameters for rebuild
NicolaCourtier Jul 31, 2024
daf3f05
Update init_ocv to _init_ocv
NicolaCourtier Jul 31, 2024
b285885
Ensure value updates alongside initial_value
NicolaCourtier Jul 31, 2024
48788af
Update multi_model_identification
NicolaCourtier Jul 31, 2024
7798af5
Merge branch 'develop' into 421-design_init_soc
NicolaCourtier Jul 31, 2024
07644d4
Update spm_electrode_design.ipynb
NicolaCourtier Jul 31, 2024
799329e
Update spm_electrode_design.ipynb
NicolaCourtier Jul 31, 2024
c7ee29b
Merge branch '421-design_init_soc' into 424-fitting-ocv
NicolaCourtier Jul 31, 2024
61d326f
Fix identation
NicolaCourtier Jul 31, 2024
5be532c
Fix test_plots design problem
NicolaCourtier Jul 31, 2024
29e7a67
Move Changelog entry to breaking changes
NicolaCourtier Aug 1, 2024
f999b96
Move Changelog entry
NicolaCourtier Aug 1, 2024
d31ee54
Merge branch 'develop' into 421-design_init_soc
NicolaCourtier Aug 1, 2024
098e3e2
style: pre-commit fixes
pre-commit-ci[bot] Aug 1, 2024
86f1d17
Fix merge mistake
NicolaCourtier Aug 1, 2024
beac8bb
Merge branch '421-design_init_soc' into 424-fitting-ocv
NicolaCourtier Aug 1, 2024
d205dc9
style: pre-commit fixes
pre-commit-ci[bot] Aug 1, 2024
eef8acf
Add tests
NicolaCourtier Aug 1, 2024
5f7761f
Merge branch '421-design_init_soc' into 424-fitting-ocv
NicolaCourtier Aug 1, 2024
cdd4d1c
Update integration tests
NicolaCourtier Aug 1, 2024
b6c8824
Update spm_weighted_cost.py
NicolaCourtier Aug 1, 2024
ebc5114
Fix tests
NicolaCourtier Aug 2, 2024
d4421fe
style: pre-commit fixes
pre-commit-ci[bot] Aug 2, 2024
043ae52
Merge branch 'develop' into 424-fitting-ocv
NicolaCourtier Aug 2, 2024
3bba9c7
Merge branch 'develop' into 424-fitting-ocv
NicolaCourtier Aug 5, 2024
ede6363
Fix model type check
NicolaCourtier Aug 5, 2024
fe1397e
Update _parameter_set to parameter_set
NicolaCourtier Aug 5, 2024
3cb4c4d
style: pre-commit fixes
pre-commit-ci[bot] Aug 5, 2024
e3b1466
Update tests with parameter set
NicolaCourtier Aug 5, 2024
cbda2ef
Add model build description
NicolaCourtier Aug 5, 2024
b19035d
Revert to _parameter_set
NicolaCourtier Aug 5, 2024
eba9def
Apply suggestions from code review
NicolaCourtier Aug 5, 2024
892eb77
Apply suggestions from code review
NicolaCourtier Aug 5, 2024
bf01b7e
Fix syntax
NicolaCourtier Aug 5, 2024
71a95cf
Fix variable name
NicolaCourtier Aug 5, 2024
4484951
Update model type check
NicolaCourtier Aug 5, 2024
08cdc4c
Update parameter_set setter
NicolaCourtier Aug 5, 2024
cd87e83
style: pre-commit fixes
pre-commit-ci[bot] Aug 5, 2024
14e4223
Add parameters.reset_initial_value
NicolaCourtier Aug 5, 2024
8a19542
Add n_outputs property
NicolaCourtier Aug 6, 2024
a67c564
style: pre-commit fixes
pre-commit-ci[bot] Aug 6, 2024
5450f21
Remove public parameter_set setter
NicolaCourtier Aug 6, 2024
676e7ed
Correct integer to float
NicolaCourtier Aug 6, 2024
80ef44e
Convert initial_state to dict
NicolaCourtier Aug 6, 2024
8e67d89
Add guidance
NicolaCourtier Aug 6, 2024
d5f63d0
Remove empty dictionary defaults
NicolaCourtier Aug 6, 2024
d0d1bd2
style: pre-commit fixes
pre-commit-ci[bot] Aug 6, 2024
5c52712
Add warning stacklevels
NicolaCourtier Aug 6, 2024
dacafc3
Catch simulation errors in problem evaluation
NicolaCourtier Aug 6, 2024
bcc7acf
Add pybamm version comment
NicolaCourtier Aug 6, 2024
8607a6b
Add set initial ocv check
NicolaCourtier Aug 6, 2024
605f509
Add model.clear and remove setters
NicolaCourtier Aug 7, 2024
a5be6ee
Merge branch '445-remove-setters' into 424-fitting-ocv
NicolaCourtier Aug 7, 2024
a188c4b
Update unscented_kalman.py
NicolaCourtier Aug 7, 2024
ca2a5b1
Update unscented_kalman.py
NicolaCourtier Aug 7, 2024
a4c0030
Update test_models.py
NicolaCourtier Aug 7, 2024
742f9d4
Update test_set_initial_state
NicolaCourtier Aug 7, 2024
db09455
Merge branch 'develop' into 424-fitting-ocv
NicolaCourtier Aug 7, 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
Prev Previous commit
Next Next commit
Update check_params
  • Loading branch information
NicolaCourtier committed Jul 24, 2024

Verified

This commit was signed with the committer’s verified signature.
jdaugherty James Daugherty
commit 5e60cee879d9379d098f55eecdc81582ef9caf50
69 changes: 38 additions & 31 deletions pybop/models/base_model.py
Original file line number Diff line number Diff line change
@@ -215,7 +215,7 @@ def rebuild(
parameters : pybop.Parameters or Dict, optional
A pybop Parameters class or dictionary containing parameter values to apply to the model.
parameter_set : pybop.parameter_set, optional
A PyBOP parameter set object or a dictionary containing the parameter values
A PyBOP parameter set object or a dictionary containing the parameter values.
check_model : bool, optional
If True, the model will be checked for correctness after construction.
init_soc : float, optional
@@ -461,10 +461,10 @@ def simulateS1(self, inputs: Inputs, t_eval: np.array):

def predict(
self,
inputs: Inputs = None,
t_eval: np.array = None,
parameter_set: ParameterSet = None,
experiment: Experiment = None,
inputs: Optional[Inputs] = None,
t_eval: Optional[np.array] = None,
parameter_set: Optional[ParameterSet] = None,
experiment: Optional[Experiment] = None,
init_soc: Optional[float] = None,
) -> dict[str, np.ndarray[np.float64]]:
"""
@@ -504,44 +504,41 @@ def predict(
if PyBaMM models are not supported by the current simulation method.

"""
inputs = self.parameters.verify(inputs)

if not self.pybamm_model._built:
self.pybamm_model.build_model()

parameter_set = parameter_set or self._unprocessed_parameter_set
if inputs is not None:
inputs = self.parameters.verify(inputs)
parameter_set.update(inputs)

if self._unprocessed_model is None:
raise ValueError(
"The predict method currently only supports PyBaMM models."
)
elif not self.pybamm_model._built:
self.pybamm_model.build_model()

if self.check_params(
inputs=inputs,
parameter_set=parameter_set,
allow_infeasible_solutions=self.allow_infeasible_solutions,
):
if self._unprocessed_model is not None:
if experiment is None:
return pybamm.Simulation(
self._unprocessed_model,
parameter_values=parameter_set,
).solve(t_eval=t_eval, initial_soc=init_soc)
else:
return pybamm.Simulation(
self._unprocessed_model,
experiment=experiment,
parameter_values=parameter_set,
).solve(initial_soc=init_soc)
if experiment is None:
return pybamm.Simulation(
model=self._unprocessed_model,
parameter_values=parameter_set,
).solve(t_eval=t_eval, initial_soc=init_soc)
else:
raise ValueError(
"This sim method currently only supports PyBaMM models"
)
return pybamm.Simulation(
model=self._unprocessed_model,
experiment=experiment,
parameter_values=parameter_set,
).solve(initial_soc=init_soc)

else:
return [np.inf]

def check_params(
self,
inputs: Inputs = None,
parameter_set: ParameterSet = None,
inputs: Optional[Inputs] = None,
parameter_set: Optional[ParameterSet] = None,
allow_infeasible_solutions: bool = True,
):
"""
@@ -551,6 +548,8 @@ def check_params(
----------
inputs : Inputs
The input parameters for the simulation.
parameter_set : pybop.parameter_set, optional
A PyBOP parameter set object or a dictionary containing the parameter values.
allow_infeasible_solutions : bool, optional
If True, infeasible parameter values will be allowed in the optimisation (default: True).

@@ -560,14 +559,20 @@ def check_params(
A boolean which signifies whether the parameters are compatible.

"""
inputs = self.parameters.verify(inputs)
inputs = self.parameters.verify(inputs) or {}
parameter_set = parameter_set or self._parameter_set

return self._check_params(
inputs=inputs, allow_infeasible_solutions=allow_infeasible_solutions
inputs=inputs,
parameter_set=parameter_set,
allow_infeasible_solutions=allow_infeasible_solutions,
)

def _check_params(
self, inputs: Inputs = None, allow_infeasible_solutions: bool = True
self,
inputs: Inputs,
parameter_set: ParameterSet,
allow_infeasible_solutions: bool = True,
):
"""
A compatibility check for the model parameters which can be implemented by subclasses
@@ -577,6 +582,8 @@ def _check_params(
----------
inputs : Inputs
The input parameters for the simulation.
parameter_set : pybop.parameter_set
A PyBOP parameter set object or a dictionary containing the parameter values.
allow_infeasible_solutions : bool, optional
If True, infeasible parameter values will be allowed in the optimisation (default: True).

11 changes: 9 additions & 2 deletions pybop/models/empirical/base_ecm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pybop.models.base_model import BaseModel, Inputs
from pybop.parameters.parameter_set import ParameterSet


class ECircuitModel(BaseModel):
@@ -85,21 +86,27 @@ def __init__(
self._disc = None
self.geometric_parameters = {}

def _check_params(self, inputs: Inputs = None, allow_infeasible_solutions=True):
def _check_params(
self,
inputs: Inputs,
parameter_set: ParameterSet,
allow_infeasible_solutions: bool = True,
):
"""
Check the compatibility of the model parameters.

Parameters
----------
inputs : Inputs
The input parameters for the simulation.
parameter_set : pybop.parameter_set
A PyBOP parameter set object or a dictionary containing the parameter values.
allow_infeasible_solutions : bool, optional
If True, infeasible parameter values will be allowed in the optimisation (default: True).

Returns
-------
bool
A boolean which signifies whether the parameters are compatible.

"""
return True
20 changes: 0 additions & 20 deletions pybop/models/empirical/ecm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from pybamm import equivalent_circuit as pybamm_equivalent_circuit

from pybop.models.empirical.base_ecm import ECircuitModel
from pybop.parameters.parameter import Inputs


class Thevenin(ECircuitModel):
@@ -44,22 +43,3 @@ def __init__(
super().__init__(
pybamm_model=pybamm_equivalent_circuit.Thevenin, name=name, **model_kwargs
)

def _check_params(self, inputs: Inputs = None, allow_infeasible_solutions=True):
"""
Check the compatibility of the model parameters.

Parameters
----------
inputs : Inputs
The input parameters for the simulation.
allow_infeasible_solutions : bool, optional
If True, infeasible parameter values will be allowed in the optimisation (default: True).

Returns
-------
bool
A boolean which signifies whether the parameters are compatible.

"""
return True
10 changes: 7 additions & 3 deletions pybop/models/lithium_ion/base_echem.py
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
from pybamm import lithium_ion as pybamm_lithium_ion

from pybop.models.base_model import BaseModel, Inputs
from pybop.parameters.parameter_set import ParameterSet


class EChemBaseModel(BaseModel):
@@ -85,7 +86,10 @@ def __init__(
self.geometric_parameters = self.set_geometric_parameters()

def _check_params(
self, inputs: Inputs = None, parameter_set=None, allow_infeasible_solutions=True
self,
inputs: Inputs,
parameter_set: ParameterSet,
allow_infeasible_solutions: bool = True,
):
"""
Check compatibility of the model parameters.
@@ -94,6 +98,8 @@ def _check_params(
----------
inputs : Inputs
The input parameters for the simulation.
parameter_set : pybop.parameter_set
A PyBOP parameter set object or a dictionary containing the parameter values.
allow_infeasible_solutions : bool, optional
If True, infeasible parameter values will be allowed in the optimisation (default: True).

@@ -102,8 +108,6 @@ def _check_params(
bool
A boolean which signifies whether the parameters are compatible.
"""
parameter_set = parameter_set or self._parameter_set

if self.pybamm_model.options["working electrode"] == "positive":
electrode_params = [
(
5 changes: 4 additions & 1 deletion tests/unit/test_models.py
Original file line number Diff line number Diff line change
@@ -85,7 +85,10 @@ def test_non_default_solver(self):
def test_predict_without_pybamm(self, model):
model._unprocessed_model = None

with pytest.raises(ValueError):
with pytest.raises(
ValueError,
match="The predict method currently only supports PyBaMM models.",
):
model.predict(None, None)

@pytest.mark.unit