#!/usr/bin/env python3 """Validation & Diagnostic Visualization for Prostate Cancer Dataset.""" import pandas as pd import numpy as np import matplotlib.pyplot as plt import os SCENARIOS = ['tertiary_oncology', 'district_hospital', 'rural_health_centre'] def load_scenarios(data_dir='data'): dfs = {} for sc in SCENARIOS: path = os.path.join(data_dir, f'pca_{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('Prostate Cancer — Validation Report', fontsize=16, fontweight='bold', y=0.98) df = dfs.get('district_hospital', list(dfs.values())[0]) colors = ['#2ecc71', '#f39c12', '#e74c3c'] ax = axes[0, 0] x = np.arange(len(SCENARIOS)) mort = [dfs[sc]['died_within_2_years'].mean()*100 for sc in SCENARIOS if sc in dfs] ax.bar(x, mort, color=colors, alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(['Tertiary', 'District', 'Rural'], fontsize=9) for i, v in enumerate(mort): ax.text(i, v + 0.5, f'{v:.0f}%', ha='center', fontsize=10) ax.set_ylabel('2-Year Mortality (%)') ax.set_title('2-Year Mortality by Scenario') ax = axes[0, 1] stages = ['I', 'II', 'III', 'IV', 'unknown'] for i, sc_name in enumerate(SCENARIOS): if sc_name in dfs: d = dfs[sc_name] vals = [(d['stage'] == s).mean()*100 for s in stages] ax.plot(range(5), vals, 'o-', label=sc_name.replace('_', ' ').title()[:10], color=colors[i], linewidth=2, markersize=6) ax.set_xticks(range(5)) ax.set_xticklabels(stages, fontsize=9) ax.set_ylabel('Proportion (%)') ax.set_title('Stage at Diagnosis (late = poor outcome)') ax.legend(fontsize=7) ax = axes[1, 0] symptoms = df['presenting_symptom'].value_counts() ax.barh(range(len(symptoms)), symptoms.values, color='#3498db', alpha=0.7) ax.set_yticks(range(len(symptoms))) ax.set_yticklabels([s.replace('_', ' ').title() for s in symptoms.index], fontsize=8) ax.set_xlabel('Count') ax.set_title('Presenting Symptoms (LUTS & bone pain)') ax = axes[1, 1] tx = df['treatment_received'].value_counts() t_colors = ['#e74c3c', '#f39c12', '#3498db', '#9b59b6', '#2ecc71', '#e67e22', '#1abc9c'] ax.pie(tx.values, labels=[s.replace('_', ' ').title() for s in tx.index], autopct='%1.0f%%', colors=t_colors[:len(tx)], startangle=90, textprops={'fontsize': 7}) ax.set_title('Treatment Received') ax = axes[2, 0] no_tx = [dfs[sc][dfs[sc]['treatment_received'] == 'none'].shape[0] / len(dfs[sc]) * 100 for sc in SCENARIOS if sc in dfs] ax.bar(x, no_tx, color=colors, alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(['Tertiary', 'District', 'Rural'], fontsize=9) for i, v in enumerate(no_tx): ax.text(i, v + 0.5, f'{v:.0f}%', ha='center', fontsize=10) ax.set_ylabel('Rate (%)') ax.set_title('No Treatment Received') ax = axes[2, 1] psa = [dfs[sc]['psa_tested'].mean()*100 for sc in SCENARIOS if sc in dfs] bx = [dfs[sc]['biopsy_performed'].mean()*100 for sc in SCENARIOS if sc in dfs] w = 0.3 ax.bar(x - w/2, psa, w, label='PSA Tested', color='#3498db', alpha=0.8) ax.bar(x + w/2, bx, w, label='Biopsy Done', color='#f39c12', alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(['Tertiary', 'District', 'Rural'], fontsize=9) ax.set_ylabel('Rate (%)') ax.set_title('Diagnostic Access') ax.legend(fontsize=8) ax = axes[3, 0] stage4 = df[df['stage'] == 'IV'] early = df[df['stage'].isin(['I', 'II'])] cats = ['2yr Mort', 'Bone Mets', 'No Tx'] if len(stage4) > 0 and len(early) > 0: v4 = [stage4['died_within_2_years'].mean()*100, stage4['bone_metastases'].mean()*100, (stage4['treatment_received'] == 'none').mean()*100] ve = [early['died_within_2_years'].mean()*100, early['bone_metastases'].mean()*100, (early['treatment_received'] == 'none').mean()*100] w = 0.3 ax.bar(np.arange(3) - w/2, v4, w, label='Stage IV', color='#e74c3c', alpha=0.8) ax.bar(np.arange(3) + w/2, ve, w, label='Stage I-II', color='#2ecc71', alpha=0.8) ax.set_xticks(np.arange(3)) ax.set_xticklabels(cats, fontsize=9) ax.set_ylabel('Rate (%)') ax.set_title('Stage IV vs Early Stage') ax.legend(fontsize=8) ax = axes[3, 1] ref = [dfs[sc]['referral_needed'].mean()*100 for sc in SCENARIOS if sc in dfs] ref_c = [dfs[sc]['referral_completed'].mean()*100 for sc in SCENARIOS if sc in dfs] ax.bar(x - w/2, ref, w, label='Referral Needed', color='#f39c12', alpha=0.8) ax.bar(x + w/2, ref_c, w, label='Referral Completed', color='#2ecc71', alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(['Tertiary', 'District', 'Rural'], fontsize=9) ax.set_ylabel('Rate (%)') ax.set_title('Referral Gap') ax.legend(fontsize=8) 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)