#!/usr/bin/env python3 """Validation & Diagnostic Visualization for Supply Chain Integrity & Track-and-Trace Dataset.""" import pandas as pd import numpy as np import matplotlib.pyplot as plt import os SCENARIOS = ['serialized_track_trace', 'partial_visibility', 'opaque_uncontrolled'] def load_scenarios(data_dir='data'): dfs = {} for sc in SCENARIOS: path = os.path.join(data_dir, f'sct_{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( 'Supply Chain Integrity & Track-and-Trace — Validation Report\n' '(Serialized → Partial Visibility → Opaque)', fontsize=15, fontweight='bold', y=0.99) colors = ['#2ecc71', '#f39c12', '#e74c3c'] x = np.arange(len(SCENARIOS)) labels = ['Serialized', 'Partial', 'Opaque'] ax = axes[0, 0] sf = [dfs[sc]['sf_product_detected'].mean()*100 for sc in SCENARIOS if sc in dfs] ax.bar(x, sf, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(sf): ax.text(i, v+1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('SF Rate (%)'); ax.set_title('SF Product Detection Rate') ax = axes[0, 1] ser = [dfs[sc]['serialized'].mean()*100 for sc in SCENARIOS if sc in dfs] ax.bar(x, ser, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(ser): ax.text(i, v+1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Rate (%)'); ax.set_title('Serialization Coverage') ax = axes[1, 0] div = [dfs[sc]['diversion_detected'].mean()*100 for sc in SCENARIOS if sc in dfs] ax.bar(x, div, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(div): ax.text(i, v+0.3, f'{v:.1f}%', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Rate (%)'); ax.set_title('Diversion Detected') ax = axes[1, 1] interm = [dfs[sc]['intermediary_count'].mean() for sc in SCENARIOS if sc in dfs] ax.bar(x, interm, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(interm): ax.text(i, v+0.1, f'{v:.1f}', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Count'); ax.set_title('Average Intermediaries') ax = axes[2, 0] df = dfs.get('partial_visibility', list(dfs.values())[1]) ep = df[df['sf_product_detected']==1]['sf_entry_point'].value_counts() if len(ep) > 0: ax.barh(range(len(ep)), ep.values, color='#e74c3c', alpha=0.7) ax.set_yticks(range(len(ep))) ax.set_yticklabels([s.replace('_', ' ').title() for s in ep.index], fontsize=7) ax.set_xlabel('Count') ax.set_title('SF Entry Points (Partial Visibility)') ax = axes[2, 1] temp = [dfs[sc]['temperature_monitored'].mean()*100 for sc in SCENARIOS if sc in dfs] ax.bar(x, temp, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(temp): ax.text(i, v+1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Rate (%)'); ax.set_title('Temperature Monitoring') ax = axes[3, 0] transit = [dfs[sc]['transit_days'].median() for sc in SCENARIOS if sc in dfs] ax.bar(x, transit, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(transit): ax.text(i, v+0.5, f'{v:.0f}d', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Days'); ax.set_title('Median Transit Days') ax = axes[3, 1] recall = [dfs[sc]['recall_initiated'].mean()*100 for sc in SCENARIOS if sc in dfs] ax.bar(x, recall, color=colors, alpha=0.8) ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(recall): ax.text(i, v+0.2, f'{v:.1f}%', ha='center', fontsize=10, fontweight='bold') ax.set_ylabel('Rate (%)'); ax.set_title('Recall Initiated') 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)