@@ -109,19 +109,8 @@ def run_module(params):
109109 geo_res = geo ,
110110 sensor = sensor ,
111111 smoother = smoother )
112- df = geo_mapper .add_geocode (make_signal (all_columns , sensor ),
113- "state_id" ,
114- "state_code" ,
115- from_col = "state" )
116- if sensor .endswith ("_prop" ):
117- df = pop_proportion (df , geo_mapper )
118- df = make_geo (df , geo , geo_mapper )
119- df = smooth_values (df , smoother [0 ])
120- # Fix N/A MA values, see issue #1360
121- if geo == "state" and sensor .startswith (CONFIRMED_FLU ):
122- ma_filter = df .val .isna () & (df .geo_id == "ma" ) & (df .timestamp > "08-01-2021" ) & \
123- (df .timestamp .dt .day_name () == "Tuesday" )
124- df = df [~ ma_filter ]
112+ df = make_signal (all_columns , sensor )
113+ df = transform_signal (sensor , smoother , geo , df , geo_mapper )
125114 if df .empty :
126115 continue
127116 sensor_name = sensor + smoother [1 ]
@@ -155,17 +144,32 @@ def smooth_values(df, smoother):
155144 )
156145 return df
157146
158- def pop_proportion (df ,geo_mapper ):
159- """Get the population-proportionate variants as the dataframe val."""
160- pop_val = geo_mapper .add_population_column (df , "state_code" )
161- df ["val" ]= round (df ["val" ]/ pop_val ["population" ]* 100000 , 7 )
162- pop_val .drop ("population" , axis = 1 , inplace = True )
147+ def transform_signal (sensor , smoother , geo , df , geo_mapper ):
148+ """Transform base df into specified geo/smoothing/prop configuration."""
149+ df = geo_mapper .add_geocode (df , "state_id" , "state_code" , from_col = "state" )
150+ # handling population:
151+ # add population column
152+ # sum admission counts *and* population counts during make_geo
153+ # *then* divide counts by population to get the proportion
154+ if sensor .endswith ("_prop" ):
155+ df = geo_mapper .add_population_column (df , "state_code" )
156+ df = make_geo (df , geo , geo_mapper )
157+ if sensor .endswith ("_prop" ):
158+ df ["val" ]= round (df ["val" ]/ df ["population" ]* 100000 , 7 )
159+ df .drop ("population" , axis = 1 , inplace = True )
160+ df = smooth_values (df , smoother [0 ])
161+ # Fix N/A MA values, see issue #1360
162+ if geo == "state" and sensor .startswith (CONFIRMED_FLU ):
163+ ma_filter = df .val .isna () & (df .geo_id == "ma" ) & (df .timestamp > "08-01-2021" ) & \
164+ (df .timestamp .dt .day_name () == "Tuesday" )
165+ df = df [~ ma_filter ]
163166 return df
164167
165168def make_geo (state , geo , geo_mapper ):
166169 """Transform incoming geo (state) to another geo."""
167170 if geo == "state" :
168171 exported = state .rename (columns = {"state" : "geo_id" })
172+ exported = exported .drop (columns = "state_code" )
169173 else :
170174 exported = geo_mapper .replace_geocode (state , "state_code" , geo , new_col = "geo_id" )
171175 exported ["se" ] = np .nan
0 commit comments