"""Generate synthetic water scarcity & waterborne disease dataset for SSA.""" 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 = { "arid_groundwater_depletion": { "setting_probs": {"rural": 0.60, "peri_urban": 0.25, "urban": 0.15}, "rainfall_mean": 350, "rainfall_sd": 120, "temp_mean": 33, "temp_sd": 3, "water_stress_index_mean": 0.75, "water_stress_sd": 0.12, "improved_water_pct": 0.35, "piped_water_pct": 0.10, "open_defecation_pct": 0.30, "diarrhoea_rate_per1k": 180, "cholera_risk": 0.08, "typhoid_rate_per1k": 25, "trachoma_prev": 0.12, "child_stunting_pct": 0.35, "wash_intervention_pct": 0.15, }, "seasonal_scarcity": { "setting_probs": {"rural": 0.50, "peri_urban": 0.30, "urban": 0.20}, "rainfall_mean": 900, "rainfall_sd": 250, "temp_mean": 27, "temp_sd": 3, "water_stress_index_mean": 0.50, "water_stress_sd": 0.15, "improved_water_pct": 0.55, "piped_water_pct": 0.20, "open_defecation_pct": 0.18, "diarrhoea_rate_per1k": 120, "cholera_risk": 0.12, "typhoid_rate_per1k": 18, "trachoma_prev": 0.06, "child_stunting_pct": 0.28, "wash_intervention_pct": 0.30, }, "urban_water_crisis": { "setting_probs": {"urban": 0.55, "peri_urban": 0.30, "rural": 0.15}, "rainfall_mean": 1100, "rainfall_sd": 300, "temp_mean": 26, "temp_sd": 2.5, "water_stress_index_mean": 0.55, "water_stress_sd": 0.14, "improved_water_pct": 0.70, "piped_water_pct": 0.40, "open_defecation_pct": 0.08, "diarrhoea_rate_per1k": 85, "cholera_risk": 0.15, "typhoid_rate_per1k": 22, "trachoma_prev": 0.02, "child_stunting_pct": 0.20, "wash_intervention_pct": 0.45, }, } SCENARIO_FILES = { "arid_groundwater_depletion": "water_arid_groundwater.csv", "seasonal_scarcity": "water_seasonal_scarcity.csv", "urban_water_crisis": "water_urban_crisis.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)) month = int(rng.choice(range(1, 13))) setting = _choice(rng, params["setting_probs"]) age = int(np.clip(rng.normal(22, 18), 0, 80)) sex = rng.choice(["male", "female"], p=[0.48, 0.52]) is_child_u5 = int(age < 5) household_size = int(np.clip(rng.normal(5.5, 1.5), 2, 12)) rainfall = float(np.clip(rng.normal(params["rainfall_mean"], params["rainfall_sd"]), 50, 2500)) temp = float(np.clip(rng.normal(params["temp_mean"], params["temp_sd"]), 15, 45)) drought_index = float(np.clip(1 - rainfall / 1200, 0, 1)) water_stress = float(np.clip( rng.normal(params["water_stress_index_mean"], params["water_stress_sd"]) + drought_index * 0.15, 0, 1, )) water_source = rng.choice( ["piped", "borehole", "protected_well", "unprotected_source", "surface_water"], p=np.array([ params["piped_water_pct"], params["improved_water_pct"] * 0.4, params["improved_water_pct"] * 0.3, (1 - params["improved_water_pct"]) * 0.5, (1 - params["improved_water_pct"]) * 0.5, ]) / np.array([ params["piped_water_pct"], params["improved_water_pct"] * 0.4, params["improved_water_pct"] * 0.3, (1 - params["improved_water_pct"]) * 0.5, (1 - params["improved_water_pct"]) * 0.5, ]).sum(), ) improved_water = int(water_source in {"piped", "borehole", "protected_well"}) improved_sanitation = int(rng.random() < (1 - params["open_defecation_pct"])) handwashing_facility = int(rng.random() < (0.3 if setting == "rural" else 0.55)) water_treatment_household = int(rng.random() < 0.15) wash_score = float(np.clip( improved_water * 0.3 + improved_sanitation * 0.3 + handwashing_facility * 0.2 + water_treatment_household * 0.2, 0, 1, )) wash_intervention = int(rng.random() < params["wash_intervention_pct"]) risk_mult = 1 + water_stress * 0.5 + (1 - wash_score) * 0.4 + is_child_u5 * 0.3 diarrhoea = int(rng.random() < params["diarrhoea_rate_per1k"] / 1000 * risk_mult) cholera = int(rng.random() < params["cholera_risk"] * (1 + water_stress * 0.5) * (1 - wash_score)) typhoid = int(rng.random() < params["typhoid_rate_per1k"] / 1000 * risk_mult) trachoma = int(rng.random() < params["trachoma_prev"] * (1 + water_stress * 0.3)) any_waterborne = int(diarrhoea or cholera or typhoid) dehydration = int(diarrhoea and is_child_u5 and rng.random() < 0.25) ors_available = int(rng.random() < (0.6 if setting != "rural" else 0.35)) hospitalised = int(any_waterborne and rng.random() < 0.08) mortality = int(hospitalised and is_child_u5 and rng.random() < 0.03) litres_per_capita_day = float(np.clip( rng.normal(20 if improved_water else 10, 5) * (1 - water_stress * 0.4), 3, 50 )) water_collection_minutes = float(np.clip( rng.normal(45 if not improved_water else 15, 15), 0, 180 )) if setting == "rural" else float(np.clip(rng.normal(15, 8), 0, 60)) stunted = int(is_child_u5 and rng.random() < params["child_stunting_pct"] * (1 + water_stress * 0.2)) record = { "record_id": f"{name[:3].upper()}-{idx:05d}", "scenario": name, "year": year, "month": month, "setting": setting, "age": age, "sex": sex, "is_child_u5": is_child_u5, "household_size": household_size, "rainfall_mm": round(rainfall, 0), "temp_c": round(temp, 1), "drought_index": round(drought_index, 2), "water_stress_index": round(water_stress, 2), "water_source": water_source, "improved_water": improved_water, "improved_sanitation": improved_sanitation, "handwashing_facility": handwashing_facility, "water_treatment_household": water_treatment_household, "wash_score": round(wash_score, 2), "wash_intervention": wash_intervention, "diarrhoea": diarrhoea, "cholera": cholera, "typhoid": typhoid, "trachoma": trachoma, "any_waterborne_disease": any_waterborne, "dehydration": dehydration, "ors_available": ors_available, "hospitalised": hospitalised, "mortality": mortality, "litres_per_capita_day": round(litres_per_capita_day, 1), "water_collection_minutes": round(water_collection_minutes, 0), "stunted": stunted, } 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()