#!/usr/bin/env python3 """Validation & Diagnostic Visualization for Epilepsy Dataset.""" import pandas as pd import numpy as np import matplotlib.pyplot as plt import os SCENARIOS = ['neurology_clinic', 'district_hospital', 'rural_health_centre'] def load_scenarios(data_dir='data'): dfs = {} for sc in SCENARIOS: path = os.path.join(data_dir, f'epilepsy_{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, 22)) fig.suptitle('Epilepsy & Neurological Disorders — Validation Report', fontsize=16, fontweight='bold', y=0.98) colors = ['#2ecc71', '#f39c12', '#e74c3c'] x = np.arange(len(SCENARIOS)) labels = ['Neuro Clinic', 'District', 'Rural'] # Panel 1: Treatment gap by scenario ax = axes[0, 0] gap = [dfs[sc]['in_treatment_gap'].mean()*100 for sc in SCENARIOS if sc in dfs] ax.bar(x, gap, color=colors, alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(gap): ax.text(i, v + 1, f'{v:.0f}%', ha='center', fontsize=10) ax.set_ylabel('Rate (%)') ax.set_title('Treatment Gap (25% → 55% → 80%)') # Panel 2: Seizure type distribution ax = axes[0, 1] df = dfs.get('district_hospital', list(dfs.values())[0]) sz = df['seizure_type'].value_counts() ax.barh(range(len(sz)), sz.values, color='#3498db', alpha=0.7) ax.set_yticks(range(len(sz))) ax.set_yticklabels([s.replace('_', ' ').title() for s in sz.index], fontsize=7) ax.set_xlabel('Count') ax.set_title('Seizure Type Distribution') # Panel 3: AED prescribed by scenario ax = axes[1, 0] aed = [(dfs[sc]['aed_prescribed'] != 'none').mean()*100 for sc in SCENARIOS if sc in dfs] ax.bar(x, aed, color=colors, alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(aed): ax.text(i, v + 1, f'{v:.0f}%', ha='center', fontsize=10) ax.set_ylabel('Rate (%)') ax.set_title('AED Prescribed') # Panel 4: Seizure-free 12m by scenario ax = axes[1, 1] sf = [dfs[sc]['seizure_free_12m'].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 + 0.5, f'{v:.0f}%', ha='center', fontsize=10) ax.set_ylabel('Rate (%)') ax.set_title('Seizure-Free at 12 Months') # Panel 5: Stigma & psychosocial ax = axes[2, 0] impacts = ['stigma_experienced', 'conceals_diagnosis', 'depression_comorbid', 'employment_discrimination', 'marriage_affected'] i_labels = ['Stigma', 'Conceals Dx', 'Depression', 'Employment\nDiscrim.', 'Marriage\nAffected'] vals = [df[i].mean()*100 for i in impacts] ax.barh(range(5), vals, color='#9b59b6', alpha=0.7) ax.set_yticks(range(5)) ax.set_yticklabels(i_labels, fontsize=8) ax.set_xlabel('Prevalence (%)') ax.set_title('Stigma & Psychosocial Impact') # Panel 6: Traditional healer / faith healing ax = axes[2, 1] trad = [dfs[sc]['traditional_healer_consulted'].mean()*100 for sc in SCENARIOS if sc in dfs] faith = [dfs[sc]['faith_healing_sought'].mean()*100 for sc in SCENARIOS if sc in dfs] w = 0.3 ax.bar(x - w/2, trad, w, label='Traditional Healer', color='#e67e22', alpha=0.8) ax.bar(x + w/2, faith, w, label='Faith Healing', color='#9b59b6', alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(labels, fontsize=9) ax.set_ylabel('Rate (%)') ax.set_title('Alternative Care Seeking (~40% / ~30%)') ax.legend(fontsize=8) # Panel 7: Etiology ax = axes[3, 0] etio = df['etiology'].value_counts() e_colors = ['#e74c3c', '#f39c12', '#3498db', '#9b59b6', '#2ecc71', '#e67e22', '#1abc9c', '#95a5a6'] ax.pie(etio.values, labels=[s.replace('_', ' ').title() for s in etio.index], autopct='%1.0f%%', colors=e_colors[:len(etio)], startangle=90, textprops={'fontsize': 7}) ax.set_title('Etiology Distribution') # Panel 8: AED stock-out by scenario ax = axes[3, 1] stockout = [dfs[sc]['aed_stock_out_experienced'].mean()*100 for sc in SCENARIOS if sc in dfs] ax.bar(x, stockout, color=colors, alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(labels, fontsize=9) for i, v in enumerate(stockout): ax.text(i, v + 0.5, f'{v:.0f}%', ha='center', fontsize=10) ax.set_ylabel('Rate (%)') ax.set_title('AED Stock-Out Experienced') 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)