Skip to content

Commit

Permalink
Add parameters class (#322)
Browse files Browse the repository at this point in the history
* Add Parameters class

* Update error type

* Update parameters in tests

* Update parameters in examples

* Update parameters in notebooks

* Update parameter name

* Add parameters len and tests

* Delay unpacking of parameter properties

* Replace sample_initial_conditions

* Update test_thevenin_parameterisation.py

* Test without flaky

* Move x0 def to parameters

* Add parameters.update

* Define n_parameters for model

* Make fit_keys a property

* Add parameters as_dict

* Update exp_UKF.py

* Remove fit_keys

* Update comment

* Fixes for notebooks

* Check stricter of relative/absolute tolerance

* Change Thevenin parameters

* Update ground truth

* Pass parameters to get_data

* Update max values to help GradientDescent

* Reset Thevenin test but with new C1

* Equalise integration test settings

* Update default SciPy DE tol

* Relax atol a little

* Move sigma0, bounds to BaseOptimiser, update likelihoods

* Remove unused bounds_for_scipy

* Update from list to args

* Fix test_model_experiment_changes

* style: pre-commit fixes

* Add ValueError for None sigma

* Rename add_parameter as add

* Rename remove_parameter to remove

* Apply suggestions from code review

Co-authored-by: Brady Planden <55357039+BradyPlanden@users.noreply.github.com>

* Update error match

* Update tolerances and threshold

* Rename update_bounds to set_bounds

* Update all_samples to list

* Update set_bounds to get_bounds

* Move check on sigma

* Update description

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Brady Planden <55357039+BradyPlanden@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 5, 2024
1 parent 8b2524a commit 02cbb14
Show file tree
Hide file tree
Showing 62 changed files with 834 additions and 624 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,7 @@
"id": "bf63b4f9-de38-4e70-9472-1de4973a0954",
"metadata": {},
"source": [
"In this example, we are going to try to fit all five parameters at once. To do this, we define a `pybop.parameter` for each fitting parameter as,"
"In this example, we are going to try to fit all five parameters at once. To do this, we define a `pybop.Parameter` for each fitting parameter and compile them in pybop.Parameters,"
]
},
{
Expand All @@ -1484,7 +1484,7 @@
},
"outputs": [],
"source": [
"parameters = [\n",
"parameters = pybop.Parameters(\n",
" pybop.Parameter(\n",
" \"R0 [Ohm]\",\n",
" prior=pybop.Gaussian(0.005, 0.0001),\n",
Expand All @@ -1510,7 +1510,7 @@
" prior=pybop.Gaussian(3000, 2500),\n",
" bounds=[0.5, 1e4],\n",
" ),\n",
"]"
")"
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions examples/notebooks/equivalent_circuit_identification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
"id": "bf63b4f9-de38-4e70-9472-1de4973a0954",
"metadata": {},
"source": [
"In this example, we are going to try to fit all five parameters at once. This isn't recommend for real-life application as identifiablity is challenging to guarantee with this large a parameter space. To do this, we define the `pybop.parameters` as,"
"In this example, we are going to try to fit all five parameters at once. This isn't recommend for real-life application as identifiablity is challenging to guarantee with this large a parameter space. To do this, we define the `pybop.Parameters` as,"
]
},
{
Expand All @@ -256,7 +256,7 @@
},
"outputs": [],
"source": [
"parameters = [\n",
"parameters = pybop.Parameters(\n",
" pybop.Parameter(\n",
" \"R0 [Ohm]\",\n",
" prior=pybop.Gaussian(0.0002, 0.0001),\n",
Expand All @@ -278,11 +278,11 @@
" bounds=[2500, 5e4],\n",
" ),\n",
" pybop.Parameter(\n",
" \"C1 [F]\",\n",
" \"C2 [F]\",\n",
" prior=pybop.Gaussian(10000, 2500),\n",
" bounds=[2500, 5e4],\n",
" ),\n",
"]"
")"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions examples/notebooks/multi_model_identification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3729,7 +3729,7 @@
},
"outputs": [],
"source": [
"parameters = [\n",
"parameters = pybop.Parameters(\n",
" pybop.Parameter(\n",
" \"Positive electrode thickness [m]\",\n",
" prior=pybop.Gaussian(7.56e-05, 0.05e-05),\n",
Expand All @@ -3742,7 +3742,7 @@
" bounds=[65e-06, 90e-06],\n",
" true_value=parameter_set[\"Negative electrode thickness [m]\"],\n",
" ),\n",
"]"
")"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions examples/notebooks/multi_optimiser_identification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
},
"outputs": [],
"source": [
"parameters = [\n",
"parameters = pybop.Parameters(\n",
" pybop.Parameter(\n",
" \"Negative electrode active material volume fraction\",\n",
" prior=pybop.Gaussian(0.6, 0.02),\n",
Expand All @@ -270,7 +270,7 @@
" prior=pybop.Gaussian(0.48, 0.02),\n",
" bounds=[0.4, 0.7],\n",
" ),\n",
"]"
")"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions examples/notebooks/optimiser_calibration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
},
"outputs": [],
"source": [
"parameters = [\n",
"parameters = pybop.Parameters(\n",
" pybop.Parameter(\n",
" \"Negative electrode active material volume fraction\",\n",
" prior=pybop.Gaussian(0.7, 0.025),\n",
Expand All @@ -243,7 +243,7 @@
" prior=pybop.Gaussian(0.6, 0.025),\n",
" bounds=[0.5, 0.8],\n",
" ),\n",
"]"
")"
]
},
{
Expand Down
12 changes: 5 additions & 7 deletions examples/notebooks/optimiser_interface.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,11 @@
")\n",
"\n",
"# Define the parameters\n",
"parameters = [\n",
" pybop.Parameter(\n",
" \"R0 [Ohm]\",\n",
" prior=pybop.Gaussian(0.0002, 0.0001),\n",
" bounds=[1e-4, 1e-2],\n",
" )\n",
"]\n",
"parameters = pybop.Parameter(\n",
" \"R0 [Ohm]\",\n",
" prior=pybop.Gaussian(0.0002, 0.0001),\n",
" bounds=[1e-4, 1e-2],\n",
")\n",
"\n",
"# Generate synthetic data\n",
"t_eval = np.arange(0, 900, 2)\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/notebooks/pouch_cell_identification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@
},
"outputs": [],
"source": [
"parameters = [\n",
"parameters = pybop.Parameters(\n",
" pybop.Parameter(\n",
" \"Negative electrode active material volume fraction\",\n",
" prior=pybop.Gaussian(0.7, 0.05),\n",
Expand All @@ -334,7 +334,7 @@
" prior=pybop.Gaussian(0.58, 0.05),\n",
" bounds=[0.5, 0.8],\n",
" ),\n",
"]"
")"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions examples/notebooks/spm_electrode_design.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
},
"outputs": [],
"source": [
"parameters = [\n",
"parameters = pybop.Parameters(\n",
" pybop.Parameter(\n",
" \"Positive electrode thickness [m]\",\n",
" prior=pybop.Gaussian(7.56e-05, 0.05e-05),\n",
Expand All @@ -154,7 +154,7 @@
" prior=pybop.Gaussian(5.22e-06, 0.05e-06),\n",
" bounds=[2e-06, 9e-06],\n",
" ),\n",
"]"
")"
]
},
{
Expand Down
12 changes: 3 additions & 9 deletions examples/scripts/BPX_spm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
model = pybop.lithium_ion.SPM(parameter_set=parameter_set)

# Fitting parameters
parameters = [
parameters = pybop.Parameters(
pybop.Parameter(
"Negative particle radius [m]",
prior=pybop.Gaussian(6e-06, 0.1e-6),
Expand All @@ -23,7 +23,7 @@
bounds=[1e-7, 9e-7],
true_value=parameter_set["Positive particle radius [m]"],
),
]
)

# Generate data
sigma = 0.001
Expand All @@ -47,13 +47,7 @@

# Run the optimisation
x, final_cost = optim.run()
print(
"True parameters:",
[
parameters[0].true_value,
parameters[1].true_value,
],
)
print("True parameters:", parameters.true_value())
print("Estimated parameters:", x)

# Plot the timeseries output
Expand Down
4 changes: 2 additions & 2 deletions examples/scripts/ecm_CMAES.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
)

