Skip to content

Commit 1a51082

Browse files
committed
DOC: updating notebook example and class documentation after callback refactoring
1 parent 1a09345 commit 1a51082

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

docs/notebooks/monte_carlo_analysis/monte_carlo_class_usage.ipynb

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@
11291129
"source": [
11301130
"We have shown, so far, how to perform to use the `MonteCarlo` class and visualize its results. By default, some variables exported to the output files, such as *apogee* and *x_impact*. The `export_list` argument provides a simplified way for the user to export additional variables listed in the documentation, such as *inclination* and *heading*. \n",
11311131
"\n",
1132-
"There are applications in which you might need to extract more information in the results than the `export_list` argument can handle. To that end, the `MonteCarlo` class has a `export_function` argument which allows you customize further the output of the simulation.\n",
1132+
"There are applications in which you might need to extract more information in the results than the `export_list` argument can handle. To that end, the `MonteCarlo` class has a `data_collector` argument which allows you customize further the output of the simulation.\n",
11331133
"\n",
11341134
"To exemplify its use, we show how to export the *date* of the environment used in the simulation together with the *average reynolds number* along with the default variables."
11351135
]
@@ -1138,36 +1138,39 @@
11381138
"cell_type": "markdown",
11391139
"metadata": {},
11401140
"source": [
1141-
"We will use the `stochastic_env`, `stochastic_rocket` and `stochastic_flight` objects previously defined, and only change the `MonteCarlo` object. First, we need to define our customized export function."
1141+
"We will use the `stochastic_env`, `stochastic_rocket` and `stochastic_flight` objects previously defined, and only change the `MonteCarlo` object. First, we need to define our customized data collector."
11421142
]
11431143
},
11441144
{
11451145
"cell_type": "code",
1146-
"execution_count": 1,
1146+
"execution_count": null,
11471147
"metadata": {},
11481148
"outputs": [],
11491149
"source": [
11501150
"import numpy as np\n",
11511151
"\n",
1152-
"\n",
1153-
"def custom_export_function(flight):\n",
1152+
"# Defining custom callback functions\n",
1153+
"def get_average_reynolds_number(flight):\n",
11541154
" reynold_number_list = flight.reynolds_number(flight.time)\n",
11551155
" average_reynolds_number = np.mean(reynold_number_list)\n",
1156-
" custom_exports = {\n",
1157-
" \"average_reynolds_number\": average_reynolds_number,\n",
1158-
" \"date\": flight.env.date,\n",
1159-
" }\n",
1160-
" return custom_exports"
1156+
" return average_reynolds_number\n",
1157+
"\n",
1158+
"def get_date(flight):\n",
1159+
" return flight.env.date\n",
1160+
"\n",
1161+
"custom_data_collector = {\n",
1162+
" \"average_reynolds_number\": get_average_reynolds_number,\n",
1163+
" \"date\": get_date,\n",
1164+
"}"
11611165
]
11621166
},
11631167
{
11641168
"cell_type": "markdown",
11651169
"metadata": {},
11661170
"source": [
1171+
"The `data_collector` must be a dictionary whose keys are the names of the variables we want to export and the values are callback functions (python callables) that compute these variable values. Notice how we can compute complex expressions in this function and just export the result. For instance, the *get_average_reynolds_number* calls the `flight.reynolds_number` method for each value in `flight.time` list and computes the average value using numpy's `mean`. The *date* variable is straightforward.\n",
11671172
"\n",
1168-
"The `export_function` must be a function which takes a `Flight` object and outputs a dictionary whose keys are variables names to export and their values. Notice how we can compute complex expressions in this function and just export the result. For instance, the *average_reynolds_number* calls the `flight.reynolds_number` method for each value in `flight.time` list and computes the average value using numpy's `mean`. The *date* variable is straightforward.\n",
1169-
"\n",
1170-
"After we define the export function, we pass it as an argument to the `MonteCarlo` class."
1173+
"After we define the data collector, we pass it as an argument to the `MonteCarlo` class."
11711174
]
11721175
},
11731176
{
@@ -1181,7 +1184,8 @@
11811184
" environment=stochastic_env,\n",
11821185
" rocket=stochastic_rocket,\n",
11831186
" flight=stochastic_flight,\n",
1184-
" export_function=custom_export_function,\n",
1187+
" export_list=[\"apogee\", \"apogee_time\", \"x_impact\"],\n",
1188+
" data_collector=custom_data_collector,\n",
11851189
")"
11861190
]
11871191
},

rocketpy/simulation/monte_carlo.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class MonteCarlo:
4848
The stochastic flight object to be iterated over.
4949
export_list : list
5050
The list of variables to export at each simulation.
51+
data_collector : dict
52+
A dictionary whose keys are the names of the additional
53+
exported variables and the values are callback functions.
5154
inputs_log : list
5255
List of dictionaries with the inputs used in each simulation.
5356
outputs_log : list
@@ -112,8 +115,15 @@ def __init__(
112115
`lateral_surface_wind`. Default is None.
113116
data_collector : dict, optional
114117
A dictionary whose keys are the names of the exported variables
115-
and the values are callback functions. The callback functions receive
116-
a Flight object and returns a value of that variable.
118+
and the values are callback functions. The keys (variable names) must not
119+
overwrite the default names on 'export_list'. The callback functions receive
120+
a Flight object and returns a value of that variable. For instance
121+
122+
.. code-block:: python
123+
custom_data_collector = {
124+
"max_acceleration": lambda flight: max(flight.acceleration(flight.time)),
125+
"date": lambda flight: flight.env.date,
126+
}
117127
118128
Returns
119129
-------

0 commit comments

Comments
 (0)