Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maternal/Newborn health cohort model #1320

Draft
wants to merge 49 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
6a29cee
update contraception.py logging to capture df for new pregnancies
joehcollins Apr 19, 2024
50c2238
Merge branch 'master' into jcollins/mnh_cohort
joehcollins Apr 19, 2024
ad7ec65
Merge branch 'master' into jcollins/mnh_cohort
joehcollins Apr 24, 2024
19cb935
Merge branch 'master' into jcollins/mnh_cohort
joehcollins May 30, 2024
e23faaf
create cohort module and cohort resource file. updated demography log…
joehcollins May 31, 2024
460f870
updates to mnh module
joehcollins May 31, 2024
878695d
updates to mnh module
joehcollins Jun 10, 2024
12a56a7
update to cohort and util.py
joehcollins Jun 26, 2024
973cbff
update to cohort and util.py
joehcollins Jul 24, 2024
da7080b
Merge branch 'master' into jcollins/mnh_cohort
joehcollins Oct 1, 2024
06246c4
update to cohort
joehcollins Oct 2, 2024
86d130a
update to cohort
joehcollins Oct 2, 2024
3a90ab7
small updates to prevent scheduling of events in the past when sim st…
joehcollins Oct 4, 2024
930973e
new calibration file. prevent new pregnancies during cohort run.
joehcollins Oct 4, 2024
f1354c5
update scenario for azure test
joehcollins Oct 7, 2024
3c0d485
update scenario for azure test
joehcollins Oct 7, 2024
d51ff89
make cohort df come from excel
joehcollins Oct 7, 2024
0c7aa41
created mnh_outcome_logger to eventually replace logging for complica…
joehcollins Oct 7, 2024
87d6f1f
created mnh_outcome_logger to eventually replace logging for complica…
joehcollins Oct 7, 2024
cdd9842
added first logging to pregnancy supervisor
joehcollins Oct 7, 2024
b9a193d
finalising logging event
joehcollins Oct 8, 2024
ada05e0
add health system logging
joehcollins Oct 8, 2024
bfac245
old logging removed
joehcollins Oct 8, 2024
905091b
fix
joehcollins Oct 8, 2024
5a04b44
fix
joehcollins Oct 8, 2024
c62d5df
fix
joehcollins Oct 8, 2024
d8423e1
comments for clarity
joehcollins Oct 8, 2024
bfd1e1a
Merge branch 'master' into jcollins/mnh_cohort
joehcollins Oct 8, 2024
6cf3710
Merge branch 'jcollins/updated_mnh_logging' into jcollins/mnh_cohort
joehcollins Oct 8, 2024
cd3bd34
improved commenting
joehcollins Oct 8, 2024
a6b12fe
removed counting which wasnt needed
joehcollins Oct 8, 2024
5ca2314
fixes to pregnancy_supervisor test
joehcollins Oct 9, 2024
64b53c8
Merge branch 'jcollins/updated_mnh_logging' into jcollins/mnh_cohort
joehcollins Oct 9, 2024
8c6309c
Merge branch 'master' into jcollins/mnh_cohort
joehcollins Oct 9, 2024
ba22a99
fix logic in HIV causing crashes
joehcollins Oct 9, 2024
c271e72
Merge branch 'master' into jcollins/updated_mnh_logging
joehcollins Oct 10, 2024
011a001
updates to tests and new dummy analysis script
joehcollins Oct 10, 2024
09a216f
fix denom error
joehcollins Oct 10, 2024
03161e0
fix indentation error
joehcollins Oct 10, 2024
05473ff
Merge branch 'jcollins/updated_mnh_logging' into jcollins/mnh_cohort
joehcollins Oct 10, 2024
fd239bc
fix indentation error
joehcollins Oct 10, 2024
ae57c40
revert comit
joehcollins Oct 10, 2024
de3ee93
added missing logging
joehcollins Oct 10, 2024
bb7de0f
Merge branch 'jcollins/updated_mnh_logging' into jcollins/mnh_cohort
joehcollins Oct 10, 2024
7c3bf25
work on cohort
joehcollins Oct 14, 2024
53ed3bb
work on cohort
joehcollins Oct 15, 2024
0c3af64
work on cohort
joehcollins Oct 16, 2024
53fa8fa
work on cohort
joehcollins Oct 16, 2024
699b608
work on cohort
joehcollins Oct 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions resources/ResourceFile_LabourSkilledBirthAttendance.xlsx
Git LFS file not shown
3 changes: 3 additions & 0 deletions resources/ResourceFile_PregnancyCohort.xlsx
Git LFS file not shown
4 changes: 2 additions & 2 deletions resources/ResourceFile_PregnancySupervisor.xlsx
Git LFS file not shown
Git LFS file not shown
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import numpy as np
import pandas as pd

from pathlib import Path

