Skip to content

Commit

Permalink
Add support for TIGER years 2000 and 2009-2023 (incl.)
Browse files Browse the repository at this point in the history
Small refactor in us_census module, to remove awkward concept of "CensusYear".
  • Loading branch information
Tyler Coles committed Jul 8, 2024
1 parent 9a2608a commit 77cc8a1
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 95 deletions.
63 changes: 24 additions & 39 deletions doc/devlog/2024-05-03.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
"\n",
"_Author: Tyler Coles_\n",
"\n",
"Testing our us_census functions for loading canonical sets of IDs for Census granularities from state to block group for the years 2000, 2010, and 2020.\n",
"Testing our us_census functions for loading canonical sets of IDs for Census granularities from state to block group for all supported TIGER years (2000 and 2009-2023).\n",
"\n",
"Since this is our source of truth for these delineations, we want to make sure we're getting complete data. One thing we can test is that at each level of granularity (above block group) each node should contain at least one child node. That is every state should contain a county, every county a tract, and every tract a block group. Otherwise we know something is missing.\n",
"\n",
"(This may seem like a trivial test, but in fact it discovered that my original assumptions about how TIGER provides the data were invalid and has already saved us from bugs!)"
"(This may seem like a trivial test, but in fact it discovered that my original assumptions about how TIGER provides the data were invalid and has already saved us from bugs!)\n",
"\n",
"WARNING: this will take a very long time if you don't have the TIGER files cached."
]
},
{
Expand All @@ -22,13 +24,14 @@
"outputs": [],
"source": [
"import epymorph.geography.us_census as c\n",
"import epymorph.geography.us_tiger as t\n",
"\n",
"\n",
"class Fail(Exception):\n",
" pass\n",
"\n",
"\n",
"def test_year(year: c.CensusYear) -> None:\n",
"def test_year(year: int) -> None:\n",
" # 1. test that we have 52 states\n",
" states = c.get_us_states(year).geoid\n",
"\n",
Expand Down Expand Up @@ -80,46 +83,28 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Census year 2020 passed!\n"
]
}
],
"source": [
"test_year(2020)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Census year 2010 passed!\n"
]
}
],
"source": [
"test_year(2010)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Census year 2000 passed!\n"
"Census year 2000 passed!\n",
"Census year 2009 passed!\n",
"Census year 2010 passed!\n",
"Census year 2011 passed!\n",
"Census year 2012 passed!\n",
"Census year 2013 passed!\n",
"Census year 2014 passed!\n",
"Census year 2015 passed!\n",
"Census year 2016 passed!\n",
"Census year 2017 passed!\n",
"Census year 2018 passed!\n",
"Census year 2019 passed!\n",
"Census year 2020 passed!\n",
"Census year 2021 passed!\n",
"Census year 2022 passed!\n",
"Census year 2023 passed!\n"
]
}
],
"source": [
"test_year(2000)"
"for year in t.TIGER_YEARS:\n",
" test_year(year)"
]
}
],
Expand Down
17 changes: 11 additions & 6 deletions epymorph/geo/adrio/census/adrio_census.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

from epymorph.data_shape import Shapes
from epymorph.data_type import CentroidDType
from epymorph.error import DataResourceException, GeoValidationException
from epymorph.error import (DataResourceException, GeographyError,
GeoValidationException)
from epymorph.geo.adrio.adrio import ADRIO, ADRIOMaker
from epymorph.geo.spec import TimePeriod, Year
from epymorph.geography.us_census import (BLOCK_GROUP, COUNTY, STATE, TRACT,
Expand All @@ -22,7 +23,7 @@
StateScopeAll, TractScope)
from epymorph.geography.us_tiger import (get_block_groups_geo,
get_counties_geo, get_states_geo,
get_tracts_geo)
get_tracts_geo, is_tiger_year)
from epymorph.simulation import AttributeDef, geo_attrib


Expand Down Expand Up @@ -197,18 +198,22 @@ def fetch_acs5(self, variables: list[str], scope: CensusScope, year: int) -> Dat
def fetch_sf(self, scope: CensusScope) -> GeoDataFrame:
"""Utility function to fetch shape files from Census for specified regions."""
# call appropriate pygris function based on granularity and sort result
scope_year = scope.year
if not is_tiger_year(scope_year):
raise GeographyError(f"Unsupported year: {scope_year}")

match scope:
case StateScopeAll() | StateScope():
df = get_states_geo(year=scope.year)
df = get_states_geo(year=scope_year)

case CountyScope():
df = get_counties_geo(year=scope.year)
df = get_counties_geo(year=scope_year)

case TractScope():
df = get_tracts_geo(year=scope.year)
df = get_tracts_geo(year=scope_year)

case BlockGroupScope():
df = get_block_groups_geo(year=scope.year)
df = get_block_groups_geo(year=scope_year)

case _:
raise DataResourceException("Unsupported query.")
Expand Down
Loading

0 comments on commit 77cc8a1

Please sign in to comment.