# Fitting parameters
parameters = [
parameters = pybop.Parameters(
pybop.Parameter(
"R0 [Ohm]",
prior=pybop.Gaussian(0.0002, 0.0001),
Expand All @@ -56,7 +56,7 @@
prior=pybop.Gaussian(0.0001, 0.0001),
bounds=[1e-5, 1e-2],
),
]
)

sigma = 0.001
t_eval = np.arange(0, 900, 3)
Expand Down
26 changes: 12 additions & 14 deletions examples/scripts/exp_UKF.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,41 @@
# Parameter set and model definition
parameter_set = pybamm.ParameterValues({"k": "[input]", "y0": "[input]"})
model = ExponentialDecay(parameter_set=parameter_set, n_states=1)
x0 = np.array([0.1, 1.0])

# Fitting parameters
parameters = [
parameters = pybop.Parameters(
pybop.Parameter(
"k",
prior=pybop.Gaussian(0.1, 0.05),
bounds=[0, 1],
true_value=0.1,
),
pybop.Parameter(
"y0",
prior=pybop.Gaussian(1, 0.05),
bounds=[0, 3],
true_value=1.0,
),
]

# Verification: save fixed inputs for testing
inputs = dict()
for i, param in enumerate(parameters):
inputs[param.name] = x0[i]
)

