#!/usr/bin/env python3 """ Literature-Informed Asthma & COPD Dataset ========================================== Generates realistic synthetic records of chronic respiratory disease patients in sub-Saharan Africa, including asthma, COPD, risk factors, diagnosis, treatment access, and outcomes. References (web-searched): ----------- [1] PMC 2025. Household air pollution and respiratory health in Africa. Biomass = leading risk factor. [2] PMC 2022. Ambient air pollution and NCRDs in SSA. Asthma, COPD, chronic bronchitis. [3] Nature 2011. Asthma/COPD in SSA. 90% rural use biomass. Under-recognised, under-diagnosed. [4] PubMed 2011. Asthma/COPD under-treated in SSA. Priority: awareness, risk factors, surveys. [5] PubMed 2017. Biomass fuel, asthma severity, underdiagnosis in rural Nigeria. [6] PubMed 2018. COPD neglected in LMICs. Unknown in rural areas. Treatment complications. [7] PubMed 2024. CHEST-Africa observatory. CRD data for UN NCD meeting 2025. """ import numpy as np import pandas as pd import argparse import os SCENARIOS = { 'urban_respiratory_clinic': { 'description': 'Urban respiratory clinic with spirometry, ' 'peak flow, inhalers, nebulisers, oxygen ' '(e.g., Groote Schuur, Muhimbili)', 'spirometry_available': True, 'inhaler_available': True, 'oxygen_available': True, 'specialist_available': True, 'exacerbation_mort': 0.02, }, 'district_hospital': { 'description': 'District hospital with peak flow meter, ' 'salbutamol MDI, limited steroids, no ' 'spirometry (e.g., district hospitals Uganda)', 'spirometry_available': False, 'inhaler_available': True, 'oxygen_available': True, 'specialist_available': False, 'exacerbation_mort': 0.05, }, 'rural_health_centre': { 'description': 'Rural health centre, clinical diagnosis ' 'only, no inhalers, no oxygen ' '(e.g., rural DRC, Chad, Niger)', 'spirometry_available': False, 'inhaler_available': False, 'oxygen_available': False, 'specialist_available': False, 'exacerbation_mort': 0.10, }, } def generate_dataset(n=10000, seed=42, scenario='district_hospital'): rng = np.random.default_rng(seed) sc = SCENARIOS[scenario] records = [] for idx in range(n): rec = {'id': idx + 1} # ── 1. Demographics ── rec['age'] = max(5, min(85, int(rng.normal(40, 18)))) rec['sex'] = rng.choice(['M', 'F'], p=[0.48, 0.52]) rec['child'] = 1 if rec['age'] < 18 else 0 rec['urban'] = 1 if rng.random() < 0.40 else 0 rec['education'] = rng.choice( ['none', 'primary', 'secondary', 'tertiary'], p=[0.20, 0.35, 0.35, 0.10]) rec['bmi'] = round(max(14, min(45, rng.normal(23, 5))), 1) # ── 2. Risk factors [1][3][5] ── rec['biomass_fuel_cooking'] = 1 if rng.random() < (0.40 if rec['urban'] else 0.90) else 0 rec['tobacco_smoking'] = 0 if rec['age'] >= 15: rec['tobacco_smoking'] = 1 if rng.random() < (0.18 if rec['sex'] == 'M' else 0.03) else 0 rec['passive_smoking'] = 1 if rng.random() < 0.25 else 0 rec['occupational_dust'] = 0 if rec['age'] >= 18: rec['occupational_dust'] = 1 if rng.random() < 0.15 else 0 rec['outdoor_air_pollution'] = 1 if rec['urban'] and rng.random() < 0.60 else 0 rec['childhood_respiratory_infection'] = 1 if rng.random() < 0.20 else 0 rec['family_history_asthma'] = 1 if rng.random() < 0.15 else 0 rec['allergic_rhinitis'] = 1 if rng.random() < 0.20 else 0 rec['tb_history'] = 1 if rng.random() < 0.08 else 0 rec['hiv_positive'] = 1 if rng.random() < 0.06 else 0 # ── 3. Diagnosis [4][6] ── rec['diagnosis'] = rng.choice( ['asthma', 'copd', 'asthma_copd_overlap', 'chronic_bronchitis'], p=[0.45, 0.30, 0.10, 0.15]) if rec['child']: rec['diagnosis'] = 'asthma' rec['severity'] = rng.choice( ['mild', 'moderate', 'severe'], p=[0.35, 0.40, 0.25]) rec['spirometry_done'] = 0 if sc['spirometry_available']: rec['spirometry_done'] = 1 if rng.random() < 0.60 else 0 rec['peak_flow_done'] = 0 if sc['inhaler_available']: rec['peak_flow_done'] = 1 if rng.random() < 0.40 else 0 rec['fev1_percent_predicted'] = 0 if rec['spirometry_done']: if rec['diagnosis'] in ('copd', 'asthma_copd_overlap'): rec['fev1_percent_predicted'] = max(15, min(90, int(rng.normal(55, 15)))) else: rec['fev1_percent_predicted'] = max(30, min(110, int(rng.normal(75, 15)))) rec['clinical_diagnosis_only'] = 1 if not rec['spirometry_done'] and not rec['peak_flow_done'] else 0 rec['misdiagnosed'] = 0 if rec['clinical_diagnosis_only']: rec['misdiagnosed'] = 1 if rng.random() < 0.25 else 0 # ── 4. Symptoms ── rec['chronic_cough'] = 1 if rng.random() < 0.75 else 0 rec['wheezing'] = 1 if rng.random() < (0.80 if rec['diagnosis'] == 'asthma' else 0.50) else 0 rec['dyspnoea'] = 1 if rng.random() < (0.85 if rec['severity'] == 'severe' else 0.50) else 0 rec['sputum_production'] = 0 if rec['diagnosis'] in ('copd', 'chronic_bronchitis'): rec['sputum_production'] = 1 if rng.random() < 0.70 else 0 rec['nocturnal_symptoms'] = 0 if rec['diagnosis'] == 'asthma': rec['nocturnal_symptoms'] = 1 if rng.random() < 0.50 else 0 rec['exercise_limitation'] = 1 if rec['severity'] in ('moderate', 'severe') and rng.random() < 0.60 else 0 # ── 5. Treatment [4][6] ── rec['inhaler_prescribed'] = 0 if sc['inhaler_available']: rec['inhaler_prescribed'] = 1 if rng.random() < 0.75 else 0 rec['inhaler_type'] = 'none' if rec['inhaler_prescribed']: rec['inhaler_type'] = rng.choice( ['salbutamol_mdi', 'beclomethasone_mdi', 'combination', 'nebuliser'], p=[0.45, 0.25, 0.15, 0.15]) rec['inhaler_technique_correct'] = 0 if rec['inhaler_prescribed']: rec['inhaler_technique_correct'] = 1 if rng.random() < 0.35 else 0 rec['oral_steroids'] = 0 if rec['severity'] == 'severe': rec['oral_steroids'] = 1 if rng.random() < 0.50 else 0 rec['oral_theophylline'] = 0 if not rec['inhaler_prescribed'] and rec['severity'] in ('moderate', 'severe'): rec['oral_theophylline'] = 1 if rng.random() < 0.30 else 0 rec['inhaler_affordable'] = 0 if rec['inhaler_prescribed']: rec['inhaler_affordable'] = 1 if rng.random() < 0.40 else 0 rec['inhaler_available_pharmacy'] = 0 if rec['inhaler_prescribed']: rec['inhaler_available_pharmacy'] = 1 if rng.random() < (0.70 if rec['urban'] else 0.20) else 0 rec['action_plan_given'] = 0 if sc['specialist_available'] and rec['diagnosis'] == 'asthma': rec['action_plan_given'] = 1 if rng.random() < 0.40 else 0 # ── 6. Exacerbations ── rec['exacerbations_past_year'] = max(0, min(12, int(rng.exponential(1.5 if rec['severity'] == 'severe' else 0.8)))) rec['hospitalised_exacerbation'] = 0 if rec['exacerbations_past_year'] > 0: rec['hospitalised_exacerbation'] = 1 if rng.random() < 0.20 else 0 rec['emergency_visit'] = 0 if rec['exacerbations_past_year'] > 0: rec['emergency_visit'] = 1 if rng.random() < 0.30 else 0 rec['icu_admission'] = 0 if rec['hospitalised_exacerbation'] and rec['severity'] == 'severe': rec['icu_admission'] = 1 if rng.random() < 0.15 else 0 rec['oxygen_given'] = 0 if rec['hospitalised_exacerbation'] and sc['oxygen_available']: rec['oxygen_given'] = 1 if rng.random() < 0.80 else 0 # ── 7. Outcome ── mort = 0.002 if rec['severity'] == 'severe': mort = sc['exacerbation_mort'] if rec['diagnosis'] in ('copd', 'asthma_copd_overlap') and rec['age'] > 50: mort *= 1.5 if not rec['inhaler_prescribed']: mort *= 1.5 rec['died'] = 1 if rng.random() < min(mort, 0.15) else 0 rec['school_days_missed'] = 0 if rec['child'] and rec['exacerbations_past_year'] > 0: rec['school_days_missed'] = max(0, min(60, int(rng.exponential(10)))) rec['work_days_missed'] = 0 if not rec['child'] and rec['exacerbations_past_year'] > 0: rec['work_days_missed'] = max(0, min(60, int(rng.exponential(8)))) records.append(rec) df = pd.DataFrame(records) print(f"\n{'='*65}") print(f"Asthma/COPD — {scenario} (n={n}, seed={seed})") print(f"{'='*65}") print(f"\n Asthma: {(df['diagnosis']=='asthma').mean()*100:.1f}%") print(f" COPD: {(df['diagnosis']=='copd').mean()*100:.1f}%") print(f" Biomass fuel: {df['biomass_fuel_cooking'].mean()*100:.1f}%") print(f" Inhaler prescribed: {df['inhaler_prescribed'].mean()*100:.1f}%") print(f" Mortality: {df['died'].mean()*100:.2f}%") return df if __name__ == '__main__': parser = argparse.ArgumentParser( description='Generate asthma/COPD dataset') parser.add_argument('--scenario', type=str, default='district_hospital', 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'respiratory_{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'respiratory_{args.scenario}.csv') df.to_csv(out, index=False) print(f" -> Saved to {out}")