77Created: 2020-04-18
88Last modified: 2020-04-30 by Aaron Rumack (add megacounty code)
99"""
10+ from functools import partial
1011
1112import pandas as pd
1213from 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
0 commit comments