Skip to content

Commit

Permalink
Ensembles for variables
Browse files Browse the repository at this point in the history
  • Loading branch information
burggraaff committed Sep 18, 2024
1 parent dd9f053 commit f96b641
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 2 deletions.
154 changes: 152 additions & 2 deletions demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,10 @@
"id": "d5144658-0015-41af-8278-20da55694238",
"metadata": {},
"source": [
"### Estimating uncertainty using WOFOST ensembles\n",
"### Testing model sensitivity using WOFOST ensembles\n",
"Ensembles can also be used to estimate the uncertainty in outputs by using different combinations of input parameters.\n",
"Here, we will look at an example using one site."
"Here, we will look at an example using one site and varying one parameter.\n",
"First, we select a site from the BRP as before:"
]
},
{
Expand All @@ -603,6 +604,155 @@
"id": "37f25316-cc52-4f07-95ec-c443b855bc0d",
"metadata": {},
"outputs": [],
"source": [
"# Setup BRP\n",
"year = 2020\n",
"brp = fpcup.io.load_brp(year)\n",
"index = 1 # Change this to any index in the BRP table. Try some values and see what happens!\n",
"\n",
"# Select plot\n",
"brp_plot = brp.iloc[index]\n",
"brp_plot"
]
},
{
"cell_type": "markdown",
"id": "524b52c2-3077-4890-b333-81aa53d4f467",
"metadata": {},
"source": [
"Next, we set the parameters that we want to remain fixed:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0e00e7d9-ff8e-4a1a-a090-7eeb8b16da54",
"metadata": {},
"outputs": [],
"source": [
"# Setup WOFOST\n",
"crop_name = brp_plot[\"crop\"]\n",
"crop = fpcup.crop.select_crop(crop_name)\n",
"agromanagement = crop.agromanagement_first_sowingdate(year) # First sowing date of the year\n",
"\n",
"soildata = fpcup.soil.soil_types[\"ec1\"] # In the future, it will be possible to retrieve the soil type specific to this plot\n",
"\n",
"coordinates = brp_plot[\"latitude\"], brp_plot[\"longitude\"] # Center of the plot\n",
"weatherdata = fpcup.weather.load_weather_data_NASAPower(coordinates)\n",
"\n",
"# Combine constants\n",
"constants = {\"agromanagement\": agromanagement, \"soildata\": soildata, \"weatherdata\": weatherdata, \"latitude\": coordinates[0], \"longitude\": coordinates[1]}"
]
},
{
"cell_type": "markdown",
"id": "20e6d8ed-1e48-4401-8d12-a9b0584d8de4",
"metadata": {},
"source": [
"Now, we set the variable parameters.\n",
"In this case, we will look at the effects of varying `WAV`, that is the initial amount of water in the rootable zone in excess of wilting point.\n",
"WOFOST allows for `WAV` values between 0 and 50 cm, with a default of 10 cm (which was implicitly used in the previous runs).\n",
"You can use the variable `n` to determine how many values of `WAV` will be used."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4cad40e5-3f5b-47d7-975c-25656b097d15",
"metadata": {},
"outputs": [],
"source": [
"n = 51 # Number of values to test\n",
"variable_names = [\"WAV\"]\n",
"combined_parameters = fpcup.parameters.generate_ensemble_space(*variable_names, n=n)\n",
"variables = {\"override\": combined_parameters}\n",
"variables = fpcup.tools.dict_product(variables) # Pack the variables into one iterable of dictionaries\n",
"\n",
"# Setup output folder\n",
"output_dir = fpcup.io.Path(\"outputs/demo/WAV/\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "601b28b5-d506-4d3d-9ed3-5daa678373d0",
"metadata": {},
"outputs": [],
"source": [
"model_statuses = fpcup.model.run_pcse_ensemble(variables, output_dir, run_data_constants=constants)"
]
},
{
"cell_type": "markdown",
"id": "9de5a38c-064e-40a3-b3f6-9a30a4534fad",
"metadata": {},
"source": [
"The outputs can be plotted as follows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1746ee07-998e-48f7-aa2d-14dd2c9933f5",
"metadata": {},
"outputs": [],
"source": [
"# TO DO: Refactor ensemble plot"
]
},
{
"cell_type": "markdown",
"id": "484c541f-4c15-4c52-b9ef-c5715f7cdd9a",
"metadata": {},
"source": [
"#### Ensembles testing multiple parameters\n",
"It is also possible to run an ensemble for multiple variables at the same time, although this exponentially increases the run time.\n",
"Here, we will look at an example where both `WAV` (as before) and `RDMSOL` (the maximum rooting depth allowed by the soil) are varied at the same time.\n",
"All other parameters are re-used from the previous cells."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "860fbcc7-1316-4ac1-872b-3c9175626f57",
"metadata": {},
"outputs": [],
"source": [
"n = 51 # Number of values to test\n",
"variable_names = [\"WAV\", \"RDMSOL\"]\n",
"combined_parameters = fpcup.parameters.generate_ensemble_space(*variable_names, n=n)\n",
"variables = {\"override\": combined_parameters}\n",
"variables = fpcup.tools.dict_product(variables) # Pack the variables into one iterable of dictionaries\n",
"\n",
"# Setup output folder\n",
"output_dir = fpcup.io.Path(\"outputs/demo/RDMSOL-WAV/\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d226a6f-2e25-45c8-83b2-0906e9a6370f",
"metadata": {},
"outputs": [],
"source": [
"model_statuses = fpcup.model.run_pcse_ensemble(variables, output_dir, run_data_constants=constants)"
]
},
{
"cell_type": "markdown",
"id": "7763a722-4088-428f-b927-c84da98f8662",
"metadata": {},
"source": [
"#### Multiple sowing dates\n",
"As an additional example, we can run WOFOST using fixed or variable parameters for multiple sowing dates, to see the effects this has on crop growth."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3415eb26-fa28-4e33-8916-20ae281dd865",
"metadata": {},
"outputs": [],
"source": []
}
],
Expand Down
Empty file.
Empty file added outputs/demo/WAV/placeholder
Empty file.
Empty file added outputs/demo/placeholder
Empty file.

0 comments on commit f96b641

Please sign in to comment.