Skip to content

Add HHS to geomapper util #610

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

Merged
merged 5 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions _delphi_utils_python/delphi_utils/geomap.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
"msa": join(DATA_PATH, "zip_msa_table.csv"),
"pop": join(DATA_PATH, "zip_pop.csv"),
"state": join(DATA_PATH, "zip_state_code_table.csv"),
"hhs_region_number": join(DATA_PATH, "zip_hhs_table.csv")
},
"fips": {
"zip": join(DATA_PATH, "fips_zip_table.csv"),
"hrr": join(DATA_PATH, "fips_hrr_table.csv"),
"msa": join(DATA_PATH, "fips_msa_table.csv"),
"pop": join(DATA_PATH, "fips_pop.csv"),
"state": join(DATA_PATH, "fips_state_table.csv"),
"hhs_region_number": join(DATA_PATH, "fips_hhs_table.csv"),
},
"state": {"state": join(DATA_PATH, "state_codes_table.csv")},
"state_code": {
Expand All @@ -55,12 +57,14 @@ class GeoMapper: # pylint: disable=too-many-public-methods
- [x] zip -> hrr : unweighted
- [x] zip -> msa : unweighted
- [x] zip -> state
- [x] zip -> hhs_region_number
- [x] zip -> population
- [x] state code -> hhs_region_number
- [x] fips -> state : unweighted
- [x] fips -> msa : unweighted
- [x] fips -> megacounty
- [x] fips -> hrr
- [x] fips -> hhs_region_number
- [x] nation
- [ ] zip -> dma (postponed)

Expand Down Expand Up @@ -102,8 +106,10 @@ def __init__(self):
"""
self.crosswalk_filepaths = CROSSWALK_FILEPATHS
self.crosswalks = {
"zip": {"fips": None, "hrr": None, "msa": None, "pop": None, "state": None},
"fips": {"zip": None, "hrr": None, "msa": None, "pop": None, "state": None},
"zip": {"fips": None, "hrr": None, "msa": None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these always going to have None as the value? Might be more scalable/futureproof to write as a dictionary comprehension:

"zip": {x:None for x in ["fips",...]}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, unless we end up preloading crosswalks sometime? I'll add your suggestion.

"pop": None, "state": None, "hhs_region_number": None},
"fips": {"zip": None, "hrr": None, "msa": None,
"pop": None, "state": None, "hhs_region_number": None},
"state": {"state": None},
"state_code": {"hhs_region_number": None},
"jhu_uid": {"fips": None},
Expand All @@ -123,6 +129,7 @@ def _load_crosswalk(self, from_code, to_code):
("jhu_uid", "fips"),
("zip", "msa"),
("fips", "hrr"),
("zip", "hhs_region_number")
]:
self.crosswalks[from_code][to_code] = pd.read_csv(
stream,
Expand All @@ -136,6 +143,8 @@ def _load_crosswalk(self, from_code, to_code):
elif (from_code, to_code) in [
("zip", "hrr"),
("fips", "msa"),
("fips", "hhs_region_number"),
("state_code", "hhs_region_number")
]:
self.crosswalks[from_code][to_code] = pd.read_csv(
stream,
Expand All @@ -151,11 +160,6 @@ def _load_crosswalk(self, from_code, to_code):
"state_name": str,
},
)
elif (from_code, to_code) == ("state_code", "hhs_region_number"):
self.crosswalks[from_code][to_code] = pd.read_csv(
stream,
dtype={"state_code": str, "hhs_region_number": str},
)
elif (from_code, to_code) == ("zip", "state"):
self.crosswalks[from_code][to_code] = pd.read_csv(
stream,
Expand Down Expand Up @@ -255,8 +259,8 @@ def add_geocode(
"""Add a new geocode column to a dataframe.

Currently supported conversions:
- fips -> state_code, state_id, state_name, zip, msa, hrr, nation
- zip -> state_code, state_id, state_name, fips, msa, hrr, nation
- fips -> state_code, state_id, state_name, zip, msa, hrr, nation, hhs_region_number
- zip -> state_code, state_id, state_name, fips, msa, hrr, nation, hhs_region_number
- jhu_uid -> fips
- state_x -> state_y, where x and y are in {code, id, name}
- state_code -> hhs_region_number
Expand Down
31 changes: 31 additions & 0 deletions _delphi_utils_python/tests/test_geomap.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ def test_crosswalks(self):
# assert cw.groupby("zip")["weight"].sum().round(5).eq(1.0).all()
cw = gmpr._load_crosswalk(from_code="zip", to_code="state")
assert cw.groupby("zip")["weight"].sum().round(5).eq(1.0).all()
cw = gmpr._load_crosswalk(from_code="zip", to_code="hhs_region_number")
assert cw.groupby("zip")["weight"].sum().round(5).eq(1.0).all()


def test_load_zip_fips_table(self):
gmpr = GeoMapper()
Expand Down Expand Up @@ -261,3 +264,31 @@ def test_add_geocode(self):
}
)
)

# fips -> hhs
new_data = gmpr.replace_geocode(self.fips_data_3.drop(columns=["date"]),
"fips", "hhs_region_number", date_col=None)
assert new_data.equals(
pd.DataFrame().from_dict(
{
"hhs_region_number": {0: "2", 1: "6"},
"count": {0: 12, 1: 6},
"total": {0: 111, 1: 13}
}
)
)

# zip -> hhs
new_data = gmpr.replace_geocode(self.zip_data, "zip", "hhs_region_number")
new_data = new_data.round(10) # get rid of a floating point error with 99.00000000000001
assert new_data.equals(
pd.DataFrame().from_dict(
{
"date": {0: pd.Timestamp("2018-01-01"), 1: pd.Timestamp("2018-01-01"),
2: pd.Timestamp("2018-01-03"), 3: pd.Timestamp("2018-01-03")},
"hhs_region_number": {0: "5", 1: "9", 2: "5", 3: "9"},
"count": {0: 99.0, 1: 801.0, 2: 100.0, 3: 786.0},
"total": {0: 198.0, 1: 1602.0, 2: 200.0, 3: 1572.0}
}
)
)