from tlo import Date, logging
from tlo.methods import mnh_cohort_module
from tlo.methods.fullmodel import fullmodel
from tlo.scenario import BaseScenario


class BaselineScenario(BaseScenario):
def __init__(self):
super().__init__()
self.seed = 537184
self.start_date = Date(2024, 1, 1)
self.end_date = Date(2025, 1, 2)
self.pop_size = 5000
self.number_of_draws = 3
self.runs_per_draw = 10

def log_configuration(self):
return {
'filename': 'block_intervention_test', 'directory': './outputs',
"custom_levels": {
"*": logging.WARNING,
"tlo.methods.demography": logging.INFO,
"tlo.methods.demography.detail": logging.INFO,
"tlo.methods.contraception": logging.INFO,
"tlo.methods.healthsystem.summary": logging.INFO,
"tlo.methods.healthburden": logging.INFO,
"tlo.methods.labour": logging.INFO,
"tlo.methods.labour.detail": logging.INFO,
"tlo.methods.newborn_outcomes": logging.INFO,
"tlo.methods.care_of_women_during_pregnancy": logging.INFO,
"tlo.methods.pregnancy_supervisor": logging.INFO,
"tlo.methods.postnatal_supervisor": logging.INFO,
}
}

def modules(self):
return [*fullmodel(resourcefilepath=self.resources,
module_kwargs={'Schisto': {'mda_execute': False}}),
mnh_cohort_module.MaternalNewbornHealthCohort(resourcefilepath=self.resources)]

def draw_parameters(self, draw_number, rng):
interventions_for_analysis = ['blood_transfusion', 'pph_treatment_uterotonics', 'sepsis_treatment']

return {'PregnancySupervisor': {
'analysis_year': 2024,
'interventions_analysis': True,
'interventions_under_analysis':[interventions_for_analysis[draw_number-1]],
'intervention_analysis_availability': 0.0}}


if __name__ == '__main__':
from tlo.cli import scenario_run
scenario_run([__file__])
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os

import pandas as pd

from tlo.analysis.utils import extract_results, get_scenario_outputs, summarize

outputspath = './outputs/[email protected]/'
scenario_filename = 'cohort_test-2024-10-09T130546Z'
# scenario_filename2 = 'cohort_test-2024-10-15T122825Z'
scenario_filename2 = 'cohort_test-2024-10-16T071357Z'

results_folder_old = get_scenario_outputs(scenario_filename, outputspath)[-1]
results_folder_new = get_scenario_outputs(scenario_filename2, outputspath)[-1]

def get_data_frames(key, results_folder):
def sort_df(_df):
_x = _df.drop(columns=['date'], inplace=False)
return _x.iloc[0]

results_df = summarize (extract_results(
results_folder,
module="tlo.methods.pregnancy_supervisor",
key=key,
custom_generate_series=sort_df,
do_scaling=False
))

return results_df

results_old = {k:get_data_frames(k, results_folder_old) for k in
['mat_comp_incidence', 'nb_comp_incidence', 'deaths_and_stillbirths','service_coverage',
'yearly_mnh_counter_dict']}

results_new = {k:get_data_frames(k, results_folder_new) for k in
['mat_comp_incidence', 'nb_comp_incidence', 'deaths_and_stillbirths', 'service_coverage',
'yearly_mnh_counter_dict']}

import matplotlib.pyplot as plt
import numpy as np
# Sample data
mmr_data = {
'int_1': [(235, 250, 265), (335, 350, 365)],
'int_2': [(170, 195, 200), (290, 305, 320)],
'int_3': [(280, 295, 310), (295 ,310, 325)],
'int_4': [(165, 180, 195), (385, 400, 415)]
}
# Plotting
fig, ax = plt.subplots()
for key, intervals in mmr_data.items():
for idx, (lower, mean, upper) in enumerate(intervals):
x = np.arange(len(mmr_data)) * len(intervals) + idx
ax.plot(x, mean, 'o', label=f'{key}' if idx == 0 else "")
ax.fill_between([x, x], [lower, lower], [upper, upper], alpha=0.2)
ax.set_xticks(np.arange(len(mmr_data)) * len(intervals) + 0.5)
ax.set_xticklabels(mmr_data.keys())
plt.legend()
plt.show()
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import os
from pathlib import Path

import pandas as pd

from tlo import Date, Simulation, logging
from tlo.methods import mnh_cohort_module
from tlo.methods.fullmodel import fullmodel
from tlo.analysis.utils import parse_log_file


resourcefilepath = Path('./resources')
outputpath = Path("./outputs/cohort_testing") # folder for convenience of storing outputs
population_size = 2000

sim = Simulation(start_date=Date(2024, 1, 1),
seed=456,
log_config={"filename": "log_cohort_calibration",
"custom_levels": {"*": logging.DEBUG},
"directory": outputpath})

