"""Generate synthetic urban heat island & cardiovascular risk 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 = { "megacity_tropical": { "setting_probs": {"high_density_urban": 0.45, "medium_density": 0.30, "peri_urban": 0.25}, "uhi_intensity_mean": 4.5, "uhi_intensity_sd": 1.5, "ambient_temp_mean": 32, "ambient_temp_sd": 2.5, "green_space_pct_mean": 0.08, "impervious_surface_pct": 0.70, "hypertension_prev": 0.30, "cvd_event_rate_per1k": 12, "stroke_rate_per1k": 5, "heart_failure_rate_per1k": 4, "heat_syncope_rate": 0.06, "outdoor_worker_pct": 0.40, "ac_access_pct": 0.08, }, "secondary_city_arid": { "setting_probs": {"high_density_urban": 0.30, "medium_density": 0.40, "peri_urban": 0.30}, "uhi_intensity_mean": 3.5, "uhi_intensity_sd": 1.2, "ambient_temp_mean": 35, "ambient_temp_sd": 3, "green_space_pct_mean": 0.12, "impervious_surface_pct": 0.55, "hypertension_prev": 0.25, "cvd_event_rate_per1k": 10, "stroke_rate_per1k": 4, "heart_failure_rate_per1k": 3.5, "heat_syncope_rate": 0.08, "outdoor_worker_pct": 0.50, "ac_access_pct": 0.05, }, "highland_urbanizing": { "setting_probs": {"high_density_urban": 0.25, "medium_density": 0.35, "peri_urban": 0.40}, "uhi_intensity_mean": 2.5, "uhi_intensity_sd": 1.0, "ambient_temp_mean": 24, "ambient_temp_sd": 3, "green_space_pct_mean": 0.18, "impervious_surface_pct": 0.40, "hypertension_prev": 0.22, "cvd_event_rate_per1k": 8, "stroke_rate_per1k": 3.5, "heart_failure_rate_per1k": 3, "heat_syncope_rate": 0.03, "outdoor_worker_pct": 0.35, "ac_access_pct": 0.03, }, } SCENARIO_FILES = { "megacity_tropical": "uhi_megacity_tropical.csv", "secondary_city_arid": "uhi_secondary_city_arid.csv", "highland_urbanizing": "uhi_highland_urbanizing.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(45, 15), 18, 85)) sex = rng.choice(["male", "female"], p=[0.48, 0.52]) is_elderly = int(age >= 60) ambient_temp = float(np.clip(rng.normal(params["ambient_temp_mean"], params["ambient_temp_sd"]), 15, 48)) uhi_intensity = float(np.clip(rng.normal(params["uhi_intensity_mean"], params["uhi_intensity_sd"]), 0, 10)) if setting == "peri_urban": uhi_intensity *= 0.5 experienced_temp = float(ambient_temp + uhi_intensity) heatwave = int(experienced_temp > ambient_temp + 5) nighttime_temp = float(np.clip(experienced_temp - rng.normal(6, 1.5), 15, 38)) nighttime_uhi = int(nighttime_temp > 25) green_space_access = float(np.clip( rng.normal(params["green_space_pct_mean"], 0.05) * (0.5 if setting == "high_density_urban" else 1.2), 0, 0.5, )) impervious_surface = float(np.clip( rng.normal(params["impervious_surface_pct"], 0.1), 0.1, 0.95, )) outdoor_worker = int(rng.random() < params["outdoor_worker_pct"]) ac_access = int(rng.random() < params["ac_access_pct"]) fan_access = int(rng.random() < 0.35) hydration_adequate = int(rng.random() < 0.50) hypertension = int(rng.random() < params["hypertension_prev"] * (1 + is_elderly * 0.3)) diabetes = int(rng.random() < 0.08 * (1 + is_elderly * 0.3)) obesity = int(rng.random() < 0.12) smoker = int(rng.random() < 0.10) pre_existing_cvd = int(rng.random() < 0.05 * (1 + is_elderly * 0.5)) heat_risk = float(np.clip( (experienced_temp - 28) / 15 + is_elderly * 0.15 + hypertension * 0.15 + outdoor_worker * 0.1 - ac_access * 0.1 - green_space_access * 0.2 + nighttime_uhi * 0.1, 0, 1, )) cvd_event = int(rng.random() < params["cvd_event_rate_per1k"] / 1000 * (1 + heat_risk * 1.5)) stroke = int(rng.random() < params["stroke_rate_per1k"] / 1000 * (1 + heat_risk * 1.3)) heart_failure_exacerbation = int( pre_existing_cvd and rng.random() < params["heart_failure_rate_per1k"] / 1000 * (1 + heat_risk * 2) ) heat_syncope = int(rng.random() < params["heat_syncope_rate"] * (1 + heat_risk)) arrhythmia = int(rng.random() < 0.02 * (1 + heat_risk * 1.5)) any_cv_outcome = int(cvd_event or stroke or heart_failure_exacerbation or arrhythmia) systolic_bp = float(np.clip( rng.normal(130 if hypertension else 120, 12) + heat_risk * 8, 85, 200, )) er_visit = int(any_cv_outcome and rng.random() < 0.30) hospitalised = int(er_visit and rng.random() < 0.50) mortality = int(hospitalised and rng.random() < 0.08) record = { "record_id": f"{name[:3].upper()}-{idx:05d}", "scenario": name, "year": year, "month": month, "setting": setting, "age": age, "sex": sex, "is_elderly": is_elderly, "ambient_temp_c": round(ambient_temp, 1), "uhi_intensity_c": round(uhi_intensity, 1), "experienced_temp_c": round(experienced_temp, 1), "heatwave": heatwave, "nighttime_temp_c": round(nighttime_temp, 1), "nighttime_uhi": nighttime_uhi, "green_space_access": round(green_space_access, 2), "impervious_surface_pct": round(impervious_surface, 2), "outdoor_worker": outdoor_worker, "ac_access": ac_access, "fan_access": fan_access, "hydration_adequate": hydration_adequate, "hypertension": hypertension, "diabetes": diabetes, "obesity": obesity, "smoker": smoker, "pre_existing_cvd": pre_existing_cvd, "heat_risk_score": round(heat_risk, 3), "cvd_event": cvd_event, "stroke": stroke, "heart_failure_exacerbation": heart_failure_exacerbation, "heat_syncope": heat_syncope, "arrhythmia": arrhythmia, "any_cv_outcome": any_cv_outcome, "systolic_bp_mmhg": round(systolic_bp, 0), "er_visit": er_visit, "hospitalised": hospitalised, "mortality": mortality, } 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()