From 2ee7cd5e882883f7f9562463fb9985ed6144ab4a Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 9 Dec 2020 02:02:16 -0800 Subject: [PATCH 1/2] Add hhs and nation --- .../delphi_doctor_visits/geo_maps.py | 34 +++++++++++++++++++ doctor_visits/delphi_doctor_visits/run.py | 2 +- .../delphi_doctor_visits/update_sensor.py | 11 ++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/doctor_visits/delphi_doctor_visits/geo_maps.py b/doctor_visits/delphi_doctor_visits/geo_maps.py index 9ba935683..3bc0cc2b1 100644 --- a/doctor_visits/delphi_doctor_visits/geo_maps.py +++ b/doctor_visits/delphi_doctor_visits/geo_maps.py @@ -61,6 +61,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. diff --git a/doctor_visits/delphi_doctor_visits/run.py b/doctor_visits/delphi_doctor_visits/run.py index d85e0b632..1feb6c945 100644 --- a/doctor_visits/delphi_doctor_visits/run.py +++ b/doctor_visits/delphi_doctor_visits/run.py @@ -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 diff --git a/doctor_visits/delphi_doctor_visits/update_sensor.py b/doctor_visits/delphi_doctor_visits/update_sensor.py index 725d4ca4f..b2981ad32 100644 --- a/doctor_visits/delphi_doctor_visits/update_sensor.py +++ b/doctor_visits/delphi_doctor_visits/update_sensor.py @@ -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 @@ -142,8 +142,15 @@ def update_sensor( data_groups, _ = geo_map.county_to_msa(data) elif geo.lower() == "hrr": data_groups, _ = geo_map.county_to_hrr(data) + elif geo.lower() == "hhs": + data_groups, _ = geo_map.county_to_hhs(data) + elif geo.lower() == "nation": + data_groups, _ = geo_map.county_to_nation(data) else: - logging.error(f"{geo} is invalid, pick one of 'county', 'state', 'msa', 'hrr'") + + logging.error( + f"{geo} is invalid, pick one of 'county', 'state', 'msa', 'hrr', 'hhs', 'nation'" + ) return {} unique_geo_ids = list(data_groups.groups.keys()) From 5a0b2712391dcb30efbf74ae937f4b80367537ed Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 17 Mar 2021 14:44:23 -0700 Subject: [PATCH 2/2] Change ifelse block to a dict of functions --- .../delphi_doctor_visits/geo_maps.py | 9 ++++++++ .../delphi_doctor_visits/update_sensor.py | 22 ++----------------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/doctor_visits/delphi_doctor_visits/geo_maps.py b/doctor_visits/delphi_doctor_visits/geo_maps.py index 3bc0cc2b1..716e8899d 100644 --- a/doctor_visits/delphi_doctor_visits/geo_maps.py +++ b/doctor_visits/delphi_doctor_visits/geo_maps.py @@ -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 @@ -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): diff --git a/doctor_visits/delphi_doctor_visits/update_sensor.py b/doctor_visits/delphi_doctor_visits/update_sensor.py index b2981ad32..01e1647fe 100644 --- a/doctor_visits/delphi_doctor_visits/update_sensor.py +++ b/doctor_visits/delphi_doctor_visits/update_sensor.py @@ -132,26 +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) - elif geo.lower() == "hhs": - data_groups, _ = geo_map.county_to_hhs(data) - elif geo.lower() == "nation": - data_groups, _ = geo_map.county_to_nation(data) - else: - - logging.error( - f"{geo} is invalid, pick one of 'county', 'state', 'msa', 'hrr', 'hhs', 'nation'" - ) - 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)