sim.register(*fullmodel(resourcefilepath=resourcefilepath),
mnh_cohort_module.MaternalNewbornHealthCohort(resourcefilepath=resourcefilepath))

sim.make_initial_population(n=population_size)
sim.simulate(end_date=Date(2025, 1, 1))

output = parse_log_file(sim.log_filepath)

# output = parse_log_file(
# '/Users/j_collins/PycharmProjects/TLOmodel/outputs/log_cohort_calibration__2024-10-04T101535.log')

# Make output dataframe
results = pd.DataFrame(columns=['model', 'data', 'source'],
index= ['deaths',
'MMR',
'DALYs',
'twins',
'ectopic',
'abortion',
'miscarriage',
'syphilis',
'anaemia_an',
'anaemia_pn'
'gdm',
'PROM',
'pre_eclampsia',
'gest-htn',
'severe_gest-htn',
'severe pre-eclampsia',
'eclampsia',
'praevia',
'abruption',
'aph',
'OL',
'UR',
'sepsis',
'PPH'])

# total_pregnancies = population_size
total_pregnancies = 2000
total_births = len(output['tlo.methods.demography']['on_birth'])
prop_live_births = (total_births/total_pregnancies) * 100

# Mortality/DALY
deaths_df = output['tlo.methods.demography']['death']
prop_deaths_df = output['tlo.methods.demography.detail']['properties_of_deceased_persons']

dir_mat_deaths = deaths_df.loc[(deaths_df['label'] == 'Maternal Disorders')]
init_indir_mat_deaths = prop_deaths_df.loc[(prop_deaths_df['is_pregnant'] | prop_deaths_df['la_is_postpartum']) &
(prop_deaths_df['cause_of_death'].str.contains('Malaria|Suicide|ever_stroke|diabetes|'
'chronic_ischemic_hd|ever_heart_attack|'
'chronic_kidney_disease') |
(prop_deaths_df['cause_of_death'] == 'TB'))]

hiv_mat_deaths = prop_deaths_df.loc[(prop_deaths_df['is_pregnant'] | prop_deaths_df['la_is_postpartum']) &
(prop_deaths_df['cause_of_death'].str.contains('AIDS_non_TB|AIDS_TB'))]

indir_mat_deaths = len(init_indir_mat_deaths) + (len(hiv_mat_deaths) * 0.3)
total_deaths = len(dir_mat_deaths) + indir_mat_deaths

# TOTAL_DEATHS
results.at['deaths', 'model'] = total_deaths
results.at['MMR', 'model'] = (total_deaths / total_births) * 100_000
results.at['DALYs', 'model'] = output['tlo.methods.healthburden']['dalys_stacked']['Maternal Disorders'].sum()

# Maternal conditions
an_comps = output['tlo.methods.pregnancy_supervisor']['maternal_complication']
la_comps = output['tlo.methods.labour']['maternal_complication']
pn_comps = output['tlo.methods.postnatal_supervisor']['maternal_complication']

twin_births = len(output['tlo.methods.newborn_outcomes']['twin_birth'])

total_completed_pregnancies = (len(an_comps.loc[an_comps['type'] == 'ectopic_unruptured']) +
len(an_comps.loc[an_comps['type'] == 'induced_abortion']) +
len(an_comps.loc[an_comps['type'] == 'spontaneous_abortion']) +
(total_births - twin_births) +
len(output['tlo.methods.pregnancy_supervisor']['antenatal_stillbirth']) +
len(output['tlo.methods.labour']['intrapartum_stillbirth']))

print(total_completed_pregnancies) # this value may be less than the starting population size due to antenatal
# maternal deaths

# Twins (todo)

# Ectopic
results.at['ectopic', 'model'] = (len(an_comps.loc[an_comps['type'] == 'ectopic_unruptured']) / total_pregnancies) * 1000
results.at['ectopic', 'data'] = 10.0
results.at['ectopic', 'source'] = 'Panelli et al.'

# Abortion


# Miscarriage

# Health system
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from tlo import Date, logging
from tlo.methods.fullmodel import fullmodel

from tlo.scenario import BaseScenario


class FullModelRunForCohort(BaseScenario):
def __init__(self):
super().__init__()
self.seed = 537184
self.start_date = Date(2010, 1, 1)
self.end_date = Date(2025, 1, 1)
self.pop_size = 200_000
self.number_of_draws = 1
self.runs_per_draw = 1

def log_configuration(self):
return {
'filename': 'fullmodel_200k_cohort', 'directory': './outputs',
"custom_levels": {
"*": logging.WARNING,
"tlo.methods.contraception": logging.DEBUG,
}
}

def modules(self):
return fullmodel(resourcefilepath=self.resources)

def draw_parameters(self, draw_number, rng):
return {}


if __name__ == '__main__':
from tlo.cli import scenario_run
scenario_run([__file__])
Loading
Loading