Skip to content

Commit

Permalink
Pass Inputs dictionary (#359)
Browse files Browse the repository at this point in the history
* Add optimiser.parameters, remove problem.x0

* Update integration tests

* Pass inputs instead of x

* Specify inputs as Inputs

* Update notebooks

* Add initial and true options to as_dict

* Update parameter_values to inputs

* Update notebooks

* Add parameters tests

* Add quick_plot test

* Add test_no_optimisation_parameters

* Add test_error_in_cost_calculation

* Add parameters.verify

* Fix change to base_model

* Add more base_model tests

* fix: restore ValueError on incorrect parameter __getitem__

* Apply suggestions from code review

Co-authored-by: Brady Planden <[email protected]>

* style: pre-commit fixes

* Import Union and Inputs

* Move Inputs definition to parameter.py

* Retrieve x0 from SciPyDE

* Add test for evaluate(List)

* Refactor as suggested

Co-authored-by: Brady Planden <[email protected]>

* style: pre-commit fixes

* Remove duplicate line

* Change quick_plot inputs to problem_inputs

* Remove duplicate lines

---------

Co-authored-by: Brady Planden <[email protected]>
Co-authored-by: Brady Planden <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Jul 4, 2024
1 parent d495ed7 commit 4c4a31e
Show file tree
Hide file tree
Showing 58 changed files with 465 additions and 302 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,7 @@
"source": [
"optim = pybop.PSO(cost, max_unchanged_iterations=55, threshold=1e-6)\n",
"x, final_cost = optim.run()\n",
"print(\"Initial parameters:\", cost.x0)\n",
"print(\"Initial parameters:\", optim.x0)\n",
"print(\"Estimated parameters:\", x)"
]
},
Expand Down Expand Up @@ -1679,7 +1679,7 @@
}
],
"source": [
"pybop.quick_plot(problem, parameter_values=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
]
},
{
Expand Down Expand Up @@ -1850,7 +1850,7 @@
}
],
"source": [
"pybop.quick_plot(problem, parameter_values=x, title=\"Parameter Extrapolation\");"
"pybop.quick_plot(problem, problem_inputs=x, title=\"Parameter Extrapolation\");"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions examples/notebooks/equivalent_circuit_identification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@
"source": [
"optim = pybop.CMAES(cost, max_iterations=300)\n",
"x, final_cost = optim.run()\n",
"print(\"Initial parameters:\", cost.x0)\n",
"print(\"Initial parameters:\", optim.x0)\n",
"print(\"Estimated parameters:\", x)"
]
},
Expand Down Expand Up @@ -457,7 +457,7 @@
}
],
"source": [
"pybop.quick_plot(problem, parameter_values=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/notebooks/multi_model_identification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3905,7 +3905,7 @@
"source": [
"for optim, x in zip(optims, xs):\n",
" pybop.quick_plot(\n",
" optim.cost.problem, parameter_values=x, title=optim.cost.problem.model.name\n",
" optim.cost.problem, problem_inputs=x, title=optim.cost.problem.model.name\n",
" )"
]
},
Expand Down
2 changes: 1 addition & 1 deletion examples/notebooks/multi_optimiser_identification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@
],
"source": [
"for optim, x in zip(optims, xs):\n",
" pybop.quick_plot(optim.cost.problem, parameter_values=x, title=optim.name())"
" pybop.quick_plot(optim.cost.problem, problem_inputs=x, title=optim.name())"
]
},
{
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 @@ -404,7 +404,7 @@
}
],
"source": [
"pybop.quick_plot(problem, parameter_values=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
]
},
{
Expand Down Expand Up @@ -723,7 +723,7 @@
"source": [
"optim = pybop.GradientDescent(cost, sigma0=0.0115)\n",
"x, final_cost = optim.run()\n",
"pybop.quick_plot(problem, parameter_values=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
]
},
{
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 @@ -517,7 +517,7 @@
}
],
"source": [
"pybop.quick_plot(problem, parameter_values=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
]
},
{
Expand Down Expand Up @@ -1539,7 +1539,7 @@
}
],
"source": [
"sol = problem.evaluate(x)\n",
"sol = problem.evaluate(parameters.as_dict(x))\n",
"\n",
"go.Figure(\n",
" [\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/notebooks/spm_AdamW.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@
}
],
"source": [
"pybop.quick_plot(problem, parameter_values=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
]
},
{
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 @@ -277,7 +277,7 @@
"source": [
"x, final_cost = optim.run()\n",
"print(\"Estimated parameters:\", x)\n",
"print(f\"Initial gravimetric energy density: {-cost(cost.x0):.2f} Wh.kg-1\")\n",
"print(f\"Initial gravimetric energy density: {-cost(optim.x0):.2f} Wh.kg-1\")\n",
"print(f\"Optimised gravimetric energy density: {-final_cost:.2f} Wh.kg-1\")"
]
},
Expand Down Expand Up @@ -329,7 +329,7 @@
"source": [
"if cost.update_capacity:\n",
" problem._model.approximate_capacity(x)\n",
"pybop.quick_plot(problem, parameter_values=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/BPX_spm.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
print("Estimated parameters:", x)

# Plot the timeseries output
pybop.quick_plot(problem, parameter_values=x, title="Optimised Comparison")
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison")

# Plot convergence
pybop.plot_convergence(optim)
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/ecm_CMAES.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
pybop.plot_dataset(dataset)

# Plot the timeseries output
pybop.quick_plot(problem, parameter_values=x, title="Optimised Comparison")
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison")

# Plot convergence
pybop.plot_convergence(optim)
Expand Down
9 changes: 5 additions & 4 deletions examples/scripts/exp_UKF.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
sigma = 1e-2
t_eval = np.linspace(0, 20, 10)
model.parameters = parameters
values = model.predict(t_eval=t_eval, inputs=parameters.true_value())
true_inputs = parameters.as_dict("true")
values = model.predict(t_eval=t_eval, inputs=true_inputs)
values = values["2y"].data
corrupt_values = values + np.random.normal(0, sigma, len(t_eval))

Expand All @@ -40,7 +41,7 @@
model.build(parameters=parameters)
simulator = pybop.Observer(parameters, model, signal=["2y"])
simulator._time_data = t_eval
measurements = simulator.evaluate(parameters.true_value())
measurements = simulator.evaluate(true_inputs)

# Verification step: Compare by plotting
go = pybop.PlotlyManager().go
Expand Down Expand Up @@ -83,7 +84,7 @@
)

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

