Skip to content

Add hhs and nation to DV #935

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 2 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 43 additions & 0 deletions doctor_visits/delphi_doctor_visits/geo_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Created: 2020-04-18
Last modified: 2020-04-30 by Aaron Rumack (add megacounty code)
"""
from functools import partial

import pandas as pd
from delphi_utils.geomap import GeoMapper
Expand All @@ -20,6 +21,14 @@ class GeoMaps:
def __init__(self):
"""Create the underlying GeoMapper."""
self.gmpr = GeoMapper()
self.geo_func = {"county": partial(self.county_to_megacounty,
threshold_visits=Config.MIN_RECENT_VISITS,
threshold_len=Config.RECENT_LENGTH),
"state": self.county_to_state,
"msa": self.county_to_msa,
"hrr": self.county_to_hrr,
"hhs": self.county_to_hhs,
"nation": self.county_to_nation}

@staticmethod
def convert_fips(x):
Expand Down Expand Up @@ -61,6 +70,40 @@ def county_to_state(self, data):

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

def county_to_hhs(self, data):
"""Aggregate county data to the HHS region resolution.

Args:
data: dataframe aggregated to the daily-county resolution (all 7 cols expected)

Returns: tuple of dataframe at the daily-HHS resolution, and geo_id column name
"""
data = self.gmpr.add_geocode(data,
"fips",
"hhs",
from_col="PatCountyFIPS")
data.drop(columns="PatCountyFIPS", inplace=True)
data = data.groupby(["ServiceDate", "hhs"]).sum().reset_index()

return data.groupby("hhs"), "hhs"

def county_to_nation(self, data):
"""Aggregate county data to the nation resolution.

Args:
data: dataframe aggregated to the daily-county resolution (all 7 cols expected)

Returns: tuple of dataframe at the daily-nation resolution, and geo_id column name
"""
data = self.gmpr.add_geocode(data,
"fips",
"nation",
from_col="PatCountyFIPS")
data.drop(columns="PatCountyFIPS", inplace=True)
data = data.groupby(["ServiceDate", "nation"]).sum().reset_index()

return data.groupby("nation"), "nation"

def county_to_hrr(self, data):
"""Aggregate county data to the HRR resolution.

Expand Down
2 changes: 1 addition & 1 deletion doctor_visits/delphi_doctor_visits/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def run_module(params):
logging.info("n_waiting_days:\t{n_waiting_days}")

## geographies
geos = ["state", "msa", "hrr", "county"]
geos = ["state", "msa", "hrr", "county", "hhs", "nation"]


## print out other vars
Expand Down
17 changes: 3 additions & 14 deletions doctor_visits/delphi_doctor_visits/update_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def update_sensor(
startdate: first sensor date (YYYY-mm-dd)
enddate: last sensor date (YYYY-mm-dd)
dropdate: data drop date (YYYY-mm-dd)
geo: geographic resolution, one of ["county", "state", "msa", "hrr"]
geo: geographic resolution, one of ["county", "state", "msa", "hrr", "nation", "hhs"]
parallel: boolean to run the sensor update in parallel
weekday: boolean to adjust for weekday effects
se: boolean to write out standard errors, if true, use an obfuscated name
Expand Down Expand Up @@ -132,19 +132,8 @@ def update_sensor(

# get right geography
geo_map = GeoMaps()
if geo.lower() == "county":
data_groups, _ = geo_map.county_to_megacounty(
data, Config.MIN_RECENT_VISITS, Config.RECENT_LENGTH
)
elif geo.lower() == "state":
data_groups, _ = geo_map.county_to_state(data)
elif geo.lower() == "msa":
data_groups, _ = geo_map.county_to_msa(data)
elif geo.lower() == "hrr":
data_groups, _ = geo_map.county_to_hrr(data)
else:
logging.error(f"{geo} is invalid, pick one of 'county', 'state', 'msa', 'hrr'")
return {}
mapping_func = geo_map.geo_func[geo.lower()]
data_groups, _ = mapping_func(data)
unique_geo_ids = list(data_groups.groups.keys())

# run sensor fitting code (maybe in parallel)
Expand Down