Datasets:
File size: 4,632 Bytes
f332b66 | 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 | #!/usr/bin/env python3
"""Validation & Diagnostic Visualization for Online Pharmacy & E-Pharmacy Regulation Dataset."""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
SCENARIOS = ['licensed_e_pharmacy', 'social_media_marketplace', 'rogue_website_darknet']
def load_scenarios(data_dir='data'):
dfs = {}
for sc in SCENARIOS:
path = os.path.join(data_dir, f'epharmacy_{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(
'Online Pharmacy & E-Pharmacy Regulation — Validation Report\n'
'(Licensed E-Pharmacy → Social Media → Rogue/Darknet)',
fontsize=15, fontweight='bold', y=0.99)
colors = ['#2ecc71', '#f39c12', '#e74c3c']
x = np.arange(len(SCENARIOS))
labels = ['Licensed', 'Social Media', 'Rogue/Darknet']
ax = axes[0, 0]
sf = [dfs[sc]['quality_test_result'].eq('fail').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 Rate by Platform Type')
ax = axes[0, 1]
lic = [dfs[sc]['seller_licensed'].mean()*100 for sc in SCENARIOS if sc in dfs]
ax.bar(x, lic, color=colors, alpha=0.8)
ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9)
for i, v in enumerate(lic):
ax.text(i, v+1, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold')
ax.set_ylabel('Rate (%)'); ax.set_title('Seller Licensed')
ax = axes[1, 0]
df = dfs.get('social_media_marketplace', list(dfs.values())[1])
plat = df.groupby('platform_name')['quality_test_result'].apply(
lambda x: (x == 'fail').mean()*100).sort_values()
ax.barh(range(len(plat)), plat.values, color='#e74c3c', alpha=0.7)
ax.set_yticks(range(len(plat)))
ax.set_yticklabels([s.replace('_', ' ').title() for s in plat.index], fontsize=7)
ax.set_xlabel('SF Rate (%)'); ax.set_title('SF by Platform (Social Media)')
ax = axes[1, 1]
cat = df.groupby('product_category')['quality_test_result'].apply(
lambda x: (x == 'fail').mean()*100).sort_values()
ax.barh(range(len(cat)), cat.values, color='#9b59b6', alpha=0.7)
ax.set_yticks(range(len(cat)))
ax.set_yticklabels([s.replace('_', ' ').title() for s in cat.index], fontsize=8)
ax.set_xlabel('SF Rate (%)'); ax.set_title('SF by Product Category')
ax = axes[2, 0]
norx = [(1-dfs[sc]['prescription_verified'].mean())*100 for sc in SCENARIOS if sc in dfs]
ax.bar(x, norx, color=colors, alpha=0.8)
ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9)
for i, v in enumerate(norx):
ax.text(i, v+0.5, f'{v:.0f}%', ha='center', fontsize=10, fontweight='bold')
ax.set_ylabel('Rate (%)'); ax.set_title('No Prescription Verified')
ax = axes[2, 1]
price = df['price_vs_reference'].values
ax.hist(price, bins=30, color='#3498db', alpha=0.7, edgecolor='white')
ax.axvline(x=1.0, color='red', linestyle='--', label='Reference price')
ax.set_xlabel('Price vs Reference'); ax.set_title('Price Distribution (Social Media)')
ax.legend(fontsize=8)
ax = axes[3, 0]
w = 0.35
fals = [dfs[sc]['sf_classification'].eq('falsified').mean()*100 for sc in SCENARIOS if sc in dfs]
subs = [dfs[sc]['sf_classification'].eq('substandard').mean()*100 for sc in SCENARIOS if sc in dfs]
ax.bar(x - w/2, subs, w, label='Substandard', color='#f39c12', alpha=0.8)
ax.bar(x + w/2, fals, w, label='Falsified', color='#e74c3c', alpha=0.8)
ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9)
ax.set_ylabel('Rate (%)'); ax.set_title('Substandard vs Falsified'); ax.legend(fontsize=8)
ax = axes[3, 1]
comp = [dfs[sc]['consumer_complaint_filed'].mean()*100 for sc in SCENARIOS if sc in dfs]
ax.bar(x, comp, color=colors, alpha=0.8)
ax.set_xticks(x); ax.set_xticklabels(labels, fontsize=9)
for i, v in enumerate(comp):
ax.text(i, v+0.3, f'{v:.1f}%', ha='center', fontsize=10, fontweight='bold')
ax.set_ylabel('Rate (%)'); ax.set_title('Consumer Complaints Filed')
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)
|