"""Generate synthetic visual impairment & low vision services dataset for SSA. Research-based parameterization: - WHO: 26M visually impaired in Africa; 4.8M blind; 80% avoidable. - Lancet Global Health: Cataract leading cause (50%+), followed by uncorrected refractive error, glaucoma, trachoma. - IAPB Vision Atlas: SSA has lowest ophthalmologist density; 2.7 per million vs 79 per million in high-income countries. - PMC6210163: Access to vision rehabilitation services 3-62% in LMICs. """ from __future__ import annotations from pathlib import Path import numpy as np import pandas as pd SEED = 42 N_PER_SCENARIO = 10_000 YEAR_RANGE = np.arange(2010, 2025) YEAR_WEIGHTS = np.linspace(0.85, 1.3, len(YEAR_RANGE)) YEAR_WEIGHTS = YEAR_WEIGHTS / YEAR_WEIGHTS.sum() SCENARIOS = { "urban_eye_clinic": { "setting_probs": {"eye_hospital": 0.30, "ophthalmology_dept": 0.25, "optometry_clinic": 0.25, "private_eye_clinic": 0.20}, "cause_probs": {"cataract": 0.35, "refractive_error": 0.25, "glaucoma": 0.12, "diabetic_retinopathy": 0.08, "trachoma": 0.05, "childhood_blindness": 0.05, "corneal_opacity": 0.05, "other": 0.05}, "service_access_pct": 0.55, "spectacle_coverage": 0.35, "cataract_surgery_rate": 0.40, "ophthalmologist_available": 0.60, }, "district_outreach": { "setting_probs": {"district_hospital": 0.30, "health_centre": 0.25, "outreach_camp": 0.25, "school_screening": 0.20}, "cause_probs": {"cataract": 0.40, "refractive_error": 0.20, "trachoma": 0.10, "glaucoma": 0.08, "corneal_opacity": 0.08, "childhood_blindness": 0.05, "river_blindness": 0.04, "other": 0.05}, "service_access_pct": 0.25, "spectacle_coverage": 0.12, "cataract_surgery_rate": 0.15, "ophthalmologist_available": 0.15, }, "rural_community": { "setting_probs": {"health_post": 0.30, "community_screening": 0.25, "traditional_healer": 0.15, "home_visit": 0.30}, "cause_probs": {"cataract": 0.45, "trachoma": 0.15, "refractive_error": 0.15, "river_blindness": 0.08, "glaucoma": 0.05, "corneal_opacity": 0.05, "childhood_blindness": 0.04, "other": 0.03}, "service_access_pct": 0.10, "spectacle_coverage": 0.05, "cataract_surgery_rate": 0.05, "ophthalmologist_available": 0.03, }, } SCENARIO_FILES = { "urban_eye_clinic": "vision_urban.csv", "district_outreach": "vision_district.csv", "rural_community": "vision_rural.csv", } def _choice(rng, prob_map): keys = list(prob_map.keys()) weights = np.array(list(prob_map.values()), dtype=float) weights = weights / weights.sum() return rng.choice(keys, p=weights) def _simulate_scenario(name, params, seed): rng = np.random.default_rng(seed) records = [] for idx in range(N_PER_SCENARIO): year = int(rng.choice(YEAR_RANGE, p=YEAR_WEIGHTS)) setting = _choice(rng, params["setting_probs"]) age = int(np.clip(rng.normal(45, 20), 0, 90)) sex = rng.choice(["male", "female"], p=[0.45, 0.55]) is_child = int(age < 15) cause = _choice(rng, params["cause_probs"]) severity = rng.choice(["mild_VI", "moderate_VI", "severe_VI", "blind"], p=[0.30, 0.30, 0.25, 0.15]) bilateral = int(rng.random() < 0.60) visual_acuity_better_eye = float(np.clip( rng.lognormal(np.log(0.3), 0.8), 0.01, 1.0)) avoidable = int(cause in ("cataract", "refractive_error", "trachoma", "river_blindness", "corneal_opacity")) # Service access screened = int(rng.random() < params["service_access_pct"]) referred = int(screened and rng.random() < 0.50) referral_completed = int(referred and rng.random() < 0.34) ophthalmologist_seen = int(referral_completed and rng.random() < params["ophthalmologist_available"]) # Interventions spectacles_needed = int(cause == "refractive_error" or rng.random() < 0.30) spectacles_received = int(spectacles_needed and rng.random() < params["spectacle_coverage"]) cataract_surgery_needed = int(cause == "cataract" and severity in ("severe_VI", "blind")) cataract_surgery_done = int(cataract_surgery_needed and rng.random() < params["cataract_surgery_rate"]) surgery_outcome_good = int(cataract_surgery_done and rng.random() < 0.80) low_vision_device = int(severity in ("severe_VI", "blind") and rng.random() < 0.05) orientation_mobility_training = int(severity == "blind" and rng.random() < 0.08) # Barriers cost_barrier = int(rng.random() < 0.50) distance_barrier = int(rng.random() < 0.40) awareness_barrier = int(rng.random() < 0.45) fear_surgery = int(cataract_surgery_needed and rng.random() < 0.30) # Outcomes vision_improved = int((spectacles_received or surgery_outcome_good) and rng.random() < 0.75) school_performance = int(is_child and (vision_improved or not spectacles_needed)) employment_impacted = int(age >= 18 and severity in ("severe_VI", "blind") and not vision_improved) quality_of_life_improved = int(vision_improved and rng.random() < 0.70) unmet_need = int((spectacles_needed and not spectacles_received) or (cataract_surgery_needed and not cataract_surgery_done)) record = { "record_id": f"{name[:3].upper()}-{idx:05d}", "scenario": name, "year": year, "setting": setting, "age": age, "sex": sex, "cause": cause, "severity": severity, "bilateral": bilateral, "avoidable": avoidable, "screened": screened, "referred": referred, "referral_completed": referral_completed, "ophthalmologist_seen": ophthalmologist_seen, "spectacles_needed": spectacles_needed, "spectacles_received": spectacles_received, "cataract_surgery_needed": cataract_surgery_needed, "cataract_surgery_done": cataract_surgery_done, "surgery_outcome_good": surgery_outcome_good, "low_vision_device": low_vision_device, "cost_barrier": cost_barrier, "distance_barrier": distance_barrier, "awareness_barrier": awareness_barrier, "vision_improved": vision_improved, "employment_impacted": employment_impacted, "quality_of_life_improved": quality_of_life_improved, "unmet_need": unmet_need, } records.append(record) return pd.DataFrame(records) def main(): output_dir = Path("data") output_dir.mkdir(parents=True, exist_ok=True) for idx, (name, params) in enumerate(SCENARIOS.items()): df = _simulate_scenario(name, params, SEED + idx * 211) df.to_csv(output_dir / SCENARIO_FILES[name], index=False) print(f"Saved {name} -> {SCENARIO_FILES[name]}") if __name__ == "__main__": main()