From a5135b14754f866931e837d479b27133855ca654 Mon Sep 17 00:00:00 2001 From: Chen Wang Date: Thu, 13 Apr 2023 13:46:10 -0500 Subject: [PATCH] 193 galveston community notebook (#196) * galveston notebook for the community app * update the notebook * add a link to the notebook * update bldg functionality with latest network * update hua id * add sector shock * update building; add sector shock * remove outputs * update notebook to use clipped hua * update sector shock id * add CGE * move to correct location * Update galveston_community_app.ipynb * Update CHANGELOG.md * add clustering code for functionality * udpate community app notebook with new clustering method --------- Co-authored-by: Vismayak Mohanarajan --- CHANGELOG.md | 6 + manual_jb/content/notebooks_other.md | 1 + ...ombined_wind_wave_surge_building_dmg.ipynb | 2 +- notebooks/galveston_community_app.ipynb | 742 ++++++++++++++++++ notebooks/slc_community_app.ipynb | 54 +- 5 files changed, 782 insertions(+), 23 deletions(-) create mode 100644 notebooks/galveston_community_app.ipynb diff --git a/CHANGELOG.md b/CHANGELOG.md index 3995b530..25b04a06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to the INCORE documents generated by Sphinx package will be The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## Unreleased + +### Added +- Galveston community app example notebook [#196](https://github.com/IN-CORE/incore-docs/issues/196) + + ## [4.0.0] - 2023-03-15 ### Added diff --git a/manual_jb/content/notebooks_other.md b/manual_jb/content/notebooks_other.md index d6c9cd17..ea35c5fb 100644 --- a/manual_jb/content/notebooks_other.md +++ b/manual_jb/content/notebooks_other.md @@ -14,6 +14,7 @@ Note that some Notebooks might not work with the latest version of pyIncore. [cumulative_building_dmg.ipynb](https://github.com/IN-CORE/incore-docs/blob/main/notebooks/cumulative_building_dmg.ipynb)
[epf_dmg.ipynb](https://github.com/IN-CORE/incore-docs/blob/main/notebooks/epf_dmg.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)
[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)
[housingunitallocation.ipynb](https://github.com/IN-CORE/incore-docs/blob/main/notebooks/housingunitallocation.ipynb)
diff --git a/notebooks/combined_wind_wave_surge_building_dmg.ipynb b/notebooks/combined_wind_wave_surge_building_dmg.ipynb index 4978f7f8..108a0925 100644 --- a/notebooks/combined_wind_wave_surge_building_dmg.ipynb +++ b/notebooks/combined_wind_wave_surge_building_dmg.ipynb @@ -284,4 +284,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} +} \ No newline at end of file diff --git a/notebooks/galveston_community_app.ipynb b/notebooks/galveston_community_app.ipynb new file mode 100644 index 00000000..22b0bca4 --- /dev/null +++ b/notebooks/galveston_community_app.ipynb @@ -0,0 +1,742 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "957d73f9", + "metadata": {}, + "outputs": [], + "source": [ + "import warnings\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 IncoreClient, Dataset, DataService, HazardService, FragilityService, MappingSet, FragilityCurveSet\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 CombinedWindWaveSurgeBuildingDamage\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 PopulationDislocation, PopulationDislocationUtil\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": {}, + "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": "351fe20a", + "metadata": {}, + "source": [ + "## Combined Building Damage" + ] + }, + { + "cell_type": "markdown", + "id": "565bcb53", + "metadata": {}, + "source": [ + "### Wind building damage for Galveston, TX using Hurricane Ike" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "39b41540", + "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", + "# Building Wind Fragility mapping\n", + "mapping_id = \"62fef3a6cef2881193f2261d\"\n", + "fragility_service = FragilityService(client)\n", + "mapping_set = MappingSet(fragility_service.get_mapping(mapping_id))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b8bb07e1", + "metadata": {}, + "outputs": [], + "source": [ + "# 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-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": "6fb045b4", + "metadata": {}, + "source": [ + "### Surge-Wave building damage for Galveston, TX using Hurricane Ike" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "58b1565c", + "metadata": {}, + "outputs": [], + "source": [ + "# Surge-wave mapping\n", + "mapping_id = \"6303e51bd76c6d0e1f6be080\"\n", + "mapping_set = MappingSet(fragility_service.get_mapping(mapping_id))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9812c012", + "metadata": {}, + "outputs": [], + "source": [ + "# surge-wave building damage\n", + "sw_bldg_dmg = BuildingDamage(client)\n", + "sw_bldg_dmg.load_remote_input_dataset(\"buildings\", bldg_dataset_id)\n", + "sw_bldg_dmg.set_input_dataset('dfr3_mapping_set', mapping_set)\n", + "sw_bldg_dmg.set_parameter(\"result_name\", \"Galveston-sw-dmg\")\n", + "sw_bldg_dmg.set_parameter(\"hazard_type\", hazard_type)\n", + "sw_bldg_dmg.set_parameter(\"hazard_id\", hazard_id)\n", + "sw_bldg_dmg.set_parameter(\"num_cpu\", 8)\n", + "sw_bldg_dmg.run_analysis()" + ] + }, + { + "cell_type": "markdown", + "id": "72fd8814", + "metadata": {}, + "source": [ + "### Flood building damage for Galveston, TX using Hurricane Ike" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bfb2b053", + "metadata": {}, + "outputs": [], + "source": [ + "# flood mapping\n", + "mapping_id = \"62fefd688a30d30dac57bbd7\"\n", + "mapping_set = MappingSet(fragility_service.get_mapping(mapping_id))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3445913d", + "metadata": {}, + "outputs": [], + "source": [ + "# flood building damage\n", + "f_bldg_dmg = BuildingDamage(client)\n", + "f_bldg_dmg.load_remote_input_dataset(\"buildings\", bldg_dataset_id)\n", + "f_bldg_dmg.set_input_dataset('dfr3_mapping_set', mapping_set)\n", + "f_bldg_dmg.set_parameter(\"result_name\", \"Galveston-flood-dmg\")\n", + "f_bldg_dmg.set_parameter(\"hazard_type\", hazard_type)\n", + "f_bldg_dmg.set_parameter(\"hazard_id\", hazard_id)\n", + "f_bldg_dmg.set_parameter(\"num_cpu\", 8)\n", + "f_bldg_dmg.run_analysis()" + ] + }, + { + "cell_type": "markdown", + "id": "f9e16601", + "metadata": {}, + "source": [ + "### Combine wind, wave and surge building damage" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ef7f95aa", + "metadata": {}, + "outputs": [], + "source": [ + "surge_wave_damage = sw_bldg_dmg.get_output_dataset(\"ds_result\")\n", + "wind_damage = w_bldg_dmg.get_output_dataset(\"ds_result\")\n", + "flood_damage = f_bldg_dmg.get_output_dataset(\"ds_result\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "36d3114e", + "metadata": {}, + "outputs": [], + "source": [ + "combined_bldg_dmg = CombinedWindWaveSurgeBuildingDamage(client)\n", + "result_name = \"Galveston-combined-dmg\"\n", + "combined_bldg_dmg.set_input_dataset(\"surge_wave_damage\", surge_wave_damage)\n", + "combined_bldg_dmg.set_input_dataset(\"wind_damage\", wind_damage)\n", + "combined_bldg_dmg.set_input_dataset(\"flood_damage\", flood_damage)\n", + "combined_bldg_dmg.set_parameter(\"result_name\", result_name)\n", + "combined_bldg_dmg.run_analysis()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5f4ebe2d", + "metadata": {}, + "outputs": [], + "source": [ + "combined_dmg = combined_bldg_dmg.get_output_dataset(\"ds_result\")\n", + "combined_dmg_df = combined_dmg.get_dataframe_from_csv(low_memory=False)\n", + "\n", + "# Display top 5 rows of output data\n", + "combined_dmg_df.head()" + ] + }, + { + "cell_type": "markdown", + "id": "828e824b", + "metadata": {}, + "source": [ + "### Archetype Mapping" + ] + }, + { + "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(\"63dc1f1362b9d001e6a1b485\", data_service).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_df)\n", + "max_dmg_state_df" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "53b21c48", + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "ret_json = DataProcessUtil.create_mapped_dmg_result(bldg_gdf, max_dmg_state_df, arch_mapping, \"max_state\", \"arch_wind\")\n", + "ret_json" + ] + }, + { + "cell_type": "markdown", + "id": "88a5d556", + "metadata": {}, + "source": [ + "## Electric Power Pole Damage" + ] + }, + { + "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(\"epfs\", \"62fc000f88470b319561b58d\")\n", + "epf_dmg_hurricane_galveston.set_input_dataset('dfr3_mapping_set', epf_mapping_set)\n", + "epf_dmg_hurricane_galveston.set_parameter(\"result_name\", \"Galveston-hurricane-epf-damage\")\n", + "epf_dmg_hurricane_galveston.set_parameter(\"fragility_key\", \"Non-Retrofit Fragility ID Code\")\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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f8699e6b", + "metadata": {}, + "outputs": [], + "source": [ + "mc = MonteCarloFailureProbability(client)\n", + "mc.set_input_dataset(\"damage\", combined_dmg)\n", + "mc.set_parameter(\"result_name\", \"combined_bldg_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_bldg_damage_mcs_samples = mc.get_output_dataset('sample_failure_state')\n", + "gal_bldg_damage_mcs_samples_df = gal_bldg_damage_mcs_samples.get_dataframe_from_csv()\n", + "gal_bldg_damage_mcs_samples_df.head()" + ] + }, + { + "cell_type": "markdown", + "id": "507233a6", + "metadata": {}, + "source": [ + "### Clustering" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "da807655", + "metadata": {}, + "outputs": [], + "source": [ + "ret_json = DataProcessUtil.create_mapped_func_result(bldg_gdf, gal_bldg_damage_mcs_samples_df, arch_mapping, \n", + " arch_col=\"arch_wind\")\n", + "with open(\"galveston_mcs_cluster.json\", \"w\") as f:\n", + " json.dump(ret_json, f, indent=2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "46ed6e01", + "metadata": {}, + "outputs": [], + "source": [ + "gal_bldg_failure_probability = mc.get_output_dataset('failure_probability') # 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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9412fc5a", + "metadata": {}, + "outputs": [], + "source": [ + "gal_bldg_func = BuildingFunctionality(client)\n", + " \n", + "gal_bldg_func.set_input_dataset(\"building_damage_mcs_samples\", gal_bldg_damage_mcs_samples)\n", + "gal_bldg_func.set_input_dataset(\"poles_damage_mcs_samples\", gal_pole_damage_mcs_samples)\n", + "gal_bldg_func.load_remote_input_dataset(\"interdependency_dictionary\", \"63e5af0262d82d2f5a1058ea\")\n", + "\n", + "gal_bldg_func.set_parameter(\"result_name\", \"gal_mcs_functionality_probability\")\n", + "gal_bldg_func.run_analysis()\n", + "\n", + "gal_bldg_func_samples_df = gal_bldg_func.get_output_dataset('functionality_samples').get_dataframe_from_csv()\n", + "gal_bldg_func_samples_df.head()" + ] + }, + { + "cell_type": "raw", + "id": "7af54393", + "metadata": {}, + "source": [ + "df_gal_bldg_func_probability = gal_bldg_func.get_output_dataset('functionality_probability').get_dataframe_from_csv()\n", + "df_gal_bldg_func_probability = df_gal_bldg_func_probability.rename(columns={\"building_guid\": \"guid\"})\n", + "func_prob_target = 0.40\n", + "df_gal_bldg_func_probability.loc[df_gal_bldg_func_probability['probability'].isna(), 'functionality'] = 1 # Assumed to be connected to an undamaged/unanalyzed power network for now.\n", + "df_gal_bldg_func_probability.loc[df_gal_bldg_func_probability['probability'] <= func_prob_target, 'functionality'] = 0 # Non-Functional\n", + "df_gal_bldg_func_probability.loc[df_gal_bldg_func_probability['probability'] > func_prob_target, 'functionality'] = 1 # Functional\n", + "df_gal_bldg_func_probability.loc[df_gal_bldg_func_probability['probability'].isna(), 'functionality_state'] = 'Functional' # Assumed to be connected to an undamaged/unanalyzed power network for now.\n", + "df_gal_bldg_func_probability.loc[df_gal_bldg_func_probability['probability'] <= func_prob_target, 'functionality_state'] = 'Non-Functional' # Non-Functional\n", + "df_gal_bldg_func_probability.loc[df_gal_bldg_func_probability['probability'] > func_prob_target, 'functionality_state'] = 'Functional' # Functional\n", + "df_gal_bldg_func_probability.head()\n" + ] + }, + { + "cell_type": "raw", + "id": "dca9720d", + "metadata": { + "scrolled": false + }, + "source": [ + "### Plot of the distribution of functionality state for buildings\n", + "indexes = df_gal_bldg_func_probability['functionality_state'].value_counts(normalize=True).mul(100).index.tolist()\n", + "values = df_gal_bldg_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)', pad=15)\n", + "ax.set(frame_on=False);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dbd1a228", + "metadata": {}, + "outputs": [], + "source": [ + "ret_json = DataProcessUtil.create_mapped_func_result(bldg_gdf, \n", + " gal_bldg_func_samples_df, \n", + " arch_mapping, \n", + " arch_col=\"arch_wind\")\n", + "with open(\"galveston_bldg_func_cluster.json\", \"w\") as f:\n", + " json.dump(ret_json, f, indent=2)" + ] + }, + { + "cell_type": "markdown", + "id": "08f62ae4", + "metadata": {}, + "source": [ + "# Capital Shocks" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1fada1a7", + "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(\"result_name\", \"sector_shocks\") # name of csv file with results" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9bbd01cd", + "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" + ] + }, + { + "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" + ] + }, + { + "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)\n", + "\n", + "result_name = \"galveston-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(low_memory=False)\n", + "population_dislocation_result_df.head()" + ] + }, + { + "cell_type": "markdown", + "id": "123208de", + "metadata": {}, + "source": [ + "## Housing Houshold Recovery Sequentials" + ] + }, + { + "cell_type": "markdown", + "id": "183e1e00", + "metadata": {}, + "source": [ + "### Social Vulnerabilities" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cf28d307", + "metadata": {}, + "outputs": [], + "source": [ + "social_vulnerability = SocialVulnerability(client)\n", + "\n", + "social_vulnerability.set_parameter(\"result_name\", \"gal_social_vulnerabilty\")\n", + "social_vulnerability.load_remote_input_dataset(\"national_vulnerability_feature_averages\", \"63d178c2a011a9746c948115\")\n", + "social_vulnerability.load_remote_input_dataset(\"social_vulnerability_demographic_factors\", \"63d17e81a011a9746c94811b\")\n", + "social_vulnerability.run_analysis()\n", + "sv_result = social_vulnerability.get_output_dataset(\"sv_result\")" + ] + }, + { + "cell_type": "markdown", + "id": "6d697f61", + "metadata": {}, + "source": [ + "### HHRS" + ] + }, + { + "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(\"population_dislocation_block\", population_dislocation_result)\n", + "housing_recovery.load_remote_input_dataset(\"tpm\", transition_probability_matrix)\n", + "housing_recovery.load_remote_input_dataset(\"initial_stage_probabilities\", initial_probability_vector)\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 = \"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(\"ds_result\").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)" + ] + } + ], + "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.8.16" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/notebooks/slc_community_app.ipynb b/notebooks/slc_community_app.ipynb index 3da9b204..ded9cc1c 100644 --- a/notebooks/slc_community_app.ipynb +++ b/notebooks/slc_community_app.ipynb @@ -390,6 +390,20 @@ "bdmcs.head()" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "d1a408f6", + "metadata": {}, + "outputs": [], + "source": [ + "ret_json = DataProcessUtil.create_mapped_func_result(bldg_gdf, \n", + " bdmcs, \n", + " arch_mapping, \n", + " arch_col=\"occ_type\")\n", + "ret_json" + ] + }, { "cell_type": "code", "execution_count": null, @@ -740,18 +754,15 @@ { "cell_type": "code", "execution_count": null, - "id": "7b51fee7", + "id": "d4c7cec9", "metadata": {}, "outputs": [], "source": [ - "bldg_func_probability = bldg_func.get_output_dataset('functionality_probability')\n", - "df_bldg_func = bldg_func_probability.get_dataframe_from_csv()\n", - "df_bldg_func = df_bldg_func.rename(columns={\"building_guid\": \"guid\"})\n", - "func_prob_target = 0.40\n", - "df_bldg_func.loc[df_bldg_func['probability'] <= func_prob_target, 'functionality'] = 0 # Non-Functional\n", - "df_bldg_func.loc[df_bldg_func['probability'] > func_prob_target, 'functionality'] = 1 # Functional\n", - "df_bldg_func.loc[df_bldg_func['probability'] <= func_prob_target, 'functionality_state'] = 'Non-Functional' # Non-Functional\n", - "df_bldg_func.loc[df_bldg_func['probability'] > func_prob_target, 'functionality_state'] = 'Functional' # Functional" + "ret_json = DataProcessUtil.create_mapped_func_result(bldg_gdf, \n", + " df_bldg_samples, \n", + " arch_mapping, \n", + " arch_col=\"occ_type\")\n", + "ret_json" ] }, { @@ -761,15 +772,22 @@ "metadata": {}, "outputs": [], "source": [ + "bldg_func_probability = bldg_func.get_output_dataset('functionality_probability')\n", + "df_bldg_func = bldg_func_probability.get_dataframe_from_csv()\n", + "df_bldg_func = df_bldg_func.rename(columns={\"building_guid\": \"guid\"})\n", + "func_prob_target = 0.40\n", + "df_bldg_func.loc[df_bldg_func['probability'] <= func_prob_target, 'functionality'] = 0 # Non-Functional\n", + "df_bldg_func.loc[df_bldg_func['probability'] > func_prob_target, 'functionality'] = 1 # Functional\n", + "df_bldg_func.loc[df_bldg_func['probability'] <= func_prob_target, 'functionality_state'] = 'Non-Functional' # Non-Functional\n", + "df_bldg_func.loc[df_bldg_func['probability'] > func_prob_target, 'functionality_state'] = 'Functional' # Functional\n", + "\n", "df_bldg_func.head()" ] }, { - "cell_type": "code", - "execution_count": null, - "id": "cff9fdd8", + "cell_type": "raw", + "id": "cf9c5a25", "metadata": {}, - "outputs": [], "source": [ "### Plot of the distribution of functionality state for buildings\n", "indexes = df_bldg_func['functionality_state'].value_counts(normalize=True).mul(100).index.tolist()\n", @@ -1471,14 +1489,6 @@ "# stage = HH_stagehistory_DF[t]\n", "# hhr_barchart_data[\"t\" + t] = [(stage==1.0).sum(),(stage==2.0).sum(),(stage==3.0).sum(),(stage==4.0).sum(),(stage==5.0).sum()]" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5958d1c4", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -1497,7 +1507,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.10" + "version": "3.8.16" } }, "nbformat": 4,