From 1a4fa54e4287c1e5215e82d61ca33f231b9b4102 Mon Sep 17 00:00:00 2001 From: Vismayak Mohanarajan Date: Wed, 10 Apr 2024 08:26:23 -0500 Subject: [PATCH] 339 Add galveston community app notebook with retrofit (#357) * Create galveston_community_app_retrofit.ipynb * Temporary store for retrofit_data * Cleared cells and full test run done * Update galveston_community_app_retrofit.ipynb * Update galveston_community_app_retrofit.ipynb * Update galveston_community_app_retrofit.ipynb * Clean up * Update CHANGELOG.md * Update galveston_community_app_retrofit.ipynb * Update notebooks_other.md --- CHANGELOG.md | 1 + manual_jb/content/notebooks_other.md | 1 + .../galveston_community_app_retrofit.ipynb | 808 ++++++++++++++++++ 3 files changed, 810 insertions(+) create mode 100644 notebooks/galveston_community_app_retrofit.ipynb diff --git a/CHANGELOG.md b/CHANGELOG.md index d07de6d2..4589db52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed - Changed the mapping function used to generate the FEMA table [#343](https://github.com/IN-CORE/incore-docs/issues/343) - PyIncore-viz example notebook updated with new hazard dataset update [#335](https://github.com/IN-CORE/incore-docs/issues/335) +- Added Galveston community notebook with retrofit [#337](https://github.com/IN-CORE/incore-docs/issues/337) ## [4.8.2] - 2024-02-07 diff --git a/manual_jb/content/notebooks_other.md b/manual_jb/content/notebooks_other.md index b6764e53..46696679 100644 --- a/manual_jb/content/notebooks_other.md +++ b/manual_jb/content/notebooks_other.md @@ -17,6 +17,7 @@ Note that some Notebooks might not work with the latest version of pyIncore. [epf_repair_cost.ipynb](https://github.com/IN-CORE/incore-docs/blob/main/notebooks/epf_repair_cost.ipynb)
[epn_functionality.ipynb](https://github.com/IN-CORE/incore-docs/blob/main/notebooks/epn_functionality.ipynb)
[galveston_community_app.ipynb](https://github.com/IN-CORE/incore-docs/blob/main/notebooks/galveston_community_app.ipynb)
+[galveston_community_app_retrofit.ipynb](https://github.com/IN-CORE/incore-docs/blob/main/notebooks/galveston_community_app_retrofit.ipynb)
[galveston_cge.ipynb](https://github.com/IN-CORE/incore-docs/blob/main/notebooks/galveston_cge.ipynb)
[housing household recovery](https://github.com/IN-CORE/incore-docs/blob/main/notebooks/housing_household_recovery.ipynb)
[housing_recovery.ipynb](https://github.com/IN-CORE/incore-docs/blob/main/notebooks/housing_recovery.ipynb)
diff --git a/notebooks/galveston_community_app_retrofit.ipynb b/notebooks/galveston_community_app_retrofit.ipynb new file mode 100644 index 00000000..80bd551e --- /dev/null +++ b/notebooks/galveston_community_app_retrofit.ipynb @@ -0,0 +1,808 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "957d73f9", + "metadata": {}, + "outputs": [], + "source": [ + "import warnings\n", + "\n", + "warnings.filterwarnings(\"ignore\")\n", + "import pandas as pd\n", + "import geopandas as gpd\n", + "import numpy as np\n", + "import sys\n", + "import os\n", + "import matplotlib.pyplot as plt\n", + "import contextily as ctx\n", + "import copy\n", + "import math\n", + "from scipy.stats import norm\n", + "from pathlib import Path\n", + "\n", + "from pyincore import (\n", + " IncoreClient,\n", + " Dataset,\n", + " DataService,\n", + " HazardService,\n", + " FragilityService,\n", + " MappingSet,\n", + " FragilityCurveSet,\n", + ")\n", + "from pyincore import HHRSOutputProcess\n", + "from pyincore.utils.dataprocessutil import DataProcessUtil\n", + "\n", + "\n", + "# importing pyIncone analyses:\n", + "from pyincore.analyses.buildingdamage import BuildingDamage\n", + "from pyincore.analyses.buildingfunctionality import BuildingFunctionality\n", + "from pyincore.analyses.combinedwindwavesurgebuildingdamage import (\n", + " CombinedWindWaveSurgeBuildingDamage,\n", + ")\n", + "from pyincore.analyses.epfdamage import EpfDamage\n", + "from pyincore.analyses.montecarlofailureprobability import MonteCarloFailureProbability\n", + "from pyincore.analyses.housingunitallocation import HousingUnitAllocation\n", + "from pyincore.analyses.populationdislocation import (\n", + " PopulationDislocation,\n", + " PopulationDislocationUtil,\n", + ")\n", + "from pyincore.analyses.housingrecoverysequential import HousingRecoverySequential\n", + "from pyincore.analyses.socialvulnerability import SocialVulnerability\n", + "from pyincore.analyses.capitalshocks import CapitalShocks\n", + "from pyincore.analyses.galvestoncge import GalvestonCGEModel\n", + "import json" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4e50116d", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "client = IncoreClient()\n", + "# IN-CORE caches files on the local machine, it might be necessary to clear the memory\n", + "# client.clear_cache()\n", + "data_service = DataService(client) # create data_service object for loading files\n", + "hazard_service = HazardService(client)\n", + "fragility_services = FragilityService(client)" + ] + }, + { + "cell_type": "markdown", + "id": "70388c7a-ed69-4efe-8114-f8567c14a9f6", + "metadata": {}, + "source": [ + "## Combined Building Damage with retrofit" + ] + }, + { + "cell_type": "markdown", + "id": "1b84e1cf-6266-42f2-9e32-764adf0a655a", + "metadata": {}, + "source": [ + "Retrofit is currently only for Surge-wave and flood damage" + ] + }, + { + "cell_type": "markdown", + "id": "654c3396-d286-4034-ba9d-27381df68149", + "metadata": {}, + "source": [ + "### Wind building damage for Galveston, TX using Hurricane Ike" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3f759503-4477-41b8-a6f1-a17c904f3672", + "metadata": {}, + "outputs": [], + "source": [ + "hazard_type = \"hurricane\"\n", + "\n", + "# Galveston Hurricane Ike\n", + "hazard_id = \"5fa5a228b6429615aeea4410\"\n", + "\n", + "# Galveston inventory data\n", + "# bldg_dataset_id = \"63053ddaf5438e1f8c517fed\" # Full city\n", + "bldg_dataset_id = \"63ff6b135c35c0353d5ed3ac\" # Island Only\n", + "\n", + "# Retrofit planning\n", + "# retrofit_plan = \"660c605d099327393f82902b\" # Galveston Retrofit Plan (Elevation - 10m)\n", + "retrofit_plan = \"660c5fa71fb1c519c6fd23bb\" # Galveston Retrofit Plan (Elevation - 5m)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5f1399f7-1bcd-431b-bf68-c9865df99ecd", + "metadata": {}, + "outputs": [], + "source": [ + "# Building Wind Fragility mapping\n", + "mapping_id = \"62fef3a6cef2881193f2261d\"\n", + "fragility_service = FragilityService(client)\n", + "mapping_set = MappingSet(fragility_service.get_mapping(mapping_id))\n", + "\n", + "# wind building damage\n", + "w_bldg_dmg = BuildingDamage(client)\n", + "w_bldg_dmg.load_remote_input_dataset(\"buildings\", bldg_dataset_id)\n", + "w_bldg_dmg.set_input_dataset(\"dfr3_mapping_set\", mapping_set)\n", + "w_bldg_dmg.set_parameter(\"result_name\", \"Galveston-w-retrofit-wind-dmg\")\n", + "w_bldg_dmg.set_parameter(\"hazard_type\", hazard_type)\n", + "w_bldg_dmg.set_parameter(\"hazard_id\", hazard_id)\n", + "w_bldg_dmg.set_parameter(\"num_cpu\", 8)\n", + "w_bldg_dmg.run_analysis()" + ] + }, + { + "cell_type": "markdown", + "id": "97c326dd-946a-4c6a-95af-15bd40e88feb", + "metadata": {}, + "source": [ + "## Surge-Wave building damage for Galveston" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f6730bfb-fc39-4343-becf-d08074996cae", + "metadata": {}, + "outputs": [], + "source": [ + "sw_bldg_dmg_retrofit = BuildingDamage(client)\n", + "sw_bldg_dmg_retrofit.load_remote_input_dataset(\"buildings\", bldg_dataset_id)\n", + "\n", + "# Surge-wave mapping\n", + "mapping_id = \"6303e51bd76c6d0e1f6be080\"\n", + "mapping_set = MappingSet(fragility_service.get_mapping(mapping_id))\n", + "\n", + "\n", + "sw_bldg_dmg_retrofit.load_remote_input_dataset(\"retrofit_strategy\", retrofit_plan)\n", + "sw_bldg_dmg_retrofit.set_input_dataset('dfr3_mapping_set', mapping_set)\n", + "sw_bldg_dmg_retrofit.set_parameter(\"result_name\", \"Galveston-w-retrofit-sw-dmg-w\")\n", + "sw_bldg_dmg_retrofit.set_parameter(\"hazard_type\", hazard_type)\n", + "sw_bldg_dmg_retrofit.set_parameter(\"hazard_id\", hazard_id)\n", + "sw_bldg_dmg_retrofit.set_parameter(\"num_cpu\", 8)\n", + "sw_bldg_dmg_retrofit.run_analysis()" + ] + }, + { + "cell_type": "markdown", + "id": "3aa5aa02-4930-4824-9bbc-a2bd6e737092", + "metadata": {}, + "source": [ + "## Flood building damage for Galveston" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "194f3541-c98f-4e51-a824-cd0ccab34573", + "metadata": {}, + "outputs": [], + "source": [ + "# flood building damage\n", + "f_bldg_dmg_retrofit = BuildingDamage(client)\n", + "f_bldg_dmg_retrofit.load_remote_input_dataset(\"buildings\", bldg_dataset_id)\n", + "\n", + "# Flood mapping\n", + "mapping_id = \"62fefd688a30d30dac57bbd7\"\n", + "mapping_set = MappingSet(fragility_service.get_mapping(mapping_id))\n", + "\n", + "f_bldg_dmg_retrofit.load_remote_input_dataset(\"retrofit_strategy\", retrofit_plan)\n", + "f_bldg_dmg_retrofit.set_input_dataset('dfr3_mapping_set', mapping_set)\n", + "f_bldg_dmg_retrofit.set_parameter(\"result_name\", \"Galveston-w-retrofit-flood-dmg\")\n", + "f_bldg_dmg_retrofit.set_parameter(\"hazard_type\", hazard_type)\n", + "f_bldg_dmg_retrofit.set_parameter(\"hazard_id\", hazard_id)\n", + "f_bldg_dmg_retrofit.set_parameter(\"num_cpu\", 8)\n", + "f_bldg_dmg_retrofit.run_analysis()" + ] + }, + { + "cell_type": "markdown", + "id": "a9112606-e97f-4ea1-a287-1563bed54693", + "metadata": {}, + "source": [ + "## Combine wind, wave and surge building damage" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f89c1e66-a8f2-44ac-a1d7-7ef676eea6dd", + "metadata": {}, + "outputs": [], + "source": [ + "surge_wave_damage_r = sw_bldg_dmg_retrofit.get_output_dataset(\"ds_result\")\n", + "wind_damage = w_bldg_dmg.get_output_dataset(\"ds_result\") # no change\n", + "flood_damage_r = f_bldg_dmg_retrofit.get_output_dataset(\"ds_result\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b16dea3d-2fd2-483c-8c8a-84665df31d88", + "metadata": {}, + "outputs": [], + "source": [ + "combined_bldg_dmg_retrofit = CombinedWindWaveSurgeBuildingDamage(client)\n", + "result_name = \"Galveston-w-retrofit-combined-dmg-w\"\n", + "combined_bldg_dmg_retrofit.set_input_dataset(\"surge_wave_damage\", surge_wave_damage_r)\n", + "combined_bldg_dmg_retrofit.set_input_dataset(\"wind_damage\", wind_damage)\n", + "combined_bldg_dmg_retrofit.set_input_dataset(\"flood_damage\", flood_damage_r)\n", + "combined_bldg_dmg_retrofit.set_parameter(\"result_name\", result_name)\n", + "combined_bldg_dmg_retrofit.run_analysis()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ca8dc90e-b1c8-4ff3-99da-54e041700e07", + "metadata": {}, + "outputs": [], + "source": [ + "combined_dmg_retrofit = combined_bldg_dmg_retrofit.get_output_dataset(\"ds_result\")\n", + "combined_dmg_retrofit_df = combined_dmg_retrofit.get_dataframe_from_csv(low_memory=False)\n", + "\n", + "# Display top 5 rows of output data\n", + "combined_dmg_retrofit_df.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4ad1f635-d4d2-44ed-987d-5db2114a154a", + "metadata": {}, + "outputs": [], + "source": [ + "combined_dmg_retrofit_rs = combined_bldg_dmg_retrofit.get_output_dataset(\"result\")\n", + "combined_dmg_retrofit_df_rs = combined_dmg_retrofit_rs.get_dataframe_from_csv(low_memory=False)\n", + "\n", + "# Display top 5 rows of output data\n", + "combined_dmg_retrofit_df_rs" + ] + }, + { + "cell_type": "markdown", + "id": "828e824b", + "metadata": {}, + "source": [ + "### Archetype Mapping\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d975ad1d", + "metadata": {}, + "outputs": [], + "source": [ + "buildings = Dataset.from_data_service(bldg_dataset_id, data_service)\n", + "bldg_gdf = buildings.get_dataframe_from_shapefile()\n", + "bldg_gdf.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b055f2d7", + "metadata": {}, + "outputs": [], + "source": [ + "arch_mapping = Dataset.from_data_service(\n", + " \"63dc1f1362b9d001e6a1b485\", data_service\n", + ").get_dataframe_from_csv()\n", + "arch_mapping" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61854765", + "metadata": {}, + "outputs": [], + "source": [ + "max_dmg_state_df = DataProcessUtil.get_max_damage_state(combined_dmg_retrofit_df)\n", + "max_dmg_state_df" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "27114b4d", + "metadata": {}, + "outputs": [], + "source": [ + "ret_json = DataProcessUtil.create_mapped_dmg_result_gal(\n", + " bldg_gdf, combined_dmg_retrofit_df_rs, arch_mapping, \"max_state\", \"arch_wind\"\n", + ")\n", + "ret_json" + ] + }, + { + "cell_type": "markdown", + "id": "88a5d556", + "metadata": {}, + "source": [ + "## Electric Power Pole Damage\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ed824b73", + "metadata": {}, + "outputs": [], + "source": [ + "# EPF fragility mapping\n", + "epf_mapping_id = \"62fac92ecef2881193f22613\"\n", + "epf_mapping_set = MappingSet(fragility_service.get_mapping(epf_mapping_id))\n", + "\n", + "epf_dmg_hurricane_galveston = EpfDamage(client)\n", + "epf_dmg_hurricane_galveston.load_remote_input_dataset(\n", + " \"epfs\", \"62fc000f88470b319561b58d\"\n", + ")\n", + "epf_dmg_hurricane_galveston.set_input_dataset(\"dfr3_mapping_set\", epf_mapping_set)\n", + "epf_dmg_hurricane_galveston.set_parameter(\n", + " \"result_name\", \"Galveston-hurricane-epf-damage\"\n", + ")\n", + "epf_dmg_hurricane_galveston.set_parameter(\n", + " \"fragility_key\", \"Non-Retrofit Fragility ID Code\"\n", + ")\n", + "epf_dmg_hurricane_galveston.set_parameter(\"hazard_type\", hazard_type)\n", + "epf_dmg_hurricane_galveston.set_parameter(\"hazard_id\", hazard_id)\n", + "epf_dmg_hurricane_galveston.set_parameter(\"num_cpu\", 8)\n", + "\n", + "# Run Analysis\n", + "epf_dmg_hurricane_galveston.run_analysis()\n", + "epf_dmg_result = epf_dmg_hurricane_galveston.get_output_dataset(\"result\")" + ] + }, + { + "cell_type": "markdown", + "id": "9e8e1ca0", + "metadata": {}, + "source": [ + "## Monte Carlo Failure Probability\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "761889f1-3dc1-448a-8ef4-6faf0fda0f69", + "metadata": {}, + "outputs": [], + "source": [ + "mc_retrofit = MonteCarloFailureProbability(client)\n", + "mc_retrofit.set_input_dataset(\"damage\", combined_dmg_retrofit)\n", + "mc_retrofit.set_parameter(\"result_name\", \"galveston_w_retrofit_combined_bldg_mc\")\n", + "mc_retrofit.set_parameter(\"num_cpu\", 8)\n", + "mc_retrofit.set_parameter(\"num_samples\", 100)\n", + "mc_retrofit.set_parameter(\"damage_interval_keys\", [\"DS_0\", \"DS_1\", \"DS_2\", \"DS_3\"])\n", + "mc_retrofit.set_parameter(\"failure_state_keys\", [\"DS_2\", \"DS_3\", \"DS_4\"])\n", + "mc_retrofit.run_analysis()\n", + "\n", + "gal_bldg_damage_mcs_samples_retrofit = mc_retrofit.get_output_dataset(\"sample_failure_state\")\n", + "gal_bldg_damage_mcs_samples_retrofit_df = gal_bldg_damage_mcs_samples_retrofit.get_dataframe_from_csv()\n", + "gal_bldg_damage_mcs_samples_retrofit_df.head()" + ] + }, + { + "cell_type": "markdown", + "id": "507233a6", + "metadata": {}, + "source": [ + "### Clustering\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d165ebb3-9ea4-40a8-83ba-19f134bc8cad", + "metadata": {}, + "outputs": [], + "source": [ + "ret_json = DataProcessUtil.create_mapped_func_result(\n", + " bldg_gdf, gal_bldg_damage_mcs_samples_retrofit_df, arch_mapping, arch_col=\"arch_wind\"\n", + ")\n", + "with open(\"galveston_w_retrofit_mcs_cluster.json\", \"w\") as f:\n", + " json.dump(ret_json, f, indent=2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "81ca775d-9c49-47ec-b3ef-e768552bac58", + "metadata": {}, + "outputs": [], + "source": [ + "gal_bldg_failure_probability = mc_retrofit.get_output_dataset(\n", + " \"failure_probability\"\n", + ") # get buildings failure probabilities" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "837544c4", + "metadata": {}, + "outputs": [], + "source": [ + "mc = MonteCarloFailureProbability(client)\n", + "mc.set_input_dataset(\"damage\", epf_dmg_result)\n", + "mc.set_parameter(\"result_name\", \"epf_mc\")\n", + "mc.set_parameter(\"num_cpu\", 8)\n", + "mc.set_parameter(\"num_samples\", 100)\n", + "mc.set_parameter(\"damage_interval_keys\", [\"DS_0\", \"DS_1\", \"DS_2\", \"DS_3\"])\n", + "mc.set_parameter(\"failure_state_keys\", [\"DS_2\", \"DS_3\", \"DS_4\"])\n", + "mc.run_analysis()\n", + "\n", + "gal_pole_damage_mcs_samples = mc.get_output_dataset(\"sample_failure_state\")\n", + "gal_pole_damage_mcs_samples_df = gal_pole_damage_mcs_samples.get_dataframe_from_csv()\n", + "gal_pole_damage_mcs_samples_df.head()" + ] + }, + { + "cell_type": "markdown", + "id": "44c772d4", + "metadata": {}, + "source": [ + "## Building Functionality\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "81e795e5-c336-4abd-b79a-8a1358f4f9e2", + "metadata": {}, + "outputs": [], + "source": [ + "gal_bldg_func_retrofit = BuildingFunctionality(client)\n", + "\n", + "gal_bldg_func_retrofit.set_input_dataset(\n", + " \"building_damage_mcs_samples\", gal_bldg_damage_mcs_samples_retrofit\n", + ")\n", + "gal_bldg_func_retrofit.set_input_dataset(\"poles_damage_mcs_samples\", gal_pole_damage_mcs_samples)\n", + "gal_bldg_func_retrofit.load_remote_input_dataset(\n", + " \"interdependency_dictionary\", \"63e5af0262d82d2f5a1058ea\"\n", + ")\n", + "\n", + "gal_bldg_func_retrofit.set_parameter(\"result_name\", \"gal_w_retrofit_mcs_functionality_probability\")\n", + "gal_bldg_func_retrofit.run_analysis()\n", + "\n", + "gal_bldg_func_retrofit_samples_df = gal_bldg_func_retrofit.get_output_dataset(\n", + " \"functionality_samples\"\n", + ").get_dataframe_from_csv()\n", + "gal_bldg_func_retrofit_samples_df.head()" + ] + }, + { + "cell_type": "raw", + "id": "91383ed4-b626-43e5-8ff3-918357be908f", + "metadata": {}, + "source": [ + "df_gal_bldg_retrofit_func_probability = gal_bldg_func_retrofit.get_output_dataset('functionality_probability').get_dataframe_from_csv()\n", + "df_gal_bldg_retrofit_func_probability = df_gal_bldg_retrofit_func_probability.rename(columns={\"building_guid\": \"guid\"})\n", + "func_prob_target = 0.40\n", + "df_gal_bldg_retrofit_func_probability.loc[df_gal_bldg_retrofit_func_probability['probability'].isna(), 'functionality'] = 1 # Assumed to be connected to an undamaged/unanalyzed power network for now.\n", + "df_gal_bldg_retrofit_func_probability.loc[df_gal_bldg_retrofit_func_probability['probability'] <= func_prob_target, 'functionality'] = 0 # Non-Functional\n", + "df_gal_bldg_retrofit_func_probability.loc[df_gal_bldg_retrofit_func_probability['probability'] > func_prob_target, 'functionality'] = 1 # Functional\n", + "df_gal_bldg_retrofit_func_probability.loc[df_gal_bldg_retrofit_func_probability['probability'].isna(), 'functionality_state'] = 'Functional' # Assumed to be connected to an undamaged/unanalyzed power network for now.\n", + "df_gal_bldg_retrofit_func_probability.loc[df_gal_bldg_retrofit_func_probability['probability'] <= func_prob_target, 'functionality_state'] = 'Non-Functional' # Non-Functional\n", + "df_gal_bldg_retrofit_func_probability.loc[df_gal_bldg_retrofit_func_probability['probability'] > func_prob_target, 'functionality_state'] = 'Functional' # Functional\n", + "df_gal_bldg_retrofit_func_probability.head()" + ] + }, + { + "cell_type": "raw", + "id": "7cdcf98c-5ae1-4f8c-8107-bad56d935cc8", + "metadata": {}, + "source": [ + "### Plot of the distribution of functionality state for buildings\n", + "indexes = df_gal_bldg_retrofit_func_probability['functionality_state'].value_counts(normalize=True).mul(100).index.tolist()\n", + "values = df_gal_bldg_retrofit_func_probability['functionality_state'].value_counts(normalize=True).mul(100).tolist()\n", + "\n", + "fig, ax = plt.subplots(figsize=(4, 2.5), dpi=200)\n", + "\n", + "bars = ax.bar(x=indexes, height=values,)\n", + "\n", + "for bar in bars:\n", + " ax.text(bar.get_x() + bar.get_width() / 2,\n", + " bar.get_height() + 3,f'% {bar.get_height() :.1f}',\n", + " horizontalalignment='center')\n", + "\n", + "fig.tight_layout()\n", + "ax.set_ylim([0,100])\n", + "ax.set_xlabel('Damage State', labelpad=15)\n", + "ax.set_ylabel('Percentage', labelpad=15)\n", + "ax.set_title('Functionality Percentage (Buildings and Substations) with Retrofit', pad=15)\n", + "ax.set(frame_on=False);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "801cee9d-0170-4817-a98e-62ac7ffa7748", + "metadata": {}, + "outputs": [], + "source": [ + "ret_json = DataProcessUtil.create_mapped_func_result(\n", + " bldg_gdf, gal_bldg_func_retrofit_samples_df, arch_mapping, arch_col=\"arch_wind\"\n", + ")\n", + "with open(\"galveston_w_retrofit_bldg_func_cluster.json\", \"w\") as f:\n", + " json.dump(ret_json, f, indent=2)" + ] + }, + { + "cell_type": "markdown", + "id": "08f62ae4", + "metadata": {}, + "source": [ + "# Capital Shocks\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8b64b451-0049-47f1-8b04-d128fd98b28e", + "metadata": {}, + "outputs": [], + "source": [ + "# Create Capital shocks analysis\n", + "capital_shocks = CapitalShocks(client)\n", + "building_to_sectors_id = \"640200da475d895dfc2de1bd\"\n", + "# Load remote datasets\n", + "capital_shocks.load_remote_input_dataset(\"buildings_to_sectors\", building_to_sectors_id)\n", + "capital_shocks.load_remote_input_dataset(\"buildings\", bldg_dataset_id)\n", + "capital_shocks.set_input_dataset(\"failure_probability\", gal_bldg_failure_probability)\n", + "\n", + "capital_shocks.set_parameter(\n", + " \"result_name\", \"gal_w_retrofit_sector_shocks\"\n", + ") # name of csv file with results" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "94a9200a-029f-4ccf-ae27-e7f9b1dcea7c", + "metadata": {}, + "outputs": [], + "source": [ + "# Run capital shocks analysis\n", + "capital_shocks.run_analysis()\n", + "\n", + "sector_shocks_result = capital_shocks.get_output_dataset(\"sector_shocks\")\n", + "sector_shocks_result.get_dataframe_from_csv()" + ] + }, + { + "cell_type": "markdown", + "id": "0ff5fc0a", + "metadata": {}, + "source": [ + "# CGE\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "471efa22", + "metadata": {}, + "outputs": [], + "source": [ + "galveston_cge = GalvestonCGEModel(client)\n", + "\n", + "galveston_cge.set_parameter(\"model_iterations\", 1)\n", + "galveston_cge.load_remote_input_dataset(\"SAM\", \"641e1fccea308903b2b8c51a\")\n", + "galveston_cge.load_remote_input_dataset(\"BB\", \"641e1fff34810d74880b3985\")\n", + "galveston_cge.load_remote_input_dataset(\"MISCH\", \"641e21a702414a24a5436dfe\")\n", + "galveston_cge.load_remote_input_dataset(\"EMPLOY\", \"641e2152ea308903b2b8c51c\")\n", + "galveston_cge.load_remote_input_dataset(\"JOBCR\", \"641e209eea308903b2b8c51b\")\n", + "galveston_cge.load_remote_input_dataset(\"OUTCR\", \"641e20f134810d74880b3986\")\n", + "galveston_cge.set_input_dataset(\"sector_shocks\", sector_shocks_result)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bd952c8f", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "galveston_cge.run_analysis()" + ] + }, + { + "cell_type": "markdown", + "id": "34636105", + "metadata": {}, + "source": [ + "## Population Dislocation\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5687353b", + "metadata": {}, + "outputs": [], + "source": [ + "pop_dis = PopulationDislocation(client)\n", + "value_loss = \"60354810e379f22e16560dbd\"\n", + "bg_data = \"603545f2dcda03378087e708\"\n", + "# hua_result_id = \"63ee615d62b9d001e6a2b660\" # city\n", + "hua_result_id = \"63ff8e895367c2261b4cb2ef\" # island\n", + "\n", + "pop_dis.load_remote_input_dataset(\"block_group_data\", bg_data)\n", + "pop_dis.load_remote_input_dataset(\"value_loss_param\", value_loss)\n", + "pop_dis.load_remote_input_dataset(\"housing_unit_allocation\", hua_result_id)\n", + "\n", + "pop_dis.set_input_dataset(\"building_dmg\", combined_dmg_retrofit)\n", + "\n", + "result_name = \"galveston-w-retrofit-pop-disl-results\"\n", + "seed = 1111\n", + "\n", + "pop_dis.set_parameter(\"result_name\", result_name)\n", + "pop_dis.set_parameter(\"seed\", seed)\n", + "pop_dis.run_analysis()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "be72da00", + "metadata": {}, + "outputs": [], + "source": [ + "population_dislocation_result = pop_dis.get_output_dataset(\"result\")\n", + "population_dislocation_result_df = population_dislocation_result.get_dataframe_from_csv(\n", + " low_memory=False\n", + ")\n", + "population_dislocation_result_df.head()" + ] + }, + { + "cell_type": "markdown", + "id": "123208de", + "metadata": {}, + "source": [ + "## Housing Houshold Recovery Sequentials\n" + ] + }, + { + "cell_type": "markdown", + "id": "183e1e00", + "metadata": {}, + "source": [ + "### Social Vulnerabilities\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cf28d307", + "metadata": {}, + "outputs": [], + "source": [ + "social_vulnerability = SocialVulnerability(client)\n", + "\n", + "social_vulnerability.set_parameter(\"result_name\", \"gal_w_retrofit_social_vulnerabilty\")\n", + "social_vulnerability.load_remote_input_dataset(\n", + " \"national_vulnerability_feature_averages\", \"63d178c2a011a9746c948115\"\n", + ")\n", + "social_vulnerability.load_remote_input_dataset(\n", + " \"social_vulnerability_demographic_factors\", \"63d17e81a011a9746c94811b\"\n", + ")\n", + "social_vulnerability.run_analysis()\n", + "sv_result = social_vulnerability.get_output_dataset(\"sv_result\")" + ] + }, + { + "cell_type": "markdown", + "id": "6d697f61", + "metadata": {}, + "source": [ + "### HHRS\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9011551e", + "metadata": {}, + "outputs": [], + "source": [ + "# Population dislocation result aggregated to the block group level.\n", + "# Transition probability matrix per social vulnerability level.\n", + "transition_probability_matrix = \"60f5e2ae544e944c3cec0794\"\n", + "# Initial mass probability function for stage 0.\n", + "initial_probability_vector = \"60f5e918544e944c3cec668b\" # this is only for galveston???\n", + "\n", + "# Create housing recovery instance\n", + "housing_recovery = HousingRecoverySequential(client)\n", + "\n", + "# Load input datasets\n", + "housing_recovery.set_input_dataset(\n", + " \"population_dislocation_block\", population_dislocation_result\n", + ")\n", + "housing_recovery.load_remote_input_dataset(\"tpm\", transition_probability_matrix)\n", + "housing_recovery.load_remote_input_dataset(\n", + " \"initial_stage_probabilities\", initial_probability_vector\n", + ")\n", + "\n", + "# Chain with SV output\n", + "housing_recovery.set_input_dataset(\"sv_result\", sv_result)\n", + "\n", + "# Initial value to seed the random number generator to ensure replication\n", + "seed = 1111\n", + "# A size of the analysis time step.\n", + "t_delta = 1.0\n", + "# Total duration.\n", + "t_final = 90.0\n", + "\n", + "\n", + "# Specify the result name\n", + "result_name = \"gal_w_retrofit_housing_recovery_result\"\n", + "\n", + "# Set analysis parameters\n", + "housing_recovery.set_parameter(\"result_name\", result_name)\n", + "housing_recovery.set_parameter(\"seed\", seed)\n", + "housing_recovery.set_parameter(\"t_delta\", t_delta)\n", + "housing_recovery.set_parameter(\"t_final\", t_final)\n", + "\n", + "housing_recovery.run()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "62a0730a", + "metadata": {}, + "outputs": [], + "source": [ + "HH_stagehistory_DF = housing_recovery.get_output_dataset(\n", + " \"ds_result\"\n", + ").get_dataframe_from_csv()\n", + "timesteps = [\"1\", \"7\", \"13\", \"25\", \"49\", \"85\"] # t0, t6, t12, t24, t48\n", + "HHRSOutputProcess.get_hhrs_stage_count(timesteps, HH_stagehistory_DF)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f8f98416-c962-44f0-b656-600755264d6a", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}