Skip to content

Commit 63f41b1

Browse files
authored
Only retrieve the fasttrack population attribute if necessary (#1589)
- Profiling revealed that around 15% of runtime was being spent in `enforce_priority_policy` - Specifically, getting all the fasttrack attributes for the event's target individual, whether that is needed or not - Note that vast majority of events do not have fasttrack priorities (or, in policies that do, not all attributes do)
1 parent 6d1f6e0 commit 63f41b1

File tree

1 file changed

+22
-35
lines changed

1 file changed

+22
-35
lines changed

src/tlo/methods/healthsystem.py

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,44 +1361,31 @@ def _add_hsi_event_queue_item_to_hsi_event_queue(self, priority, topen, tclose,
13611361
# This is where the priority policy is enacted
13621362
def enforce_priority_policy(self, hsi_event) -> int:
13631363
"""Return priority for HSI_Event based on policy under consideration """
1364+
priority_ranking = self.priority_rank_dict
13641365

1365-
pr = self.priority_rank_dict
1366-
pdf = self.sim.population.props
1367-
1368-
if hsi_event.TREATMENT_ID in pr:
1369-
_priority_ranking = pr[hsi_event.TREATMENT_ID]['Priority']
1370-
1371-
# Check whether fast-tracking routes are available for this treatment. If person qualifies for one
1372-
# don't check remaining.
1373-
1374-
# Look up relevant attributes for HSI_Event's target
1375-
list_targets = [_t[0] for _t in self.list_fasttrack]
1376-
target_attributes = pdf.loc[hsi_event.target, list_targets]
1377-
1378-
# Warning: here assuming that the first fast-tracking eligibility encountered
1379-
# will determine the priority to be used. If different fast-tracking channels have
1380-
# different priorities for the same treatment, this will be a problem!
1381-
# First item in Lists is age-related, therefore need to invoke different logic.
1382-
if (
1383-
(pr[hsi_event.TREATMENT_ID][self.list_fasttrack[0][1]] > -1)
1384-
and (target_attributes['age_exact_years'] <= 5)
1385-
):
1386-
return pr[hsi_event.TREATMENT_ID][self.list_fasttrack[0][1]]
1387-
1388-
# All other attributes are looked up the same way, so can do this in for loop
1389-
for i in range(1, len(self.list_fasttrack)):
1390-
if (
1391-
(pr[hsi_event.TREATMENT_ID][self.list_fasttrack[i][1]] > - 1)
1392-
and target_attributes[i]
1393-
):
1394-
return pr[hsi_event.TREATMENT_ID][self.list_fasttrack[i][1]]
1366+
if hsi_event.TREATMENT_ID not in priority_ranking:
1367+
# If treatment is not ranked in the policy, issue a warning and assign priority=3 by default
1368+
warnings.warn(UserWarning(f"Couldn't find priority ranking for TREATMENT_ID {hsi_event.TREATMENT_ID}"))
1369+
return self.lowest_priority_considered
13951370

1396-
return _priority_ranking
1371+
# Check whether fast-tracking routes are available for this treatment.
1372+
# If person qualifies for one don't check remaining.
1373+
# Warning: here assuming that the first fast-tracking eligibility encountered
1374+
# will determine the priority to be used. If different fast-tracking channels have
1375+
# different priorities for the same treatment, this will be a problem!
1376+
# First item in Lists is age-related, therefore need to invoke different logic.
1377+
df = self.sim.population.props
1378+
treatment_ranking = priority_ranking[hsi_event.TREATMENT_ID]
1379+
for attribute, fasttrack_code in self.list_fasttrack:
1380+
if treatment_ranking[fasttrack_code] > -1:
1381+
if attribute == 'age_exact_years':
1382+
if df.at[hsi_event.target, attribute] <= 5:
1383+
return treatment_ranking[fasttrack_code]
1384+
else:
1385+
if df.at[hsi_event.target, attribute]:
1386+
return treatment_ranking[fasttrack_code]
13971387

1398-
else: # If treatment is not ranked in the policy, issue a warning and assign priority=3 by default
1399-
warnings.warn(UserWarning(f"Couldn't find priority ranking for TREATMENT_ID \n"
1400-
f"{hsi_event.TREATMENT_ID}"))
1401-
return self.lowest_priority_considered
1388+
return treatment_ranking["Priority"]
14021389

14031390
def check_hsi_event_is_valid(self, hsi_event):
14041391
"""Check the integrity of an HSI_Event."""

0 commit comments

Comments
 (0)