#!/usr/bin/env python3 """Validation & Diagnostic Visualization for HIV Test Kit & ARV Supply Dataset.""" import pandas as pd import numpy as np import matplotlib.pyplot as plt import os SCENARIOS = ['pepfar_supported_urban', 'district_hospital_art', 'rural_health_centre_art'] def load_scenarios(data_dir='data'): dfs = {} for sc in SCENARIOS: path = os.path.join(data_dir, f'hiv_supply_{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, 24)) fig.suptitle( 'HIV Test Kit & ARV Supply — Validation Report\n' '(PEPFAR Urban → District Hospital → Rural HC)', fontsize=15, fontweight='bold', y=0.99) colors = ['#2ecc71', '#f39c12', '#e74c3c'] x = np.arange(len(SCENARIOS)) labels = ['PEPFAR Urban', 'District Hosp', 'Rural HC'] # Panel 1: Availability on survey day ax = axes[0, 0] avail = [dfs[sc]['available_on_survey_day'].mean()*100 for sc in SCENARIOS if sc in dfs] ax.bar(x, avail, color=colors, alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(avail): ax.text(i, v + 1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Availability (%)') ax.set_title('HIV Commodity Availability on Survey Day') ax.set_ylim(0, 100) # Panel 2: Availability by commodity category (district) ax = axes[0, 1] df = dfs.get('district_hospital_art', list(dfs.values())[0]) cat_avail = df.groupby('commodity_category')['available_on_survey_day'].mean().sort_values() ax.barh(range(len(cat_avail)), cat_avail.values * 100, color='#3498db', alpha=0.7) ax.set_yticks(range(len(cat_avail))) ax.set_yticklabels([s.replace('_', ' ').title() for s in cat_avail.index], fontsize=7) ax.set_xlabel('Availability (%)') ax.set_title('Availability by Commodity Category (District)') # Panel 3: Stockout in last 6 months ax = axes[1, 0] so = [dfs[sc]['stocked_out_in_last_6m'].mean()*100 for sc in SCENARIOS if sc in dfs] ax.bar(x, so, color=colors, alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(so): ax.text(i, v + 1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Rate (%)') ax.set_title('Stocked Out ≥1x in Last 6 Months') # Panel 4: Mean stockout duration ax = axes[1, 1] dur = [] for sc in SCENARIOS: if sc in dfs: subset = dfs[sc][dfs[sc]['stocked_out_in_last_6m'] == 1] dur.append(subset['stockout_days_last_6m'].mean() if len(subset) > 0 else 0) ax.bar(x, dur, color=colors, alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(dur): ax.text(i, v + 0.5, f'{v:.0f}d', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Days') ax.set_title('Mean Stockout Duration (among stocked-out)') # Panel 5: ART interruption & testing not done ax = axes[2, 0] art_int = [dfs[sc]['art_interruption_due_to_stockout'].mean()*100 for sc in SCENARIOS if sc in dfs] ax.bar(x, art_int, color=colors, alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(art_int): ax.text(i, v + 0.3, f'{v:.1f}%', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Rate (%)') ax.set_title('ART Interruption Due to Stockout') # Panel 6: Stockout causes (district) ax = axes[2, 1] so_df = df[df['stocked_out_in_last_6m'] == 1] if len(so_df) > 0: causes = so_df['stockout_cause'].value_counts().head(8) ax.barh(range(len(causes)), causes.values, color='#e74c3c', alpha=0.7) ax.set_yticks(range(len(causes))) ax.set_yticklabels([s.replace('_', ' ').title() for s in causes.index], fontsize=7) ax.set_xlabel('Count') ax.set_title('Top Stockout Causes (District)') # Panel 7: Order fill rate ax = axes[3, 0] ofr = [dfs[sc]['order_fill_rate_pct'].mean() for sc in SCENARIOS if sc in dfs] ax.bar(x, ofr, color=colors, alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(ofr): ax.text(i, v + 1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Fill Rate (%)') ax.set_title('Order Fill Rate') # Panel 8: Months of stock on hand ax = axes[3, 1] mos = [dfs[sc]['months_of_stock_on_hand'].mean() for sc in SCENARIOS if sc in dfs] ax.bar(x, mos, color=colors, alpha=0.8) ax.axhline(y=2, color='blue', linestyle='--', alpha=0.5, label='Min 2 months') ax.set_xticks(x) ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(mos): ax.text(i, v + 0.05, f'{v:.1f}m', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Months') ax.set_title('Months of Stock on Hand') ax.legend(fontsize=8) 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)