| |
| """Validation for HIV Viral Load & CD4 Testing Dataset.""" |
| import pandas as pd, numpy as np, matplotlib.pyplot as plt, os, glob |
|
|
| def load_scenarios(data_dir='data'): |
| dfs = {} |
| for f in sorted(glob.glob(os.path.join(data_dir, 'hiv_vl_cd4_*.csv'))): |
| name = os.path.basename(f).replace('.csv', '')[11:] |
| dfs[name] = pd.read_csv(f) |
| return dfs |
|
|
| def main(): |
| dfs = load_scenarios() |
| if not dfs: return |
| all_df = pd.concat([df.assign(scenario=n) for n, df in dfs.items()], ignore_index=True) |
| fig, axes = plt.subplots(4, 2, figsize=(16, 20)) |
| fig.suptitle('HIV Viral Load & CD4 Testing — Validation Report', fontsize=14, fontweight='bold', y=0.98) |
| colors = {'vl_accessible': '#2ecc71', 'vl_limited': '#f39c12', 'no_vl_access': '#e74c3c'} |
| labels = {'vl_accessible': 'VL Access (SA/BW)', 'vl_limited': 'Limited (KE/GH/TZ)', 'no_vl_access': 'None (DRC/SLE)'} |
| scenarios = list(dfs.keys()) |
|
|
| ax = axes[0, 0] |
| metrics = ['VL Ordered %', 'CD4 Ordered %', 'Result\nReturned %', 'EAC %', 'Stockout %'] |
| for i, s in enumerate(scenarios): |
| d = dfs[s]; vals = [d['vl_ordered'].mean()*100, d['cd4_ordered'].mean()*100, d['result_returned'].mean()*100, d['eac_provided'].mean()*100, d['reagent_stockout'].mean()*100] |
| ax.bar(np.arange(len(metrics))+i*0.25, vals, 0.25, label=labels.get(s,s), color=colors[s], alpha=0.8) |
| ax.set_xticks(np.arange(len(metrics))+0.25); ax.set_xticklabels(metrics, fontsize=6); ax.set_ylabel('%'); ax.set_title('Panel 1: Key Metrics'); ax.legend(fontsize=6) |
|
|
| ax = axes[0, 1] |
| for s in scenarios: |
| vl = dfs[s][dfs[s]['vl_ordered']==1]['vl_result'].dropna() |
| if len(vl) > 0: ax.hist(np.log10(vl.clip(lower=1)), bins=25, alpha=0.5, label=labels.get(s,s), color=colors[s], density=True) |
| ax.axvline(3, color='black', linestyle='--', alpha=0.5, label='1000 cp/mL') |
| ax.set_xlabel('Log10 VL (copies/mL)'); ax.set_title('Panel 2: Viral Load Distribution'); ax.legend(fontsize=7) |
|
|
| ax = axes[1, 0] |
| for s in scenarios: |
| cd4 = dfs[s][dfs[s]['cd4_ordered']==1]['cd4_result'].dropna() |
| if len(cd4) > 0: ax.hist(cd4.clip(upper=1000), bins=25, alpha=0.5, label=labels.get(s,s), color=colors[s], density=True) |
| ax.axvline(200, color='black', linestyle='--', alpha=0.5, label='200 cells') |
| ax.set_xlabel('CD4 (cells/uL)'); ax.set_title('Panel 3: CD4 Distribution'); ax.legend(fontsize=7) |
|
|
| ax = axes[1, 1] |
| for s in scenarios: |
| vl_tat = dfs[s][dfs[s]['vl_ordered']==1]['vl_tat_days'].dropna() |
| if len(vl_tat) > 0: ax.hist(vl_tat.clip(upper=60), bins=25, alpha=0.5, label=labels.get(s,s), color=colors[s], density=True) |
| ax.set_xlabel('VL TAT (days)'); ax.set_title('Panel 4: VL Turnaround Time'); ax.legend(fontsize=7) |
|
|
| ax = axes[2, 0] |
| plats = ['conventional_central','poc_vl','dbs_referred','not_done'] |
| for i, s in enumerate(scenarios): |
| vals = [dfs[s]['vl_platform'].value_counts(normalize=True).get(p,0)*100 for p in plats] |
| ax.bar(np.arange(len(plats))+i*0.25, vals, 0.25, label=labels.get(s,s), color=colors[s], alpha=0.8) |
| ax.set_xticks(np.arange(len(plats))+0.25); ax.set_xticklabels([p.replace('_','\n') for p in plats], fontsize=5); ax.set_ylabel('%'); ax.set_title('Panel 5: VL Platform'); ax.legend(fontsize=6) |
|
|
| ax = axes[2, 1] |
| acts = ['EAC', 'Regimen\nSwitch', 'OI\nProphylaxis', 'Transport\nIssue', 'Sample\nRejected'] |
| for i, s in enumerate(scenarios): |
| d = dfs[s]; vals = [d['eac_provided'].mean()*100, d['regimen_switch'].mean()*100, d['oi_prophylaxis'].mean()*100, d['specimen_transport_issue'].mean()*100, d['sample_rejected'].mean()*100] |
| ax.bar(np.arange(len(acts))+i*0.25, vals, 0.25, label=labels.get(s,s), color=colors[s], alpha=0.8) |
| ax.set_xticks(np.arange(len(acts))+0.25); ax.set_xticklabels(acts, fontsize=5); ax.set_ylabel('%'); ax.set_title('Panel 6: Clinical Actions & Issues'); ax.legend(fontsize=6) |
|
|
| ax = axes[3, 0] |
| regs = ['TLD','TLE','AZT_based','PI_based','none'] |
| for i, s in enumerate(scenarios): |
| vals = [dfs[s]['art_regimen'].value_counts(normalize=True).get(r,0)*100 for r in regs] |
| ax.bar(np.arange(len(regs))+i*0.20, vals, 0.20, label=labels.get(s,s), color=colors[s], alpha=0.8) |
| ax.set_xticks(np.arange(len(regs))+0.20); ax.set_xticklabels(regs, fontsize=6); ax.set_ylabel('%'); ax.set_title('Panel 7: ART Regimen'); ax.legend(fontsize=6) |
|
|
| ax = axes[3, 1] |
| num_cols = ['vl_ordered','cd4_ordered','result_returned','reagent_stockout','eac_provided','on_art'] |
| corr = all_df[num_cols].corr() |
| im = ax.imshow(corr, cmap='RdBu_r', vmin=-1, vmax=1, aspect='auto') |
| ax.set_xticks(range(len(num_cols))); ax.set_yticks(range(len(num_cols))) |
| ax.set_xticklabels([c.replace('_','\n') for c in num_cols], fontsize=5, rotation=45, ha='right') |
| ax.set_yticklabels([c.replace('_','\n') for c in num_cols], fontsize=5) |
| ax.set_title('Panel 8: Correlation Heatmap'); fig.colorbar(im, ax=ax, fraction=0.046) |
|
|
| plt.tight_layout(rect=[0,0,1,0.96]); plt.savefig('validation_report.png', dpi=150, bbox_inches='tight'); plt.close() |
| print("Saved validation_report.png") |
|
|
| if __name__ == '__main__': |
| main() |
|
|