Skip to content

Commit

Permalink
use unexpected key's ids as suffix; drop UT sex group handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nmdefries committed Feb 26, 2025
1 parent ab41fa0 commit 5938b46
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 36 deletions.
24 changes: 7 additions & 17 deletions src/acquisition/flusurv/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,7 @@ def get_data(self, location):

# extract
print("[reformatting flusurv result...]")
data_out = self._add_sex_breakdowns_ut(
self._group_by_epiweek(data_in), location
)
data_out = self._group_by_epiweek(data_in), location

# return
print(f"[successfully fetched data for {location}]")
Expand Down Expand Up @@ -325,6 +323,8 @@ def _groupid_to_name(self, ageid, sexid, raceid, fluid):
raise ValueError("Expect at least three of four group ids to be 0")
if (ageid, sexid, raceid, fluid).count(0) == 4:
group = "overall"
# In all cases, if id is not available as a key in the dict, use the
# raw id as the name suffix
elif ageid != 0:
if ageid == 6:
# Ageid of 6 used to be used for the "overall" category.
Expand All @@ -333,23 +333,13 @@ def _groupid_to_name(self, ageid, sexid, raceid, fluid):
# has gone wrong.
raise ValueError("Ageid cannot be 6; please check for changes in the API")
else:
age_group = self.metadata.id_to_group["Age"][ageid]
age_group = self.metadata.id_to_group["Age"].get(ageid, ageid)
group = "age_" + age_group
elif sexid != 0:
group = "sex_" + self.metadata.id_to_group["Sex"][sexid]
group = "sex_" + self.metadata.id_to_group["Sex"].get(sexid, ageid)
elif raceid != 0:
group = "race_" + self.metadata.id_to_group["Race"][raceid]
group = "race_" + self.metadata.id_to_group["Race"].get(raceid, ageid)
elif fluid != 0:
group = "flu_" + self.metadata.id_to_group["Flutype"][fluid]
group = "flu_" + self.metadata.id_to_group["Flutype"].get(fluid, ageid)

return "rate_" + group

def _add_sex_breakdowns_ut(self, data, location):
# UT doesn't have sex breakdowns available at least for 2022-23. Fill
# in to avoid downstream errors.
if location == "UT":
for epiweek in data.keys():
for group in SEX_GROUPS:
if group not in data[epiweek].keys():
data[epiweek][group] = None
return(data)
4 changes: 2 additions & 2 deletions src/acquisition/flusurv/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@
)
SEX_GROUPS = (
"rate_sex_male",
"rate_sex_female"
"rate_sex_female",
)
FLU_GROUPS = (
"rate_flu_a",
"rate_flu_b"
"rate_flu_b",
)
EXPECTED_GROUPS = HISTORICAL_GROUPS + NEW_AGE_GROUPS + RACE_GROUPS + SEX_GROUPS + FLU_GROUPS

Expand Down
23 changes: 6 additions & 17 deletions tests/acquisition/flusurv/test_flusurv.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ def test_groupids_to_name(self):
(0, 0, 0, 1),
(13, 0, 0, 0),
(97, 0, 0, 0),
(999, 0, 0, 0),
(0, 0, 111, 0),
)
expected_list = [
"rate_age_0",
Expand All @@ -368,30 +370,17 @@ def test_groupids_to_name(self):
"rate_flu_b",
"rate_flu_a",
"rate_age_0tlt1",
"rate_age_97",
"rate_age_lt18",
"rate_age_999",
"rate_race_111",
]

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

with self.assertRaisesRegex(ValueError, "Ageid cannot be 6"):
api_fetcher._groupid_to_name(6, 0, 0)
api_fetcher._groupid_to_name(6, 0, 0, 0)
with self.assertRaisesRegex(ValueError, "Expect at least three of four group ids to be 0"):
api_fetcher._groupid_to_name(1, 1, 0, 0)
api_fetcher._groupid_to_name(0, 1, 1, 0)
api_fetcher._groupid_to_name(1, 1, 1, 1)

def test_groupids_to_name(self):
input_data = api_fetcher._group_by_epiweek(metadata_result)
self.assertEqual(api_fetcher._add_sex_breakdowns_ut(input_data, "network_all"), by_epiweek_example_data)

ut_expected = by_epiweek_example_data.copy()
ut_expected[201014]["rate_sex_male"] = None
ut_expected[200940]["rate_sex_female"] = None
ut_expected[200940]["rate_sex_male"] = None
ut_expected[201011]["rate_sex_female"] = None
ut_expected[201011]["rate_sex_male"] = None
ut_expected[201008]["rate_sex_female"] = None
ut_expected[201008]["rate_sex_male"] = None

self.assertEqual(api_fetcher._add_sex_breakdowns_ut(input_data, "UT"), ut_expected)

0 comments on commit 5938b46

Please sign in to comment.