Skip to content

Commit 5a0b271

Browse files
committed
Change ifelse block to a dict of functions
1 parent 2ee7cd5 commit 5a0b271

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

doctor_visits/delphi_doctor_visits/geo_maps.py

Lines changed: 9 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):

doctor_visits/delphi_doctor_visits/update_sensor.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,26 +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-
elif geo.lower() == "hhs":
146-
data_groups, _ = geo_map.county_to_hhs(data)
147-
elif geo.lower() == "nation":
148-
data_groups, _ = geo_map.county_to_nation(data)
149-
else:
150-
151-
logging.error(
152-
f"{geo} is invalid, pick one of 'county', 'state', 'msa', 'hrr', 'hhs', 'nation'"
153-
)
154-
return {}
135+
mapping_func = geo_map.geo_func[geo.lower()]
136+
data_groups, _ = mapping_func(data)
155137
unique_geo_ids = list(data_groups.groups.keys())
156138

157139
# run sensor fitting code (maybe in parallel)

0 commit comments

Comments
 (0)