#!/usr/bin/env python3 """Validation & Diagnostic Visualization for Adolescent SRH Dataset.""" import pandas as pd import numpy as np import matplotlib.pyplot as plt import os SCENARIOS = ['youth_friendly_clinic', 'public_health_facility', 'rural_limited_access'] def load_scenarios(data_dir='data'): dfs = {} for sc in SCENARIOS: path = os.path.join(data_dir, f'adolescent_srh_{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('Adolescent Sexual & Reproductive Health — Validation Report', fontsize=16, fontweight='bold', y=0.98) df = dfs.get('public_health_facility', list(dfs.values())[0]) # Panel 1: Contraceptive use across scenarios ax = axes[0, 0] x = np.arange(len(SCENARIOS)) sa_dfs = {sc: dfs[sc][dfs[sc]['sexually_active'] == 1] for sc in SCENARIOS if sc in dfs} mcpr = [sa_dfs[sc]['using_modern_contraceptive'].mean() * 100 for sc in SCENARIOS if sc in sa_dfs] condom = [sa_dfs[sc]['condom_last_sex'].mean() * 100 for sc in SCENARIOS if sc in sa_dfs] width = 0.3 ax.bar(x - width/2, mcpr, width, label='mCPR', color='#2ecc71', alpha=0.8) ax.bar(x + width/2, condom, width, label='Condom Use', color='#3498db', alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(['Youth Clinic', 'Public Facility', 'Rural'], fontsize=8) ax.set_ylabel('Percentage (%)') ax.set_title('Contraceptive Use (Sexually Active)') ax.legend(fontsize=8) # Panel 2: Pregnancy rates across scenarios (females) ax = axes[0, 1] preg_curr = [] preg_ever = [] for sc in SCENARIOS: if sc in dfs: f = dfs[sc][dfs[sc]['sex'] == 'F'] preg_curr.append(f['currently_pregnant'].mean() * 100) preg_ever.append(f['ever_pregnant'].mean() * 100) ax.bar(x - width/2, preg_curr, width, label='Currently Pregnant', color='#e74c3c', alpha=0.8) ax.bar(x + width/2, preg_ever, width, label='Ever Pregnant', color='#f39c12', alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(['Youth Clinic', 'Public Facility', 'Rural'], fontsize=8) ax.set_ylabel('Percentage (%)') ax.set_title('Pregnancy Rates (Females)') ax.legend(fontsize=8) # Panel 3: HIV testing & STI screening ax = axes[1, 0] hiv_test = [sa_dfs[sc]['hiv_tested_12mo'].mean() * 100 for sc in SCENARIOS if sc in sa_dfs] sti_scr = [sa_dfs[sc]['sti_screened'].mean() * 100 for sc in SCENARIOS if sc in sa_dfs] ax.bar(x - width/2, hiv_test, width, label='HIV Tested', color='#e74c3c', alpha=0.8) ax.bar(x + width/2, sti_scr, width, label='STI Screened', color='#9b59b6', alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(['Youth Clinic', 'Public Facility', 'Rural'], fontsize=8) ax.set_ylabel('Percentage (%)') ax.set_title('HIV Testing & STI Screening (Sexually Active)') ax.legend(fontsize=8) # Panel 4: GBV & depression ax = axes[1, 1] gbv = [] dep = [] for sc in SCENARIOS: if sc in dfs: d = dfs[sc] gbv_s = d[d['gbv_screened'] == 1] dep_s = d[d['depression_screened'] == 1] gbv.append(gbv_s['gbv_positive'].mean() * 100 if len(gbv_s) else 0) dep.append(dep_s['depression_positive'].mean() * 100 if len(dep_s) else 0) ax.bar(x - width/2, gbv, width, label='GBV+', color='#e74c3c', alpha=0.8) ax.bar(x + width/2, dep, width, label='Depression+', color='#9b59b6', alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(['Youth Clinic', 'Public Facility', 'Rural'], fontsize=8) ax.set_ylabel('Prevalence (%)') ax.set_title('GBV & Depression Across Scenarios') ax.legend(fontsize=8) # Panel 5: Contraceptive method distribution ax = axes[2, 0] users = df[(df['using_modern_contraceptive'] == 1)] if len(users) > 0: meth = users['contraceptive_method'].value_counts() colors = ['#2ecc71', '#3498db', '#f39c12', '#e74c3c', '#9b59b6', '#1abc9c'] ax.pie(meth.values, labels=[m.replace('_', ' ').title() for m in meth.index], autopct='%1.1f%%', colors=colors[:len(meth)], startangle=90, textprops={'fontsize': 7}) ax.set_title('Contraceptive Method Mix') # Panel 6: Age distribution ax = axes[2, 1] ax.hist(df['age_years'], bins=15, color='#3498db', alpha=0.7, edgecolor='white') ax.set_xlabel('Age (years)') ax.set_title('Age Distribution') # Panel 7: Unmet need across scenarios ax = axes[3, 0] unmet = [sa_dfs[sc]['unmet_need_contraception'].mean() * 100 for sc in SCENARIOS if sc in sa_dfs] ax.bar(x, unmet, color=['#2ecc71', '#f39c12', '#e74c3c'], alpha=0.8) ax.set_xticks(x) ax.set_xticklabels(['Youth Clinic', 'Public Facility', 'Rural'], fontsize=8) for i, v in enumerate(unmet): ax.text(i, v + 0.5, f'{v:.1f}%', ha='center', fontsize=10) ax.set_ylabel('Percentage (%)') ax.set_title('Unmet Need for Contraception') # Panel 8: Education level ax = axes[3, 1] edu = df['education_level'].value_counts() edu_order = ['none', 'primary', 'lower_secondary', 'upper_secondary', 'tertiary'] edu_colors = ['#e74c3c', '#f39c12', '#f1c40f', '#2ecc71', '#3498db'] vals = [edu.get(e, 0) for e in edu_order] ax.bar(range(5), vals, color=edu_colors) ax.set_xticks(range(5)) ax.set_xticklabels(['None', 'Primary', 'Lower Sec', 'Upper Sec', 'Tertiary'], fontsize=7) ax.set_ylabel('Count') ax.set_title('Education 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)