"""Validate synthetic urban heat island & cardiovascular risk dataset.""" from __future__ import annotations from pathlib import Path import matplotlib.pyplot as plt import pandas as pd SCENARIO_FILES = { "megacity_tropical": "uhi_megacity_tropical.csv", "secondary_city_arid": "uhi_secondary_city_arid.csv", "highland_urbanizing": "uhi_highland_urbanizing.csv", } COLORS = {"megacity_tropical": "#e6550d", "secondary_city_arid": "#d95f02", "highland_urbanizing": "#31a354"} def load_data() -> pd.DataFrame: frames = [] for scenario, filename in SCENARIO_FILES.items(): df = pd.read_csv(Path("data") / filename) frames.append(df) return pd.concat(frames, ignore_index=True) def plot_validation(df: pd.DataFrame, output_path: Path) -> None: fig, axes = plt.subplots(4, 2, figsize=(14, 16)) axes = axes.flatten() for s in SCENARIO_FILES: subset = df[df["scenario"] == s] axes[0].hist(subset["experienced_temp_c"], bins=30, alpha=0.5, color=COLORS[s], label=s) axes[0].set_title("Experienced Temperature Distribution") axes[0].set_xlabel("Temperature (°C)") axes[0].legend(fontsize=7) cv_cols = ["cvd_event", "stroke", "arrhythmia", "heat_syncope"] rates = df.groupby("scenario")[cv_cols].mean() * 100 rates.plot(kind="bar", ax=axes[1]) axes[1].set_title("CV Event Rates (%)") axes[1].legend(fontsize=7) for s in SCENARIO_FILES: subset = df[df["scenario"] == s] axes[2].scatter(subset["heat_risk_score"], subset["any_cv_outcome"], s=6, alpha=0.1, color=COLORS[s], label=s) axes[2].set_title("Heat Risk Score vs CV Outcome") axes[2].legend(fontsize=7) uhi_data = [df[df["scenario"] == s]["uhi_intensity_c"] for s in SCENARIO_FILES] bp = axes[3].boxplot(uhi_data, tick_labels=SCENARIO_FILES.keys(), patch_artist=True) for patch, s in zip(bp["boxes"], SCENARIO_FILES.keys()): patch.set_facecolor(COLORS[s]) patch.set_alpha(0.6) axes[3].set_title("UHI Intensity by Scenario") axes[3].set_ylabel("°C above ambient") risk_cols = ["hypertension", "diabetes", "obesity", "pre_existing_cvd"] risk = df.groupby("scenario")[risk_cols].mean() * 100 risk.plot(kind="bar", ax=axes[4]) axes[4].set_title("CVD Risk Factor Prevalence (%)") axes[4].legend(fontsize=7) cool_cols = ["ac_access", "fan_access", "green_space_access"] cool = df.groupby("scenario")[["ac_access", "fan_access"]].mean() * 100 cool["green_space"] = df.groupby("scenario")["green_space_access"].mean() * 100 cool.plot(kind="bar", ax=axes[5]) axes[5].set_title("Cooling Access (%)") axes[5].legend(fontsize=7) bp_data = [df[df["scenario"] == s]["systolic_bp_mmhg"] for s in SCENARIO_FILES] bp2 = axes[6].boxplot(bp_data, tick_labels=SCENARIO_FILES.keys(), patch_artist=True) for patch, s in zip(bp2["boxes"], SCENARIO_FILES.keys()): patch.set_facecolor(COLORS[s]) patch.set_alpha(0.6) axes[6].set_title("Systolic BP Distribution") axes[6].set_ylabel("mmHg") cascade_cols = ["any_cv_outcome", "er_visit", "hospitalised", "mortality"] cascade = df.groupby("scenario")[cascade_cols].mean() * 100 cascade.plot(kind="bar", ax=axes[7]) axes[7].set_title("Health Outcomes Cascade (%)") axes[7].legend(fontsize=7) plt.tight_layout() fig.savefig(output_path, dpi=200) plt.close(fig) def main() -> None: df = load_data() plot_validation(df, Path("validation_report.png")) print("Saved validation_report.png") if __name__ == "__main__": main()