# Make a prediction with measurement noise
sigma = 1e-2
t_eval = np.linspace(0, 20, 10)
values = model.predict(t_eval=t_eval, inputs=inputs)
model.parameters = parameters
values = model.predict(t_eval=t_eval, inputs=parameters.true_value())
values = values["2y"].data
corrupt_values = values + np.random.normal(0, sigma, len(t_eval))

# Verification step: compute the analytical solution for 2y
expected_values = 2 * inputs["y0"] * np.exp(-inputs["k"] * t_eval)
expected_values = (
2 * parameters["y0"].true_value * np.exp(-parameters["k"].true_value * t_eval)
)

# Verification step: make another prediction using the Observer class
model.build(parameters=parameters)
simulator = pybop.Observer(parameters, model, signal=["2y"], x0=x0)
simulator = pybop.Observer(parameters, model, signal=["2y"])
simulator._time_data = t_eval
measurements = simulator.evaluate(x0)
measurements = simulator.evaluate(parameters.true_value())

# Verification step: Compare by plotting
go = pybop.PlotlyManager().go
Expand Down Expand Up @@ -82,11 +81,10 @@
measurement_noise,
dataset,
signal=signal,
x0=x0,
)

# Verification step: Find the maximum likelihood estimate given the true parameters
estimation = observer.evaluate(x0)
estimation = observer.evaluate(parameters.true_value())

# Verification step: Add the estimate to the plot
line4 = go.Scatter(
Expand Down
14 changes: 6 additions & 8 deletions examples/scripts/gitt.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@
# Define the cost to optimise
model = pybop.lithium_ion.WeppnerHuggins(parameter_set=parameter_set)

parameters = [
pybop.Parameter(
"Positive electrode diffusivity [m2.s-1]",
prior=pybop.Gaussian(5e-14, 1e-13),
bounds=[1e-16, 1e-11],
true_value=parameter_set["Positive electrode diffusivity [m2.s-1]"],
),
]
parameters = pybop.Parameter(
"Positive electrode diffusivity [m2.s-1]",
prior=pybop.Gaussian(5e-14, 1e-13),
bounds=[1e-16, 1e-11],
true_value=parameter_set["Positive electrode diffusivity [m2.s-1]"],
)

problem = pybop.FittingProblem(
model,
Expand Down
12 changes: 3 additions & 9 deletions examples/scripts/spm_CMAES.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
model = pybop.lithium_ion.SPM(parameter_set=parameter_set)

# Fitting parameters
parameters = [
parameters = pybop.Parameters(
pybop.Parameter(
"Negative particle radius [m]",
prior=pybop.Gaussian(6e-06, 0.1e-6),
Expand All @@ -20,7 +20,7 @@
bounds=[1e-6, 9e-6],
true_value=parameter_set["Positive particle radius [m]"],
),
]
)

# Generate data
sigma = 0.001
Expand All @@ -46,13 +46,7 @@

# Run the optimisation
x, final_cost = optim.run()
print(
"True parameters:",
[
parameters[0].true_value,
parameters[1].true_value,
],
)
print("True parameters:", parameters.true_value())
print("Estimated parameters:", x)

# Plot the time series
Expand Down
4 changes: 2 additions & 2 deletions examples/scripts/spm_IRPropMin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
model = pybop.lithium_ion.SPM(parameter_set=parameter_set)

# Fitting parameters
parameters = [
parameters = pybop.Parameters(
pybop.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Gaussian(0.6, 0.05),
Expand All @@ -16,7 +16,7 @@
"Positive electrode active material volume fraction",
prior=pybop.Gaussian(0.48, 0.05),
),
]
)

# Generate data
sigma = 0.001
Expand Down
4 changes: 2 additions & 2 deletions examples/scripts/spm_MAP.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
model = pybop.lithium_ion.SPM(parameter_set=parameter_set)

# Fitting parameters
parameters = [
parameters = pybop.Parameters(
pybop.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Gaussian(0.6, 0.05),
Expand All @@ -18,7 +18,7 @@
prior=pybop.Gaussian(0.48, 0.05),
bounds=[0.4, 0.7],
),
]
)

# Set initial parameter values
parameter_set.update(
Expand Down
4 changes: 2 additions & 2 deletions examples/scripts/spm_MLE.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
model = pybop.lithium_ion.SPM(parameter_set=parameter_set)

# Fitting parameters
parameters = [
parameters = pybop.Parameters(
pybop.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Gaussian(0.6, 0.05),
Expand All @@ -18,7 +18,7 @@
prior=pybop.Gaussian(0.48, 0.05),
bounds=[0.4, 0.7],
),
]
)

# Set initial parameter values
parameter_set.update(
Expand Down
Loading

0 comments on commit 02cbb14

Please sign in to comment.