-
Notifications
You must be signed in to change notification settings - Fork 57
Add saving/loading example #945
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7db9c4f
Update tests
NicolaCourtier ac5fdc5
Save and load parameter names
NicolaCourtier 16953b6
Create saving_and_loading_results.py
NicolaCourtier ee0c0ee
Allow list or array
NicolaCourtier 652771e
Format example scripts
NicolaCourtier 1b70675
Fix indentation
NicolaCourtier 4186127
Create save path
NicolaCourtier 553b4cb
implement Sarah's suggestion
brosaplanella File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
examples/scripts/getting_started/saving_and_loading_results.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import os | ||
|
|
||
| import numpy as np | ||
| import pybamm | ||
|
|
||
| import pybop | ||
|
|
||
| """ | ||
| This example shows how to save and load an optimisation (or sampling) result. | ||
| First we run an example optimisation to generate a result. | ||
| """ | ||
|
|
||
| # Define model and parameter values | ||
| model = pybamm.lithium_ion.SPM() | ||
| parameter_values = pybamm.ParameterValues("Chen2020") | ||
|
|
||
| # Generate a synthetic dataset | ||
| sigma = 5e-3 | ||
| t_eval = np.linspace(0, 500, 240) | ||
| solution = pybamm.Simulation(model, parameter_values=parameter_values).solve( | ||
| t_eval=t_eval | ||
| ) | ||
| dataset = pybop.Dataset( | ||
| { | ||
| "Time [s]": t_eval, | ||
| "Current [A]": solution["Current [A]"](t_eval), | ||
| "Voltage [V]": pybop.add_noise(solution["Voltage [V]"](t_eval), sigma), | ||
| } | ||
| ) | ||
|
|
||
| # Fitting parameters | ||
| parameter_values.update( | ||
| { | ||
| "Negative electrode active material volume fraction": pybop.Parameter( | ||
| distribution=pybop.Gaussian(0.68, 0.05, truncated_at=[0.4, 0.9]), | ||
| initial_value=0.45, | ||
| ), | ||
| "Positive electrode active material volume fraction": pybop.Parameter( | ||
| distribution=pybop.Gaussian(0.58, 0.05, truncated_at=[0.4, 0.9]), | ||
| initial_value=0.45, | ||
| ), | ||
| } | ||
| ) | ||
|
|
||
| # Build the problem | ||
| simulator = pybop.pybamm.Simulator( | ||
| model, parameter_values=parameter_values, protocol=dataset | ||
| ) | ||
| cost = pybop.MeanAbsoluteError(dataset) | ||
| problem = pybop.Problem(simulator, cost) | ||
|
|
||
| # Set up the optimiser | ||
| options = pybop.PintsOptions(max_iterations=150, verbose=True) | ||
| optim = pybop.PSO(problem, options=options) | ||
|
|
||
| # Run the optimisation | ||
| result = optim.run() | ||
|
|
||
| # Save the result: either pickle the whole result or save the data in | ||
| # one of these formats: "pickle", "json", "matlab" | ||
| save_path = "examples/results/" | ||
| os.makedirs(os.path.dirname(save_path), exist_ok=True) | ||
| result.save(save_path + "saved_result_object.pkl") | ||
| result.save_data(save_path + "saved_result_data.json", to_format="json") | ||
|
|
||
| # Load the result | ||
| result_from_pkl = pybop.Result.load(save_path + "saved_result_object.pkl") | ||
| result_from_json = pybop.Result.load_data( | ||
| save_path + "saved_result_data.json", file_format="json" | ||
| ) | ||
|
|
||
| # Plot the optimisation result from .pkl | ||
| result_from_pkl.plot_convergence() | ||
| result_from_pkl.plot_parameters() | ||
| result_from_pkl.plot_surface(bounds=problem.parameters.get_bounds_array()) | ||
|
|
||
| # Plot the optimisation result from .json (it is the same) | ||
| result_from_json.plot_convergence() | ||
| result_from_json.plot_parameters() | ||
| result_from_json.plot_surface(bounds=problem.parameters.get_bounds_array()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to have two Result objects here, one for the result loaded from the pickle file and one for the result reconstructed from the data in the json file? Maybe even plot both to illustrate that they hold the same data?
It might otherwise look like the load_data function is needed to populate the result object loaded from the pickle file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, either is fine by me