#!/usr/bin/env python3 """ Literature-Informed Hearing Loss & Ear Disease Dataset ======================================================= Generates realistic synthetic records of ear disease and hearing loss patients in sub-Saharan Africa, including otitis media, hearing assessment, hearing aids, and outcomes. References (web-searched): ----------- [1] PMC 2017. CSOM prevalence in school-age children Malawi. Population-representative sample. [2] Cambridge 2024. Global burden hearing impairment. Highest incidence otitis media in LMICs. [3] Frontiers 2024. Global burden otitis media 204 countries. SSA disproportionate burden. [4] WHO AFRO. Up to 88% of cases preventable. <1 ENT per million in most SSA countries. [5] ScienceDirect 2019. 56.6% fail rate school ear exams. OM with effusion most common. """ import numpy as np import pandas as pd import argparse import os SCENARIOS = { 'ent_specialist_centre': { 'description': 'ENT specialist centre with audiometry, ' 'tympanometry, hearing aids, surgery ' '(e.g., Groote Schuur, LUTH)', 'audiometry_available': True, 'hearing_aids_available': True, 'surgery_available': True, 'ent_specialist': True, }, 'district_hospital': { 'description': 'District hospital with otoscopy, basic ' 'tuning fork tests, ear syringing, ' 'antibiotics (e.g., district hospitals)', 'audiometry_available': False, 'hearing_aids_available': False, 'surgery_available': False, 'ent_specialist': False, }, 'rural_health_centre': { 'description': 'Rural health centre, clinical diagnosis ' 'only, no otoscope often, traditional ' 'remedies common (e.g., rural DRC, Niger)', 'audiometry_available': False, 'hearing_aids_available': False, 'surgery_available': False, 'ent_specialist': False, }, } 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(0, min(90, int(rng.exponential(20) + 2))) rec['sex'] = rng.choice(['M', 'F'], p=[0.52, 0.48]) rec['child'] = 1 if rec['age'] < 15 else 0 rec['urban'] = 1 if rng.random() < 0.35 else 0 rec['education'] = rng.choice( ['none', 'primary', 'secondary', 'tertiary'], p=[0.25, 0.35, 0.30, 0.10]) # ── 2. Ear condition ── rec['ear_condition'] = rng.choice( ['csom', 'aom', 'ome', 'wax_impaction', 'foreign_body', 'noise_induced', 'presbycusis', 'congenital', 'ototoxicity'], p=[0.25, 0.15, 0.12, 0.10, 0.05, 0.08, 0.12, 0.05, 0.08]) if rec['child']: rec['ear_condition'] = rng.choice( ['csom', 'aom', 'ome', 'foreign_body', 'congenital', 'wax_impaction'], p=[0.30, 0.25, 0.15, 0.10, 0.10, 0.10]) elif rec['age'] > 60: rec['ear_condition'] = rng.choice( ['presbycusis', 'csom', 'wax_impaction', 'noise_induced', 'ototoxicity'], p=[0.40, 0.20, 0.15, 0.15, 0.10]) rec['laterality'] = rng.choice(['unilateral', 'bilateral'], p=[0.55, 0.45]) rec['duration_months'] = max(0, min(240, int(rng.exponential(12)))) # ── 3. Risk factors ── rec['recurrent_ari'] = 1 if rec['child'] and rng.random() < 0.40 else 0 rec['malnutrition'] = 1 if rec['child'] and rng.random() < 0.20 else 0 rec['overcrowding'] = 1 if rng.random() < 0.45 else 0 rec['noise_exposure'] = 0 if rec['age'] >= 15: rec['noise_exposure'] = 1 if rng.random() < 0.15 else 0 rec['ototoxic_medication'] = 0 if rec['ear_condition'] == 'ototoxicity' or rng.random() < 0.05: rec['ototoxic_medication'] = 1 rec['hiv_positive'] = 1 if rng.random() < 0.06 else 0 rec['traditional_ear_drops'] = 1 if rng.random() < 0.25 else 0 # ── 4. Symptoms ── rec['ear_discharge'] = 0 if rec['ear_condition'] in ('csom', 'aom'): rec['ear_discharge'] = 1 if rng.random() < 0.80 else 0 rec['ear_pain'] = 0 if rec['ear_condition'] in ('aom', 'foreign_body'): rec['ear_pain'] = 1 if rng.random() < 0.85 else 0 rec['hearing_difficulty'] = 1 if rng.random() < 0.70 else 0 rec['tinnitus'] = 1 if rng.random() < 0.25 else 0 rec['vertigo'] = 1 if rng.random() < 0.10 else 0 # ── 5. Assessment ── rec['otoscopy_done'] = 1 if rng.random() < (0.90 if sc['ent_specialist'] else 0.50) else 0 rec['tympanic_membrane_perforation'] = 0 if rec['ear_condition'] == 'csom': rec['tympanic_membrane_perforation'] = 1 if rng.random() < 0.85 else 0 rec['audiometry_done'] = 0 if sc['audiometry_available']: rec['audiometry_done'] = 1 if rng.random() < 0.60 else 0 rec['hearing_loss_severity'] = 'normal' if rec['hearing_difficulty']: rec['hearing_loss_severity'] = rng.choice( ['mild', 'moderate', 'severe', 'profound'], p=[0.35, 0.30, 0.20, 0.15]) rec['hearing_loss_type'] = 'none' if rec['hearing_loss_severity'] != 'normal': if rec['ear_condition'] in ('csom', 'aom', 'ome', 'wax_impaction'): rec['hearing_loss_type'] = 'conductive' elif rec['ear_condition'] in ('noise_induced', 'presbycusis', 'ototoxicity', 'congenital'): rec['hearing_loss_type'] = 'sensorineural' else: rec['hearing_loss_type'] = rng.choice( ['conductive', 'sensorineural', 'mixed'], p=[0.40, 0.40, 0.20]) # ── 6. Treatment ── rec['antibiotics_given'] = 0 if rec['ear_condition'] in ('csom', 'aom'): rec['antibiotics_given'] = 1 if rng.random() < 0.70 else 0 rec['ear_drops_given'] = 0 if rec['ear_condition'] in ('csom', 'aom'): rec['ear_drops_given'] = 1 if rng.random() < 0.60 else 0 rec['ear_syringing'] = 0 if rec['ear_condition'] == 'wax_impaction': rec['ear_syringing'] = 1 if rng.random() < 0.60 else 0 rec['foreign_body_removal'] = 0 if rec['ear_condition'] == 'foreign_body': rec['foreign_body_removal'] = 1 if rng.random() < 0.70 else 0 rec['surgery_performed'] = 0 if sc['surgery_available'] and rec['ear_condition'] == 'csom' and rec['tympanic_membrane_perforation']: rec['surgery_performed'] = 1 if rng.random() < 0.20 else 0 rec['surgery_type'] = 'none' if rec['surgery_performed']: rec['surgery_type'] = rng.choice( ['tympanoplasty', 'mastoidectomy', 'myringoplasty'], p=[0.45, 0.30, 0.25]) rec['hearing_aid_fitted'] = 0 if sc['hearing_aids_available'] and rec['hearing_loss_severity'] in ('moderate', 'severe', 'profound'): rec['hearing_aid_fitted'] = 1 if rng.random() < 0.15 else 0 rec['referred_ent'] = 0 if not sc['ent_specialist'] and rec['ear_condition'] in ('csom', 'congenital'): rec['referred_ent'] = 1 if rng.random() < 0.30 else 0 rec['referral_completed'] = 0 if rec['referred_ent']: rec['referral_completed'] = 1 if rng.random() < 0.20 else 0 # ── 7. Outcome ── rec['hearing_improved'] = 0 if rec['ear_condition'] in ('wax_impaction', 'foreign_body', 'ome') and ( rec['ear_syringing'] or rec['foreign_body_removal']): rec['hearing_improved'] = 1 if rng.random() < 0.80 else 0 elif rec['surgery_performed']: rec['hearing_improved'] = 1 if rng.random() < 0.60 else 0 elif rec['hearing_aid_fitted']: rec['hearing_improved'] = 1 if rng.random() < 0.70 else 0 rec['speech_development_affected'] = 0 if rec['child'] and rec['hearing_loss_severity'] in ('moderate', 'severe', 'profound'): rec['speech_development_affected'] = 1 if rng.random() < 0.60 else 0 rec['school_performance_affected'] = 0 if rec['child'] and rec['hearing_difficulty']: rec['school_performance_affected'] = 1 if rng.random() < 0.40 else 0 rec['complication'] = 0 if rec['ear_condition'] == 'csom' and not rec['antibiotics_given']: rec['complication'] = 1 if rng.random() < 0.10 else 0 rec['complication_type'] = 'none' if rec['complication']: rec['complication_type'] = rng.choice( ['mastoiditis', 'meningitis', 'brain_abscess', 'cholesteatoma'], p=[0.40, 0.15, 0.10, 0.35]) records.append(rec) df = pd.DataFrame(records) print(f"\n{'='*65}") print(f"Hearing/Ear — {scenario} (n={n}, seed={seed})") print(f"{'='*65}") print(f"\n CSOM: {(df['ear_condition']=='csom').mean()*100:.1f}%") print(f" Hearing difficulty: {df['hearing_difficulty'].mean()*100:.1f}%") print(f" Audiometry done: {df['audiometry_done'].mean()*100:.1f}%") print(f" Hearing aid: {df['hearing_aid_fitted'].mean()*100:.1f}%") print(f" ENT referral: {df['referred_ent'].mean()*100:.1f}%") return df if __name__ == '__main__': parser = argparse.ArgumentParser( description='Generate hearing loss/ear disease 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'ear_{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'ear_{args.scenario}.csv') df.to_csv(out, index=False) print(f" -> Saved to {out}")