#!/usr/bin/env python3 """Validation & Diagnostic Visualization for HIV/ART Treatment Cascade Dataset.""" import pandas as pd import numpy as np import matplotlib.pyplot as plt import os SCENARIOS = ['high_performing', 'moderate_performing', 'low_performing'] def load_scenarios(data_dir='data'): dfs = {} for sc in SCENARIOS: path = os.path.join(data_dir, f'hiv_art_{sc}.csv') if os.path.exists(path): dfs[sc] = pd.read_csv(path) return dfs def make_report(dfs, output='validation_report.png'): fig, axes = plt.subplots(4, 2, figsize=(16, 22)) fig.suptitle('HIV/ART Treatment Cascade — Validation Report', fontsize=16, fontweight='bold', y=0.98) df = dfs.get('moderate_performing', list(dfs.values())[0]) # Panel 1: Cascade stages across scenarios ax = axes[0, 0] stages = ['undiagnosed', 'diagnosed_not_on_ART', 'on_ART'] colors = ['#e74c3c', '#f39c12', '#2ecc71'] x = np.arange(len(SCENARIOS)) width = 0.25 for j, stage in enumerate(stages): vals = [] for sc in SCENARIOS: if sc in dfs: vals.append((dfs[sc]['cascade_stage'] == stage).mean() * 100) ax.bar(x + j * width, vals, width, label=stage.replace('_', ' '), color=colors[j], alpha=0.8) ax.set_xticks(x + width) ax.set_xticklabels(['High', 'Moderate', 'Low'], fontsize=9) ax.set_ylabel('Percentage (%)') ax.set_title('Treatment Cascade Stages Across Scenarios') ax.legend(fontsize=7) # Panel 2: Viral load distribution (on ART) ax = axes[0, 1] on_art = df[df['cascade_stage'] == 'on_ART'] vl_log = np.log10(on_art['viral_load_copies_ml'].clip(1)) ax.hist(vl_log, bins=50, color='#3498db', alpha=0.7, edgecolor='white') ax.axvline(3, color='red', ls='--', lw=1.5, label='1000 copies/mL (WHO threshold)') ax.set_xlabel('log₁₀ Viral Load (copies/mL)') ax.set_ylabel('Count') ax.set_title('Viral Load Distribution — On ART') ax.legend(fontsize=8) # Panel 3: CD4 current by cascade stage ax = axes[1, 0] data_cd4 = [] labels_cd4 = [] for stage in stages: sub = df[df['cascade_stage'] == stage]['cd4_current'] if len(sub) > 0: data_cd4.append(sub.values) labels_cd4.append(stage.replace('_', '\n')) bp = ax.boxplot(data_cd4, tick_labels=labels_cd4, patch_artist=True) for patch, c in zip(bp['boxes'], colors): patch.set_facecolor(c) patch.set_alpha(0.6) ax.set_ylabel('CD4 (cells/µL)') ax.set_title('Current CD4 by Cascade Stage') # Panel 4: ART regimen distribution ax = axes[1, 1] reg_counts = on_art['art_regimen'].value_counts() reg_colors = ['#2ecc71', '#3498db', '#f39c12', '#e74c3c'] ax.barh(range(len(reg_counts)), reg_counts.values, color=reg_colors[:len(reg_counts)]) ax.set_yticks(range(len(reg_counts))) ax.set_yticklabels(reg_counts.index, fontsize=8) ax.set_xlabel('Count') ax.set_title('ART Regimen Distribution') # Panel 5: VL suppression by adherence ax = axes[2, 0] adh_cats = ['good', 'moderate', 'poor'] adh_colors = ['#2ecc71', '#f39c12', '#e74c3c'] supp_rates = [] for cat in adh_cats: sub = on_art[on_art['adherence_category'] == cat] if len(sub) > 0: supp_rates.append(sub['vl_suppressed'].mean() * 100) else: supp_rates.append(0) ax.bar(range(3), supp_rates, color=adh_colors) ax.set_xticks(range(3)) ax.set_xticklabels(['Good', 'Moderate', 'Poor']) for i, v in enumerate(supp_rates): ax.text(i, v + 1, f'{v:.1f}%', ha='center', fontsize=10) ax.set_ylabel('VL Suppression (%)') ax.set_title('Viral Suppression by Adherence Category') # Panel 6: Cross-scenario key metrics ax = axes[2, 1] metrics = ['Deaths %', 'LTFU %', 'Pop VL Supp %'] x = np.arange(len(metrics)) width = 0.25 sc_colors = ['#2ecc71', '#f39c12', '#e74c3c'] for i, sc in enumerate(SCENARIOS): if sc in dfs: d = dfs[sc] vals = [ (d['outcome'] == 'died').mean() * 100, (d['outcome'] == 'ltfu').mean() * 100, d['vl_suppressed'].mean() * 100, ] ax.bar(x + i * width, vals, width, label=sc.replace('_', ' ').title(), color=sc_colors[i], alpha=0.8) ax.set_xticks(x + width) ax.set_xticklabels(metrics, fontsize=9) ax.set_ylabel('Percentage (%)') ax.set_title('Key Metrics Across Scenarios') ax.legend(fontsize=7) # Panel 7: Age-sex distribution ax = axes[3, 0] males = df[df['sex'] == 'M']['age_years'] females = df[df['sex'] == 'F']['age_years'] ax.hist(males, bins=25, alpha=0.6, color='#3498db', label=f'Male (n={len(males)})', edgecolor='white') ax.hist(females, bins=25, alpha=0.6, color='#e74c3c', label=f'Female (n={len(females)})', edgecolor='white') ax.set_xlabel('Age (years)') ax.set_title('Age Distribution by Sex') ax.legend(fontsize=9) # Panel 8: WHO clinical stage distribution ax = axes[3, 1] who_counts = df['who_clinical_stage'].value_counts().sort_index() who_colors = ['#2ecc71', '#f39c12', '#e67e22', '#e74c3c'] ax.bar(who_counts.index, who_counts.values, color=who_colors[:len(who_counts)]) ax.set_xlabel('WHO Clinical Stage') ax.set_ylabel('Count') ax.set_title('WHO Clinical Stage Distribution') plt.tight_layout(rect=[0, 0, 1, 0.97]) plt.savefig(output, dpi=150, bbox_inches='tight') print(f'Saved validation report to {output}') plt.close() if __name__ == '__main__': dfs = load_scenarios() if dfs: make_report(dfs)