#!/usr/bin/env python3 """Validation & Diagnostic Visualization for HCW Workforce Dataset.""" import pandas as pd import numpy as np import matplotlib.pyplot as plt import os SCENARIOS = ['urban_tertiary', 'district_hospital', 'rural_health_centre'] def load_scenarios(data_dir='data'): dfs = {} for sc in SCENARIOS: path = os.path.join(data_dir, f'hcw_{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('Healthcare Worker Workforce & Retention — Validation Report', fontsize=16, fontweight='bold', y=0.98) df = dfs.get('district_hospital', list(dfs.values())[0]) colors = ['#2ecc71', '#f39c12', '#e74c3c'] ax = axes[0, 0] x = np.arange(len(SCENARIOS)) burnout = [dfs[sc][dfs[sc]['burnout_level'] == 'high'].shape[0] / len(dfs[sc]) * 100 for sc in SCENARIOS if sc in dfs] ax.bar(x, burnout, color=colors, alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(['Urban Tertiary', 'District', 'Rural'], fontsize=9) for i, v in enumerate(burnout): ax.text(i, v + 0.5, f'{v:.0f}%', ha='center', fontsize=10) ax.set_ylabel('High Burnout (%)') ax.set_title('Burnout by Scenario') ax = axes[0, 1] cadres = df['cadre'].value_counts() c_colors = ['#3498db', '#e74c3c', '#f39c12', '#9b59b6', '#2ecc71', '#e67e22', '#1abc9c'] ax.pie(cadres.values, labels=[s.replace('_', ' ').title() for s in cadres.index], autopct='%1.0f%%', colors=c_colors[:len(cadres)], startangle=90, textprops={'fontsize': 8}) ax.set_title('Cadre Distribution') ax = axes[1, 0] leave = [dfs[sc]['intention_to_leave'].mean()*100 for sc in SCENARIOS if sc in dfs] migrate = [dfs[sc]['intention_to_migrate'].mean()*100 for sc in SCENARIOS if sc in dfs] w = 0.3 ax.bar(x - w/2, leave, w, label='Leave', color='#e74c3c', alpha=0.8) ax.bar(x + w/2, migrate, w, label='Migrate', color='#f39c12', alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(['Urban Tertiary', 'District', 'Rural'], fontsize=9) ax.set_ylabel('Rate (%)') ax.set_title('Intention to Leave / Migrate') ax.legend(fontsize=8) ax = axes[1, 1] reasons = df[df['reason_to_leave'] != 'none']['reason_to_leave'].value_counts() if len(reasons) > 0: ax.barh(range(len(reasons)), reasons.values, color='#3498db', alpha=0.8) ax.set_yticks(range(len(reasons))) ax.set_yticklabels([s.replace('_', ' ').title() for s in reasons.index], fontsize=8) ax.set_xlabel('Count') ax.set_title('Reasons to Leave (salary #1)') ax = axes[2, 0] vacancy = [(1 - dfs[sc]['staffing_adequate'].mean())*100 for sc in SCENARIOS if sc in dfs] ax.bar(x, vacancy, color=colors, alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(['Urban Tertiary', 'District', 'Rural'], fontsize=9) for i, v in enumerate(vacancy): ax.text(i, v + 0.5, f'{v:.0f}%', ha='center', fontsize=10) ax.set_ylabel('Vacancy Rate (%)') ax.set_title('Staffing Vacancy Rate') ax = axes[2, 1] sat = df['salary_satisfaction'].value_counts() s_order = ['satisfied', 'neutral', 'dissatisfied'] vals = [sat.get(s, 0) for s in s_order] ax.bar(range(3), vals, color=['#2ecc71', '#f39c12', '#e74c3c'], alpha=0.8) ax.set_xticks(range(3)) ax.set_xticklabels(['Satisfied', 'Neutral', 'Dissatisfied'], fontsize=9) ax.set_ylabel('Count') ax.set_title('Salary Satisfaction (~55% dissatisfied)') ax = axes[3, 0] dest = df[df['migration_destination'] != 'none']['migration_destination'].value_counts() if len(dest) > 0: ax.barh(range(len(dest)), dest.values, color=c_colors[:len(dest)], alpha=0.8) ax.set_yticks(range(len(dest))) ax.set_yticklabels([s.replace('_', ' ').title() for s in dest.index], fontsize=8) ax.set_xlabel('Count') ax.set_title('Migration Destinations') ax = axes[3, 1] safety = ['workplace_violence_past_year', 'needlestick_injury_past_year', 'covid_infected', 'ppe_adequate'] s_labels = ['Violence', 'Needlestick', 'COVID', 'PPE Adequate'] vals = [df[s].mean()*100 for s in safety] ax.bar(range(4), vals, color=['#e74c3c', '#f39c12', '#9b59b6', '#2ecc71'], alpha=0.8) ax.set_xticks(range(4)) ax.set_xticklabels(s_labels, fontsize=9) ax.set_ylabel('Rate (%)') ax.set_title('Workplace Safety') 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)