#!/usr/bin/env python3 """Validation & Diagnostic Visualization for Medicine Quality Control Laboratories Dataset.""" import pandas as pd import numpy as np import matplotlib.pyplot as plt import os SCENARIOS = ['who_prequalified_lab', 'national_reference_lab', 'district_basic_lab'] def load_scenarios(data_dir='data'): dfs = {} for sc in SCENARIOS: path = os.path.join(data_dir, f'qc_lab_{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( 'Medicine Quality Control Laboratories — Validation Report\n' '(WHO-PQ Lab → National Reference → District Basic)', fontsize=15, fontweight='bold', y=0.99) colors = ['#2ecc71', '#f39c12', '#e74c3c'] x = np.arange(len(SCENARIOS)) labels = ['WHO-PQ', 'National Ref', 'District'] ax = axes[0, 0] staff = [dfs[sc]['staff_analysts'].mean() for sc in SCENARIOS if sc in dfs] ax.bar(x, staff, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(staff): ax.text(i, v+0.3, f'{v:.0f}', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Staff'); ax.set_title('Average Analyst Staff') ax = axes[0, 1] equip = [dfs[sc]['equipment_functional_pct'].mean() for sc in SCENARIOS if sc in dfs] ax.bar(x, equip, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(equip): ax.text(i, v+1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Rate (%)'); ax.set_title('Equipment Functional Rate') ax = axes[1, 0] samp = [dfs[sc]['samples_tested_per_year'].mean() for sc in SCENARIOS if sc in dfs] ax.bar(x, samp, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(samp): ax.text(i, v+30, f'{v:.0f}', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Samples/Year'); ax.set_title('Annual Testing Volume') ax = axes[1, 1] prof = [] for sc in SCENARIOS: if sc in dfs: enrolled = dfs[sc][dfs[sc]['proficiency_testing_enrolled']==1] prof.append(enrolled['proficiency_testing_pass_rate'].mean() if len(enrolled) > 0 else 0) ax.bar(x, prof, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(prof): ax.text(i, v+1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Pass Rate (%)'); ax.set_title('Proficiency Testing Pass Rate') ax = axes[2, 0] tat = [dfs[sc]['turnaround_days_median'].median() for sc in SCENARIOS if sc in dfs] ax.bar(x, tat, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(tat): ax.text(i, v+0.5, f'{v:.0f}d', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Days'); ax.set_title('Median Turnaround Time') ax = axes[2, 1] budget = [dfs[sc]['annual_budget_usd'].mean()/1000 for sc in SCENARIOS if sc in dfs] ax.bar(x, budget, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(budget): ax.text(i, v+5, f'${v:.0f}K', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Budget (USD K)'); ax.set_title('Average Annual Budget') ax = axes[3, 0] sf = [dfs[sc]['sf_detection_rate_pct'].mean() for sc in SCENARIOS if sc in dfs] ax.bar(x, sf, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(sf): ax.text(i, v+0.5, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('SF Rate (%)'); ax.set_title('SF Detection Rate') ax = axes[3, 1] stockout = [dfs[sc]['reagent_stockout_days_per_year'].mean() for sc in SCENARIOS if sc in dfs] ax.bar(x, stockout, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(stockout): ax.text(i, v+0.5, f'{v:.0f}d', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Days/Year'); ax.set_title('Reagent Stockout Days') 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)