Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Sep 23, 2024
1 parent 2419fc0 commit 4b63590
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 85 deletions.
33 changes: 27 additions & 6 deletions docs/examples/asset_level_impacts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,34 @@
"\n",
"fig1 = make_subplots(rows=1, cols=2)\n",
"fig1.add_trace(\n",
" go.Scatter(x=exceedance_histo[\"exceed_probabilities\"], y=exceedance_histo[\"values\"], name=\"baseline wind\"),\n",
" go.Scatter(\n",
" x=exceedance_histo[\"exceed_probabilities\"],\n",
" y=exceedance_histo[\"values\"],\n",
" name=\"baseline wind\",\n",
" ),\n",
" row=1,\n",
" col=1,\n",
")\n",
"fig1.add_trace(\n",
" go.Scatter(x=exceedance_ssp585[\"exceed_probabilities\"], y=exceedance_ssp585[\"values\"], name=\"wind SSP585\"),\n",
" go.Scatter(\n",
" x=exceedance_ssp585[\"exceed_probabilities\"],\n",
" y=exceedance_ssp585[\"values\"],\n",
" name=\"wind SSP585\",\n",
" ),\n",
" row=1,\n",
" col=1,\n",
")\n",
"fig1.update_xaxes(\n",
" title=\"Annual exceedance probability\", title_font={\"size\": 14}, row=1, col=1, type=\"log\", autorange=\"reversed\"\n",
" title=\"Annual exceedance probability\",\n",
" title_font={\"size\": 14},\n",
" row=1,\n",
" col=1,\n",
" type=\"log\",\n",
" autorange=\"reversed\",\n",
")\n",
"fig1.update_yaxes(title=\"Damage as fraction of insurable value\", title_font={\"size\": 14}, row=1, col=1)"
"fig1.update_yaxes(\n",
" title=\"Damage as fraction of insurable value\", title_font={\"size\": 14}, row=1, col=1\n",
")"
]
},
{
Expand Down Expand Up @@ -231,7 +246,9 @@
"asset_measures_dict = {}\n",
"for i in asset_measures:\n",
" key = i[\"key\"]\n",
" asset_measures_dict[Key(key[\"hazard_type\"], key[\"measure_id\"], key[\"scenario_id\"], key[\"year\"])] = i\n",
" asset_measures_dict[\n",
" Key(key[\"hazard_type\"], key[\"measure_id\"], key[\"scenario_id\"], key[\"year\"])\n",
" ] = i\n",
"\n",
"wind_impact_scores = asset_measures_dict[Key(\"Wind\", \"measure_set_0\", \"ssp585\", \"2050\")]\n",
"\n",
Expand Down Expand Up @@ -282,7 +299,11 @@
}
],
"source": [
"pp.pprint(response[\"risk_measures\"][\"score_based_measure_set_defn\"][\"score_definitions\"][\"measure_1\"])"
"pp.pprint(\n",
" response[\"risk_measures\"][\"score_based_measure_set_defn\"][\"score_definitions\"][\n",
" \"measure_1\"\n",
" ]\n",
")"
]
},
{
Expand Down
145 changes: 120 additions & 25 deletions docs/examples/hazard_indicators.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,61 @@
"request = {\n",
" \"items\": [\n",
" {\n",
" \"longitudes\": [69.4787, 68.71, 20.1047, 19.8936, 19.6359, 0.5407, 6.9366, 6.935, 13.7319, 13.7319],\n",
" \"latitudes\": [34.556, 35.9416, 39.9116, 41.6796, 42.0137, 35.7835, 36.8789, 36.88, -12.4706, -12.4706],\n",
" \"longitudes\": [\n",
" 69.4787,\n",
" 68.71,\n",
" 20.1047,\n",
" 19.8936,\n",
" 19.6359,\n",
" 0.5407,\n",
" 6.9366,\n",
" 6.935,\n",
" 13.7319,\n",
" 13.7319,\n",
" ],\n",
" \"latitudes\": [\n",
" 34.556,\n",
" 35.9416,\n",
" 39.9116,\n",
" 41.6796,\n",
" 42.0137,\n",
" 35.7835,\n",
" 36.8789,\n",
" 36.88,\n",
" -12.4706,\n",
" -12.4706,\n",
" ],\n",
" \"request_item_id\": \"my_flood_request\",\n",
" \"hazard_type\": \"RiverineInundation\",\n",
" \"indicator_id\": \"flood_depth\",\n",
" \"scenario\": \"historical\",\n",
" \"year\": 1980,\n",
" },\n",
" {\n",
" \"longitudes\": [69.4787, 68.71, 20.1047, 19.8936, 19.6359, 0.5407, 6.9366, 6.935, 13.7319, 13.7319],\n",
" \"latitudes\": [34.556, 35.9416, 39.9116, 41.6796, 42.0137, 35.7835, 36.8789, 36.88, -12.4706, -12.4706],\n",
" \"longitudes\": [\n",
" 69.4787,\n",
" 68.71,\n",
" 20.1047,\n",
" 19.8936,\n",
" 19.6359,\n",
" 0.5407,\n",
" 6.9366,\n",
" 6.935,\n",
" 13.7319,\n",
" 13.7319,\n",
" ],\n",
" \"latitudes\": [\n",
" 34.556,\n",
" 35.9416,\n",
" 39.9116,\n",
" 41.6796,\n",
" 42.0137,\n",
" 35.7835,\n",
" 36.8789,\n",
" 36.88,\n",
" -12.4706,\n",
" -12.4706,\n",
" ],\n",
" \"request_item_id\": \"my_flood_request\",\n",
" \"hazard_type\": \"RiverineInundation\",\n",
" \"indicator_id\": \"flood_depth\",\n",
Expand Down Expand Up @@ -172,14 +216,44 @@
],
"source": [
"fig1 = make_subplots(rows=1, cols=2)\n",
"fig1.add_scatter(x=flood_results_baseline[0][\"index_values\"], y=flood_results_baseline[0][\"intensities\"], name=\"baseline flood\", row=1, col=1)\n",
"fig1.add_scatter(x=flood_results_rcp585[0][\"index_values\"], y=flood_results_rcp585[0][\"intensities\"], name=\"flood RCP 8.5 2050\", row=1, col=1)\n",
"fig1.update_xaxes(title=\"Return period (years)\", title_font={\"size\": 14}, row=1, col=1, type=\"log\")\n",
"fig1.add_scatter(\n",
" x=flood_results_baseline[0][\"index_values\"],\n",
" y=flood_results_baseline[0][\"intensities\"],\n",
" name=\"baseline flood\",\n",
" row=1,\n",
" col=1,\n",
")\n",
"fig1.add_scatter(\n",
" x=flood_results_rcp585[0][\"index_values\"],\n",
" y=flood_results_rcp585[0][\"intensities\"],\n",
" name=\"flood RCP 8.5 2050\",\n",
" row=1,\n",
" col=1,\n",
")\n",
"fig1.update_xaxes(\n",
" title=\"Return period (years)\", title_font={\"size\": 14}, row=1, col=1, type=\"log\"\n",
")\n",
"fig1.update_yaxes(title=\"Flood depth (m)\", title_font={\"size\": 14}, row=1, col=1)\n",
"fig1.add_scatter(x=wind_results_baseline[0][\"index_values\"], y=wind_results_baseline[0][\"intensities\"], name=\"baseline wind\", row=1, col=2)\n",
"fig1.add_scatter(x=wind_results_ssp585[0][\"index_values\"], y=wind_results_ssp585[0][\"intensities\"], name=\"wind SSP585 2050\", row=1, col=2)\n",
"fig1.update_xaxes(title=\"Return period (years)\", title_font={\"size\": 14}, row=1, col=2, type=\"log\")\n",
"fig1.update_yaxes(title=\"Max (1 minute) wind speed (m/s)\", title_font={\"size\": 14}, row=1, col=2)"
"fig1.add_scatter(\n",
" x=wind_results_baseline[0][\"index_values\"],\n",
" y=wind_results_baseline[0][\"intensities\"],\n",
" name=\"baseline wind\",\n",
" row=1,\n",
" col=2,\n",
")\n",
"fig1.add_scatter(\n",
" x=wind_results_ssp585[0][\"index_values\"],\n",
" y=wind_results_ssp585[0][\"intensities\"],\n",
" name=\"wind SSP585 2050\",\n",
" row=1,\n",
" col=2,\n",
")\n",
"fig1.update_xaxes(\n",
" title=\"Return period (years)\", title_font={\"size\": 14}, row=1, col=2, type=\"log\"\n",
")\n",
"fig1.update_yaxes(\n",
" title=\"Max (1 minute) wind speed (m/s)\", title_font={\"size\": 14}, row=1, col=2\n",
")"
]
},
{
Expand Down Expand Up @@ -312,11 +386,15 @@
"hazard_types = set(r[\"hazard_type\"] for r in hazard_resources)\n",
"print(f\"Hazards: {hazard_types}\")\n",
"wind_resources = [r for r in hazard_resources if r[\"hazard_type\"] == \"Wind\"]\n",
"print(f\"{len(hazard_resources)} hazard resources in the inventory, of which {len(wind_resources)} are Wind resources.\")\n",
"print(\n",
" f\"{len(hazard_resources)} hazard resources in the inventory, of which {len(wind_resources)} are Wind resources.\"\n",
")\n",
"iris_model_resource = next(r for r in wind_resources if \"iris\" in r[\"path\"])\n",
"# pp.pprint(iris_model_resource[0])\n",
"path = iris_model_resource[\"path\"]\n",
"print(f\"The resource 'path' is a unique identifier. For the IRIS Wind resource, for example, this is: '{path}'.\")\n",
"print(\n",
" f\"The resource 'path' is a unique identifier. For the IRIS Wind resource, for example, this is: '{path}'.\"\n",
")\n",
"print(\n",
" \"Where resources are multi-dimensional arrays (as opposed to an external API), this is also the path of the array.\"\n",
")\n",
Expand Down Expand Up @@ -479,11 +557,16 @@
}
],
"source": [
"#from typing import List\n",
"# from typing import List\n",
"import pandas as pd\n",
"import json\n",
"\n",
"rcp_display_names = {\"rcp2p6\": \"RCP 2.6\", \"rcp4p5\": \"RCP 4.5\" ,\"rcp6p0\": \"RCP 6.0\", \"rcp8p5\": \"RCP 8.5\", }\n",
"rcp_display_names = {\n",
" \"rcp2p6\": \"RCP 2.6\",\n",
" \"rcp4p5\": \"RCP 4.5\",\n",
" \"rcp6p0\": \"RCP 6.0\",\n",
" \"rcp8p5\": \"RCP 8.5\",\n",
"}\n",
"\n",
"\n",
"def scenario_code(id: str):\n",
" if id.startswith(\"ssp\"):\n",
Expand All @@ -493,19 +576,31 @@
" else:\n",
" return id\n",
"\n",
"\n",
"def format_scenario(scenarios):\n",
" return \", \".join(f\"{scenario_code(scen['id'])} ({', '.join(str(y) for y in scen['years'])})\" for scen in scenarios)\n",
" return \", \".join(\n",
" f\"{scenario_code(scen['id'])} ({', '.join(str(y) for y in scen['years'])})\"\n",
" for scen in scenarios\n",
" )\n",
"\n",
"\n",
"resource_df = pd.json_normalize(hazard_resources)\n",
"resource_df = resource_df.filter([\"hazard_type\", \"indicator_id\", \"display_name\", \"path\", \"scenarios\"])\n",
"resource_df = resource_df.sort_values(by=[\"hazard_type\", \"indicator_id\", \"display_name\"])\n",
"resource_df = resource_df.filter(\n",
" [\"hazard_type\", \"indicator_id\", \"display_name\", \"path\", \"scenarios\"]\n",
")\n",
"resource_df = resource_df.sort_values(\n",
" by=[\"hazard_type\", \"indicator_id\", \"display_name\"]\n",
")\n",
"resource_df.scenarios = resource_df.scenarios.apply(lambda s: format_scenario(s))\n",
"resource_df = resource_df.rename(columns={\"hazard_type\": \"Hazard type\",\n",
" \"indicator_id\": \"Indicator ID\",\n",
" \"display_name\": \"Display name\",\n",
" \"path\": \"Path (ID)\",\n",
" \"scenarios\": \"Scenarios\",\n",
" })\n",
"resource_df = resource_df.rename(\n",
" columns={\n",
" \"hazard_type\": \"Hazard type\",\n",
" \"indicator_id\": \"Indicator ID\",\n",
" \"display_name\": \"Display name\",\n",
" \"path\": \"Path (ID)\",\n",
" \"scenarios\": \"Scenarios\",\n",
" }\n",
")\n",
"\n",
"md = resource_df.to_markdown(index=False)\n",
"display(Markdown(md))"
Expand Down
33 changes: 22 additions & 11 deletions docs/user-guide/vulnerability_config.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,16 @@
"outputs": [],
"source": [
"# pip install nbformat pandas plotly requests\n",
"from dotenv import load_dotenv\n",
"from io import StringIO\n",
"import plotly.graph_objs as go\n",
"import plotly.io\n",
"from plotly.subplots import make_subplots\n",
"\n",
"plotly.io.renderers.default = \"notebook\"\n",
"from physrisk.vulnerability_models.config_based_model import config_items_from_csv, config_items_to_df"
"from physrisk.vulnerability_models.config_based_model import (\n",
" config_items_from_csv,\n",
" config_items_to_df,\n",
")"
]
},
{
Expand Down Expand Up @@ -354,8 +356,17 @@
}
],
"source": [
"c1 = next(c for c in config_items if c.asset_class == \"RealEstateAsset\" and c.indicator_id==\"flood_depth\")\n",
"c2 = next(c for c in config_items if c.asset_class == \"ManufacturingAsset\" and c.indicator_id==\"days_tas/above/{temp_c}c\")\n",
"c1 = next(\n",
" c\n",
" for c in config_items\n",
" if c.asset_class == \"RealEstateAsset\" and c.indicator_id == \"flood_depth\"\n",
")\n",
"c2 = next(\n",
" c\n",
" for c in config_items\n",
" if c.asset_class == \"ManufacturingAsset\"\n",
" and c.indicator_id == \"days_tas/above/{temp_c}c\"\n",
")\n",
"\n",
"fig1 = make_subplots(rows=1, cols=2)\n",
"fig1.add_trace(\n",
Expand All @@ -368,14 +379,14 @@
" row=1,\n",
" col=2,\n",
")\n",
"fig1.update_xaxes(\n",
" title=\"Flood depth (m)\", title_font={\"size\": 14}, row=1, col=1\n",
")\n",
"fig1.update_xaxes(\n",
" title=\"Threshold (°C)\", title_font={\"size\": 14}, row=1, col=2\n",
"fig1.update_xaxes(title=\"Flood depth (m)\", title_font={\"size\": 14}, row=1, col=1)\n",
"fig1.update_xaxes(title=\"Threshold (°C)\", title_font={\"size\": 14}, row=1, col=2)\n",
"fig1.update_yaxes(\n",
" title=\"Damage as fraction of insurable value\", title_font={\"size\": 14}, row=1, col=1\n",
")\n",
"fig1.update_yaxes(title=\"Damage as fraction of insurable value\", title_font={\"size\": 14}, row=1, col=1)\n",
"fig1.update_yaxes(title=\"Fractional loss of production\", title_font={\"size\": 14}, row=1, col=2)"
"fig1.update_yaxes(\n",
" title=\"Fractional loss of production\", title_font={\"size\": 14}, row=1, col=2\n",
")"
]
}
],
Expand Down
Loading

0 comments on commit 4b63590

Please sign in to comment.