j40-cejst-2/score/ipython/calenviroscreen_etl.ipynb
Lucas Merrill Brown 11d13e034e
Comparison tool refactor & ETL HUD RECAP (#272)
* Refactoring comparison tool and creating two new ETL notebooks
2021-07-06 12:10:58 -05:00

141 lines
4.2 KiB
Text

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "20aa3891",
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import numpy as np\n",
"import pandas as pd\n",
"import csv\n",
"import sys\n",
"import os\n",
"\n",
"module_path = os.path.abspath(os.path.join(\"..\"))\n",
"if module_path not in sys.path:\n",
" sys.path.append(module_path)\n",
"\n",
"from etl.sources.census.etl_utils import get_state_fips_codes\n",
"from utils import unzip_file_from_url, remove_all_from_dir\n",
"\n",
"DATA_PATH = Path.cwd().parent / \"data\"\n",
"TMP_PATH = DATA_PATH / \"tmp\"\n",
"CALENVIROSCREEN_FTP_URL = \"https://justice40-data.s3.amazonaws.com/CalEnviroScreen/CalEnviroScreen_4.0_2021.zip\"\n",
"CSV_PATH = DATA_PATH / \"dataset\" / \"calenviroscreen4\"\n",
"\n",
"# Definining some variable names\n",
"CALENVIROSCREEN_SCORE_FIELD_NAME = \"calenviroscreen_score\"\n",
"CALENVIROSCREEN_PERCENTILE_FIELD_NAME = \"calenviroscreen_percentile\"\n",
"CALENVIROSCREEN_PRIORITY_COMMUNITY_FIELD_NAME = \"calenviroscreen_priority_community\"\n",
"GEOID_TRACT_FIELD_NAME = \"GEOID10_TRACT\"\n",
"\n",
"# Choosing constants.\n",
"# None of these numbers are final, but just for the purposes of comparison.\n",
"CALENVIROSCREEN_PRIORITY_COMMUNITY_THRESHOLD = 75\n",
"\n",
"print(DATA_PATH)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc3fb9ec",
"metadata": {},
"outputs": [],
"source": [
"# download file from ejscreen ftp\n",
"unzip_file_from_url(CALENVIROSCREEN_FTP_URL, TMP_PATH, TMP_PATH)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15f66756",
"metadata": {},
"outputs": [],
"source": [
"# Data from https://calenviroscreen-oehha.hub.arcgis.com/#Data, specifically:\n",
"# https://oehha.ca.gov/media/downloads/calenviroscreen/document/calenviroscreen40resultsdatadictionaryd12021.zip\n",
"calenviroscreen_4_csv_name = \"CalEnviroScreen_4.0_2021.csv\"\n",
"calenviroscreen_data_path = TMP_PATH.joinpath(calenviroscreen_4_csv_name)\n",
"\n",
"# Load comparison index (CalEnviroScreen 4)\n",
"calenviroscreen_df = pd.read_csv(\n",
" calenviroscreen_data_path, dtype={\"Census Tract\": \"string\"}\n",
")\n",
"\n",
"calenviroscreen_df.rename(\n",
" columns={\n",
" \"Census Tract\": GEOID_TRACT_FIELD_NAME,\n",
" \"DRAFT CES 4.0 Score\": CALENVIROSCREEN_SCORE_FIELD_NAME,\n",
" \"DRAFT CES 4.0 Percentile\": CALENVIROSCREEN_PERCENTILE_FIELD_NAME,\n",
" },\n",
" inplace=True,\n",
")\n",
"\n",
"# Add a leading \"0\" to the Census Tract to match our format in other data frames.\n",
"\n",
"calenviroscreen_df[GEOID_TRACT_FIELD_NAME] = (\n",
" \"0\" + calenviroscreen_df[GEOID_TRACT_FIELD_NAME]\n",
")\n",
"\n",
"# Calculate the top K% of prioritized communities\n",
"calenviroscreen_df[CALENVIROSCREEN_PRIORITY_COMMUNITY_FIELD_NAME] = (\n",
" calenviroscreen_df[CALENVIROSCREEN_PERCENTILE_FIELD_NAME]\n",
" >= CALENVIROSCREEN_PRIORITY_COMMUNITY_THRESHOLD\n",
")\n",
"\n",
"calenviroscreen_df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9fa2077a",
"metadata": {},
"outputs": [],
"source": [
"# write csv\n",
"CSV_PATH.mkdir(parents=True, exist_ok=True)\n",
"\n",
"# Matching other conventions in the ETL scripts, write only for the state (FIPS code 06).\n",
"calenviroscreen_df.to_csv(CSV_PATH / \"data06.csv\", index=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "81b977f8",
"metadata": {},
"outputs": [],
"source": [
"# cleanup\n",
"remove_all_from_dir(TMP_PATH)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}