warehouse-inventory-management / validate_dataset.py
rishirajpathak's picture
Duplicate from electricsheepafrica/warehouse-inventory-management
023398b
#!/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)