{"cells":[{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"%%javascript\nIPython.OutputArea.auto_scroll_threshold = 9999;","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"from IPython.core.display import HTML\nHTML(\"\"\"\n<style>\n@import url('http://fonts.googleapis.com/css?family=Crimson+Text');\n@import url('http://fonts.googleapis.com/css?family=Source+Code+Pro');\n\n/* Change code font */\n.CodeMirror pre {\n    font-family: 'Input Mono Narrow', 'Source Code Pro', Consolas, monocco, monospace;\n}\n\ndiv.input_area {\n    border-color: rgba(0,0,0,0.10);\n}\n\ndiv.text_cell {\n    max-width: 105ex; /* instead of 100%, */\n}\n\ndiv.text_cell_render {\n    font-family: \"Crimson Text\";\n    font-size: 12pt;\n    line-height: 145%; /* added for some line spacing of text. */\n}\n\ndiv.text_cell_render h1,\ndiv.text_cell_render h2,\ndiv.text_cell_render h3,\ndiv.text_cell_render h4,\ndiv.text_cell_render h5,\ndiv.text_cell_render h6 {\n    font-family: 'Crimson Text';\n}\n\n.rendered_html pre,\n.rendered_html code {\n    font-size: medium;\n}\n\n.rendered_html ol {\n    list-style:decimal;\n    margin: 1em 2em;\n}\n\n.prompt.input_prompt {\n    color: rgba(0,0,0,0.5);\n}\n\n.cell.command_mode.selected {\n    border-color: rgba(0,0,0,0.1);\n}\n\n.cell.edit_mode.selected {\n    border-color: rgba(0,0,0,0.15);\n    box-shadow: 0px 0px 5px #f0f0f0;\n    -webkit-box-shadow: 0px 0px 5px #f0f0f0;\n}\n\ndiv.output_scroll {\n    -webkit-box-shadow: inset 0 2px 8px rgba(0,0,0,0.1);\n    box-shadow: inset 0 2px 8px rgba(0,0,0,0.1);\n    border-radious: 2px;\n}\n\n#menubar .navbar-inner {\n    -webkit-box-shadow: none;\n    box-shadow: none;\n    border-radius: 0;\n    border: none;\n    font-family: lato;\n    font-weight: 400;\n}\n\n.navbar-fixed-top .navbar-inner,\n.navbar-static-top .navbar-inner {\n    box-shadow: none;\n    -webkit-box-shadow: none;\n    border: none;\n}\n\ndiv#notebook_panel {\n    box-shadow: none;\n    -webkit-box-shadow: none;\n    border-top: none;\n}\n\ndiv#notebook {\n    border-top: 1px solid rgba(0,0,0,0.15);\n}\n\n/* \n    This is a lazy fix, we *should* fix the \n    background for each Bootstrap button type\n*/\n#site * .btn {\n    -webkit-box-shadow: none;\n    box-shadow: none;\n}\n\n\nspan.ansiblack {color: #073642;}\nspan.ansiblue {color: #2aa198;}\nspan.ansigray {color: #839496;}\nspan.ansigreen {color: #859900;}\nspan.ansipurple {color: #6c71c4;}\nspan.ansired {color: #dc322f;}\nspan.ansiyellow {color: #b58900;}\n\ndiv.output_stderr {background-color: #dc322f;}\ndiv.output_stderr pre {color: #eee8d5;}\n\n.cm-s-ipython.CodeMirror {background: #fdf6e3; color: #073642;}\n.cm-s-ipython div.CodeMirror-selected {background: #eee8d5 !important;}\n.cm-s-ipython .CodeMirror-gutters {background: #fdf6e3; border-right: 0px;}\n.cm-s-ipython .CodeMirror-linenumber {color: #839496;}\n.cm-s-ipython .CodeMirror-cursor {border-left: 1px solid #657b83 !important;}\n\n.cm-s-ipython span.cm-comment {color: #d33682;}\n.cm-s-ipython span.cm-atom {color: #6c71c4;}\n.cm-s-ipython span.cm-number {color: #6c71c4;}\n\n.cm-s-ipython span.cm-property, .cm-s-ipython span.cm-attribute {color: #859900;}\n.cm-s-ipython span.cm-keyword {color: #dc322f;}\n.cm-s-ipython span.cm-string {color: #b58900;}\n.cm-s-ipython span.cm-operator {color: #d33682;}\n.cm-s-ipython span.cm-builtin {color: #6c71c4;}\n\n.cm-s-ipython span.cm-variable {color: #859900;}\n.cm-s-ipython span.cm-variable-2 {color: #268bd2;}\n.cm-s-ipython span.cm-def {color: #cb4b16;}\n.cm-s-ipython span.cm-error {background: #dc322f; color: #657b83;}\n.cm-s-ipython span.cm-bracket {color: #586e75;}\n.cm-s-ipython span.cm-tag {color: #dc322f;}\n.cm-s-ipython span.cm-link {color: #6c71c4;}\n\n.cm-s-ipython .CodeMirror-matchingbracket { text-decoration: underline; color: #073642 !important;}\nth, td { \n      font-size: 18px;\n      border-collapse: collapse;\n      border-width:3px;\n    }\n    </style>\n\n\"\"\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Inequity, Vulnerability, and Resilience: Understanding the Risks that Cities Face & Finding New Synergies with Corporations"},{"metadata":{},"cell_type":"markdown","source":"# <a id='toc'>Table of Contents</a>\n1. **Overview**\n2. **Cities**<br>\n    2.1 Inequity<br>\n    2.2 Vulnerability<br>\n    2.3 Resilience<br>\n3. **A Tale of Three Cities**<br>\n    3.1 Sacramento, CA<br>\n    3.2 Somerville, MA<br>\n    3.3 Philadelphia, PA<br>\n4. **The Future** <br>\n"},{"metadata":{},"cell_type":"markdown","source":"#### Climate change is the single greatest threat facing humanity today. The future of civilization is largely uncertain and bound to be messy. The central theme that I kept coming back to throughout working on this project was the importance of community-based action. To that end, I chose to focus most of my analysis on cities. In particular, I focused on cities in the US because of the abundance of public, external datasets for American cities. \n\nFor this analysis, I will be largely be focusing on three of the four broad questions that CDP put forth. \n\n\n       How can we measure the intersection between environmental risks and social equity, as a contributor to resiliency?\n       \n       How do you help cities adapt to a rapidly changing climate amidst a global pandemic, but do it in a way that is socially equitable?\n\n       What are the practical and actionable points where city and corporate ambition join, i.e. where do cities have problems that corporations affected by those problems could solve, and vice versa?\n\n\nThe framework I will present, though, can be further refined to focus on the fourth question of what concrete projects can cities and corporations collaborate on? \n\nLet's start with the first (and perhaps the most important) question: How can we measure the intersection between environmental risks and social equity, as a contributor to resiliency? To do this, I set out to build summary metrics that can capture the degree of environmental **vulnerability**, magnitude of social **inequity**, and **adaptability** for any given city. \n\nAfter combining the CDP cities survey with a dozen or so external datasets, I derived more than 100 KPIs for each city under those three broad categories (vulnerability, inequity, adaptability). The gory details of each metric can be found in the linked notebooks near the end of the notebook. I will first present maps of the US colored by aggregated KPIs in the three different categories."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom scipy import stats\nfrom sklearn.preprocessing import RobustScaler, QuantileTransformer\nimport rasterio\nfrom matplotlib import colors\nfrom scipy import stats\nfrom tqdm import *\n%matplotlib inline\nfrom pyproj import Transformer\nimport ast\nfrom sklearn.preprocessing import RobustScaler, QuantileTransformer, StandardScaler\nimport altair as alt\nimport plotly.express as px\nimport plotly.graph_objects as go\n\nplt.style.use(\"../input/harshmpl/harsh\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"cities_2020 = pd.read_csv(\"../input/externald/Supplementary Data/cities_updated_geo_us_2020.csv\")\nacct_number_cities_2020 = cities_2020[\"Account Number\"]\ncounties = (cities_2020['counties'])\ncounties_weights = (cities_2020['counties_weights'])\nstates = (cities_2020['state'])\nflag_multi_counties = cities_2020['flag_multiple_counties']\nmatch_fips = cities_2020['FIPS']\nmatch_fips = [ast.literal_eval(match_fips[i]) if flag_multi_counties[i] else int(match_fips[i]) for i in range(len(match_fips))]\ncities = cities_2020[\"city_bing\"]\n\ncity_locations = ((cities_2020[\"loc\"])).apply(lambda x: (eval(x.split(\"POINT \")[1].replace(\" \",\",\").replace(\"(\", \"[\").replace(\")\", \"]\"))) if type(x) == str else Point(0,0))\ncity_locations = np.vstack(city_locations)\nclat = city_locations[:,1]\nclng = city_locations[:,0]","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":false,"trusted":true},"cell_type":"code","source":"city_aggr_equity = pd.read_csv(\"../input/derivedkpis/KPIs/equity_cities_us_aggr_kpis.csv\")\ncity_aggr_vul = pd.read_csv(\"../input/derivedkpis/KPIs/vulnerability_cities_us_all_aggr_kpis.csv\")\ncity_aggr_adapt = pd.read_csv(\"../input/derivedkpis/KPIs/adapt_cities_us_aggr_kpis.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"def figure_equity(field, title, cmap='plasma_r'):\n    fig = go.Figure()\n\n    string_hover = cities + \"<br>\" + \"Overall: \" +  city_aggr_equity[\"aggr_all_inequity\"].astype(str).str[0:4] + \"<br>\" + \"Health: \" + \\\n    city_aggr_equity[\"aggr_health_kpis\"].astype(str).str[0:4] + \"<br>\" + \"Socioeconomic: \" + city_aggr_equity[\"aggr_socio_economic_kpis\"].astype(str).str[0:4]\\\n    + \"<br>\" + \"Housing: \" + city_aggr_equity[\"aggr_housing_kpis\"].astype(str).str[0:4]\n\n    fig = go.Figure(go.Scattergeo(\n        lat=clat,\n        lon=clng,\n        hovertext=string_hover,\n        marker=dict(opacity=0.8,color=(city_aggr_equity[field]),autocolorscale = False,\n                    colorscale=cmap,\n                    size=10, colorbar=dict(\n                      title=title + \"<br>\" + \"Inequity\", x=0.9), line_color='rgb(40,40,40)')\n    ))\n\n    fig.update_layout(\n            title = {\"text\":\n                     title + ' Inequity Index',\n                     'y':0.9,\n            'x':0.5,\n            'xanchor': 'center',\n            'yanchor': 'top'},\n            geo = dict(\n                scope='usa',\n                projection_type='albers usa',\n                showland = True,\n                landcolor = \"rgb(240, 240, 240)\",\n                subunitcolor = \"rgb(217, 217, 217)\",\n                countrycolor = \"rgb(217, 217, 217)\",\n                countrywidth = 0.5,\n                subunitwidth = 0.5\n            ),\n            font=dict(\n            family=\"Garamond\",\n            color=\"black\",\n            size=22\n        )\n    )\n\n    fig.show()\n    \ndef figure_vul(field, title, cmap=\"viridis\"):\n    fig = go.Figure()\n\n    string_hover = cities + \"<br>\" + \"Overall: \" +  city_aggr_vul[\"aggr_vul_all_kpis\"].astype(str).str[0:4] + \"<br>\" + \"Heat: \" + \\\n    city_aggr_vul[\"aggr_heat_kpis\"].astype(str).str[0:4] + \"<br>\" + \"Cold: \" + city_aggr_vul[\"aggr_cold_kpis\"].astype(str).str[0:4]\\\n    + \"<br>\" + \"Drought: \" + city_aggr_vul[\"aggr_drought_kpis\"].astype(str).str[0:4] + \"<br>\" + \"Severe Weather: \" + city_aggr_vul[\"aggr_severe_kpis\"].astype(str).str[0:4] \\\n    + \"<br>\" + \"Floods: \" + city_aggr_vul[\"aggr_flood_kpis\"].astype(str).str[0:4]\n\n\n    fig = go.Figure(go.Scattergeo(\n        lat=clat,\n        lon=clng,\n        hovertext=string_hover,\n        marker=dict(opacity=0.8,color=(city_aggr_vul[field]),autocolorscale = False,\n                    colorscale=cmap,\n                    size=10, colorbar=dict(\n                      title=title + \"<br>\" + \"Vulnerability\", x=0.9), line_color='rgb(40,40,40)')\n    ))\n\n    fig.update_layout(\n            title = {\"text\":\n                     title + ' Vulnerability Index',\n                     'y':0.9,\n            'x':0.5,\n            'xanchor': 'center',\n            'yanchor': 'top'},\n            geo = dict(\n                scope='usa',\n                projection_type='albers usa',\n                showland = True,\n                landcolor = \"rgb(240, 240, 240)\",\n                subunitcolor = \"rgb(217, 217, 217)\",\n                countrycolor = \"rgb(217, 217, 217)\",\n                countrywidth = 0.5,\n                subunitwidth = 0.5\n            ),\n            font=dict(\n            family=\"Garamond\",\n            color=\"black\",\n            size=24\n        )\n    )\n\n    fig.show()\n\ndef figure_adapt(field, title, cmap=\"haline_r\"):\n    fig = go.Figure()\n\n    string_hover = cities + \"<br>\" + \"Overall: \" +  city_aggr_adapt[\"aggr_adaptability_kpis\"].astype(str).str[0:4] + \"<br>\" + \"Resilience: \" + \\\n    city_aggr_adapt[\"aggr_resilience_kpis\"].astype(str).str[0:4] + \"<br>\" + \"Sentiment: \" + city_aggr_adapt[\"aggr_sentiment_kpis\"].astype(str).str[0:4] + \"<br>\" \n\n\n    fig = go.Figure(go.Scattergeo(\n        lat=clat,\n        lon=clng,\n        hovertext=string_hover,\n        marker=dict(opacity=0.8,color=(city_aggr_adapt[field]),autocolorscale = False,\n                    colorscale=cmap,\n                    size=10, colorbar=dict(\n                      title=title + \"<br>\", x=0.9), line_color='rgb(40,40,40)')\n    ))\n\n    fig.update_layout(\n            title = {\"text\":\n                     title,\n                     'y':0.9,\n            'x':0.5,\n            'xanchor': 'center',\n            'yanchor': 'top'},\n            geo = dict(\n                scope='usa',\n                projection_type='albers usa',\n                showland = True,\n                landcolor = \"rgb(240, 240, 240)\",\n                subunitcolor = \"rgb(217, 217, 217)\",\n                countrycolor = \"rgb(217, 217, 217)\",\n                countrywidth = 0.5,\n                subunitwidth = 0.5\n            ),\n            font=dict(\n            family=\"Garamond\",\n            color=\"black\",\n            size=24\n        )\n    )\n\n    fig.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Inequity\n\n## Health Inequity \n\nHeath inequity-specific KPIs are listed in the cell below. We combine these different scores by taking their average and then ranking each city. "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"figure_equity(\"aggr_health_kpis\", \"Health\")","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"figure_equity(\"aggr_socio_economic_kpis\", \"Socioeconomic\", cmap=\"electric_r\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"figure_equity(\"aggr_housing_kpis\", \"Housing\", cmap=\"brwnyl\")","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"figure_equity(\"aggr_all_inequity\", \"Combined Social\", cmap=\"haline_r\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Health KPIs\n\n| KPI Name                | KPI Interpretation                                                     | Public Data Source                 |\n|-------------------------|------------------------------------------------------------------------|------------------------------------|\n| minority_language       | CDC's Social Vulnerability Index for minorities                        | CDC                                |\n| food_insecurity         | Food insecure percentage                                               | Mind the Meal Gap                  |\n| child_food_insecurity   | Food insecure percentage (children only)                               | Mind the Meal Gap                  |\n| premature_death_rate    | Premature death rate                                                   | County Health Rankings Trends Data |\n| poor_health             | % of people reporting to have poor health                              | County Health Rankings Trends Data |\n| healthy_food_access     | Access to healthy food                                                 | County Health Rankings Trends Data |\n| percent_uninsured       | Percentage uninsured                                                   | County Health Rankings Trends Data |\n| primary_care_physicians | Number of primary care physicians per x people                         | County Health Rankings Trends Data |\n| premature_death_trend   | Premature death trend over last ten year                               | County Health Rankings Trends Data |\n| pcp_trend               | Number of primary care physicians per x people over the last ten years | County Health Rankings Trends Data |\n\n\n# Socioeconomic KPIs\n\n| KPI Name               | KPI Unit    | KPI Interpretation                                              | Public Data Source                 |\n|------------------------|-------------|-----------------------------------------------------------------|------------------------------------|\n| socio_economic         | unitless    | CDC's Social Vulnerability Index for socioeconomic indicators   | CDC                                |\n| absolute_mobility      | percentile  | Absolute upward mobility for children compared to their parents | Chetty et al. (2014)               |\n| job_density            | jobs/sqmile | Job density                                                     | Chetty et al. (2014) / Census      |\n| avg_job_growth         | %           | Average job growth                                              | Chetty et al. (2014) / Census      |\n| unemployment_trend     | unitless    | Change in unemployment over last ten years                      | County Health Rankings Trends Data |\n| children_poverty_trend | unitless    | Change in child poverty over last ten years                     | County Health Rankings Trends Data |\n\n\n\n# Housing KPIs\n\n| KPI Name                     | KPI Interpretation                             | Public Data Source                 |\n|------------------------------|------------------------------------------------|------------------------------------|\n| house_composition_disability | House composition / disability index           | CDC                                |\n| house_type_transportation    | Housing type and transportation inequity index | CDC                                |\n| segregation_black_white      | Segregation black/white                        | County Health Rankings Trends Data |\n| segregation_nonwhite_white   | Segregation non-white/white                    | County Health Rankings Trends Data |\n| housing_cost_burden          | Burden in housing cost (% of income)           | County Health Rankings Trends Data |"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"figure_vul(\"aggr_heat_kpis\", \"Heat\", cmap=\"hot_r\")\nfigure_vul(\"aggr_cold_kpis\", \"Cold\", cmap=\"blues\")\nfigure_vul(\"aggr_drought_kpis\", \"Drought\", cmap=\"ylorbr\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"figure_vul(\"aggr_flood_kpis\", \"Floods\", cmap=\"ylgnbu\")\nfigure_vul(\"aggr_severe_kpis\", \"Severe Storm\", cmap=\"sunsetdark\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"figure_vul(\"aggr_vul_all_kpis\", \"Combined\", cmap=\"bluered\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Heat \n\n| KPI Name                  | KPI Interpretation                                                   | Public Data Source       |\n|---------------------------|----------------------------------------------------------------------|--------------------------|\n| score_heat_wave           | Calculated score based on survey response                            | CDP Survey               |\n| score_hot_days            | Calculated score based on survey response                            | CDP Survey               |\n| all_locs_construction     | Ratio of construction workers in city compared to rest of the US     | OECS                     |\n| all_locs_transportation   | Ratio of transporation workers in city compared to rest of the US    | OECS                     |\n| all_locs_protective       | Ratio of protective workers in city compared to rest of the US       | OECS                     |\n| all_locs_building_grounds | Ratio of building grounds workers in city compared to rest of the US | OECS                     |\n| all_locs_farming          | Ratio of farming/ag workers in city compared to rest of the US       | OECS                     |\n| heat_maximum_temp         | Heat wave days based on maximum temperature                          | CDC Wonder               |\n| heat_maximum_stress       | Heat wave days based on net daily heat stress                        | CDC Wonder               |\n| heat_maximum_index        | Heat wave days based on maximum heat index                           | CDC Wonder               |\n| heat_maximum_temp_std     | Heat wave days variability based on maximum temperature              | CDC Wonder               |\n| heat_maximum_stress_std   | Heat wave days variability based on net daily heat stress            | CDC Wonder               |\n| heat_maximum_index_std    | Heat wave days variability based on maximum heat index               | CDC Wonder               |\n| heat_wave_eals            | Heat wave expected annual loss                                       | FEMA National Risk Index |\n\n# Cold\n\n| KPI Name                | KPI Interpretation                                                        | Public Data Source                |\n|-------------------------|---------------------------------------------------------------------------|-----------------------------------|\n| score_cold_wave         | Calculated score based on survey response                                 | CDP Survey                        |\n| score_cold_days         | Calculated score based on survey response                                 | CDP Survey                        |\n| score_winter_conditions | Calculated score based on survey response                                 | CDP Survey                        |\n| score_heavy_snow        | Calculated score based on survey response                                 | CDP Survey                        |\n| fema_cold_events        | # of FEMA disaster declarations near city related to cold weather         | FEMA Disaster Declaration Summary |\n| tmin_averaged           | Minimum temp per month averaged over the last 20 years                    | NOAA tmin data                    |\n| tmin_std                | Variability in minimum temp per month averaged over the last 20 years     | NOAA tmin data                    |\n| tmin_dt                 | Rate of change in minimum temp per month averaged over the last 20 years  | NOAA tmin data                    |\n| all_locs_construction   | Ratio of construction workers in city compared to rest of the US          | OECS                              |\n| all_locs_transportation | Ratio of Transporation workers in city compared to rest of the US         | OECS                              |\n| winter_weather_risk     | Winter weather expected annual loss score (buildings, people, etc.)       | FEMA National Risk Index          |\n| icestorm_risk           | Ice-storm expected annual loss score (buildings, people, etc.)            | FEMA National Risk Index          |\n| hail_risk               | Hail expected annual loss score (buildings, people, etc.)                 | FEMA National Risk Index          |\n| cold_wave_risk          | Cold wave expected annual loss score (buildings, people, etc.)            | FEMA National Risk Index          |\n\n# Drought \n\n| KPI Name                        | KPI Interpretation                                               | Public Data Source             |\n|---------------------------------|------------------------------------------------------------------|--------------------------------|\n| drought_severity                | drought severity index                                           | UNL/drougt.gov  (custom query) |\n| drought_severity_variability    | drought variability index                                        | UNL/drougt.gov (custom query)  |\n| water_loss                      | freshwater loss                                                  | SEDAC                          |\n| all_locs_construction           | Ratio of construction workers in city compared to rest of the US | OECS                           |\n| all_locs_farming                | Ratio of farming/ag workers in city compared to rest of the US   | OECS                           |\n| score_drought                   | Calculated score based on survey response                        | CDP                            |\n| score_wild_fire                 | Calculated score based on survey response                        | CDP                            |\n| score_land_fire                 | Calculated score based on survey response                        | CDP                            |\n| fema_drought_fire_events_ranked | # of FEMA drought / wild fire events disasters                   | FEMA National Risk Index       |\n| drought_eals                    | drought expected annual loss                                     | FEMA National Risk Index       |\n| wildfire_eals                   | wildfire expected annual loss                                    | FEMA National Risk Index       |\n\n# Severe Weather \n\n| Severe                    |                                                                           |                          |\n|---------------------------|---------------------------------------------------------------------------|--------------------------|\n| KPI Name                  | KPI Interpretation                                                        | Public Data Source       |\n| severe_storm_score        | Calculated score based on survey response                                 | CDP Survey               |\n| fema_extreme_events       | # of FEMA severe storm disasters                                          | FEMA National Risk Index |\n| percent_built_before_1999 | Percentage of buildings built before 1999                                 | US Census                |\n| precip_averaged           | Minimum temp per month averaged over the last 20 years                    | NOAA tmin data           |\n| precip_std                | Variability in minimum temp per month averaged over the last 20 years     | NOAA tmin data           |\n| precip_dt                 | Rate of change in minimum temp per month averaged over the last 20 years  | NOAA tmin data           |\n\n# Floods\n\n| KPI Name                  | KPI Interpretation                                                   | Public Data Source         |\n|---------------------------|----------------------------------------------------------------------|----------------------------|\n| flood_score               | Calculated score based on survey response                            | CDP Survey                 |\n| insured_locations         | # of NFIP insured locations (should be indication of exposure)       | FEMA NFIP                  |\n| fema_flood_events         | # of FEMA disaster declarations near city related to floods          | FEMA Disaster Declarations |\n| fema_coastal_storms       | # of FEMA disaster declarations near city related to coastal storms  | FEMA Disaster Declarations |\n| fema_landslides           | # of FEMA disaster declarations near city related to landslides      | FEMA Disaster Declarations |\n| all_locs_building_grounds | Ratio of building grounds workers in city compared to rest of the US | OECS                       |\n| all_locs_farming          | Ratio of farming/ag workers in city compared to rest of the US       | OECS                       |\n| percent_built_before_1999 | Percentage of buildings built before 1999                            | US Census                  |\n| coastal_flood_eals        | Coastal flood expected annual loss                                   | FEMA National Risk Index   |\n| river_flood_eals          | River flood expected annual loss                                     | FEMA National Risk Index   |\n| landslide_eals            | Landslide expected annual loss                                       | FEMA National Risk Index   |","attachments":{}},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"figure_adapt(\"aggr_resilience_kpis\", \"Resilience\", cmap=\"aggrnyl_r\")\nfigure_adapt(\"aggr_sentiment_kpis\", \"Sentiment\", cmap=\"rdbu\")\nfigure_adapt(\"aggr_adaptability_kpis\", \"Overall Adaptability\", cmap=\"pubugn\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Adaptability \n| KPI Name                       | KPI Interpretation                                       | Public Data Source |\n|--------------------------------|----------------------------------------------------------|--------------------|\n| percent_reduction_per_year     | Reduction in carbon emissions                            | CDP survey         |\n| percent_target_acheived_ranked | Target in GHG emissions reduction reached                | CDP survey         |\n| water_security_adaptation      | water_security_adaptation score                          | CDP survey         |\n| opportunity_city_sentiment     | sentiment analysis on free-text related to opportunities | CDP survey         |\n| collab_city_sentiment          | sentiment analysis on free-text related to opportunities | CDP survey         |\n| social_resilience              | Social resilience score                                  | BRIC indices       |\n| econ_resilience                | Economic resilience score                                    | BRIC indices       |\n| infra_resilience               | Infrastructure resilience score                                   | BRIC indices       |\n| comm_resilience                | Community resilience score                                    | BRIC indices       |\n| inst_resilience                | Institutional resilience score                                    | BRIC indices       |\n| env_resilience                 | Environmental resilience score                                     | BRIC indices       |\n| tot_resilience                 | Total resilience score                                     | BRIC indices       |\n| delta_social_resilience        | delta_social_resilience score (change in 5 years)        | BRIC indices       |\n| delta_econ_resilience          | delta_econ_resilience score (change in 5 years)          | BRIC indices       |\n| delta_infra_resilience         | delta_infra_resilience score (change in 5 years)         | BRIC indices       |\n| delta_comm_resilience          | delta_comm_resilience score (change in 5 years)          | BRIC indices       |\n| delta_inst_resilience          | delta_inst_resilience score (change in 5 years)          | BRIC indices       |\n| delta_env_resilience           | delta_env_resilience score (change in 5 years)           | BRIC indices       |\n| delta_tot_resilience           | delta_tot_resilience score (change in 5 years)           | BRIC indices       |"},{"metadata":{},"cell_type":"markdown","source":"There is a lot of information in the these maps. If you hover over any given point, it will show you the different scores split by category. \n\nA few good sanity checks: \n* The south is more vulnerable to flooding, severe weather\n* The west is more vulnerable to droughts, heat\n* The northeast and the midwest are more vulnerable to cold weather\n* Housing inequity is rampant in large metropolitan areas\n* Socioeconomic vulnerability is large in the south\n\nLet's slice and dice the aggregated KPIs a bit more. "},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"def scatter(x,y,hovertext=cities_2020[\"city_bing\"],color=None,cmap=\"viridis\", xlabel=\"\", ylabel=\"\", title=\"plot\", cbar_title=\"title\"):\n    fig = go.Figure(data=go.Scatter(\n        x = x,\n        y = y,\n        hovertext=hovertext,\n        mode='markers',\n        marker=dict(\n            size=14,\n            color=color, #set color equal to a variable\n            colorscale=cmap, # one of plotly colorscales\n            showscale=True, colorbar=dict(\n                      title=cbar_title + \"<br>\", x=1.05), line_color='rgb(40,40,40)'\n        )))\n\n    \n    fig.update_layout(\n        title = {\"text\":\n                     title,\n                     'y':0.9,\n            'x':0.5,\n            'xanchor': 'center',\n            'yanchor': 'top'},\n        xaxis=dict(\n            title=xlabel,\n            gridcolor='white',\n            gridwidth=2,\n        ),\n        \n        yaxis=dict(\n            title=ylabel,\n            gridcolor='white',\n            gridwidth=2,\n        ),\n        paper_bgcolor='rgb(243, 243, 243)',\n        plot_bgcolor='rgb(243, 243, 243)',\n            font=dict(\n            family=\"Garamond\",\n            color=\"black\",\n            size=22\n        )\n    )\n    \n    fig.update_layout(xaxis=dict(scaleanchor='y', constrain='domain'),\n                  coloraxis_colorbar_x=0.5, width=900, height=800\n                 )\n\n\n    fig.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"scatter(city_aggr_equity[\"aggr_all_inequity\"], city_aggr_vul[\"aggr_vul_all_kpis\"], xlabel=\"Inequity\", ylabel=\"Vulnerability\", \n       color=city_aggr_adapt[\"aggr_adaptability_kpis\"], cmap=\"rdbu\", title=\"Social Inequity & Climate Vulnerability\",\n       cbar_title=\"Resilience\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"scatter(city_aggr_adapt[\"aggr_adaptability_kpis\"], city_aggr_vul[\"aggr_vul_all_kpis\"], xlabel=\"Adaptability\", ylabel=\"Vulnerability\", \n       color=city_aggr_equity[\"aggr_all_inequity\"], cmap=\"haline_r\", title=\"Cities' Adaptability & <br> Climate Vulnerability\",\n       cbar_title=\"Inequity\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"scatter(city_aggr_equity[\"aggr_all_inequity\"], city_aggr_adapt[\"aggr_adaptability_kpis\"], xlabel=\"Inequity\", ylabel=\"Adaptability\", \n       color=city_aggr_vul[\"aggr_vul_all_kpis\"], cmap=\"plasma_r\", title=\"Cities' Social Inequity & Adaptability\",\n       cbar_title=\"Inequity\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"These scatterplots show the relationships between cities' inequity, vulnerability, and resilience sliced a couple of different ways. \n\nA few things seem immediately clear:\n\n* There seems to be a slight positive correlation between the inequity and vulnerability of cities. This could imply that the cities most at risk from climate hazards could also be host to more social inequity than other cities.\n* There seems to be no clear connection between adaptability and vulnerability. That seems a bit worrying -- one would hope that the most vulnerable cities are at least marginally more resilient than others. \n\nLet's make a few conditional plots to understand a bit more what's going on! "},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"import seaborn as sns\nsns.set_style(\"white\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"fig, ax = plt.subplots(1,2,figsize=(24,12))\nlowres = city_aggr_adapt[\"aggr_resilience_kpis\"] < 0.5\nsns.kdeplot(city_aggr_vul[\"aggr_vul_all_kpis\"][lowres], ax=ax[0], alpha=0.5, label=\"Vulnerability\", color=\"cornflowerblue\")\nsns.kdeplot(city_aggr_equity[\"aggr_all_inequity\"][lowres], ax=ax[0], label=\"Inequity\", color=\"black\")\nlowres = city_aggr_adapt[\"aggr_adaptability_kpis\"] > 0.5\nsns.kdeplot(city_aggr_vul[\"aggr_vul_all_kpis\"][lowres], ax=ax[1], alpha=0.5,label=\"Vulnerability\", color=\"cornflowerblue\")\nsns.kdeplot(city_aggr_equity[\"aggr_all_inequity\"][lowres], ax=ax[1],  label=\"Inequity\", color=\"black\")\nax[0].legend(loc='best', fontsize=26, frameon=False)\nax[1].legend(loc='best', fontsize=26, frameon=False)\nax[0].set_title(\"Low Resiliency Cities (< 0.5)\", y=1.02, fontsize=32)\nax[1].set_title(\"High Resiliency Cities (> 0.5)\", y=1.02, fontsize=32)\nax[0].set_xlabel(\"Index\")\nax[1].set_xlabel(\"Index\")\nax[0].set_ylim([0,2.2])\nax[0].set_ylim([0,2.2])\nax[0].set_xlim([-0.1, 1.1])\nax[1].set_xlim([-0.1, 1.1])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"fig, ax = plt.subplots(1,2,figsize=(24,12))\nlowres = city_aggr_vul[\"aggr_vul_all_kpis\"] < 0.5\nsns.kdeplot(city_aggr_equity[\"aggr_all_inequity\"][lowres], ax=ax[0], label=\"Inequity\")\nsns.kdeplot(city_aggr_adapt[\"aggr_adaptability_kpis\"][lowres], ax=ax[0], alpha=0.5, label=\"Adaptability\")\nlowres = city_aggr_vul[\"aggr_vul_all_kpis\"] > 0.5\n\nsns.kdeplot(city_aggr_equity[\"aggr_all_inequity\"][lowres], ax=ax[1], label=\"Inequity\")\nsns.kdeplot(city_aggr_adapt[\"aggr_adaptability_kpis\"][lowres], ax=ax[1], alpha=0.5, label=\"Adaptability\")\nax[0].legend(loc='best', fontsize=26, frameon=False)\nax[1].legend(loc='best', fontsize=26, frameon=False)\nax[0].set_title(\"Low Vulnerability Cities (< 0.5)\", y=1.02, fontsize=32)\nax[1].set_title(\"High Vulnerability Cities (> 0.5)\", y=1.02, fontsize=32)\nax[0].set_xlabel(\"Index\")\nax[1].set_xlabel(\"Index\")\nax[0].set_xlim([-0.1, 1.1])\nax[1].set_xlim([-0.1, 1.1])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"fig, ax = plt.subplots(1,2,figsize=(24,12))\n\nlowres = city_aggr_equity[\"aggr_all_inequity\"] < 0.5  \nsns.kdeplot(city_aggr_vul[\"aggr_vul_all_kpis\"][lowres], ax=ax[0], label=\"Vulnerability\")\nsns.kdeplot(city_aggr_adapt[\"aggr_adaptability_kpis\"][lowres], ax=ax[0], alpha=0.5, label=\"Adaptability\")\n\nlowres = city_aggr_equity[\"aggr_all_inequity\"] > 0.5  \nsns.kdeplot(city_aggr_vul[\"aggr_vul_all_kpis\"][lowres], ax=ax[1], label=\"Vulnerability\")\nsns.kdeplot(city_aggr_adapt[\"aggr_adaptability_kpis\"][lowres], ax=ax[1], alpha=0.5, label=\"Adaptability\")\nax[0].legend(loc='best', fontsize=26, frameon=False)\nax[1].legend(loc='best', fontsize=26, frameon=False)\nax[0].set_title(\"Low Inequity Cities (< 0.5)\", y=1.02, fontsize=32)\nax[1].set_title(\"High Inequity Cities (> 0.5)\", y=1.02, fontsize=32)\nax[0].set_xlabel(\"Index\")\nax[1].set_xlabel(\"Index\")\nax[0].set_xlim([-0.1, 1.1])\nax[1].set_xlim([-0.1, 1.1])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Another few things come into focus after making these conditional plots: \n* **Surprisingly**, inequity increases(!) as resiliency increases. This is of great concern because resiliency should not be coming at the cost of equity! \n* Cities with high inequity seem to be more vulnerable to climate hazards than cities with low inequity. \n* Adaptability seems to largely stay constant regardless of vulnerability. I naively expected there to be some correlation. \n\n### What factors are driving these different correlations between inequity, vulnerability, and adaptability? "},{"metadata":{},"cell_type":"markdown","source":"## **Zooming into three different individual cities**\n\nI will show the following for three different cities: \n\n* Different social inequity indicators that are relevant to that city\n* Different climate vulnerabilities that are relevant\n* Resiliency and adaptability scores for that city\n* Corporations close-by with the following attributes: alignment of climate-related opportunities for the city and coporation (calculated using NLP text similarity from spaCy), alignment of climate-related collaboration potential for the city and coporation\n* The corporations' sentiment towards these opportunities and collaboration potential. \n\n### Sacramento, CA"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_sacramento = 118\nacct_sacramento = acct_number_cities_2020[idx_sacramento]\n\naggr_socio_economic_kpis = city_aggr_equity[\"aggr_socio_economic_kpis\"]\nprint(\"Sacramento's socioecnomic inequity index is %f\" % aggr_socio_economic_kpis[idx_sacramento])\n\naggr_health_kpis = city_aggr_equity[\"aggr_health_kpis\"]\nprint(\"Sacramento's health inequity index is %f\" % aggr_health_kpis[idx_sacramento])\n\naggr_housing_kpis = city_aggr_equity[\"aggr_housing_kpis\"]\nprint(\"Sacramento's housing inequity index is %f\" % aggr_housing_kpis[idx_sacramento])\n\naggr_all_kpis = city_aggr_equity[\"aggr_all_inequity\"]\nprint(\"Sacramento's overall inequity index is %f\" % aggr_all_kpis[idx_sacramento])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"def three_by_two_plot(fields, xlabels, idx_city=0, city=\"Somerville\", color=\"cornflowerblue\", log=False):\n    fig, ax = plt.subplots(3,2,figsize=(20,30))\n\n    sns.kdeplot(df[fields[0]][:], label=\"\", color=color, shade=True, ax=ax[0,0])\n    ax[0,0].axvline(df[fields[0]][idx_city], linestyle='--', label=city)\n    ax[0,0].set_xlabel(xlabels[0])\n    ax[0,0].legend(loc='best', fontsize=24, frameon=False)\n\n    sns.kdeplot(df[fields[1]][:], label=\"\", color=color, shade=True, ax=ax[0,1])\n    ax[0,1].axvline(df[fields[1]][idx_city], linestyle='--')\n    ax[0,1].set_xlabel(xlabels[1])\n    ax[0,1].legend(loc='best', fontsize=24, frameon=False)\n    \n    if(log):\n        df[fields[2]][:] = np.log10(df[fields[2]][:])\n    sns.kdeplot(np.nan_to_num(df[fields[2]][:], posinf=0.0), label=\"\", color=color, shade=True, ax=ax[1,0])\n    ax[1,0].axvline(np.nan_to_num(df[fields[2]][idx_city], posinf=0.0), linestyle='--', label=city)\n    ax[1,0].set_xlabel(xlabels[2])\n    ax[1,0].legend(loc='best', fontsize=24, frameon=False)\n\n    sns.kdeplot(np.nan_to_num(df[fields[3]][:], posinf=0.0), label=\"\", color=color, shade=True, ax=ax[1,1])\n    ax[1,1].axvline(np.nan_to_num(df[fields[3]][idx_city], posinf=0.0), linestyle='--')\n    ax[1,1].set_xlabel(xlabels[3])\n    ax[1,1].legend(loc='best', fontsize=24, frameon=False)\n\n    sns.kdeplot((df[fields[4]][:]), label=\"\", color=color, shade=True, ax=ax[2,0])\n    ax[2,0].axvline((df[fields[4]][idx_city]), linestyle='--', label=city)\n    ax[2,0].set_xlabel(xlabels[4])\n    ax[2,0].legend(loc='best', fontsize=24, frameon=False)\n\n\n    sns.kdeplot((df[fields[5]][:]), label=\"\", color=color, shade=True, ax=ax[2,1])\n    ax[2,1].axvline((df[fields[5]][idx_city]), linestyle='--')\n    ax[2,1].set_xlabel(xlabels[5])\n    ax[2,1].legend(loc='best', fontsize=24, frameon=False)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/derivedkpis/KPIs/equity_cities_us_kpis_raw.csv\")\n\nfields_to_plot = [\"socio_economic\", \"absolute_mobility\", \"job_density\",\n                 \"avg_job_growth\", \"unemployment_trend\", \"children_poverty_trend\"]\nlabels = [\"Socioeconomic Vulnerability Index\", \"Absolute Upward Mobility\", \"log(Job Density)\",\n         \"Average Job Growth\", \"Unemployment Trend\", \"Children in Poverty Trend\"]\n\n\nthree_by_two_plot(fields_to_plot, labels, idx_city=idx_sacramento, city=\"Sacramento\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"print(\"Sacramento's overall vulnerability index is %f\" % city_aggr_vul[\"aggr_vul_all_kpis\"][idx_sacramento])\n\nprint(\"Sacramento's cold vulnerability index is %f\" % city_aggr_vul[\"aggr_cold_kpis\"][idx_sacramento])\n\nprint(\"Sacramento's heat vulnerability index is %f\" % city_aggr_vul[\"aggr_heat_kpis\"][idx_sacramento])\n\nprint(\"Sacramento's flood vulnerability index is %f\" % city_aggr_vul[\"aggr_flood_kpis\"][idx_sacramento])\n\nprint(\"Sacramento's drought vulnerability index is %f\" % city_aggr_vul[\"aggr_drought_kpis\"][idx_sacramento])\n\nprint(\"Sacramento's severe weather vulnerability index is %f\" % city_aggr_vul[\"aggr_severe_kpis\"][idx_sacramento])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/derivedkpis/KPIs/vulnerability_cities_us_drought_kpis_raw.csv\")\n\nfields_to_plot = [\"water_loss\", \"drought_severity_index\", \"drought_severity_index_variability\",\n                 \"fema_drought_fire_events\", \"drought_expected_annual_loss\", \"wildfire_expected_annual_loss\"]\nlabels = [\"Freshwater Loss\", \"Drought Severity Index\", \"Drought Severity Index Variability\", \"FEMA Drought/Fire Disasters\",\n         \"Drought EALS\", \"Wildfire EALS\"]\n\n\nthree_by_two_plot(fields_to_plot, labels, idx_city=idx_sacramento, city=\"Sacramento\", color=\"#a15f3b\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/derivedkpis/KPIs/vulnerability_cities_us_heat_kpis_raw.csv\")\n\nfields_to_plot = [\"heat_maximum_temp\", \"heat_maximum_stress\", \"heat_maximum_temp_std\",\n                 \"heat_maximum_stress_std\", \"LOC_construction\", \"LOC_protective\"]\nlabels = [\"Heat Wave Days (Temp)\", \"Heat Wave Days (Stress)\", \"Heat Wave Days (Temp) Variability\", \"Heat Wave Days (Stress) Variability\",\n         \"Overrepresentation of Construction \\nWorkers\", \"Overrepresentation of \\nProtective Workers\"]\n\n\nthree_by_two_plot(fields_to_plot, labels, idx_city=idx_sacramento, city=\"Sacramento\", color=\"indianred\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/derivedkpis/KPIs/adapt_cities_us_general_kpis_raw.csv\")\n\nfields_to_plot = [\"social_resilience\", \"comm_resilience\", \"delta_social_resilience\",\n                 \"delta_comm_resilience\", \"opportunity_city_sentiment\", \"collab_city_sentiment\"]\nlabels = [\"Social Resilience\", \"Community Resilience\", \"$\\Delta$ Social Resilience \\n (5 Years)\", \"$\\Delta$ Community Resilience \\n (5 Years)\",\n         \"Opportunity Sentiment\", \"Collaboration Sentiment\"]\n\n\nthree_by_two_plot(fields_to_plot, labels, idx_city=idx_sacramento, city=\"Sacramento\", color=\"dodgerblue\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"import pickle\nwith open('../input/derivedkpis/KPIs/city_corp_collab.pickle', 'rb') as handle:\n    city_corp = pickle.load(handle)\n    \ncity_corp_collab = city_corp[0]\ncity_corp_distances = city_corp[1]\n\ncity_corp_opp = city_corp[2]\ncity_corp_opp_distances = city_corp[3]\n\ncity_corp_risk = city_corp[3]\ncity_corp_risk_distances = city_corp[4]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"def standardize_rank(arr, direction=1):\n    rank = (stats.mstats.rankdata(np.ma.masked_invalid(arr)))\n    rank[rank == 0] = np.nan\n    if(direction == 1):\n        return rank/(np.nanmax(rank)) \n    else:\n        return 1-(rank/(np.nanmax(rank)))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"corp_accts_collabs = list(city_corp_collab[acct_number_cities_2020[idx_sacramento]].keys())\ncollab_shared_sentiment = list(city_corp_collab[acct_number_cities_2020[idx_sacramento]].values())\ncollab_shared_sentiment_rank = standardize_rank(collab_shared_sentiment)\n\ncorp_accts_opportunity = list(city_corp_opp[acct_number_cities_2020[idx_sacramento]].keys())\nopportunity_shared_sentiment = list(city_corp_opp[acct_number_cities_2020[idx_sacramento]].values())\nopportunity_shared_sentiment_rank = standardize_rank(opportunity_shared_sentiment)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"corporations_2020 = pd.read_csv(\"../input/externald/Supplementary Data/corporations_2020_climate_change_geo.csv\")\nacct_org = dict(zip(np.array(corporations_2020[\"account_number\"]), np.array(corporations_2020[\"organization\"])))\n\nall_corp_locations = np.c_[np.array(corporations_2020[\"lat\"]), np.array(corporations_2020[\"lng\"])]\nall_corp_accounts = np.array(corporations_2020[\"account_number\"])\nunique_corp_accounts, unique_corp_idxs = np.unique(all_corp_accounts, axis=0, return_index=True)\n\ncorp_locs_lat = all_corp_locations[:,0][unique_corp_idxs]\ncorp_locs_lng = all_corp_locations[:,1][unique_corp_idxs]\ncorp_names = np.array(corporations_2020[\"organization\"].iloc[unique_corp_idxs])\ncorp_cities = np.array(corporations_2020[\"city\"].iloc[unique_corp_idxs])\ncorp_states = np.array(corporations_2020[\"state\"].iloc[unique_corp_idxs])\ncorp_countries = np.array(corporations_2020[\"country\"].iloc[unique_corp_idxs])\n\ncorp_locs = np.c_[corp_locs_lat, corp_locs_lng]\ncorp_locs = np.nan_to_num(corp_locs, nan=-999)\n\ncorp_kpis = pd.read_csv(\"../input/derivedkpis/KPIs/corporate_kpis_general.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"acct_sentiment_risk = dict(zip(corp_kpis[\"account_number\"], corp_kpis[\"sentiment_risk\"]))\nacct_sentiment_opp = dict(zip(corp_kpis[\"account_number\"], corp_kpis[\"sentiment_opportunity\"]))\nacct_sentiment_collab = dict(zip(corp_kpis[\"account_number\"], corp_kpis[\"sentiment_collaboration\"]))\n\nsentiment_risk_sacramento = standardize_rank([acct_sentiment_risk[corp_accts_collabs[i]] for i in range(len(corp_accts_collabs))])\nsentiment_opportunity_sacramento = standardize_rank([acct_sentiment_opp[corp_accts_collabs[i]] for i in range(len(corp_accts_collabs))])\nsentiment_collaboration_sacramento = standardize_rank([acct_sentiment_collab[corp_accts_collabs[i]] for i in range(len(corp_accts_collabs))])\n\norgs_to_consider_collabs = [acct_org[corp_accts_collabs[i]] for i in range(len(corp_accts_collabs))]\norgs_to_consider_opps = [acct_org[corp_accts_opportunity[i]] for i in range(len(corp_accts_opportunity))]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"hovertext = \"Organization: \" + pd.Series(orgs_to_consider_opps) + \"<br>\" + \"Risk Sentiment: \" + pd.Series(sentiment_risk_sacramento.astype(str)).str[:4] + \"<br>\" + \"Opportunity Sentiment: \" + pd.Series(sentiment_opportunity_sacramento.astype(str)).str[:4] + \"<br>\" + \"Collaboration Sentiment: \" + pd.Series(sentiment_collaboration_sacramento.astype(str)).str[:4]\nscatter(opportunity_shared_sentiment_rank, collab_shared_sentiment_rank, xlabel=\"Alignment of Opportunities\", ylabel=\"Alignment of Collaboration Possibilities\", \n       color=list(city_corp_distances[acct_sacramento].values()), title=\"Corporations Within 150 Miles of Sacramento, CA\",\n       cbar_title=\"Distance\", hovertext=hovertext, cmap=\"inferno_r\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"scatter(opportunity_shared_sentiment_rank, collab_shared_sentiment_rank, xlabel=\"Alignment of Opportunities\", ylabel=\"Alignment of Collaboration Possibilities\", \n       color=np.nan_to_num(sentiment_collaboration_sacramento), title=\"Corporations Within 150 Miles of Sacramento, CA\",\n       cbar_title=\"Collaboration Sentiment\", hovertext=hovertext, cmap=\"bluered_r\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_somerville = 88\nacct_somerville = acct_number_cities_2020[idx_somerville]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Somerville, MA"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"aggr_socio_economic_kpis = city_aggr_equity[\"aggr_socio_economic_kpis\"]\nprint(\"Somerville's socioecnomic inequity index is %f\" % aggr_socio_economic_kpis[idx_somerville])\n\naggr_health_kpis = city_aggr_equity[\"aggr_health_kpis\"]\nprint(\"Somerville's health inequity index is %f\" % aggr_health_kpis[idx_somerville])\n\naggr_housing_kpis = city_aggr_equity[\"aggr_housing_kpis\"]\nprint(\"Somerville's housing inequity index is %f\" % aggr_housing_kpis[idx_somerville])\n\naggr_all_kpis = city_aggr_equity[\"aggr_all_inequity\"]\nprint(\"Somerville's overall inequity index is %f\" % aggr_all_kpis[idx_somerville])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/derivedkpis/KPIs/equity_cities_us_kpis_raw.csv\")\nfields_to_plot = [\"house_composition_disability\", \"house_type_transportation\", \"segregation_black_white\",\n                 \"segregation_nonwhite_white\", \"housing_cost_burden\", \"sovi_risk_index\"]\nlabels = [\"House Composition / Disability Index\", \"Housing Type / Transportation Index\", \"Segregation (Black/White)\",\n         \"Segregation (Non-white/white)\", \"Housing Cost Burden\", \"Social Vulnerability Risk Index\"]\n\nthree_by_two_plot(fields_to_plot, labels, idx_city=idx_somerville, city=\"Somerville\", color=\"plum\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"print(\"Somerville's overall vulnerability index is %f\" % city_aggr_vul[\"aggr_vul_all_kpis\"][idx_somerville])\n\nprint(\"Somerville's cold vulnerability index is %f\" % city_aggr_vul[\"aggr_cold_kpis\"][idx_somerville])\n\nprint(\"Somerville's heat vulnerability index is %f\" % city_aggr_vul[\"aggr_heat_kpis\"][idx_somerville])\n\nprint(\"Somerville's flood vulnerability index is %f\" % city_aggr_vul[\"aggr_flood_kpis\"][idx_somerville])\n\nprint(\"Somerville's drought vulnerability index is %f\" % city_aggr_vul[\"aggr_drought_kpis\"][idx_somerville])\n\nprint(\"Somerville's severe weather vulnerability index is %f\" % city_aggr_vul[\"aggr_severe_kpis\"][idx_somerville])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/derivedkpis/KPIs/vulnerability_cities_us_cold_kpis_raw.csv\")\n\nfields_to_plot = [\"tmin_averaged\", \"tmin_std\",\n                 \"cold_wave_expected_annual_loss\", \"hail_risk_expected_annual_loss\", \"winter_weather_expected_annual_loss\", \"icestorm_expected_annual_loss\"]\nlabels = [\"Averaged Minimum Temperature\", \"Variability in Averaged Minimum Temperature\", \"Cold Wave Expected Annual Loss\",\n         \"Hail Storm Expected Annual Loss\", \"Winter Weather Expected Annual Loss\", \"Ice-Storm Expected Annual Loss\"]\n\nthree_by_two_plot(fields_to_plot, labels, idx_city=idx_somerville, city=\"Somerville\", color=\"darkblue\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/derivedkpis/KPIs/vulnerability_cities_us_severe_storms_kpis_raw.csv\")\n\nfields_to_plot = [\"precip_max_averaged\", \"precip_max_std\",\n                 \"percent_built_before_1999\", \"strong_wind_expected_annual_loss\", \"tornado_expected_annual_loss\", \"earthquake_expected_annual_loss\"]\nlabels = [\"Max Precipitation\", \"Variability in Precipitation\", \"Percent of Buildings Built Before 1999\",\n         \"Strong Wind Expected Annual Loss\", \"Tornado Expected Annual Loss\", \"Earthquake Expected Annual Loss\"]\n\nthree_by_two_plot(fields_to_plot, labels, idx_city=idx_somerville, city=\"Somerville\", color=\"darkorange\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/derivedkpis/KPIs/adapt_cities_us_general_kpis_raw.csv\")\n\nfields_to_plot = [\"social_resilience\", \"comm_resilience\",\n                 \"delta_infra_resilience\", \"delta_tot_resilience\", \"opportunity_city_sentiment\", \"collab_city_sentiment\"]\nlabels = [\"Social Resilience\", \"Community Resilience\", r\"$\\Delta$ Infrastructure Resilience\",\n         r\"$\\Delta$ Total Resilience\", \"Opportunity Sentiment\", \"Collaboration Sentiment\"]\n\nthree_by_two_plot(fields_to_plot, labels, idx_city=idx_somerville, city=\"Somerville\", color=\"forestgreen\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"corp_accts_collabs = list(city_corp_collab[acct_number_cities_2020[idx_somerville]].keys())\ncollab_shared_sentiment = list(city_corp_collab[acct_number_cities_2020[idx_somerville]].values())\ncollab_shared_sentiment_rank = standardize_rank(collab_shared_sentiment)\n\ncorp_accts_opportunity = list(city_corp_opp[acct_number_cities_2020[idx_somerville]].keys())\nopportunity_shared_sentiment = list(city_corp_opp[acct_number_cities_2020[idx_somerville]].values())\nopportunity_shared_sentiment_rank = standardize_rank(opportunity_shared_sentiment)\n\nacct_sentiment_risk = dict(zip(corp_kpis[\"account_number\"], corp_kpis[\"sentiment_risk\"]))\nacct_sentiment_opp = dict(zip(corp_kpis[\"account_number\"], corp_kpis[\"sentiment_opportunity\"]))\nacct_sentiment_collab = dict(zip(corp_kpis[\"account_number\"], corp_kpis[\"sentiment_collaboration\"]))\n\nsentiment_risk_sacramento = standardize_rank([acct_sentiment_risk[corp_accts_collabs[i]] for i in range(len(corp_accts_collabs))])\nsentiment_opportunity_sacramento = standardize_rank([acct_sentiment_opp[corp_accts_collabs[i]] for i in range(len(corp_accts_collabs))])\nsentiment_collaboration_sacramento = standardize_rank([acct_sentiment_collab[corp_accts_collabs[i]] for i in range(len(corp_accts_collabs))])\n\norgs_to_consider_collabs = [acct_org[corp_accts_collabs[i]] for i in range(len(corp_accts_collabs))]\norgs_to_consider_opps = [acct_org[corp_accts_opportunity[i]] for i in range(len(corp_accts_opportunity))]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"hovertext = \"Organization: \" + pd.Series(orgs_to_consider_opps) + \"<br>\" + \"Risk Sentiment: \" + pd.Series(sentiment_risk_sacramento.astype(str)).str[:4] + \"<br>\" + \"Opportunity Sentiment: \" + pd.Series(sentiment_opportunity_sacramento.astype(str)).str[:4] + \"<br>\" + \"Collaboration Sentiment: \" + pd.Series(sentiment_collaboration_sacramento.astype(str)).str[:4]\nscatter(opportunity_shared_sentiment_rank, collab_shared_sentiment_rank, xlabel=\"Alignment of Opportunities\", ylabel=\"Alignment of Collaboration Possibilities\", \n       color=list(city_corp_distances[acct_somerville].values()), title=\"Corporations Within 150 Miles of Somerville, MA\",\n       cbar_title=\"Distance\", hovertext=hovertext, cmap=\"inferno_r\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"scatter(opportunity_shared_sentiment_rank, collab_shared_sentiment_rank, xlabel=\"Alignment of Opportunities\", ylabel=\"Alignment of Collaboration Possibilities\", \n       color=np.nan_to_num(sentiment_collaboration_sacramento), title=\"Corporations Within 150 Miles of Somerville, MA\",\n       cbar_title=\"Collaboration Sentiment\", hovertext=hovertext, cmap=\"bluered_r\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Philadelphia, PA"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"idx_philly = 42\nacct_philly = acct_number_cities_2020[idx_philly]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/derivedkpis/KPIs/equity_cities_us_kpis_raw.csv\")\n\nfields_to_plot = [\"food_insecurity\", \"healthy_food_access\", \"premature_death_rate\",\n                 \"poor_health\", \"percent_uninsured\", \"exercise_access\"]\nlabels = [\"Food Insecurity\", \"Healthy Food Access\", \"Premature Death Rate\",\n         \"Poor Health\", \"Percent Uninsured\", \"Exercise Access\"]\n\nthree_by_two_plot(fields_to_plot, labels, idx_city=idx_philly, city=\"Philadelphia\", color=\"crimson\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/derivedkpis/KPIs/vulnerability_cities_us_floods_kpis_raw.csv\")\n\nfields_to_plot = [\"fema_flood_events\", \"fema_coastal_storms\", \"percent_built_before_1999\",\n                 \"coastal_flood_expected_annual_loss\", \"river_flood_expected_annual_loss\", \"landslide_expected_annual_loss\"]\nlabels = [\"FEMA Flood Events\", \"FEMA Coastal Storm Events\", \"Percent Buildings Built Before 1999\",\n         \"Coastal Flood EALS\", \"River Flood EALS\", \"Landslide EALS\"]\n\nthree_by_two_plot(fields_to_plot, labels, idx_city=idx_philly, city=\"Philadelphia\", color=\"royalblue\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/derivedkpis/KPIs/adapt_cities_us_general_kpis_raw.csv\")\n\nfields_to_plot = [\"social_resilience\", \"comm_resilience\",\n                 \"delta_infra_resilience\", \"delta_tot_resilience\", \"opportunity_city_sentiment\", \"collab_city_sentiment\"]\nlabels = [\"Social Resilience\", \"Community Resilience\", r\"$\\Delta$ Infrastructure Resilience\",\n         r\"$\\Delta$ Total Resilience\", \"Opportunity Sentiment\", \"Collaboration Sentiment\"]\n\n\nthree_by_two_plot(fields_to_plot, labels, idx_city=idx_philly, city=\"Philadelphia\", color=\"olive\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"corp_accts_collabs = list(city_corp_collab[acct_number_cities_2020[idx_philly]].keys())\ncollab_shared_sentiment = list(city_corp_collab[acct_number_cities_2020[idx_philly]].values())\ncollab_shared_sentiment_rank = standardize_rank(collab_shared_sentiment)\n\ncorp_accts_opportunity = list(city_corp_opp[acct_number_cities_2020[idx_philly]].keys())\nopportunity_shared_sentiment = list(city_corp_opp[acct_number_cities_2020[idx_philly]].values())\nopportunity_shared_sentiment_rank = standardize_rank(opportunity_shared_sentiment)\n\nacct_sentiment_risk = dict(zip(corp_kpis[\"account_number\"], corp_kpis[\"sentiment_risk\"]))\nacct_sentiment_opp = dict(zip(corp_kpis[\"account_number\"], corp_kpis[\"sentiment_opportunity\"]))\nacct_sentiment_collab = dict(zip(corp_kpis[\"account_number\"], corp_kpis[\"sentiment_collaboration\"]))\n\nsentiment_risk_sacramento = standardize_rank([acct_sentiment_risk[corp_accts_collabs[i]] for i in range(len(corp_accts_collabs))])\nsentiment_opportunity_sacramento = standardize_rank([acct_sentiment_opp[corp_accts_collabs[i]] for i in range(len(corp_accts_collabs))])\nsentiment_collaboration_sacramento = standardize_rank([acct_sentiment_collab[corp_accts_collabs[i]] for i in range(len(corp_accts_collabs))])\n\norgs_to_consider_collabs = [acct_org[corp_accts_collabs[i]] for i in range(len(corp_accts_collabs))]\norgs_to_consider_opps = [acct_org[corp_accts_opportunity[i]] for i in range(len(corp_accts_opportunity))]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"hovertext = \"Organization: \" + pd.Series(orgs_to_consider_opps) + \"<br>\" + \"Risk Sentiment: \" + pd.Series(sentiment_risk_sacramento.astype(str)).str[:4] + \"<br>\" + \"Opportunity Sentiment: \" + pd.Series(sentiment_opportunity_sacramento.astype(str)).str[:4] + \"<br>\" + \"Collaboration Sentiment: \" + pd.Series(sentiment_collaboration_sacramento.astype(str)).str[:4]\nscatter(opportunity_shared_sentiment_rank, collab_shared_sentiment_rank, xlabel=\"Alignment of Opportunities\", ylabel=\"Alignment of Collaboration Possibilities\", \n       color=list(city_corp_distances[acct_philly].values()), title=\"Corporations Within 150 Miles of Philadephia, PA\",\n       cbar_title=\"Distance\", hovertext=hovertext, cmap=\"inferno_r\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"scatter(opportunity_shared_sentiment_rank, collab_shared_sentiment_rank, xlabel=\"Alignment of Opportunities\", ylabel=\"Alignment of Collaboration Possibilities\", \n       color=np.nan_to_num(sentiment_collaboration_sacramento), title=\"Corporations Within 150 Miles of Philadelphia, PA\",\n       cbar_title=\"Collaboration Sentiment\", hovertext=hovertext, cmap=\"bluered_r\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Future \n\n## Key insights\n\n* Inequity increases(!) as resiliency increases for cities. This is of great concern because resiliency should not be coming at the cost of equity! \n* Cities with high inequity seem to be more vulnerable to climate hazards than cities with low inequity. \n* Adaptability seems to largely stay constant regardless of vulnerability. I naively expected there to be some correlation. \n* Deeply studying cities' inequities and vulnerabilities\n* New way to connect close-by cities and corporations using similarity and sentiment scores. \n\nThe focus of this kernel has largely been on visualizing the KPIs that I developed. These simple visualizations already show the power of aggregating different indicators and building an index to study the intersection of inequity, vulnerability, and resilience. We also show how different corporations can be matched to cities with different challenges using sentiment analysis and text similarity. Specific details about the KPIs are in the notebooks below. In the future, I would want to work on smarter ways to weight these different indicators. "},{"metadata":{},"cell_type":"markdown","source":"Links to all the notebooks where the KPIs are calculated:\n\nCorporation cleaning: https://www.kaggle.com/haakakak/reduce-and-add-geo-corps\n\nCity cleaning: https://www.kaggle.com/haakakak/reduce-and-add-geo-cities\n\nOverall vulnerability : https://www.kaggle.com/haakakak/vulnerability-overall\n\nVulnerability due to cold/winter weather: https://www.kaggle.com/haakakak/vulnerability-cold\n\nVulnerability due to heat: https://www.kaggle.com/haakakak/vulnerability-heat\n\nVulnerability due to floods: https://www.kaggle.com/haakakak/vulnerability-floods\n\nVulnerability due to droughts: https://www.kaggle.com/haakakak/vulnerability-drought\n\nVulnerability due to severe storms: https://www.kaggle.com/haakakak/vulnerability-severe\n\nAdaptability: https://www.kaggle.com/haakakak/adaptability\n\nSocial equity: https://www.kaggle.com/haakakak/equity\n\nCorporation sentiments: https://www.kaggle.com/haakakak/corp-sentiments\n\nCity-corporation collaborations: https://www.kaggle.com/haakakak/city-corp-collaboration\n\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}