"""Generate synthetic pharmacovigilance & adverse drug reaction dataset for SSA. Research-based parameterization: - PMC4796322: ADR reporting in Africa; targeted spontaneous reporting and cohort event monitoring could strengthen PV. - PLOS ONE (2022): PV systems for medication error reporting in Africa. - PMC9900287: ADR report completeness in South Africa; VigiFlow system. - CJGH: West African PV systems; inadequate training, deficient reporting culture, underdeveloped regulatory frameworks. - SSA context: Massive underreporting; <10% of ADRs reported in most countries; limited PV centres; few trained personnel. """ from __future__ import annotations from pathlib import Path import numpy as np import pandas as pd SEED = 42 N_PER_SCENARIO = 10_000 YEAR_RANGE = np.arange(2010, 2025) YEAR_WEIGHTS = np.linspace(0.85, 1.3, len(YEAR_RANGE)) YEAR_WEIGHTS = YEAR_WEIGHTS / YEAR_WEIGHTS.sum() SCENARIOS = { "tertiary_hospital_pv": { "setting_probs": {"tertiary_hospital": 0.40, "regional_hospital": 0.30, "teaching_hospital": 0.20, "specialist_clinic": 0.10}, "drug_class_probs": {"antiretroviral": 0.20, "antibiotic": 0.15, "antimalarial": 0.12, "NSAID_analgesic": 0.12, "antiepileptic": 0.08, "antihypertensive": 0.08, "anti_TB": 0.08, "chemotherapy": 0.07, "antidiabetic": 0.05, "other": 0.05}, "adr_reporting_rate": 0.15, "serious_adr_pct": 0.25, "medication_error_pct": 0.08, "vigiflow_reporting": 0.30, "pv_trained_staff_pct": 0.20, }, "community_primary_care": { "setting_probs": {"health_centre": 0.35, "district_hospital": 0.25, "community_pharmacy": 0.25, "private_clinic": 0.15}, "drug_class_probs": {"antibiotic": 0.25, "antimalarial": 0.20, "NSAID_analgesic": 0.15, "antihypertensive": 0.10, "contraceptive": 0.08, "antiretroviral": 0.08, "antidiabetic": 0.05, "supplement": 0.05, "other": 0.04}, "adr_reporting_rate": 0.03, "serious_adr_pct": 0.15, "medication_error_pct": 0.12, "vigiflow_reporting": 0.05, "pv_trained_staff_pct": 0.05, }, "mass_treatment_campaign": { "setting_probs": {"community_outreach": 0.35, "school_campaign": 0.20, "health_post": 0.25, "mobile_clinic": 0.20}, "drug_class_probs": {"ivermectin_MDA": 0.25, "praziquantel_MDA": 0.20, "albendazole_MDA": 0.15, "azithromycin_MDA": 0.10, "antimalarial_SMC": 0.10, "vaccine": 0.10, "DEC_MDA": 0.05, "other": 0.05}, "adr_reporting_rate": 0.08, "serious_adr_pct": 0.05, "medication_error_pct": 0.06, "vigiflow_reporting": 0.10, "pv_trained_staff_pct": 0.08, }, } SCENARIO_FILES = { "tertiary_hospital_pv": "pv_tertiary_hospital.csv", "community_primary_care": "pv_community_primary.csv", "mass_treatment_campaign": "pv_mass_treatment.csv", } def _choice(rng, prob_map): keys = list(prob_map.keys()) weights = np.array(list(prob_map.values()), dtype=float) weights = weights / weights.sum() return rng.choice(keys, p=weights) def _simulate_scenario(name, params, seed): rng = np.random.default_rng(seed) records = [] for idx in range(N_PER_SCENARIO): year = int(rng.choice(YEAR_RANGE, p=YEAR_WEIGHTS)) setting = _choice(rng, params["setting_probs"]) age = int(np.clip(rng.normal(30, 18), 0, 80)) sex = rng.choice(["male", "female"], p=[0.45, 0.55]) is_child = int(age < 15) pregnant = int(sex == "female" and 15 <= age <= 45 and rng.random() < 0.08) drug_class = _choice(rng, params["drug_class_probs"]) polypharmacy = int(rng.random() < 0.25) num_medicines = int(np.clip(rng.poisson(2 if polypharmacy else 1), 1, 10)) # ADR occurrence adr_occurred = int(rng.random() < 0.10) adr_type = rng.choice(["skin_rash", "GI_disturbance", "hepatotoxicity", "nephrotoxicity", "haematological", "neurological", "anaphylaxis", "Stevens_Johnson", "other"], p=[0.25, 0.20, 0.10, 0.08, 0.08, 0.08, 0.03, 0.02, 0.16]) if adr_occurred else "none" adr_severity = rng.choice(["mild", "moderate", "severe", "fatal"], p=[0.50, 0.30, 0.15, 0.05]) if adr_occurred else "none" serious_adr = int(adr_occurred and adr_severity in ("severe", "fatal")) hospitalisation_due_adr = int(serious_adr and rng.random() < 0.50) death_due_adr = int(adr_severity == "fatal") # Causality causality = rng.choice(["certain", "probable", "possible", "unlikely", "unassessable", "not_assessed"], p=[0.05, 0.20, 0.30, 0.10, 0.10, 0.25]) if adr_occurred else "not_applicable" # Medication error medication_error = int(rng.random() < params["medication_error_pct"]) error_type = rng.choice(["wrong_dose", "wrong_drug", "wrong_route", "omission", "wrong_frequency", "dispensing_error"], p=[0.30, 0.10, 0.05, 0.25, 0.15, 0.15]) if medication_error else "none" # Reporting (massive underreporting in SSA) adr_reported = int(adr_occurred and rng.random() < params["adr_reporting_rate"]) reported_to_nmra = int(adr_reported and rng.random() < 0.40) vigiflow_submitted = int(reported_to_nmra and rng.random() < params["vigiflow_reporting"]) report_complete = int(adr_reported and rng.random() < 0.35) time_to_report_days = int(np.clip(rng.exponential(30), 1, 365)) if adr_reported else 0 # Reporter reporter_type = rng.choice(["physician", "pharmacist", "nurse", "patient", "other"], p=[0.30, 0.25, 0.25, 0.10, 0.10]) if adr_reported else "none" # PV system capacity pv_focal_person = int(rng.random() < params["pv_trained_staff_pct"]) adr_form_available = int(rng.random() < 0.30) pv_training_received = int(rng.random() < params["pv_trained_staff_pct"]) knows_reporting_process = int(rng.random() < 0.15) # Signal detection signal_detected = int(adr_reported and rng.random() < 0.05) regulatory_action = int(signal_detected and rng.random() < 0.20) record = { "record_id": f"{name[:3].upper()}-{idx:05d}", "scenario": name, "year": year, "setting": setting, "age": age, "sex": sex, "is_child": is_child, "drug_class": drug_class, "polypharmacy": polypharmacy, "num_medicines": num_medicines, "adr_occurred": adr_occurred, "adr_type": adr_type, "adr_severity": adr_severity, "serious_adr": serious_adr, "hospitalisation_due_adr": hospitalisation_due_adr, "death_due_adr": death_due_adr, "causality": causality, "medication_error": medication_error, "error_type": error_type, "adr_reported": adr_reported, "reported_to_nmra": reported_to_nmra, "vigiflow_submitted": vigiflow_submitted, "report_complete": report_complete, "time_to_report_days": time_to_report_days, "reporter_type": reporter_type, "pv_focal_person": pv_focal_person, "adr_form_available": adr_form_available, "pv_training_received": pv_training_received, "knows_reporting_process": knows_reporting_process, "signal_detected": signal_detected, "regulatory_action": regulatory_action, } records.append(record) return pd.DataFrame(records) def main(): output_dir = Path("data") output_dir.mkdir(parents=True, exist_ok=True) for idx, (name, params) in enumerate(SCENARIOS.items()): df = _simulate_scenario(name, params, SEED + idx * 211) df.to_csv(output_dir / SCENARIO_FILES[name], index=False) print(f"Saved {name} -> {SCENARIO_FILES[name]}") if __name__ == "__main__": main()