# Verification step: Add the estimate to the plot
line4 = go.Scatter(
Expand All @@ -101,7 +102,7 @@
print("Estimated parameters:", x)

# Plot the timeseries output (requires model that returns Voltage)
pybop.quick_plot(observer, parameter_values=x, title="Optimised Comparison")
pybop.quick_plot(observer, problem_inputs=x, title="Optimised Comparison")

# Plot convergence
pybop.plot_convergence(optim)
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/gitt.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
print("Estimated parameters:", x)

# Plot the timeseries output
pybop.quick_plot(problem, parameter_values=x, title="Optimised Comparison")
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison")

# Plot convergence
pybop.plot_convergence(optim)
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/spm_AdamW.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def noise(sigma):
print("Estimated parameters:", x)

# Plot the timeseries output
pybop.quick_plot(problem, parameter_values=x, title="Optimised Comparison")
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison")

# Plot convergence
pybop.plot_convergence(optim)
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/spm_CMAES.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
pybop.plot_dataset(dataset)

# Plot the timeseries output
pybop.quick_plot(problem, parameter_values=x, title="Optimised Comparison")
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison")

# Plot convergence
pybop.plot_convergence(optim)
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/spm_IRPropMin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
print("Estimated parameters:", x)

# Plot the timeseries output
pybop.quick_plot(problem, parameter_values=x, title="Optimised Comparison")
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison")

# Plot convergence
pybop.plot_convergence(optim)
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/spm_MAP.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
print("Estimated parameters:", x)

# Plot the timeseries output
pybop.quick_plot(problem, parameter_values=x[0:2], title="Optimised Comparison")
pybop.quick_plot(problem, problem_inputs=x[0:2], title="Optimised Comparison")

# Plot convergence
pybop.plot_convergence(optim)
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/spm_MLE.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
print("Estimated parameters:", x)

# Plot the timeseries output
pybop.quick_plot(problem, parameter_values=x[0:2], title="Optimised Comparison")
pybop.quick_plot(problem, problem_inputs=x[0:2], title="Optimised Comparison")

# Plot convergence
pybop.plot_convergence(optim)
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/spm_NelderMead.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def noise(sigma):
print("Estimated parameters:", x)

# Plot the timeseries output
pybop.quick_plot(problem, parameter_values=x, title="Optimised Comparison")
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison")

# Plot convergence
pybop.plot_convergence(optim)
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/spm_SNES.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
print("Estimated parameters:", x)

# Plot the timeseries output
pybop.quick_plot(problem, parameter_values=x, title="Optimised Comparison")
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison")

# Plot convergence
pybop.plot_convergence(optim)
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/spm_UKF.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
print("Estimated parameters:", x)

# Plot the timeseries output (requires model that returns Voltage)
pybop.quick_plot(observer, parameter_values=x, title="Optimised Comparison")
pybop.quick_plot(observer, problem_inputs=x, title="Optimised Comparison")

# # Plot convergence
# pybop.plot_convergence(optim)
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/spm_XNES.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
print("Estimated parameters:", x)

# Plot the timeseries output
pybop.quick_plot(problem, parameter_values=x, title="Optimised Comparison")
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison")

# Plot convergence
pybop.plot_convergence(optim)
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/spm_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
print("Estimated parameters:", x)

# Plot the timeseries output
pybop.quick_plot(problem, parameter_values=x, title="Optimised Comparison")
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison")

# Plot convergence
pybop.plot_convergence(optim)
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/spm_pso.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
print("Estimated parameters:", x)

# Plot the timeseries output
pybop.quick_plot(problem, parameter_values=x, title="Optimised Comparison")
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison")

# Plot convergence
pybop.plot_convergence(optim)
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/spm_scipymin.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
print("Estimated parameters:", x)

# Plot the timeseries output
pybop.quick_plot(problem, parameter_values=x, title="Optimised Comparison")
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison")

# Plot convergence
pybop.plot_convergence(optim)
Expand Down
6 changes: 3 additions & 3 deletions examples/scripts/spme_max_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# NOTE: This script can be easily adjusted to consider the volumetric
# (instead of gravimetric) energy density by changing the line which
# defines the cost and changing the output to:
# print(f"Initial volumetric energy density: {cost(cost.x0):.2f} Wh.m-3")
# print(f"Initial volumetric energy density: {cost(optim.x0):.2f} Wh.m-3")
# print(f"Optimised volumetric energy density: {final_cost:.2f} Wh.m-3")

# Define parameter set and model
Expand Down Expand Up @@ -54,13 +54,13 @@
# Run optimisation
x, final_cost = optim.run()
print("Estimated parameters:", x)
print(f"Initial gravimetric energy density: {cost(cost.x0):.2f} Wh.kg-1")
print(f"Initial gravimetric energy density: {cost(optim.x0):.2f} Wh.kg-1")
print(f"Optimised gravimetric energy density: {final_cost:.2f} Wh.kg-1")

# Plot the timeseries output
if cost.update_capacity:
problem._model.approximate_capacity(x)
pybop.quick_plot(problem, parameter_values=x, title="Optimised Comparison")
pybop.quick_plot(problem, problem_inputs=x, title="Optimised Comparison")

# Plot the cost landscape with optimisation path
if len(x) == 2:
Expand Down
9 changes: 4 additions & 5 deletions examples/standalone/cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, problem=None):
)
self.x0 = self.parameters.initial_value()

def _evaluate(self, x, grad=None):
def _evaluate(self, inputs, grad=None):
"""
Calculate the cost for a given parameter value.
Expand All @@ -52,9 +52,8 @@ def _evaluate(self, x, grad=None):
Parameters
----------
x : array-like
A one-element array containing the parameter value for which to
evaluate the cost.
inputs : Dict
The parameters for which to evaluate the cost.
grad : array-like, optional
Unused parameter, present for compatibility with gradient-based
optimizers.
Expand All @@ -65,4 +64,4 @@ def _evaluate(self, x, grad=None):
The calculated cost value for the given parameter.
"""

return x[0] ** 2 + 42
return inputs["x"] ** 2 + 42
Loading

0 comments on commit 4c4a31e

Please sign in to comment.