Skip to content

Commit cb27686

Browse files
committed
comments and cleanup
1 parent de9a277 commit cb27686

File tree

4 files changed

+149
-145
lines changed

4 files changed

+149
-145
lines changed

integrations/server/test_flusurv.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ def localSetUp(self):
1010

1111
def test_flusurv(self):
1212
"""Basic integration test for flusurv endpoint"""
13+
# TODO: Should test out the whole pipeline, where we mock the source
14+
# API by pulling more realistic data from a local JSON.
1315
self.cur.execute(
1416
"""
1517
INSERT INTO `flusurv`(

src/acquisition/flusurv/api.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,13 @@ def fetch_json(path, payload, call_count=1, requests_impl=requests):
6767
"Accept-Encoding": "gzip",
6868
"User-Agent": USER_AGENT,
6969
}
70-
if payload is not None:
71-
headers["Content-Type"] = "application/json;charset=UTF-8"
7270

7371
# send the request and read the response
7472
if payload is None:
7573
method = requests_impl.get
7674
data = None
7775
else:
76+
headers["Content-Type"] = "application/json;charset=UTF-8"
7877
method = requests_impl.post
7978
data = json.dumps(payload)
8079
resp = method(flusurv_url, headers=headers, data=data)
@@ -100,30 +99,30 @@ def fetch_json(path, payload, call_count=1, requests_impl=requests):
10099
def mmwrid_to_epiweek(mmwrid):
101100
"""Convert a CDC week index into an epiweek."""
102101

103-
# Add the difference in IDs, which are sequential, to a reference
104-
# epiweek, which is 2003w40 in this case.
102+
# Add the difference in IDs, which are sequential, to a reference epiweek,
103+
# which is 2003w40 in this case. This is the earliest date we see in the
104+
# returned data. The index-1 week on this scale is the first week of
105+
# 1962.
105106
epiweek_200340 = EpiDate(2003, 9, 28)
106107
mmwrid_200340 = 2179
107108
return epiweek_200340.add_weeks(mmwrid - mmwrid_200340).get_ew()
108109

109110

110111
class FlusurvMetadata:
111112
def __init__(self, max_age_weeks):
112-
self.metadata = self._fetch_flusurv_metadata()
113-
114-
self.issue = self._get_current_issue()
115113
self.max_age_weeks = max_age_weeks
116-
self.seasonids = self._get_recent_seasonids()
114+
self.id_to_group = ID_TO_LABEL_MAP
117115

118-
self.location_to_code = self._make_location_to_code_map()
119-
self.locations = self.location_to_code.keys()
116+
self._fetch_flusurv_metadata()
120117

121-
self.id_to_group = ID_TO_LABEL_MAP
122-
self.id_to_season = self._make_id_season_map()
118+
self._get_current_issue()
119+
self._get_recent_seasonids()
120+
self._make_location_to_code_map()
121+
self._make_id_season_map()
123122

124123
def _fetch_flusurv_metadata(self):
125124
"""Return FluSurv JSON metadata object."""
126-
return fetch_json(
125+
self.metadata = fetch_json(
127126
"PostPhase03DataTool",
128127
{"appversion": "Public", "key": "", "injson": []}
129128
)
@@ -151,7 +150,8 @@ def _make_location_to_code_map(self):
151150
location_to_code[location_name] = (
152151
int(location["networkid"]), int(location["catchmentid"])
153152
)
154-
return location_to_code
153+
self.location_to_code = location_to_code
154+
self.locations = self.location_to_code.keys()
155155

156156
def _get_current_issue(self):
157157
"""
@@ -163,25 +163,24 @@ def _get_current_issue(self):
163163
# extract
164164
date = datetime.strptime(self.metadata["loaddatetime"], "%b %d, %Y")
165165

166-
# convert and return
167-
return EpiDate(date.year, date.month, date.day).get_ew()
166+
# convert
167+
self.issue = EpiDate(date.year, date.month, date.day).get_ew()
168168

169169
def _get_recent_seasonids(self):
170-
# Ignore seasons with all dates older than one year
171-
seasonids = {
170+
# Ignore seasons with all dates older than `self.max_age_weeks` (from
171+
# user command line argument `max_age`)
172+
self.seasonids = {
172173
season_blob["seasonid"] for season_blob in self.metadata["seasons"]
173174
if delta_epiweeks(mmwrid_to_epiweek(season_blob["endweek"]), self.issue) < self.max_age_weeks
174175
}
175176

176-
return seasonids
177-
178177
def _make_id_season_map(self):
179178
"""Create a map from seasonid to season description, in the format "YYYY-YY" """
180179
id_to_label = defaultdict(lambda: defaultdict(lambda: None))
181180
for season in self.metadata["seasons"]:
182181
id_to_label[season["seasonid"]] = season["label"].strip()
183182

184-
return id_to_label
183+
self.id_to_season = id_to_label
185184

186185

187186
class FlusurvLocationFetcher:

src/acquisition/flusurv/reference/new_grasp_location_result.json

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
### New location-specific API response from https://gis.cdc.gov/GRASP/Flu3/PostPhase03DataTool?appVersion=Public
2+
#
3+
# The JSON format is invalid as-is. To be able to read these reference files
4+
# into python, first remove the comments.
5+
#
26
# with payload
37
# {"appversion": "Public", "key": "getdata", "injson": [
48
# {"networkid": 1, "catchmentid": 22, "seasonid": 30 },
@@ -19,16 +23,16 @@
1923
# Data!! Format is list of dicts. Each obs' fields are
2024
# labelled, so we could easily convert this to dataframe.
2125
'default_data': [
22-
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 1, 'rate': 4.3, 'weeklyrate': 1.7, 'mmwrid': 2493},
23-
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 1, 'rate': 20.3, 'weeklyrate': 0.1, 'mmwrid': 2513},
24-
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 1, 'rate': 20.6, 'weeklyrate': 0.1, 'mmwrid': 2516},
25-
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 1, 'rate': 20.7, 'weeklyrate': 0.0, 'mmwrid': 2519},
26-
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 2, 'rate': 11.6, 'weeklyrate': 3.6, 'mmwrid': 2493},
27-
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 2, 'rate': 39.6, 'weeklyrate': 0.3, 'mmwrid': 2513},
28-
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 2, 'rate': 40.7, 'weeklyrate': 0.5, 'mmwrid': 2516},
29-
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 2, 'rate': 41.3, 'weeklyrate': 0.1, 'mmwrid': 2519},
30-
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 3, 'rate': 12.8, 'weeklyrate': 4.8, 'mmwrid': 2493},
31-
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 3, 'rate': 36.0, 'weeklyrate': 0.1, 'mmwrid': 2513},
26+
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 1, 'rate': 4.3, 'weeklyrate': 1.7, 'mmwrid': 2493},
27+
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 1, 'rate': 20.3, 'weeklyrate': 0.1, 'mmwrid': 2513},
28+
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 1, 'rate': 20.6, 'weeklyrate': 0.1, 'mmwrid': 2516},
29+
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 1, 'rate': 20.7, 'weeklyrate': 0.0, 'mmwrid': 2519},
30+
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 2, 'rate': 11.6, 'weeklyrate': 3.6, 'mmwrid': 2493},
31+
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 2, 'rate': 39.6, 'weeklyrate': 0.3, 'mmwrid': 2513},
32+
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 2, 'rate': 40.7, 'weeklyrate': 0.5, 'mmwrid': 2516},
33+
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 2, 'rate': 41.3, 'weeklyrate': 0.1, 'mmwrid': 2519},
34+
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 3, 'rate': 12.8, 'weeklyrate': 4.8, 'mmwrid': 2493},
35+
{'networkid': 1, 'catchmentid': 22, 'seasonid': 49, 'ageid': 0, 'sexid': 0, 'raceid': 3, 'rate': 36.0, 'weeklyrate': 0.1, 'mmwrid': 2513},
3236
...
3337
]
3438
}

0 commit comments

Comments
 (0)