-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from nasa/ipynb-benchmarking
Ipynb benchmarking
- Loading branch information
Showing
1 changed file
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,171 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Welcome to ProgPy's Benchmarking Example" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The goal of this notebook is to demonstrate benchmarking Prognostic Models. Specifically, we will demonstrate how to benchmark the computational efficiency of model simulation. This is typically what you want to look at when benchmarking models, since simulation is the foundation of state estimation and prediction." | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"First, we need to import the necessary modules." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from progpy.models import BatteryCircuit\n", | ||
"from timeit import timeit" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The first import is importing a model to benchmark. In this case, ProgPy's BatteryCircuit Model. The second import is of the timeit tool, which will be used to benchmark our model." | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Next, let's create our Battery Circuit model." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Step 1: Create a model object\n", | ||
"batt = BatteryCircuit()" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Then, for our model, we will need to define a future loading function. More information on what a future loading function is and how to use it can be found here: https://nasa.github.io/progpy/prog_models_guide.html#future-loading" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Since this is a simple example, we are going to have a constant loading!" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Step 2: Define future loading function \n", | ||
"loading = batt.InputContainer({'i': 2}) # Constant loading\n", | ||
"def future_loading(t, x=None):\n", | ||
" # Constant Loading\n", | ||
" return loading" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Finally, we are ready to benchmark the simulation.\n", | ||
"\n", | ||
"We can do this by using the `timeit()` function and pass in our `simulate_to()` or `simulate_to_threshold()` function for the `stmt` argument. For more information regarding the `timeit()` function, please read its documentation located here: https://docs.python.org/3/library/timeit.html" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Step 3: Benchmark simulation of 600 seconds\n", | ||
"def sim(): \n", | ||
" batt.simulate_to(600, future_loading)\n", | ||
"time = timeit(sim, number=500)" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"In this example, we are benchmarking the simulation for the BatteryCircuit model up to 600 seconds. Furthermore, we define our `number` argument to be 500 for sake of runtime." | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's print out the results of the benchmark test!" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Print results\n", | ||
"print('Simulation Time: {} ms/sim'.format(time))" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"In this example, we benchmarked the simulation of the BatteryCircuit model up to 600 seconds by utilizing the `time` package!" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.2" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |