| |
| """Validation & Diagnostic Visualization for Laboratory Reagent Supply Dataset.""" |
|
|
| import pandas as pd |
| import numpy as np |
| import matplotlib.pyplot as plt |
| import os |
|
|
| SCENARIOS = ['reference_laboratory', 'district_laboratory', 'health_centre_lab'] |
|
|
|
|
| def load_scenarios(data_dir='data'): |
| dfs = {} |
| for sc in SCENARIOS: |
| path = os.path.join(data_dir, f'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( |
| 'Laboratory Reagent Supply — Validation Report\n' |
| '(Reference Lab → District Lab → Health Centre)', |
| fontsize=15, fontweight='bold', y=0.99) |
| colors = ['#2ecc71', '#f39c12', '#e74c3c'] |
| x = np.arange(len(SCENARIOS)) |
| labels = ['Reference Lab', 'District Lab', 'Health Centre'] |
|
|
| 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('Reagent Availability'); ax.set_ylim(0,100) |
|
|
| ax = axes[0, 1] |
| df = dfs.get('district_laboratory', list(dfs.values())[0]) |
| dept = df.groupby('department')['available_on_survey_day'].mean().sort_values() |
| ax.barh(range(len(dept)), dept.values*100, color='#3498db', alpha=0.7) |
| ax.set_yticks(range(len(dept))) |
| ax.set_yticklabels([s.replace('_',' ').title() for s in dept.index], fontsize=7) |
| ax.set_xlabel('Availability (%)'); ax.set_title('Availability by Department (District)') |
|
|
| 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') |
|
|
| ax = axes[1, 1] |
| eq = [dfs[sc]['equipment_functional'].mean()*100 for sc in SCENARIOS if sc in dfs] |
| ax.bar(x, eq, color=colors, alpha=0.8) |
| ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) |
| for i, v in enumerate(eq): |
| ax.text(i, v+1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold') |
| ax.set_ylabel('Rate (%)'); ax.set_title('Equipment Functional') |
|
|
| ax = axes[2, 0] |
| 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)') |
|
|
| ax = axes[2, 1] |
| tests_missed = [dfs[sc]['tests_not_done_no_reagent'].mean() for sc in SCENARIOS if sc in dfs] |
| ax.bar(x, tests_missed, color=colors, alpha=0.8) |
| ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) |
| for i, v in enumerate(tests_missed): |
| ax.text(i, v+0.2, f'{v:.1f}', ha='center', fontsize=10, fontweight='bold') |
| ax.set_ylabel('Mean Tests Missed'); ax.set_title('Tests Not Done Due to Reagent Stockout') |
|
|
| ax = axes[3, 0] |
| expired = [dfs[sc]['expired_reagent_found'].mean()*100 for sc in SCENARIOS if sc in dfs] |
| ax.bar(x, expired, color=colors, alpha=0.8) |
| ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) |
| for i, v in enumerate(expired): |
| ax.text(i, v+0.5, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold') |
| ax.set_ylabel('Rate (%)'); ax.set_title('Expired Reagents Found on Shelf') |
|
|
| ax = axes[3, 1] |
| 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') |
|
|
| 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) |
|
|