"""Generate synthetic stroke rehabilitation & recovery dataset for SSA. Research-based parameterization: - Lancet Neurology: Stroke incidence increasing in SSA; 70% of stroke survivors have disability; <5% access rehabilitation. - WHO: Stroke is leading cause of adult disability globally; SSA has youngest stroke patients (mean age 50-55 vs 70+ in HIC). - PMC: Physiotherapy coverage for stroke <10% in most SSA countries; speech therapy almost non-existent at district level. """ 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_stroke_unit": { "setting_probs": {"stroke_unit": 0.25, "tertiary_hospital": 0.30, "rehab_centre": 0.20, "private_hospital": 0.25}, "stroke_type_probs": {"ischaemic": 0.65, "haemorrhagic": 0.30, "TIA": 0.05}, "acute_survival": 0.80, "rehab_access_pct": 0.35, "physiotherapy_pct": 0.30, "speech_therapy_pct": 0.10, "ct_scan_done_pct": 0.60, }, "district_hospital": { "setting_probs": {"district_hospital": 0.40, "regional_hospital": 0.25, "health_centre": 0.20, "home": 0.15}, "stroke_type_probs": {"ischaemic": 0.55, "haemorrhagic": 0.35, "TIA": 0.10}, "acute_survival": 0.60, "rehab_access_pct": 0.08, "physiotherapy_pct": 0.05, "speech_therapy_pct": 0.01, "ct_scan_done_pct": 0.15, }, "rural_community": { "setting_probs": {"health_post": 0.25, "home": 0.40, "traditional_healer": 0.15, "community": 0.20}, "stroke_type_probs": {"ischaemic": 0.50, "haemorrhagic": 0.40, "TIA": 0.10}, "acute_survival": 0.40, "rehab_access_pct": 0.02, "physiotherapy_pct": 0.01, "speech_therapy_pct": 0.00, "ct_scan_done_pct": 0.03, }, } SCENARIO_FILES = { "urban_stroke_unit": "stroke_urban.csv", "district_hospital": "stroke_district.csv", "rural_community": "stroke_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(55, 14), 20, 90)) sex = rng.choice(["male", "female"], p=[0.55, 0.45]) stroke_type = _choice(rng, params["stroke_type_probs"]) risk_factors = { "hypertension": int(rng.random() < 0.70), "diabetes": int(rng.random() < 0.25), "smoking": int(rng.random() < 0.15), "atrial_fibrillation": int(rng.random() < 0.08), "hiv_positive": int(rng.random() < 0.10), } side_affected = rng.choice(["left", "right", "bilateral"], p=[0.45, 0.45, 0.10]) # Acute care survived = int(rng.random() < params["acute_survival"]) ct_scan = int(survived and rng.random() < params["ct_scan_done_pct"]) thrombolysis = int(survived and stroke_type == "ischaemic" and ct_scan and rng.random() < 0.03) time_to_hospital_hrs = float(np.clip(rng.exponential(12), 1, 72)) icu_admission = int(survived and rng.random() < 0.20) # Disability hemiplegia = int(survived and rng.random() < 0.55) aphasia = int(survived and rng.random() < 0.25) dysphagia = int(survived and rng.random() < 0.30) cognitive_impairment = int(survived and rng.random() < 0.25) depression_post = int(survived and rng.random() < 0.30) # Rehabilitation rehab_received = int(survived and rng.random() < params["rehab_access_pct"]) physiotherapy = int(survived and rng.random() < params["physiotherapy_pct"]) occupational_therapy = int(rehab_received and rng.random() < 0.15) speech_therapy = int(aphasia and rng.random() < params["speech_therapy_pct"]) early_mobilisation = int(survived and rng.random() < 0.20) # Outcomes functional_recovery = int(rehab_received and rng.random() < 0.45) independent_adl = int(survived and rng.random() < (0.40 if rehab_received else 0.15)) walking_ability = int(survived and rng.random() < (0.50 if physiotherapy else 0.20)) return_to_work = int(survived and age < 65 and rng.random() < (0.15 if rehab_received else 0.03)) recurrent_stroke = int(survived and rng.random() < 0.10) one_year_mortality = int(survived and rng.random() < (0.15 if rehab_received else 0.35)) # Secondary prevention antihypertensive = int(survived and rng.random() < 0.50) antiplatelet = int(survived and stroke_type == "ischaemic" and rng.random() < 0.35) statin = int(survived and rng.random() < 0.20) bp_controlled = int(antihypertensive and rng.random() < 0.30) caregiver_burden = int(survived and hemiplegia and rng.random() < 0.60) record = { "record_id": f"{name[:3].upper()}-{idx:05d}", "scenario": name, "year": year, "setting": setting, "age": age, "sex": sex, "stroke_type": stroke_type, "hypertension": risk_factors["hypertension"], "diabetes": risk_factors["diabetes"], "hiv_positive": risk_factors["hiv_positive"], "survived": survived, "ct_scan": ct_scan, "thrombolysis": thrombolysis, "hemiplegia": hemiplegia, "aphasia": aphasia, "dysphagia": dysphagia, "depression_post": depression_post, "rehab_received": rehab_received, "physiotherapy": physiotherapy, "speech_therapy": speech_therapy, "early_mobilisation": early_mobilisation, "functional_recovery": functional_recovery, "independent_adl": independent_adl, "walking_ability": walking_ability, "return_to_work": return_to_work, "recurrent_stroke": recurrent_stroke, "one_year_mortality": one_year_mortality, "antihypertensive": antihypertensive, "bp_controlled": bp_controlled, "caregiver_burden": caregiver_burden, } 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()