#!/usr/bin/env python3 """Validation & Diagnostic Visualization for Pharmaceutical Regulatory Capacity Dataset.""" import pandas as pd import numpy as np import matplotlib.pyplot as plt import os SCENARIOS = ['ml3_ml4_advanced', 'ml2_developing', 'ml1_minimal'] REG_FUNCS = [ 'registration_marketing_authorization', 'pharmacovigilance', 'market_surveillance_control', 'licensing_establishment', 'regulatory_inspection', 'laboratory_access_testing', 'clinical_trial_oversight', 'import_export_control', 'lot_release_biologicals', ] def load_scenarios(data_dir='data'): dfs = {} for sc in SCENARIOS: path = os.path.join(data_dir, f'reg_capacity_{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( 'Pharmaceutical Regulatory Capacity — Validation Report\n' '(ML3/ML4 Advanced → ML2 Developing → ML1 Minimal)', fontsize=15, fontweight='bold', y=0.99) colors = ['#2ecc71', '#f39c12', '#e74c3c'] x = np.arange(len(SCENARIOS)) labels = ['ML3/ML4', 'ML2', 'ML1'] ax = axes[0, 0] scores = [dfs[sc]['overall_regulatory_score'].mean() for sc in SCENARIOS if sc in dfs] ax.bar(x, scores, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(scores): ax.text(i, v+1, f'{v:.0f}', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Score (0-100)'); ax.set_title('Overall Regulatory Score') ax = axes[0, 1] sf = [dfs[sc]['sf_prevalence_estimated_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 Prevalence (%)'); ax.set_title('Estimated SF Prevalence') ax = axes[1, 0] staff = [dfs[sc]['staff_total'].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+2, f'{v:.0f}', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Staff Count'); ax.set_title('Average NRA Staff') ax = axes[1, 1] budget = [dfs[sc]['budget_usd_millions'].mean() 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+0.1, f'${v:.1f}M', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Budget (USD M)'); ax.set_title('Average NRA Budget') ax = axes[2, 0] df = dfs.get('ml2_developing', list(dfs.values())[1]) func_scores = [df[f'score_{f}'].mean() for f in REG_FUNCS] func_labels = [f.replace('_', '\n').title()[:20] for f in REG_FUNCS] ax.barh(range(len(REG_FUNCS)), func_scores, color='#3498db', alpha=0.7) ax.set_yticks(range(len(REG_FUNCS))) ax.set_yticklabels(func_labels, fontsize=6) ax.set_xlabel('Score (0-100)'); ax.set_title('Regulatory Function Scores (ML2)') ax = axes[2, 1] insp = [dfs[sc]['inspection_coverage_pct'].mean() for sc in SCENARIOS if sc in dfs] ax.bar(x, insp, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(insp): ax.text(i, v+1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Coverage (%)'); ax.set_title('Inspection Coverage') ax = axes[3, 0] pv = [dfs[sc]['pharmacovigilance_centre'].mean()*100 for sc in SCENARIOS if sc in dfs] ax.bar(x, pv, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(pv): ax.text(i, v+1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Rate (%)'); ax.set_title('Has Pharmacovigilance Centre') ax = axes[3, 1] ax.scatter(df['overall_regulatory_score'], df['sf_prevalence_estimated_pct'], alpha=0.3, s=5, color='#e74c3c') ax.set_xlabel('Regulatory Score'); ax.set_ylabel('SF Prevalence (%)') ax.set_title('Regulatory Score vs SF Prevalence (ML2)') 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)