From 8674eeb861a37b5217e3ab225a9a7dda1252d345 Mon Sep 17 00:00:00 2001 From: NicolaCourtier <45851982+NicolaCourtier@users.noreply.github.com> Date: Wed, 3 Jul 2024 16:28:52 +0100 Subject: [PATCH 1/4] Add keys to ParameterSet --- pybop/models/empirical/base_ecm.py | 6 +++--- pybop/models/lithium_ion/base_echem.py | 5 +---- pybop/parameters/parameter_set.py | 7 +++++++ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pybop/models/empirical/base_ecm.py b/pybop/models/empirical/base_ecm.py index 8d15442d1..c8252c041 100644 --- a/pybop/models/empirical/base_ecm.py +++ b/pybop/models/empirical/base_ecm.py @@ -52,14 +52,14 @@ def __init__( # Correct OCP if set to default if ( parameter_set is not None - and "Open-circuit voltage [V]" in parameter_set.params + and "Open-circuit voltage [V]" in parameter_set.keys() ): default_ocp = self.pybamm_model.default_parameter_values[ "Open-circuit voltage [V]" ] - if parameter_set.params["Open-circuit voltage [V]"] == "default": + if parameter_set["Open-circuit voltage [V]"] == "default": print("Setting open-circuit voltage to default function") - parameter_set.params["Open-circuit voltage [V]"] = default_ocp + parameter_set["Open-circuit voltage [V]"] = default_ocp super().__init__(name=name, parameter_set=parameter_set) diff --git a/pybop/models/lithium_ion/base_echem.py b/pybop/models/lithium_ion/base_echem.py index 6947774bf..23aae9d9f 100644 --- a/pybop/models/lithium_ion/base_echem.py +++ b/pybop/models/lithium_ion/base_echem.py @@ -47,10 +47,7 @@ def __init__( solver=None, **model_kwargs, ): - super().__init__( - name=name, - parameter_set=parameter_set, - ) + super().__init__(name=name, parameter_set=parameter_set) model_options = dict(build=False) for key, value in model_kwargs.items(): diff --git a/pybop/parameters/parameter_set.py b/pybop/parameters/parameter_set.py index a1ab63cd3..cf0d11423 100644 --- a/pybop/parameters/parameter_set.py +++ b/pybop/parameters/parameter_set.py @@ -1,5 +1,6 @@ import json import types +from typing import List from pybamm import ParameterValues, parameter_sets @@ -34,6 +35,12 @@ def __setitem__(self, key, value): def __getitem__(self, key): return self.params[key] + def keys(self) -> List: + """ + A list of parameter names + """ + return list(self.params.keys()) + def import_parameters(self, json_path=None): """ Imports parameters from a JSON file specified by the `json_path` attribute. From b80b136b362a77a4a95cdf74abf38d0a770b924d Mon Sep 17 00:00:00 2001 From: NicolaCourtier <45851982+NicolaCourtier@users.noreply.github.com> Date: Wed, 3 Jul 2024 16:29:32 +0100 Subject: [PATCH 2/4] Remove pybamm import from exp_UKF --- examples/scripts/exp_UKF.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/scripts/exp_UKF.py b/examples/scripts/exp_UKF.py index d469c781e..ff7a7045d 100644 --- a/examples/scripts/exp_UKF.py +++ b/examples/scripts/exp_UKF.py @@ -1,11 +1,10 @@ import numpy as np -import pybamm import pybop from examples.standalone.model import ExponentialDecay # Parameter set and model definition -parameter_set = pybamm.ParameterValues({"k": "[input]", "y0": "[input]"}) +parameter_set = {"k": "[input]", "y0": "[input]"} model = ExponentialDecay(parameter_set=parameter_set, n_states=1) # Fitting parameters From 90c8bc10930398f39094ef9e76d038a595b72ad4 Mon Sep 17 00:00:00 2001 From: NicolaCourtier <45851982+NicolaCourtier@users.noreply.github.com> Date: Wed, 3 Jul 2024 16:33:01 +0100 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 063d0e179..eedda555a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ ## Bug Fixes +- [#387](https://github.com/pybop-team/PyBOP/issues/387) - Adds keys to ParameterSet and updates ECM OCV check. - [#380](https://github.com/pybop-team/PyBOP/pull/380) - Restore self._boundaries construction for `pybop.PSO` - [#372](https://github.com/pybop-team/PyBOP/pull/372) - Converts `np.array` to `np.asarray` for Numpy v2.0 support. - [#165](https://github.com/pybop-team/PyBOP/issues/165) - Stores the attempted and best parameter values and the best cost for each iteration in the log attribute of the optimiser and updates the associated plots. From dcf1577d229cdf7698b49315ade069606a1698e6 Mon Sep 17 00:00:00 2001 From: NicolaCourtier <45851982+NicolaCourtier@users.noreply.github.com> Date: Wed, 3 Jul 2024 16:48:15 +0100 Subject: [PATCH 4/4] Update standalone model --- examples/standalone/model.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/standalone/model.py b/examples/standalone/model.py index e67477466..f5d5a7ab2 100644 --- a/examples/standalone/model.py +++ b/examples/standalone/model.py @@ -18,7 +18,8 @@ def __init__( parameter_set: pybamm.ParameterValues = None, n_states: int = 1, ): - super().__init__() + super().__init__(name=name, parameter_set=parameter_set) + self.n_states = n_states if n_states < 1: raise ValueError("The number of states (n_states) must be greater than 0") @@ -38,10 +39,11 @@ def __init__( ) self._unprocessed_model = self.pybamm_model - self.name = name self.default_parameter_values = ( - default_parameter_values if parameter_set is None else parameter_set + default_parameter_values + if self._parameter_set is None + else self._parameter_set ) self._parameter_set = self.default_parameter_values self._unprocessed_parameter_set = self._parameter_set