Skip to content

Commit 23b8aee

Browse files
authored
Merge pull request #935 from cmu-delphi/hhs-geo
Add hhs and nation to DV
2 parents c78b524 + 5a0b271 commit 23b8aee

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

doctor_visits/delphi_doctor_visits/geo_maps.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Created: 2020-04-18
88
Last modified: 2020-04-30 by Aaron Rumack (add megacounty code)
99
"""
10+
from functools import partial
1011

1112
import pandas as pd
1213
from delphi_utils.geomap import GeoMapper
@@ -20,6 +21,14 @@ class GeoMaps:
2021
def __init__(self):
2122
"""Create the underlying GeoMapper."""
2223
self.gmpr = GeoMapper()
24+
self.geo_func = {"county": partial(self.county_to_megacounty,
25+
threshold_visits=Config.MIN_RECENT_VISITS,
26+
threshold_len=Config.RECENT_LENGTH),
27+
"state": self.county_to_state,
28+
"msa": self.county_to_msa,
29+
"hrr": self.county_to_hrr,
30+
"hhs": self.county_to_hhs,
31+
"nation": self.county_to_nation}
2332

2433
@staticmethod
2534
def convert_fips(x):
@@ -61,6 +70,40 @@ def county_to_state(self, data):
6170

6271
return data.groupby("state_id"), "state_id"
6372

73+
def county_to_hhs(self, data):
74+
"""Aggregate county data to the HHS region resolution.
75+
76+
Args:
77+
data: dataframe aggregated to the daily-county resolution (all 7 cols expected)
78+
79+
Returns: tuple of dataframe at the daily-HHS resolution, and geo_id column name
80+
"""
81+
data = self.gmpr.add_geocode(data,
82+
"fips",
83+
"hhs",
84+
from_col="PatCountyFIPS")
85+
data.drop(columns="PatCountyFIPS", inplace=True)
86+
data = data.groupby(["ServiceDate", "hhs"]).sum().reset_index()
87+
88+
return data.groupby("hhs"), "hhs"
89+
90+
def county_to_nation(self, data):
91+
"""Aggregate county data to the nation resolution.
92+
93+
Args:
94+
data: dataframe aggregated to the daily-county resolution (all 7 cols expected)
95+
96+
Returns: tuple of dataframe at the daily-nation resolution, and geo_id column name
97+
"""
98+
data = self.gmpr.add_geocode(data,
99+
"fips",
100+
"nation",
101+
from_col="PatCountyFIPS")
102+
data.drop(columns="PatCountyFIPS", inplace=True)
103+
data = data.groupby(["ServiceDate", "nation"]).sum().reset_index()
104+
105+
return data.groupby("nation"), "nation"
106+
64107
def county_to_hrr(self, data):
65108
"""Aggregate county data to the HRR resolution.
66109

doctor_visits/delphi_doctor_visits/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def run_module(params):
6868
logging.info("n_waiting_days:\t{n_waiting_days}")
6969

7070
## geographies
71-
geos = ["state", "msa", "hrr", "county"]
71+
geos = ["state", "msa", "hrr", "county", "hhs", "nation"]
7272

7373

7474
## print out other vars

doctor_visits/delphi_doctor_visits/update_sensor.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def update_sensor(
7878
startdate: first sensor date (YYYY-mm-dd)
7979
enddate: last sensor date (YYYY-mm-dd)
8080
dropdate: data drop date (YYYY-mm-dd)
81-
geo: geographic resolution, one of ["county", "state", "msa", "hrr"]
81+
geo: geographic resolution, one of ["county", "state", "msa", "hrr", "nation", "hhs"]
8282
parallel: boolean to run the sensor update in parallel
8383
weekday: boolean to adjust for weekday effects
8484
se: boolean to write out standard errors, if true, use an obfuscated name
@@ -132,19 +132,8 @@ def update_sensor(
132132

133133
# get right geography
134134
geo_map = GeoMaps()
135-
if geo.lower() == "county":
136-
data_groups, _ = geo_map.county_to_megacounty(
137-
data, Config.MIN_RECENT_VISITS, Config.RECENT_LENGTH
138-
)
139-
elif geo.lower() == "state":
140-
data_groups, _ = geo_map.county_to_state(data)
141-
elif geo.lower() == "msa":
142-
data_groups, _ = geo_map.county_to_msa(data)
143-
elif geo.lower() == "hrr":
144-
data_groups, _ = geo_map.county_to_hrr(data)
145-
else:
146-
logging.error(f"{geo} is invalid, pick one of 'county', 'state', 'msa', 'hrr'")
147-
return {}
135+
mapping_func = geo_map.geo_func[geo.lower()]
136+
data_groups, _ = mapping_func(data)
148137
unique_geo_ids = list(data_groups.groups.keys())
149138

150139
# run sensor fitting code (maybe in parallel)

0 commit comments

Comments
 (0)