mirror of
https://github.com/DOI-DO/j40-cejst-2.git
synced 2025-02-24 18:44:20 -08:00
* first commit * checkpoint * checkpoint * first extract module 🎉 * completed census acs etl class * completed ejscreen etl * completed etl * score generation ready * improving census load and separation * score generation working 🎉 * completed etls * new score generation * PR reviews * run specific etl; starting docstrings * docstrings work * more docstrings * completed docstrings * adding pyenv version * more reasonable poetry req for python * PR comments
63 lines
2.4 KiB
Python
63 lines
2.4 KiB
Python
import pandas as pd
|
|
import requests
|
|
|
|
from etl.base import ExtractTransformLoad
|
|
from utils import get_module_logger
|
|
|
|
logger = get_module_logger(__name__)
|
|
|
|
|
|
class HudRecapETL(ExtractTransformLoad):
|
|
def __init__(self):
|
|
self.HUD_RECAP_CSV_URL = "https://opendata.arcgis.com/api/v3/datasets/56de4edea8264fe5a344da9811ef5d6e_0/downloads/data?format=csv&spatialRefId=4326"
|
|
self.HUD_RECAP_CSV = (
|
|
self.TMP_PATH
|
|
/ "Racially_or_Ethnically_Concentrated_Areas_of_Poverty__R_ECAPs_.csv"
|
|
)
|
|
self.CSV_PATH = self.DATA_PATH / "dataset" / "hud_recap"
|
|
|
|
# Definining some variable names
|
|
self.HUD_RECAP_PRIORITY_COMMUNITY_FIELD_NAME = "hud_recap_priority_community"
|
|
|
|
self.df: pd.DataFrame
|
|
|
|
def extract(self) -> None:
|
|
logger.info(f"Downloading HUD Recap Data")
|
|
download = requests.get(self.HUD_RECAP_CSV_URL, verify=None)
|
|
file_contents = download.content
|
|
csv_file = open(self.HUD_RECAP_CSV, "wb")
|
|
csv_file.write(file_contents)
|
|
csv_file.close()
|
|
|
|
def transform(self) -> None:
|
|
logger.info(f"Transforming HUD Recap Data")
|
|
|
|
# Load comparison index (CalEnviroScreen 4)
|
|
self.df = pd.read_csv(self.HUD_RECAP_CSV, dtype={"Census Tract": "string"})
|
|
|
|
self.df.rename(
|
|
columns={
|
|
"GEOID": self.GEOID_TRACT_FIELD_NAME,
|
|
# Interestingly, there's no data dictionary for the RECAP data that I could find.
|
|
# However, this site (http://www.schousing.com/library/Tax%20Credit/2020/QAP%20Instructions%20(2).pdf)
|
|
# suggests:
|
|
# "If RCAP_Current for the tract in which the site is located is 1, the tract is an R/ECAP. If RCAP_Current is 0, it is not."
|
|
"RCAP_Current": self.HUD_RECAP_PRIORITY_COMMUNITY_FIELD_NAME,
|
|
},
|
|
inplace=True,
|
|
)
|
|
|
|
# Convert to boolean
|
|
self.df[self.HUD_RECAP_PRIORITY_COMMUNITY_FIELD_NAME] = self.df[
|
|
self.HUD_RECAP_PRIORITY_COMMUNITY_FIELD_NAME
|
|
].astype("bool")
|
|
|
|
self.df[self.HUD_RECAP_PRIORITY_COMMUNITY_FIELD_NAME].value_counts()
|
|
|
|
self.df.sort_values(by=self.GEOID_TRACT_FIELD_NAME, inplace=True)
|
|
|
|
def load(self) -> None:
|
|
logger.info(f"Saving HUD Recap CSV")
|
|
# write nationwide csv
|
|
self.CSV_PATH.mkdir(parents=True, exist_ok=True)
|
|
self.df.to_csv(self.CSV_PATH / f"usa.csv", index=False)
|