Scores D & E (#266)

* running black throughout

* adding housing

* hud housing etl working

* got score d and e working

* updating scoring comparison

* minor fixes

* small changes

* small comments
This commit is contained in:
Lucas Merrill Brown 2021-06-29 08:20:23 -07:00 committed by GitHub
commit 41e394972c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 625 additions and 74 deletions

View file

@ -15,7 +15,12 @@
"import pandas as pd\n",
"from pathlib import Path\n",
"import requests\n",
"import zipfile"
"import zipfile\n",
"from datetime import datetime\n",
"from tqdm.notebook import tqdm_notebook\n",
"\n",
"# Turn on TQDM for pandas so that we can have progress bars when running `apply`.\n",
"tqdm_notebook.pandas()"
]
},
{
@ -50,8 +55,11 @@
"CEJST_PRIORITY_COMMUNITY_FIELD = \"cejst_priority_community\"\n",
"\n",
"# Comparison field names\n",
"any_tract_has_at_least_one_cbg = \"Tract has at least one CEJST CBG?\"\n",
"tract_has_at_least_one_cbg = \"CES Tract has at least one CEJST CBG?\"\n",
"tract_has_100_percent_cbg = \"CES Tract has 100% CEJST CBGs?\""
"tract_has_100_percent_cbg = \"CES Tract has 100% CEJST CBGs?\"\n",
"non_ces_tract_has_at_least_one_cbg = \"Non-CES Tract has at least one CEJST CBG?\"\n",
"non_ces_tract_has_100_percent_cbg = \"Non-CES Tract has 100% CEJST CBGs?\""
]
},
{
@ -69,12 +77,15 @@
"cejst_df.head()\n",
"\n",
"# Rename unclear name \"id\" to \"census_block_group_id\", as well as other renamings.\n",
"\n",
"score_used = \"Score A\"\n",
"\n",
"cejst_df.rename(\n",
" columns={\n",
" \"GEOID10\": CENSUS_BLOCK_GROUP_ID_FIELD,\n",
" \"Total population\": CENSUS_BLOCK_GROUP_POPULATION_FIELD,\n",
" \"Score C\": CEJST_SCORE_FIELD,\n",
" \"Score C (percentile)\": CEJST_PERCENTILE_FIELD,\n",
" score_used: CEJST_SCORE_FIELD,\n",
" f\"{score_used} (percentile)\": CEJST_PERCENTILE_FIELD,\n",
" },\n",
" inplace=True,\n",
" errors=\"raise\",\n",
@ -239,18 +250,42 @@
" # Recall that NaN values are not falsy, so we need to check if `is_a_ces_priority_tract` is True.\n",
" is_a_ces_priority_tract = is_a_ces_priority_tract is True\n",
"\n",
" # Calculate whether the tract (whether or not it is a CES priority tract) includes CBGs that are priority\n",
" # according to the current CEJST score.\n",
" df[any_tract_has_at_least_one_cbg] = (\n",
" frame.loc[:, CEJST_PRIORITY_COMMUNITY_FIELD].sum() > 0\n",
" )\n",
"\n",
" # Calculate comparison\n",
" # A CES priority tract has at least one CEJST priority CBG.\n",
" df[tract_has_at_least_one_cbg] = (\n",
" frame.loc[:, CEJST_PRIORITY_COMMUNITY_FIELD].sum() > 0\n",
" if is_a_ces_priority_tract\n",
" else None\n",
" )\n",
"\n",
" # A CES priority tract has all of its contained CBGs as CEJST priority CBGs.\n",
" df[tract_has_100_percent_cbg] = (\n",
" frame.loc[:, CEJST_PRIORITY_COMMUNITY_FIELD].mean() == 1\n",
" if is_a_ces_priority_tract\n",
" else None\n",
" )\n",
"\n",
" # Calculate the inverse\n",
" # A tract that is _not_ a CES priority has at least one CEJST priority CBG.\n",
" df[non_ces_tract_has_at_least_one_cbg] = (\n",
" frame.loc[:, CEJST_PRIORITY_COMMUNITY_FIELD].sum() > 0\n",
" if not is_a_ces_priority_tract\n",
" else None\n",
" )\n",
"\n",
" # A tract that is _not_ a CES priority has all of its contained CBGs as CEJST priority CBGs.\n",
" df[non_ces_tract_has_100_percent_cbg] = (\n",
" frame.loc[:, CEJST_PRIORITY_COMMUNITY_FIELD].mean() == 1\n",
" if not is_a_ces_priority_tract\n",
" else None\n",
" )\n",
"\n",
" return df\n",
"\n",
"\n",
@ -258,7 +293,7 @@
"grouped_df = merged_df.groupby(CENSUS_TRACT_ID_FIELD)\n",
"\n",
"# Run the comparison function on the groups.\n",
"comparison_df = grouped_df.apply(calculate_comparison)\n",
"comparison_df = grouped_df.progress_apply(calculate_comparison)\n",
"\n",
"# Sort descending by highest CES Score for convenience when viewing output file\n",
"comparison_df.sort_values(\n",
@ -283,15 +318,37 @@
"outputs": [],
"source": [
"# Prepare some constants for use in the following Markdown cell.\n",
"\n",
"total_cbgs_ca_only = len(cejst_df)\n",
"cejst_cbgs_ca_only = cejst_df.loc[:, CEJST_PRIORITY_COMMUNITY_FIELD].sum()\n",
"cejst_cbgs_ca_only_percent = f\"{cejst_cbgs_ca_only / total_cbgs_ca_only:.0%}\"\n",
"\n",
"total_tracts_count = len(comparison_df)\n",
"ces_tracts_count = comparison_df.loc[:, CALENVIROSCREEN_PRIORITY_COMMUNITY_FIELD].sum()\n",
"ces_tracts_count_percent = f\"{ces_tracts_count / total_tracts_count:.0%}\"\n",
"non_ces_tracts_count = total_tracts_count - ces_tracts_count\n",
"\n",
"total_tracts_count = len(comparison_df[CENSUS_TRACT_ID_FIELD])\n",
"cejst_tracts_count = comparison_df.loc[:, any_tract_has_at_least_one_cbg].sum()\n",
"cejst_tracts_count_percent = f\"{cejst_tracts_count / total_tracts_count:.0%}\"\n",
"\n",
"# CES stats\n",
"at_least_one_sum = comparison_df.loc[:, tract_has_at_least_one_cbg].sum()\n",
"at_least_one_sum_percent = f\"{at_least_one_sum / ces_tracts_count:.0%}\"\n",
"\n",
"all_100_sum = comparison_df.loc[:, tract_has_100_percent_cbg].sum()\n",
"all_100_sum_percent = f\"{all_100_sum / ces_tracts_count:.0%}\"\n",
"\n",
"# Non-CES stats:\n",
"non_ces_at_least_one_sum = comparison_df.loc[\n",
" :, non_ces_tract_has_at_least_one_cbg\n",
"].sum()\n",
"non_ces_at_least_one_sum_percent = (\n",
" f\"{non_ces_at_least_one_sum / non_ces_tracts_count:.0%}\"\n",
")\n",
"\n",
"non_ces_all_100_sum = comparison_df.loc[:, non_ces_tract_has_100_percent_cbg].sum()\n",
"non_ces_all_100_sum_percent = f\"{non_ces_all_100_sum / non_ces_tracts_count:.0%}\"\n",
"\n",
"# Note, for the following Markdown cell to render the variables properly, follow the steps at\n",
"# \"Activating variable-enabled Markdown for Jupyter notebooks\" within `score/README.md`."
]
@ -301,26 +358,44 @@
"id": "0c534966",
"metadata": {
"variables": {
"all_100_sum": "1373",
"all_100_sum_percent": "69%",
"at_least_one_sum": "1866",
"at_least_one_sum_percent": "94%",
"cejst_cbgs_ca_only": "10849",
"ces_tracts_count": "1983"
" total_tracts_count": "8057",
"all_100_sum": "1168",
"all_100_sum_percent": "59%",
"at_least_one_sum": "1817",
"at_least_one_sum_percent": "92%",
"cejst_cbgs_ca_only": "6987",
"cejst_cbgs_ca_only_percent": "30%",
"cejst_tracts_count": "3516",
"cejst_tracts_count_percent": "44%",
"ces_tracts_count": "1983",
"ces_tracts_count_percent": "25%",
"datetime.today().strftime('%Y-%m-%d')": "2021-06-28",
"non_ces_all_100_sum": "438",
"non_ces_all_100_sum_percent": "7%",
"non_ces_at_least_one_sum": "1699",
"non_ces_at_least_one_sum_percent": "28%",
"score_used": "Score A",
"total_cbgs_ca_only": "23212"
}
},
"source": [
"# Summary of findings\n",
"# Summary of findings for {{score_used}}\n",
"\n",
"(Calculated on {{datetime.today().strftime('%Y-%m-%d')}})\n",
"\n",
"Recall that census tracts contain one or more census block groups, with up to nine census block groups per tract.\n",
"\n",
"There are {{ces_tracts_count}} census tracts designated as Disadvantaged Communities by CalEnviroScreen 4.0. \n",
"There are {{ces_tracts_count}} census tracts designated as Disadvantaged Communities by CalEnviroScreen 4.0, out of {{total_tracts_count}} total tracts ({{ces_tracts_count_percent}}). \n",
"\n",
"Within California, there are {{cejst_cbgs_ca_only}} census block groups considered as priority communities by the current version of the CEJST score used in this analysis.\n",
"Within California, there are {{cejst_cbgs_ca_only}} census block groups considered as priority communities by the current version of the CEJST score used in this analysis, out of {{total_cbgs_ca_only}} CBGs in the state ({{cejst_cbgs_ca_only_percent}}). They occupy {{cejst_tracts_count}} ({{cejst_tracts_count_percent}}) of all the census tracts in California.\n",
"\n",
"Out of every CalEnviroScreen Disadvantaged Community census tract, {{at_least_one_sum}} ({{at_least_one_sum_percent}}) of these census tracts have at least one census block group within them that is considered a priority community by the current version of the CEJST score.\n",
"\n",
"Out of every CalEnviroScreen Disadvantaged Community census tract, {{all_100_sum}} ({{all_100_sum_percent}}) of these census tracts have 100% of the included census block groups within them considered priority communities by the current version of the CEJST score."
"Out of every CalEnviroScreen Disadvantaged Community census tract, {{all_100_sum}} ({{all_100_sum_percent}}) of these census tracts have 100% of the included census block groups within them considered priority communities by the current version of the CEJST score.\n",
"\n",
"Out of every census tract in California that is __not__ marked as a CalEnviroScreen Disadvantaged Community, {{non_ces_at_least_one_sum}} ({{non_ces_at_least_one_sum_percent}}) of these census tracts have at least one census block group within them that is considered a priority community by the current version of the CEJST score.\n",
"\n",
"Out of every census tract in California that is __not__ marked as a CalEnviroScreen Disadvantaged Community, {{non_ces_all_100_sum}} ({{non_ces_all_100_sum_percent}}) of these census tracts have 100% of the included census block groups within them considered priority communities by the current version of the CEJST score."
]
}
],