Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSI utils for obtaining NSI data #95

Merged
Prev Previous commit
Next Next commit
Added FIPS generation method
  • Loading branch information
ywkim312 committed Jan 8, 2025
commit 3f21523d7e99bf6203661b00d9f1eaed5ec8c742
13 changes: 13 additions & 0 deletions pyincore_data/globals.py
Original file line number Diff line number Diff line change
@@ -17,3 +17,16 @@
)
logging_config.fileConfig(LOGGING_CONFIG)
LOGGER = logging.getLogger("pyincore-data")

STATE_FIPS_CODES = {
"Alabama": "01", "Alaska": "02", "Arizona": "04", "Arkansas": "05", "California": "06",
"Colorado": "08", "Connecticut": "09", "Delaware": "10", "Florida": "12", "Georgia": "13",
"Hawaii": "15", "Idaho": "16", "Illinois": "17", "Indiana": "18", "Iowa": "19",
"Kansas": "20", "Kentucky": "21", "Louisiana": "22", "Maine": "23", "Maryland": "24",
"Massachusetts": "25", "Michigan": "26", "Minnesota": "27", "Mississippi": "28", "Missouri": "29",
"Montana": "30", "Nebraska": "31", "Nevada": "32", "New Hampshire": "33", "New Jersey": "34",
"New Mexico": "35", "New York": "36", "North Carolina": "37", "North Dakota": "38", "Ohio": "39",
"Oklahoma": "40", "Oregon": "41", "Pennsylvania": "42", "Rhode Island": "44", "South Carolina": "45",
"South Dakota": "46", "Tennessee": "47", "Texas": "48", "Utah": "49", "Vermont": "50",
"Virginia": "51", "Washington": "53", "West Virginia": "54", "Wisconsin": "55", "Wyoming": "56"
}
52 changes: 36 additions & 16 deletions pyincore_data/nsiutil.py
Original file line number Diff line number Diff line change
@@ -9,22 +9,13 @@
import requests

from pyincore_data.utils.datautil import DataUtil
from pyincore_data import globals as pyincore_globals

# Static mapping of state names to FIPS codes (since the API doesn't directly return them in this case)
STATE_FIPS_CODES = {
"Alabama": "01", "Alaska": "02", "Arizona": "04", "Arkansas": "05", "California": "06",
"Colorado": "08", "Connecticut": "09", "Delaware": "10", "Florida": "12", "Georgia": "13",
"Hawaii": "15", "Idaho": "16", "Illinois": "17", "Indiana": "18", "Iowa": "19",
"Kansas": "20", "Kentucky": "21", "Louisiana": "22", "Maine": "23", "Maryland": "24",
"Massachusetts": "25", "Michigan": "26", "Minnesota": "27", "Mississippi": "28", "Missouri": "29",
"Montana": "30", "Nebraska": "31", "Nevada": "32", "New Hampshire": "33", "New Jersey": "34",
"New Mexico": "35", "New York": "36", "North Carolina": "37", "North Dakota": "38", "Ohio": "39",
"Oklahoma": "40", "Oregon": "41", "Pennsylvania": "42", "Rhode Island": "44", "South Carolina": "45",
"South Dakota": "46", "Tennessee": "47", "Texas": "48", "Utah": "49", "Vermont": "50",
"Virginia": "51", "Washington": "53", "West Virginia": "54", "Wisconsin": "55", "Wyoming": "56"
}

class NSI:
STATE_FIPS_CODES = pyincore_globals.STATE_FIPS_CODES


class NsiUtil:
@staticmethod
def create_nsi_gdf_by_county_fips(in_fips):
"""
@@ -69,7 +60,7 @@ def create_nsi_gdf_by_counties_fips_list(fips_list):
return merged_gdf

@staticmethod
def get_county_fips_list_by_state(state_name):
def get_county_fips_by_state(state_name):
"""
Fetches all county FIPS codes for a given state using the US Census Bureau API.

@@ -123,10 +114,39 @@ def get_county_fips_only_list_by_state(state_name):
list: A list of FIPS codes (strings) for all counties in the state.
"""
try:
counties = DataUtil.get_county_fips_by_state(state_name)
counties = NsiUtil.get_county_fips_by_state(state_name)
fips_list = [county['fips'] for county in counties]
return fips_list
except ValueError as e:
print("Error:", e)
return []

@staticmethod
def get_fips_by_state_and_county(state_name, county_name):
"""
Fetches the FIPS code for a specific county in a given state.

Args:
state_name (str): Full state name (e.g., "Illinois").
county_name (str): Full county name (e.g., "Champaign").

Returns:
str: The FIPS code for the specified county.
None: If the state or county is not found.
"""
try:
# fetch all counties and their FIPS codes for the state
counties = NsiUtil.get_county_fips_by_state(state_name)

# find the county by name
for county in counties:
county_name_cleaned = county['county'].split(',')[0].replace(" County", "").strip().lower()
if county_name_cleaned == county_name.lower():
return county['fips']

# if no match is found
print(f"County '{county_name}' not found in state '{state_name}'.")
return None
except ValueError as e:
print("Error:", e)
return None
27 changes: 22 additions & 5 deletions tests/pyincore_data/nsi_test.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@

import pytest

from pyincore_data.nsiutil import NSI
from pyincore_data.nsiutil import NsiUtil


@pytest.fixture
@@ -16,26 +16,43 @@ def client():

def test_create_nsi_gdf_by_county_fips():
fips = '15005'
gdf = NSI.create_nsi_gdf_by_county_fips(fips)
gdf = NsiUtil.create_nsi_gdf_by_county_fips(fips)

assert gdf.shape[0] > 0


def test_create_nsi_gdf_by_counties_fips_list():
fips_list = ['15005', '29001', '01001']
merged_gdf = NSI.create_nsi_gdf_by_counties_fips_list(fips_list)
merged_gdf = NsiUtil.create_nsi_gdf_by_counties_fips_list(fips_list)

assert merged_gdf.shape[0] > 0


def test_get_county_fips_by_state():
state = 'illinois'
fips_list = NSI.get_county_fips_by_state(state)
fips_list = NsiUtil.get_county_fips_by_state(state)

assert len(fips_list) > 0


def test_get_county_fips_only_list_by_state():
state = 'illinois'
fips_list = NsiUtil.get_county_fips_only_list_by_state(state)

assert len(fips_list) > 0


def test_get_fips_by_state_and_county():
state = 'illinois'
county = 'champaign'
fips = NsiUtil.get_fips_by_state_and_county(state, county)

assert fips == '17019'


if __name__ == '__main__':
# test_create_nsi_gdf_by_county_fips()
# test_create_nsi_gdf_by_counties_fips_list()
test_get_county_fips_by_state()
# test_get_county_fips_by_state()
test_get_county_fips_only_list_by_state()
test_get_fips_by_state_and_county()
Loading