From 8ff85f7f0ea82e3c78f82f254294362136887bb4 Mon Sep 17 00:00:00 2001 From: Asif Tamuri Date: Thu, 13 Feb 2025 11:54:38 +0000 Subject: [PATCH 1/2] Access [three] attributes directly rather than via the full person record - Profiled, about 6x faster --- src/tlo/methods/contraception.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/tlo/methods/contraception.py b/src/tlo/methods/contraception.py index 76c401e3cb..0e3f6defd8 100644 --- a/src/tlo/methods/contraception.py +++ b/src/tlo/methods/contraception.py @@ -1175,14 +1175,14 @@ def _get_appt_footprint(self, current_method: str): def apply(self, person_id, squeeze_factor): """If the relevant consumable is available, do change in contraception and log it""" - person = self.sim.population.props.loc[person_id] - current_method = person.co_contraception - - if not (person.is_alive and not person.is_pregnant): + df = self.sim.population.props + if not df.at[person_id, 'is_alive'] or df.at[person_id, 'is_pregnant']: return + current_method = df.at[person_id, 'co_contraception'] + # Record the date that Family Planning Appointment happened for this person - self.sim.population.props.at[person_id, "co_date_of_last_fp_appt"] = self.sim.date + df.at[person_id, "co_date_of_last_fp_appt"] = self.sim.date # Measure weight, height and BP even if contraception not administrated self.add_equipment({ From 46893cca24665cc0adb355d0bd3dd344ebd478b2 Mon Sep 17 00:00:00 2001 From: Asif Tamuri Date: Thu, 13 Feb 2025 11:59:02 +0000 Subject: [PATCH 2/2] Optimise getting indices for persons ~3x faster --- src/tlo/methods/symptommanager.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/tlo/methods/symptommanager.py b/src/tlo/methods/symptommanager.py index 67389e283e..8b69440020 100644 --- a/src/tlo/methods/symptommanager.py +++ b/src/tlo/methods/symptommanager.py @@ -757,8 +757,7 @@ def apply(self, population): for symp in self.to_resolve.keys(): if date_today in self.to_resolve[symp]: person_ids = self.to_resolve[symp].pop(date_today) - persons = df.loc[sorted(person_ids)] - person_ids_alive = persons[persons.is_alive].index + person_ids_alive = df.index[df.index.isin(person_ids) & df.is_alive] self.module.change_symptom( person_id=person_ids_alive, add_or_remove='-',