File size: 5,459 Bytes
d76d275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
"""Validation & Diagnostic Visualization for Medical Oxygen Supply Dataset."""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os

SCENARIOS = ['referral_hospital', 'district_hospital', 'rural_health_centre']


def load_scenarios(data_dir='data'):
    dfs = {}
    for sc in SCENARIOS:
        path = os.path.join(data_dir, f'oxygen_{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(
        'Medical Oxygen Supply — Validation Report\n'
        '(Referral Hospital → District Hospital → Rural Health Centre)',
        fontsize=15, fontweight='bold', y=0.99)
    colors = ['#2ecc71', '#f39c12', '#e74c3c']
    x = np.arange(len(SCENARIOS))
    labels = ['Referral Hosp', 'District Hosp', 'Rural HC']

    # Panel 1: Oxygen available today
    ax = axes[0, 0]
    avail = [dfs[sc]['oxygen_available_today'].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('Oxygen Available on Day of Assessment')
    ax.set_ylim(0, 100)

    # Panel 2: Oxygen source distribution (district)
    ax = axes[0, 1]
    df = dfs.get('district_hospital', list(dfs.values())[0])
    src = df['primary_oxygen_source'].value_counts()
    ax.barh(range(len(src)), src.values, color='#3498db', alpha=0.7)
    ax.set_yticks(range(len(src)))
    ax.set_yticklabels([s.replace('_', ' ').title() for s in src.index], fontsize=8)
    ax.set_xlabel('Count')
    ax.set_title('Primary Oxygen Source (District Hospital)')

    # Panel 3: Pulse oximetry functional
    ax = axes[1, 0]
    pox = [dfs[sc]['pulse_oximeter_functional'].mean()*100 for sc in SCENARIOS if sc in dfs]
    ax.bar(x, pox, color=colors, alpha=0.8)
    ax.set_xticks(x)
    ax.set_xticklabels(labels, fontsize=9)
    for i, v in enumerate(pox):
        ax.text(i, v + 1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold')
    ax.set_ylabel('Rate (%)')
    ax.set_title('Functional Pulse Oximetry')

    # Panel 4: Patients needing vs receiving oxygen
    ax = axes[1, 1]
    w = 0.3
    need = [dfs[sc]['patients_needing_oxygen'].mean() for sc in SCENARIOS if sc in dfs]
    recv = [dfs[sc]['patients_received_oxygen'].mean() for sc in SCENARIOS if sc in dfs]
    ax.bar(x - w/2, need, w, label='Needing O₂', color='#e74c3c', alpha=0.8)
    ax.bar(x + w/2, recv, w, label='Received O₂', color='#2ecc71', alpha=0.8)
    ax.set_xticks(x)
    ax.set_xticklabels(labels, fontsize=9)
    ax.set_ylabel('Mean Patients/Month')
    ax.set_title('Oxygen Need vs Delivery Gap')
    ax.legend(fontsize=8)

    # Panel 5: Hypoxemia deaths
    ax = axes[2, 0]
    deaths = [dfs[sc]['deaths_hypoxemia_related'].mean() for sc in SCENARIOS if sc in dfs]
    ax.bar(x, deaths, color=colors, alpha=0.8)
    ax.set_xticks(x)
    ax.set_xticklabels(labels, fontsize=9)
    for i, v in enumerate(deaths):
        ax.text(i, v + 0.1, f'{v:.1f}', ha='center', fontsize=10, fontweight='bold')
    ax.set_ylabel('Mean Deaths/Month')
    ax.set_title('Hypoxemia-Related Deaths (per observation)')

    # Panel 6: Shortage causes (district)
    ax = axes[2, 1]
    short_df = df[df['shortage_cause'] != 'not_applicable']
    if len(short_df) > 0:
        causes = short_df['shortage_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 Oxygen Shortage Causes (District)')

    # Panel 7: Concentrator functional rate
    ax = axes[3, 0]
    conc_func = []
    for sc in SCENARIOS:
        if sc in dfs:
            has_conc = dfs[sc][dfs[sc]['concentrator_count'] > 0]
            if len(has_conc) > 0:
                rate = (has_conc['concentrator_functional'] / has_conc['concentrator_count']).mean() * 100
            else:
                rate = 0
            conc_func.append(rate)
    ax.bar(x, conc_func, color=colors, alpha=0.8)
    ax.set_xticks(x)
    ax.set_xticklabels(labels, fontsize=9)
    for i, v in enumerate(conc_func):
        ax.text(i, v + 1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold')
    ax.set_ylabel('Functional Rate (%)')
    ax.set_title('Concentrator Functionality (among facilities with concentrators)')

    # Panel 8: Distance to refill & transport
    ax = axes[3, 1]
    dist = [dfs[sc]['distance_to_refill_km'].mean() for sc in SCENARIOS if sc in dfs]
    ax.bar(x, dist, color=colors, alpha=0.8)
    ax.set_xticks(x)
    ax.set_xticklabels(labels, fontsize=9)
    for i, v in enumerate(dist):
        ax.text(i, v + 2, f'{v:.0f}km', ha='center', fontsize=10, fontweight='bold')
    ax.set_ylabel('Distance (km)')
    ax.set_title('Mean Distance to Cylinder Refill Point')

    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)