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

Add parameters class #322

Merged
merged 52 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
7a7d735
Add Parameters class
NicolaCourtier May 10, 2024
f9993cf
Update error type
NicolaCourtier May 10, 2024
b894247
Update parameters in tests
NicolaCourtier May 10, 2024
518b1eb
Update parameters in examples
NicolaCourtier May 10, 2024
916fa4f
Update parameters in notebooks
NicolaCourtier May 10, 2024
7a11dac
Update parameter name
NicolaCourtier May 10, 2024
75780d9
Add parameters len and tests
NicolaCourtier May 13, 2024
08db908
Delay unpacking of parameter properties
NicolaCourtier May 13, 2024
9265528
style: pre-commit fixes
pre-commit-ci[bot] May 13, 2024
76ae239
Merge branch 'develop' into 258-parameters-class
NicolaCourtier May 13, 2024
a0c0845
Merge branch 'develop' into 258-parameters-class
NicolaCourtier May 17, 2024
f46e83b
Replace sample_initial_conditions
NicolaCourtier May 17, 2024
5fd728c
Update test_thevenin_parameterisation.py
NicolaCourtier May 20, 2024
1515703
Test without flaky
NicolaCourtier May 20, 2024
e343dcf
Move x0 def to parameters
NicolaCourtier May 20, 2024
8ffc859
Add parameters.update
NicolaCourtier May 21, 2024
ece7df0
style: pre-commit fixes
pre-commit-ci[bot] May 21, 2024
288d9b3
Define n_parameters for model
NicolaCourtier May 21, 2024
acc4b7b
Make fit_keys a property
NicolaCourtier May 21, 2024
cb09d14
Add parameters as_dict
NicolaCourtier May 21, 2024
7847195
Update exp_UKF.py
NicolaCourtier May 21, 2024
261e7af
Remove fit_keys
NicolaCourtier May 21, 2024
4897a54
Update comment
NicolaCourtier May 21, 2024
382b65f
Fixes for notebooks
NicolaCourtier May 22, 2024
22d3e7f
Check stricter of relative/absolute tolerance
NicolaCourtier May 22, 2024
5dc583e
Change Thevenin parameters
NicolaCourtier May 22, 2024
d042e0d
Update ground truth
NicolaCourtier May 22, 2024
ed9de21
Pass parameters to get_data
NicolaCourtier May 22, 2024
25807a3
Update max values to help GradientDescent
NicolaCourtier May 22, 2024
ad74dc6
Reset Thevenin test but with new C1
NicolaCourtier May 22, 2024
cf7f4e0
Equalise integration test settings
NicolaCourtier May 22, 2024
f2a78e0
Merge branch 'develop' into 258-parameters-class
NicolaCourtier May 22, 2024
6555c58
Update default SciPy DE tol
NicolaCourtier May 23, 2024
242b129
Relax atol a little
NicolaCourtier May 23, 2024
dae5b82
Move sigma0, bounds to BaseOptimiser, update likelihoods
NicolaCourtier May 23, 2024
d29f383
Remove unused bounds_for_scipy
NicolaCourtier May 23, 2024
063ac74
Update from list to args
NicolaCourtier May 23, 2024
48bdd76
Fix test_model_experiment_changes
NicolaCourtier May 23, 2024
15616a3
style: pre-commit fixes
pre-commit-ci[bot] May 23, 2024
5ddad6c
Add ValueError for None sigma
NicolaCourtier May 30, 2024
38525fd
Rename add_parameter as add
NicolaCourtier May 30, 2024
248be07
Rename remove_parameter to remove
NicolaCourtier May 30, 2024
21630e4
Apply suggestions from code review
NicolaCourtier May 30, 2024
4465869
style: pre-commit fixes
pre-commit-ci[bot] May 30, 2024
5402045
Merge branch '258-parameters-class' of https://github.com/pybop-team/…
NicolaCourtier May 30, 2024
4e071f5
Update error match
NicolaCourtier May 30, 2024
455dab5
Update tolerances and threshold
NicolaCourtier May 31, 2024
0ba4b52
Rename update_bounds to set_bounds
NicolaCourtier May 31, 2024
f347ace
Update all_samples to list
NicolaCourtier May 31, 2024
5005b9a
Update set_bounds to get_bounds
NicolaCourtier May 31, 2024
1521512
Move check on sigma
NicolaCourtier Jun 5, 2024
fbb4b23
Update description
NicolaCourtier Jun 5, 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
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
Loading