#\!/usr/bin/env python3 import random, math, csv from typing import Dict, Optional, List class HypertensionCVDGenerator: def __init__(self, seed=None): if seed: random.seed(seed) def normal_random(self, m, s): u1, u2 = random.random(), random.random() return m + s * math.sqrt(-2*math.log(u1)) * math.cos(2*math.pi*u2) def truncated_normal(self, m, s, mn, mx): return max(mn, min(mx, self.normal_random(m, s))) def generate_sample(self, sid, htn_prev=0.30): age = self.truncated_normal(48, 15, 18, 85) sex = random.choice(['Male', 'Female']) residence = random.choices(['Urban', 'Rural'], [0.45, 0.55])[0] # BMI bmi = self.truncated_normal(25.5 if sex=='Female' else 23.5, 4.5, 16, 45) if residence == 'Urban': bmi += 1.5 bmi = round(bmi, 1) # Risk factors family_hx_htn = random.random() < 0.30 diabetes = random.random() < (0.10 if age < 45 else 0.18) smoking = random.random() < (0.25 if sex=='Male' else 0.05) alcohol_heavy = random.random() < (0.15 if sex=='Male' else 0.03) physically_active = random.random() < (0.35 if residence=='Urban' else 0.50) high_salt_diet = random.random() < 0.60 # ORs bmi_or = 3.50 if bmi > 30 else (1.8 if bmi > 25 else 1.0) age_or = 4.20 if age > 60 else (2.5 if age > 45 else 1.0) fh_or = 2.80 if family_hx_htn else 1.0 salt_or = 2.40 if high_salt_diet else 1.0 dm_or = 2.30 if diabetes else 1.0 inactive_or = 1.80 if not physically_active else 1.0 alc_or = 2.10 if alcohol_heavy else 1.0 urban_or = 1.95 if residence=='Urban' else 1.0 combined_or = bmi_or * age_or * fh_or * salt_or * dm_or * inactive_or * alc_or * urban_or htn_prob = min(0.90, 0.15 * combined_or) has_htn = random.random() < htn_prob # BP if has_htn: systolic = self.truncated_normal(152, 18, 130, 200) diastolic = self.truncated_normal(95, 12, 80, 120) if systolic < 140 and diastolic < 90: # Ensure diagnosed criteria systolic += 10 diagnosed = random.random() < (0.30 if residence=='Urban' else 0.20) if diagnosed: on_meds = random.random() < 0.40 adherence = random.choices(['Good','Moderate','Poor'], [0.35,0.45,0.20])[0] if on_meds else 'N/A' if on_meds and adherence=='Good': systolic -= 15; diastolic -= 10 controlled = systolic < 140 and diastolic < 90 else: controlled = False else: on_meds = False adherence = 'Undiagnosed' controlled = False else: systolic = self.truncated_normal(118, 12, 90, 129) diastolic = self.truncated_normal(75, 8, 60, 84) diagnosed, on_meds, adherence, controlled = False, False, 'N/A', False # CVD events if has_htn: stroke_risk = 0.02 * (3.80 if not controlled else 1.5) * (2.0 if diabetes else 1.0) mi_risk = 0.015 * (2.50 if not controlled else 1.2) * (2.5 if smoking else 1.0) hf_risk = 0.01 * (3.20 if not controlled else 1.3) else: stroke_risk = 0.005 * (2.0 if diabetes else 1.0) mi_risk = 0.003 * (2.5 if smoking else 1.0) hf_risk = 0.002 stroke = random.random() < min(0.25, stroke_risk * (age/50)) mi = random.random() < min(0.20, mi_risk * (age/50)) heart_failure = random.random() < min(0.15, hf_risk * (age/50)) # Labs chol = self.truncated_normal(210 if has_htn else 180, 40, 120, 350) ldl = self.truncated_normal(140 if has_htn else 115, 35, 60, 250) hdl = self.truncated_normal(46 if has_htn else 52, 12, 25, 80) creat = self.truncated_normal(1.2 if has_htn else 0.9, 0.4 if has_htn else 0.2, 0.5, 4.0) return { 'patient_id': f'HTN_{sid:06d}', 'age': round(age,1), 'sex': sex, 'residence': residence, 'bmi': bmi, 'family_history_hypertension': family_hx_htn, 'diabetes': diabetes, 'smoking': smoking, 'alcohol_heavy': alcohol_heavy, 'physically_active': physically_active, 'high_salt_diet': high_salt_diet, 'hypertension_status': 'Hypertensive' if has_htn else 'Normal', 'systolic_bp_mmhg': round(systolic,0), 'diastolic_bp_mmhg': round(diastolic,0), 'diagnosed': diagnosed, 'on_medication': on_meds, 'medication_adherence': adherence, 'bp_controlled': controlled, 'stroke_history': stroke, 'myocardial_infarction': mi, 'heart_failure': heart_failure, 'total_cholesterol_mg_dl': round(chol,0), 'ldl_mg_dl': round(ldl,0), 'hdl_mg_dl': round(hdl,0), 'creatinine_mg_dl': round(creat,2) } def generate_dataset(self, n, prev=0.30, output=None): print(f"Generating {n} HTN/CVD samples (prevalence={prev:.1%})...") samples = [self.generate_sample(i+1, prev) for i in range(n)] if output: with open(output, 'w', newline='') as f: writer = csv.DictWriter(f, fieldnames=list(samples[0].keys())) writer.writeheader(); writer.writerows(samples) print(f"✓ Saved to {output}") htn = sum(1 for s in samples if s['hypertension_status']=='Hypertensive') diagnosed = sum(1 for s in samples if s['diagnosed']) cvd = sum(1 for s in samples if s['stroke_history'] or s['myocardial_infarction']) print(f"Summary: HTN {htn} ({htn/n*100:.1f}%), Diagnosed {diagnosed} ({diagnosed/max(1,htn)*100:.1f}%), CVD events {cvd}") return samples if __name__=="__main__": import argparse p = argparse.ArgumentParser() p.add_argument('-n', '--samples', type=int, default=1000) p.add_argument('-p', '--prevalence', type=float, default=0.30) p.add_argument('-s', '--seed', type=int, default=None) p.add_argument('-o', '--output', type=str, default='hypertension_cvd_africa.csv') args = p.parse_args() HypertensionCVDGenerator(args.seed).generate_dataset(args.samples, args.prevalence, args.output)