#!/usr/bin/env python3 """ Literature-Informed Stroke & Cerebrovascular Disease Dataset ============================================================== Generates realistic synthetic records of stroke patients in sub-Saharan Africa, including risk factors, stroke type, severity, imaging, treatment, and outcomes. Target population: Adults presenting with acute stroke to SSA health facilities. References (web-searched): ----------- [1] Frontiers Stroke 2025. Africa stroke incidence up to 316/100K person-years. 3-year fatality rate reaching 84%. [2] PLOS Glob Public Health 2024. 30-day in-hospital stroke CFR in SSA: pooled ~30% (range 18-40%). [3] Sierra Leone stroke register 2021. 60% ischaemic, 22% ICH, 3% SAH, 15% undetermined. Mean age 59. [4] Uganda cohort 2020. Low consciousness at admission, severity, haemorrhagic type predict 30/90-day mortality. [5] Muhimbili Tanzania 2018. 90-day mortality 50%. Thrombolysis virtually unavailable in SSA. [6] PMC 2015. Hypertension is #1 risk factor. BP control rates <10% in many SSA populations. [7] Stroke recurrence SSA 2024. Recurrence 9.4-25%. Hypertension, alcohol, prior stroke main risk factors. """ import numpy as np import pandas as pd import argparse import os SCENARIOS = { 'tertiary_hospital': { 'description': 'Tertiary hospital with CT/MRI, stroke unit, ' 'physiotherapy, some thrombolysis capability ' '(e.g., Muhimbili Tanzania, UCH Ibadan, KBTH Ghana)', 'ct_available': True, 'mri_available': True, 'thrombolysis_available': True, 'stroke_unit': True, 'physiotherapy': True, 'mortality_mod': 0.7, }, 'district_hospital': { 'description': 'District hospital, no CT, clinical diagnosis, ' 'basic BP management, no stroke unit ' '(e.g., district hospitals Malawi, Uganda)', 'ct_available': False, 'mri_available': False, 'thrombolysis_available': False, 'stroke_unit': False, 'physiotherapy': False, 'mortality_mod': 1.0, }, 'rural_facility': { 'description': 'Rural facility, no imaging, delayed referral, ' 'minimal management ' '(e.g., rural DRC, Chad, South Sudan)', 'ct_available': False, 'mri_available': False, 'thrombolysis_available': False, 'stroke_unit': False, 'physiotherapy': False, 'mortality_mod': 1.5, }, } 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 [3] ── rec['age_years'] = max(18, min(95, int(rng.normal(59, 14)))) rec['sex'] = rng.choice(['M', 'F'], p=[0.52, 0.48]) rec['rural'] = 1 if rng.random() < 0.60 else 0 # ── 2. Risk factors [6][7] ── rec['hypertension'] = 1 if rng.random() < 0.75 else 0 rec['bp_on_treatment'] = 0 if rec['hypertension']: rec['bp_on_treatment'] = 1 if rng.random() < 0.30 else 0 rec['bp_controlled'] = 0 if rec['bp_on_treatment']: rec['bp_controlled'] = 1 if rng.random() < 0.25 else 0 rec['systolic_bp'] = max(90, min(260, int(rng.normal( 160 if rec['hypertension'] and not rec['bp_controlled'] else 130, 25)))) rec['diastolic_bp'] = max(50, min(150, int(rng.normal( 95 if rec['hypertension'] and not rec['bp_controlled'] else 80, 15)))) rec['diabetes'] = 1 if rng.random() < 0.20 else 0 rec['atrial_fibrillation'] = 1 if rng.random() < 0.08 else 0 rec['smoking'] = 1 if rng.random() < 0.15 else 0 rec['alcohol_heavy'] = 1 if rng.random() < 0.20 else 0 rec['hiv_positive'] = 1 if rng.random() < 0.08 else 0 rec['bmi'] = round(max(15, min(45, rng.normal(26, 5))), 1) rec['prior_stroke'] = 1 if rng.random() < 0.12 else 0 rec['prior_tia'] = 1 if rng.random() < 0.05 else 0 rec['sickle_cell'] = 1 if rng.random() < 0.03 else 0 # ── 3. Stroke characteristics [3] ── rec['stroke_type'] = rng.choice( ['ischaemic', 'intracerebral_haemorrhage', 'subarachnoid_haemorrhage'], p=[0.60, 0.32, 0.08]) if rec['stroke_type'] == 'ischaemic': rec['ischaemic_subtype'] = rng.choice( ['large_vessel', 'small_vessel', 'cardioembolic', 'undetermined'], p=[0.30, 0.25, 0.15, 0.30]) else: rec['ischaemic_subtype'] = 'n/a' rec['nihss_score'] = max(0, min(42, int(rng.exponential(8)))) rec['gcs_score'] = max(3, min(15, int(rng.normal( 12 if rec['nihss_score'] < 15 else 8, 3)))) rec['hemiparesis'] = 1 if rng.random() < 0.75 else 0 rec['aphasia'] = 1 if rng.random() < 0.30 else 0 rec['dysphagia'] = 1 if rng.random() < 0.35 else 0 rec['visual_field_defect'] = 1 if rng.random() < 0.15 else 0 rec['neglect'] = 1 if rng.random() < 0.10 else 0 rec['onset_to_arrival_hours'] = max(1, int(rng.exponential( 24 if scenario == 'rural_facility' else 12))) # ── 4. Diagnosis ── rec['ct_performed'] = 0 if sc['ct_available']: rec['ct_performed'] = 1 if rng.random() < 0.80 else 0 rec['mri_performed'] = 0 if sc['mri_available'] and rec['ct_performed']: rec['mri_performed'] = 1 if rng.random() < 0.20 else 0 rec['ecg_done'] = 1 if rng.random() < (0.70 if sc['stroke_unit'] else 0.30) else 0 rec['echo_done'] = 1 if rng.random() < (0.30 if sc['ct_available'] else 0.05) else 0 rec['clinical_diagnosis_only'] = 1 if not rec['ct_performed'] else 0 # ── 5. Treatment [5] ── rec['thrombolysis'] = 0 if (sc['thrombolysis_available'] and rec['stroke_type'] == 'ischaemic' and rec['onset_to_arrival_hours'] <= 4.5): rec['thrombolysis'] = 1 if rng.random() < 0.10 else 0 rec['antiplatelet'] = 0 if rec['stroke_type'] == 'ischaemic': rec['antiplatelet'] = 1 if rng.random() < (0.75 if sc['ct_available'] else 0.40) else 0 rec['anticoagulant'] = 0 if rec['atrial_fibrillation'] and rec['stroke_type'] == 'ischaemic': rec['anticoagulant'] = 1 if rng.random() < 0.20 else 0 rec['antihypertensive_acute'] = 1 if rec['systolic_bp'] > 180 and rng.random() < 0.60 else 0 rec['statin'] = 1 if rng.random() < (0.40 if sc['stroke_unit'] else 0.10) else 0 rec['surgical_evacuation'] = 0 if rec['stroke_type'] == 'intracerebral_haemorrhage' and sc['ct_available']: rec['surgical_evacuation'] = 1 if rng.random() < 0.05 else 0 rec['nasogastric_feeding'] = 0 if rec['dysphagia']: rec['nasogastric_feeding'] = 1 if rng.random() < (0.50 if sc['stroke_unit'] else 0.15) else 0 rec['physiotherapy_started'] = 0 if sc['physiotherapy']: rec['physiotherapy_started'] = 1 if rng.random() < 0.50 else 0 rec['dvt_prophylaxis'] = 1 if rng.random() < (0.40 if sc['stroke_unit'] else 0.05) else 0 # ── 6. Complications ── rec['aspiration_pneumonia'] = 0 if rec['dysphagia'] and not rec['nasogastric_feeding']: rec['aspiration_pneumonia'] = 1 if rng.random() < 0.25 else 0 rec['dvt_pe'] = 1 if rng.random() < (0.03 if rec['dvt_prophylaxis'] else 0.08) else 0 rec['pressure_ulcer'] = 1 if rng.random() < 0.10 else 0 rec['seizure'] = 1 if rng.random() < 0.08 else 0 rec['haemorrhagic_transformation'] = 0 if rec['stroke_type'] == 'ischaemic': rec['haemorrhagic_transformation'] = 1 if rng.random() < 0.05 else 0 # ── 7. Outcome [2][4][5] ── base_mort = 0.25 if rec['stroke_type'] == 'intracerebral_haemorrhage': base_mort = 0.40 elif rec['stroke_type'] == 'subarachnoid_haemorrhage': base_mort = 0.45 mort = base_mort * sc['mortality_mod'] if rec['gcs_score'] <= 8: mort *= 1.8 if rec['nihss_score'] > 20: mort *= 1.5 if rec['thrombolysis']: mort *= 0.6 if rec['aspiration_pneumonia']: mort *= 1.5 if rec['age_years'] > 75: mort *= 1.3 rec['outcome_30_day'] = 'died' if rng.random() < min(mort, 0.80) else 'survived' rec['mrs_discharge'] = 5 if rec['outcome_30_day'] == 'survived': if rec['nihss_score'] < 5: rec['mrs_discharge'] = rng.choice([0, 1, 2, 3], p=[0.10, 0.25, 0.35, 0.30]) elif rec['nihss_score'] < 15: rec['mrs_discharge'] = rng.choice([2, 3, 4], p=[0.20, 0.45, 0.35]) else: rec['mrs_discharge'] = rng.choice([3, 4, 5], p=[0.15, 0.45, 0.40]) rec['disability'] = 'none' if rec['mrs_discharge'] <= 1 else ( 'mild' if rec['mrs_discharge'] == 2 else ( 'moderate' if rec['mrs_discharge'] == 3 else 'severe')) rec['hospital_days'] = max(1, min(60, int(rng.exponential(10)))) if rec['outcome_30_day'] == 'died': rec['hospital_days'] = max(1, min(30, int(rng.exponential(5)))) records.append(rec) df = pd.DataFrame(records) print(f"\n{'='*65}") print(f"Stroke — {scenario} (n={n}, seed={seed})") print(f"{'='*65}") print(f"\n Ischaemic: {(df['stroke_type']=='ischaemic').mean()*100:.1f}%") print(f" Haemorrhagic: {(df['stroke_type']=='intracerebral_haemorrhage').mean()*100:.1f}%") print(f" Hypertension: {df['hypertension'].mean()*100:.1f}%") print(f" CT performed: {df['ct_performed'].mean()*100:.1f}%") died = (df['outcome_30_day']=='died').mean()*100 print(f" 30-day mortality: {died:.1f}%") return df if __name__ == '__main__': parser = argparse.ArgumentParser( description='Generate stroke 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'stroke_{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'stroke_{args.scenario}.csv') df.to_csv(out, index=False) print(f" → Saved to {out}")