"""Validate synthetic anti-TB medicine quality dataset.""" from __future__ import annotations from pathlib import Path import matplotlib.pyplot as plt import pandas as pd SCENARIO_FILES = { "dots_program_public": "tb_dots_public.csv", "mdr_tb_treatment": "tb_mdr_treatment.csv", "informal_private_market": "tb_informal_private.csv", } COLORS = {"dots_program_public": "#e6550d", "mdr_tb_treatment": "#756bb1", "informal_private_market": "#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["api_pct_label"], bins=50, alpha=0.5, color=COLORS[s], label=s, range=(0, 120)) axes[0].axvline(85, color="red", ls="--", lw=1, label="85% threshold") axes[0].set_title("API Content (% of label)") axes[0].legend(fontsize=6) sf_cols = ["is_substandard_falsified", "is_falsified", "rifampicin_degraded"] sf = df.groupby("scenario")[sf_cols].mean() * 100 sf.plot(kind="bar", ax=axes[1]) axes[1].set_title("SF Prevalence & Rifampicin Degradation (%)") axes[1].legend(fontsize=6) drug = df.groupby(["scenario", "drug"]).size().groupby(level=0).apply(lambda s: s / s.sum()) drug.unstack().plot(kind="bar", stacked=True, ax=axes[2]) axes[2].set_title("Drug Distribution") axes[2].legend(fontsize=4) out_cols = ["treatment_failure", "mdr_emergence", "death", "hepatotoxicity"] out = df.groupby("scenario")[out_cols].mean() * 100 out.plot(kind="bar", ax=axes[3]) axes[3].set_title("Treatment Outcomes (%)") axes[3].legend(fontsize=7) dots_cols = ["dots_observed", "treatment_completed", "lost_to_followup"] dots = df.groupby("scenario")[dots_cols].mean() * 100 dots.plot(kind="bar", ax=axes[4]) axes[4].set_title("DOTS & Adherence (%)") axes[4].legend(fontsize=7) mfg = df.groupby(["scenario", "manufacturer"]).size().groupby(level=0).apply(lambda s: s / s.sum()) mfg.unstack().plot(kind="bar", stacked=True, ax=axes[5]) axes[5].set_title("Manufacturer Origin") axes[5].legend(fontsize=5) surv_cols = ["quality_tested", "genexpert_done", "reported_to_ntp"] surv = df.groupby("scenario")[surv_cols].mean() * 100 surv.plot(kind="bar", ax=axes[6]) axes[6].set_title("Surveillance & Testing (%)") axes[6].legend(fontsize=7) clin_cols = ["hiv_coinfected", "gdf_procured", "storage_poor"] clin = df.groupby("scenario")[clin_cols].mean() * 100 clin.plot(kind="bar", ax=axes[7]) axes[7].set_title("Clinical & Supply Context (%)") 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()