File size: 3,300 Bytes
8d77690
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""Validate synthetic noise pollution & urban health dataset."""

from __future__ import annotations

from pathlib import Path

import matplotlib.pyplot as plt
import pandas as pd

SCENARIO_FILES = {
    "megacity_traffic": "noise_megacity.csv",
    "secondary_city_mixed": "noise_secondary_city.csv",
    "periurban_emerging": "noise_periurban.csv",
}

COLORS = {"megacity_traffic": "#e6550d", "secondary_city_mixed": "#756bb1", "periurban_emerging": "#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["lden_db"], bins=40, alpha=0.5, color=COLORS[s], label=s)
    axes[0].axvline(53, color="red", ls="--", lw=1, label="WHO 53 dB")
    axes[0].set_title("Lden Noise Distribution (dB)")
    axes[0].legend(fontsize=6)

    exc_cols = ["exceeds_who_lden_53", "exceeds_who_lnight_45", "exceeds_85db"]
    exc = df.groupby("scenario")[exc_cols].mean() * 100
    exc.plot(kind="bar", ax=axes[1])
    axes[1].set_title("WHO Guideline Exceedance (%)")
    axes[1].legend(fontsize=7)

    health_cols = ["hearing_loss", "tinnitus", "hypertension", "cardiovascular"]
    health = df.groupby("scenario")[health_cols].mean() * 100
    health.plot(kind="bar", ax=axes[2])
    axes[2].set_title("Physical Health Outcomes (%)")
    axes[2].legend(fontsize=7)

    mental_cols = ["sleep_disturbance", "annoyance", "stress_anxiety", "concentration_difficulty"]
    mental = df.groupby("scenario")[mental_cols].mean() * 100
    mental.plot(kind="bar", ax=axes[3])
    axes[3].set_title("Mental Health & Wellbeing (%)")
    axes[3].legend(fontsize=6)

    src = df.groupby(["scenario", "noise_source"]).size().groupby(level=0).apply(lambda s: s / s.sum())
    src.unstack().plot(kind="bar", stacked=True, ax=axes[4])
    axes[4].set_title("Noise Source Distribution")
    axes[4].legend(fontsize=5)

    for s in SCENARIO_FILES:
        subset = df[df["scenario"] == s]
        axes[5].scatter(subset["lden_db"], subset["hearing_loss"],
                        s=4, alpha=0.05, color=COLORS[s], label=s)
    axes[5].set_title("Lden vs Hearing Loss")
    axes[5].legend(fontsize=7)

    reg_cols = ["noise_regulation", "noise_monitoring", "uses_hearing_protection", "noise_complaint"]
    reg = df.groupby("scenario")[reg_cols].mean() * 100
    reg.plot(kind="bar", ax=axes[6])
    axes[6].set_title("Regulation & Protection (%)")
    axes[6].legend(fontsize=6)

    child = df[df["is_child"] == 1]
    if len(child) > 0:
        cl = child.groupby("scenario")["child_learning"].mean() * 100
        cl.plot(kind="bar", ax=axes[7], color=[COLORS[s] for s in cl.index])
    axes[7].set_title("Child Learning Impairment (%)")

    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()