#!/usr/bin/env python3 """ Literature-Informed Preterm Birth & Kangaroo Mother Care Dataset ================================================================= Generates realistic synthetic records of preterm/low birth weight neonates in sub-Saharan Africa, including gestational age, birth weight, KMC, respiratory support, and outcomes. References (web-searched): ----------- [1] WHO 2024. Preterm birth leading cause of U5 death. KMC recommended immediately for LBW infants. [2] NEJM 2021. Immediate KMC reduces mortality by 25% in LBW infants (<2 kg) before clinical stabilisation. [3] PMC 2024. KMC effectiveness in SSA. Should start within 24h, provided 8+ hours daily. Cost-effective. [4] PubMed 2023. KMC systematic review: reduces mortality, hypothermia, sepsis, and improves breastfeeding. [5] PubMed 2023. CPAP failure in SSA: significant portion of preterms <=1200g fail CPAP. Surfactant scarce. [6] Arch Public Health 2024. Preterm neonatal mortality predictors Ethiopia: GA, birth weight, RDS, sepsis. [7] WHO Africa 2024. Neonatal mortality declining but slow. 12-fold increase needed to reach SDG targets. """ import numpy as np import pandas as pd import argparse import os SCENARIOS = { 'nicu': { 'description': 'Neonatal ICU with CPAP, surfactant, KMC unit, ' 'phototherapy, trained staff ' '(e.g., KBTH Ghana, KNH Kenya, CHUK Rwanda)', 'cpap_available': True, 'surfactant_available': True, 'kmc_unit': True, 'phototherapy': True, 'oxygen_available': True, 'mortality_mod': 0.6, }, 'special_care_nursery': { 'description': 'Special care baby unit with KMC, oxygen, ' 'no CPAP/surfactant ' '(e.g., district hospitals Malawi, Uganda)', 'cpap_available': False, 'surfactant_available': False, 'kmc_unit': True, 'phototherapy': True, 'oxygen_available': True, 'mortality_mod': 1.0, }, 'postnatal_ward': { 'description': 'Postnatal ward, no special care, limited KMC, ' 'no respiratory support ' '(e.g., rural health centres DRC, Chad)', 'cpap_available': False, 'surfactant_available': False, 'kmc_unit': False, 'phototherapy': False, 'oxygen_available': False, 'mortality_mod': 1.6, }, } def generate_dataset(n=10000, seed=42, scenario='special_care_nursery'): rng = np.random.default_rng(seed) sc = SCENARIOS[scenario] records = [] for idx in range(n): rec = {'id': idx + 1} # ── 1. Maternal factors ── rec['maternal_age'] = max(15, min(45, int(rng.normal(26, 6)))) rec['parity'] = max(0, min(10, int(rng.exponential(2)))) rec['anc_visits'] = max(0, min(8, int(rng.normal(3, 2)))) rec['antenatal_steroids'] = 0 if rec['anc_visits'] >= 2: rec['antenatal_steroids'] = 1 if rng.random() < (0.40 if scenario == 'nicu' else 0.15) else 0 rec['preeclampsia'] = 1 if rng.random() < 0.12 else 0 rec['prom'] = 1 if rng.random() < 0.20 else 0 rec['multiple_pregnancy'] = 1 if rng.random() < 0.10 else 0 rec['hiv_positive'] = 1 if rng.random() < 0.08 else 0 rec['delivery_mode'] = rng.choice( ['svd', 'caesarean', 'assisted'], p=[0.65, 0.25, 0.10]) # ── 2. Neonatal characteristics ── rec['gestational_age_weeks'] = max(24, min(36, int(rng.normal(32, 3)))) rec['sex'] = rng.choice(['M', 'F'], p=[0.52, 0.48]) ga_bw_mean = 500 + (rec['gestational_age_weeks'] - 24) * 150 rec['birth_weight_g'] = max(400, min(2500, int(rng.normal(ga_bw_mean, 250)))) rec['very_low_birth_weight'] = 1 if rec['birth_weight_g'] < 1500 else 0 rec['extremely_low_birth_weight'] = 1 if rec['birth_weight_g'] < 1000 else 0 rec['small_for_gestational_age'] = 1 if rng.random() < 0.25 else 0 rec['apgar_1min'] = max(0, min(10, int(rng.normal(6, 2)))) rec['apgar_5min'] = max(rec['apgar_1min'], min(10, int(rng.normal(7, 2)))) rec['resuscitation_needed'] = 1 if rec['apgar_1min'] < 7 else 0 rec['temperature_admission'] = round(max(33, min(37.5, rng.normal(36.0, 1.0))), 1) rec['hypothermia'] = 1 if rec['temperature_admission'] < 36.5 else 0 # ── 3. Complications [5][6] ── rds_prob = 0.40 if rec['gestational_age_weeks'] < 32 else 0.15 if rec['antenatal_steroids']: rds_prob *= 0.6 rec['rds'] = 1 if rng.random() < rds_prob else 0 rec['neonatal_sepsis'] = 0 sepsis_prob = 0.15 if rec['birth_weight_g'] < 1500: sepsis_prob *= 1.5 if rec['prom']: sepsis_prob *= 1.3 rec['neonatal_sepsis'] = 1 if rng.random() < sepsis_prob else 0 rec['nec'] = 0 if rec['birth_weight_g'] < 1500: rec['nec'] = 1 if rng.random() < 0.05 else 0 rec['ivh'] = 0 if rec['gestational_age_weeks'] < 32: rec['ivh'] = 1 if rng.random() < 0.10 else 0 rec['jaundice'] = 1 if rng.random() < 0.50 else 0 rec['apnoea'] = 0 if rec['gestational_age_weeks'] < 34: rec['apnoea'] = 1 if rng.random() < 0.20 else 0 rec['hypoglycaemia'] = 0 if rec['small_for_gestational_age'] or rec['birth_weight_g'] < 1500: rec['hypoglycaemia'] = 1 if rng.random() < 0.15 else 0 # ── 4. KMC [1][2][3][4] ── rec['kmc_initiated'] = 0 if sc['kmc_unit']: rec['kmc_initiated'] = 1 if rng.random() < 0.70 else 0 else: rec['kmc_initiated'] = 1 if rng.random() < 0.15 else 0 rec['kmc_within_24h'] = 0 if rec['kmc_initiated']: rec['kmc_within_24h'] = 1 if rng.random() < 0.50 else 0 rec['kmc_hours_per_day'] = 0 if rec['kmc_initiated']: rec['kmc_hours_per_day'] = max(1, min(20, int(rng.normal(8, 4)))) rec['kmc_continuous'] = 1 if rec['kmc_hours_per_day'] >= 20 else 0 rec['exclusive_breastfeeding'] = 0 if rec['kmc_initiated']: rec['exclusive_breastfeeding'] = 1 if rng.random() < 0.55 else 0 else: rec['exclusive_breastfeeding'] = 1 if rng.random() < 0.25 else 0 # ── 5. Respiratory support [5] ── rec['oxygen_given'] = 0 if rec['rds'] or rec['apnoea']: if sc['oxygen_available']: rec['oxygen_given'] = 1 if rng.random() < 0.80 else 0 rec['cpap_given'] = 0 if rec['rds'] and sc['cpap_available']: rec['cpap_given'] = 1 if rng.random() < 0.60 else 0 rec['surfactant_given'] = 0 if rec['rds'] and sc['surfactant_available']: rec['surfactant_given'] = 1 if rng.random() < 0.30 else 0 rec['cpap_failure'] = 0 if rec['cpap_given'] and rec['birth_weight_g'] <= 1200: rec['cpap_failure'] = 1 if rng.random() < 0.40 else 0 rec['phototherapy_given'] = 0 if rec['jaundice'] and sc['phototherapy']: rec['phototherapy_given'] = 1 if rng.random() < 0.70 else 0 rec['antibiotics_given'] = 0 if rec['neonatal_sepsis'] or rec['prom']: rec['antibiotics_given'] = 1 if rng.random() < 0.80 else 0 # ── 6. Outcome ── base_mort = 0.08 if rec['gestational_age_weeks'] < 28: base_mort = 0.50 elif rec['gestational_age_weeks'] < 32: base_mort = 0.20 elif rec['gestational_age_weeks'] < 34: base_mort = 0.10 mort = base_mort * sc['mortality_mod'] if rec['kmc_initiated']: mort *= 0.65 if rec['kmc_within_24h']: mort *= 0.80 if rec['rds'] and not rec['oxygen_given']: mort *= 2.0 if rec['neonatal_sepsis']: mort *= 1.5 if rec['nec']: mort *= 2.5 if rec['antenatal_steroids']: mort *= 0.7 if rec['cpap_given']: mort *= 0.7 if rec['surfactant_given']: mort *= 0.6 rec['neonatal_death'] = 1 if rng.random() < min(mort, 0.80) else 0 rec['died_day'] = 0 if rec['neonatal_death']: rec['died_day'] = max(0, min(28, int(rng.exponential(3)))) rec['discharge_weight_g'] = 0 if not rec['neonatal_death']: rec['discharge_weight_g'] = max(rec['birth_weight_g'], int(rec['birth_weight_g'] + rng.normal(200, 100))) rec['hospital_days'] = max(1, min(60, int(rng.exponential(10)))) records.append(rec) df = pd.DataFrame(records) print(f"\n{'='*65}") print(f"Preterm/KMC — {scenario} (n={n}, seed={seed})") print(f"{'='*65}") print(f"\n Mean GA: {df['gestational_age_weeks'].mean():.1f} weeks") print(f" Mean BW: {df['birth_weight_g'].mean():.0f}g") print(f" VLBW: {df['very_low_birth_weight'].mean()*100:.1f}%") print(f" KMC initiated: {df['kmc_initiated'].mean()*100:.1f}%") print(f" RDS: {df['rds'].mean()*100:.1f}%") print(f" Neonatal mortality: {df['neonatal_death'].mean()*100:.1f}%") return df if __name__ == '__main__': parser = argparse.ArgumentParser( description='Generate preterm/KMC dataset') parser.add_argument('--scenario', type=str, default='special_care_nursery', choices=list(SCENARIOS.keys())) parser.add_argument('--n', type=int, default=10000) parser.add_argument('--seed', type=int, default=42) parser.add_argument('--output', type=str, default=None) parser.add_argument('--all-scenarios', action='store_true') args = parser.parse_args() os.makedirs('data', exist_ok=True) if args.all_scenarios: for sc_name in SCENARIOS: df = generate_dataset(n=args.n, seed=args.seed, scenario=sc_name) out = os.path.join('data', f'preterm_{sc_name}.csv') df.to_csv(out, index=False) print(f" -> Saved to {out}\n") else: df = generate_dataset(n=args.n, seed=args.seed, scenario=args.scenario) out = args.output or os.path.join('data', f'preterm_{args.scenario}.csv') df.to_csv(out, index=False) print(f" -> Saved to {out}")