File size: 4,589 Bytes
023398b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Validation & Diagnostic Visualization for Warehouse & Inventory Management Dataset."""

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

SCENARIOS = ['national_central_medical_store', 'regional_warehouse', 'district_store']


def load_scenarios(data_dir='data'):
    dfs = {}
    for sc in SCENARIOS:
        path = os.path.join(data_dir, f'warehouse_{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(
        'Warehouse & Inventory Management — Validation Report\n'
        '(National CMS → Regional Warehouse → District Store)',
        fontsize=15, fontweight='bold', y=0.99)
    colors = ['#2ecc71', '#f39c12', '#e74c3c']
    x = np.arange(len(SCENARIOS))
    labels = ['National CMS', 'Regional WH', 'District Store']

    ax = axes[0, 0]
    inv = [dfs[sc]['inventory_accuracy_pct'].mean() for sc in SCENARIOS if sc in dfs]
    ax.bar(x, inv, color=colors, alpha=0.8)
    ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9)
    for i, v in enumerate(inv):
        ax.text(i, v+1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold')
    ax.set_ylabel('Accuracy (%)'); ax.set_title('Inventory Accuracy'); ax.set_ylim(0,100)

    ax = axes[0, 1]
    ofr = [dfs[sc]['order_fulfilment_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('Fulfilment (%)'); ax.set_title('Order Fulfilment Rate')

    ax = axes[1, 0]
    waste = [dfs[sc]['wastage_rate_pct'].mean() for sc in SCENARIOS if sc in dfs]
    ax.bar(x, waste, color=colors, alpha=0.8)
    ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9)
    for i, v in enumerate(waste):
        ax.text(i, v+0.5, f'{v:.1f}%', ha='center', fontsize=10, fontweight='bold')
    ax.set_ylabel('Wastage (%)'); ax.set_title('Wastage Rate (Expired + Damaged)')

    ax = axes[1, 1]
    fefo = [dfs[sc]['fefo_compliance'].mean()*100 for sc in SCENARIOS if sc in dfs]
    ax.bar(x, fefo, color=colors, alpha=0.8)
    ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9)
    for i, v in enumerate(fefo):
        ax.text(i, v+1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold')
    ax.set_ylabel('Compliance (%)'); ax.set_title('FEFO Compliance')

    ax = axes[2, 0]
    df = dfs.get('regional_warehouse', list(dfs.values())[0])
    issue_df = df[df['inventory_issue']!='none']
    if len(issue_df)>0:
        issues = issue_df['inventory_issue'].value_counts().head(8)
        ax.barh(range(len(issues)), issues.values, color='#e74c3c', alpha=0.7)
        ax.set_yticks(range(len(issues)))
        ax.set_yticklabels([s.replace('_',' ').title() for s in issues.index], fontsize=7)
        ax.set_xlabel('Count')
    ax.set_title('Top Inventory Issues (Regional)')

    ax = axes[2, 1]
    stor = [dfs[sc]['storage_conditions_adequate'].mean()*100 for sc in SCENARIOS if sc in dfs]
    ax.bar(x, stor, color=colors, alpha=0.8)
    ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9)
    for i, v in enumerate(stor):
        ax.text(i, v+1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold')
    ax.set_ylabel('Rate (%)'); ax.set_title('Storage Conditions Adequate')

    ax = axes[3, 0]
    cap = [dfs[sc]['capacity_utilisation_pct'].mean() for sc in SCENARIOS if sc in dfs]
    ax.bar(x, cap, color=colors, alpha=0.8)
    ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9)
    for i, v in enumerate(cap):
        ax.text(i, v+1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold')
    ax.set_ylabel('Utilisation (%)'); ax.set_title('Warehouse Capacity Utilisation')

    ax = axes[3, 1]
    so = [dfs[sc]['stockout_at_warehouse'].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('Stockout at Warehouse Level')

    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)