Skip to content

Commit 5938b46

Browse files
committed
use unexpected key's ids as suffix; drop UT sex group handling
1 parent ab41fa0 commit 5938b46

File tree

3 files changed

+15
-36
lines changed

3 files changed

+15
-36
lines changed

src/acquisition/flusurv/api.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,7 @@ def get_data(self, location):
202202

203203
# extract
204204
print("[reformatting flusurv result...]")
205-
data_out = self._add_sex_breakdowns_ut(
206-
self._group_by_epiweek(data_in), location
207-
)
205+
data_out = self._group_by_epiweek(data_in), location
208206

209207
# return
210208
print(f"[successfully fetched data for {location}]")
@@ -325,6 +323,8 @@ def _groupid_to_name(self, ageid, sexid, raceid, fluid):
325323
raise ValueError("Expect at least three of four group ids to be 0")
326324
if (ageid, sexid, raceid, fluid).count(0) == 4:
327325
group = "overall"
326+
# In all cases, if id is not available as a key in the dict, use the
327+
# raw id as the name suffix
328328
elif ageid != 0:
329329
if ageid == 6:
330330
# Ageid of 6 used to be used for the "overall" category.
@@ -333,23 +333,13 @@ def _groupid_to_name(self, ageid, sexid, raceid, fluid):
333333
# has gone wrong.
334334
raise ValueError("Ageid cannot be 6; please check for changes in the API")
335335
else:
336-
age_group = self.metadata.id_to_group["Age"][ageid]
336+
age_group = self.metadata.id_to_group["Age"].get(ageid, ageid)
337337
group = "age_" + age_group
338338
elif sexid != 0:
339-
group = "sex_" + self.metadata.id_to_group["Sex"][sexid]
339+
group = "sex_" + self.metadata.id_to_group["Sex"].get(sexid, ageid)
340340
elif raceid != 0:
341-
group = "race_" + self.metadata.id_to_group["Race"][raceid]
341+
group = "race_" + self.metadata.id_to_group["Race"].get(raceid, ageid)
342342
elif fluid != 0:
343-
group = "flu_" + self.metadata.id_to_group["Flutype"][fluid]
343+
group = "flu_" + self.metadata.id_to_group["Flutype"].get(fluid, ageid)
344344

345345
return "rate_" + group
346-
347-
def _add_sex_breakdowns_ut(self, data, location):
348-
# UT doesn't have sex breakdowns available at least for 2022-23. Fill
349-
# in to avoid downstream errors.
350-
if location == "UT":
351-
for epiweek in data.keys():
352-
for group in SEX_GROUPS:
353-
if group not in data[epiweek].keys():
354-
data[epiweek][group] = None
355-
return(data)

src/acquisition/flusurv/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@
8989
)
9090
SEX_GROUPS = (
9191
"rate_sex_male",
92-
"rate_sex_female"
92+
"rate_sex_female",
9393
)
9494
FLU_GROUPS = (
9595
"rate_flu_a",
96-
"rate_flu_b"
96+
"rate_flu_b",
9797
)
9898
EXPECTED_GROUPS = HISTORICAL_GROUPS + NEW_AGE_GROUPS + RACE_GROUPS + SEX_GROUPS + FLU_GROUPS
9999

tests/acquisition/flusurv/test_flusurv.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ def test_groupids_to_name(self):
357357
(0, 0, 0, 1),
358358
(13, 0, 0, 0),
359359
(97, 0, 0, 0),
360+
(999, 0, 0, 0),
361+
(0, 0, 111, 0),
360362
)
361363
expected_list = [
362364
"rate_age_0",
@@ -368,30 +370,17 @@ def test_groupids_to_name(self):
368370
"rate_flu_b",
369371
"rate_flu_a",
370372
"rate_age_0tlt1",
371-
"rate_age_97",
373+
"rate_age_lt18",
374+
"rate_age_999",
375+
"rate_race_111",
372376
]
373377

374378
for (ageid, sexid, raceid, fluid), expected in zip(ids, expected_list):
375379
self.assertEqual(api_fetcher._groupid_to_name(ageid, sexid, raceid, fluid), expected)
376380

377381
with self.assertRaisesRegex(ValueError, "Ageid cannot be 6"):
378-
api_fetcher._groupid_to_name(6, 0, 0)
382+
api_fetcher._groupid_to_name(6, 0, 0, 0)
379383
with self.assertRaisesRegex(ValueError, "Expect at least three of four group ids to be 0"):
380384
api_fetcher._groupid_to_name(1, 1, 0, 0)
381385
api_fetcher._groupid_to_name(0, 1, 1, 0)
382386
api_fetcher._groupid_to_name(1, 1, 1, 1)
383-
384-
def test_groupids_to_name(self):
385-
input_data = api_fetcher._group_by_epiweek(metadata_result)
386-
self.assertEqual(api_fetcher._add_sex_breakdowns_ut(input_data, "network_all"), by_epiweek_example_data)
387-
388-
ut_expected = by_epiweek_example_data.copy()
389-
ut_expected[201014]["rate_sex_male"] = None
390-
ut_expected[200940]["rate_sex_female"] = None
391-
ut_expected[200940]["rate_sex_male"] = None
392-
ut_expected[201011]["rate_sex_female"] = None
393-
ut_expected[201011]["rate_sex_male"] = None
394-
ut_expected[201008]["rate_sex_female"] = None
395-
ut_expected[201008]["rate_sex_male"] = None
396-
397-
self.assertEqual(api_fetcher._add_sex_breakdowns_ut(input_data, "UT"), ut_expected)

0 commit comments

Comments
 (0)