"""Generate synthetic anti-TB medicine quality dataset for SSA. Research-based parameterization: - IntechOpen (2024): ~70% of reports detected falsified/substandard TB and HIV medicines in LMICs. - WHO: First-line TB drugs (RHZE) quality critical for DOTS success; rifampicin most commonly SF anti-TB drug. - Lancet ID: Substandard rifampicin associated with treatment failure and MDR-TB emergence in SSA. - SSA context: Fixed-dose combinations (FDCs) quality variable; limited QC testing for TB drugs; stockouts drive informal sourcing. """ 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 = { "dots_program_public": { "setting_probs": {"TB_clinic": 0.30, "district_hospital": 0.25, "health_centre": 0.25, "community_dots": 0.20}, "drug_probs": {"RHZE_FDC": 0.35, "RH_FDC": 0.20, "rifampicin": 0.15, "isoniazid": 0.10, "ethambutol": 0.05, "pyrazinamide": 0.05, "streptomycin": 0.05, "other": 0.05}, "sf_prevalence": 0.12, "rifampicin_failure_pct": 0.10, "treatment_failure_rate": 0.08, "quality_tested_pct": 0.06, "gdf_procured_pct": 0.50, }, "mdr_tb_treatment": { "setting_probs": {"MDR_TB_unit": 0.35, "tertiary_hospital": 0.25, "referral_centre": 0.20, "ambulatory_mdr": 0.20}, "drug_probs": {"bedaquiline": 0.20, "linezolid": 0.15, "moxifloxacin": 0.15, "levofloxacin": 0.12, "clofazimine": 0.10, "cycloserine": 0.08, "delamanid": 0.08, "ethionamide": 0.07, "other": 0.05}, "sf_prevalence": 0.08, "rifampicin_failure_pct": 0.0, "treatment_failure_rate": 0.15, "quality_tested_pct": 0.10, "gdf_procured_pct": 0.60, }, "informal_private_market": { "setting_probs": {"private_pharmacy": 0.30, "drug_shop": 0.25, "informal_vendor": 0.25, "cross_border": 0.20}, "drug_probs": {"rifampicin": 0.20, "isoniazid": 0.15, "RHZE_FDC": 0.15, "RH_FDC": 0.10, "ethambutol": 0.10, "streptomycin": 0.08, "pyrazinamide": 0.07, "fluoroquinolone": 0.08, "other": 0.07}, "sf_prevalence": 0.30, "rifampicin_failure_pct": 0.25, "treatment_failure_rate": 0.18, "quality_tested_pct": 0.01, "gdf_procured_pct": 0.05, }, } SCENARIO_FILES = { "dots_program_public": "tb_dots_public.csv", "mdr_tb_treatment": "tb_mdr_treatment.csv", "informal_private_market": "tb_informal_private.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(35, 14), 5, 75)) sex = rng.choice(["male", "female"], p=[0.55, 0.45]) hiv_coinfected = int(rng.random() < 0.30) treatment_phase = rng.choice(["intensive", "continuation"], p=[0.45, 0.55]) drug = _choice(rng, params["drug_probs"]) manufacturer = rng.choice(["who_prequalified", "gdf_procured", "indian_generic", "local_generic", "unknown"], p=[0.20, 0.20, 0.30, 0.15, 0.15]) gdf_procured = int(rng.random() < params["gdf_procured_pct"]) # Quality is_sf = int(rng.random() < params["sf_prevalence"]) is_falsified = int(is_sf and rng.random() < 0.20) is_substandard = int(is_sf and not is_falsified) if is_falsified: api_pct = float(np.clip(rng.normal(15, 18), 0, 50)) elif is_substandard: api_pct = float(np.clip(rng.normal(65, 15), 25, 84)) else: api_pct = float(np.clip(rng.normal(97, 4), 85, 115)) api_failure = int(api_pct < 85) dissolution_failure = int(is_sf and rng.random() < 0.35) rifampicin_degraded = int(drug in ("rifampicin", "RHZE_FDC", "RH_FDC") and is_sf and rng.random() < 0.40) fdc_content_variation = int("FDC" in drug and is_sf and rng.random() < 0.30) # Storage storage_poor = int(rng.random() < 0.30) humidity_exposed = int(storage_poor and rng.random() < 0.40) # Treatment outcomes sf_mult = 2.0 if is_sf else 1.0 treatment_failure = int(rng.random() < np.clip( params["treatment_failure_rate"] * sf_mult, 0, 0.40)) sputum_conversion_delayed = int(treatment_failure and rng.random() < 0.50) relapse = int(treatment_failure and rng.random() < 0.25) mdr_emergence = int(is_sf and rng.random() < 0.05) death = int(treatment_failure and rng.random() < 0.08) adr = int(rng.random() < 0.05) hepatotoxicity = int(adr and drug in ("RHZE_FDC", "isoniazid", "pyrazinamide", "rifampicin") and rng.random() < 0.40) # DOTS adherence dots_observed = int(setting in ("TB_clinic", "community_dots") and rng.random() < 0.60) treatment_completed = int(not treatment_failure and rng.random() < 0.75) lost_to_followup = int(rng.random() < 0.10) # Surveillance quality_tested = int(rng.random() < params["quality_tested_pct"]) genexpert_done = int(rng.random() < 0.35) dst_done = int(treatment_failure and rng.random() < 0.20) reported_to_ntp = int(quality_tested and is_sf and rng.random() < 0.30) any_adverse = int(treatment_failure or adr or death or mdr_emergence) record = { "record_id": f"{name[:3].upper()}-{idx:05d}", "scenario": name, "year": year, "setting": setting, "age": age, "sex": sex, "hiv_coinfected": hiv_coinfected, "treatment_phase": treatment_phase, "drug": drug, "manufacturer": manufacturer, "gdf_procured": gdf_procured, "is_substandard_falsified": is_sf, "is_falsified": is_falsified, "api_pct_label": round(api_pct, 1), "api_failure": api_failure, "dissolution_failure": dissolution_failure, "rifampicin_degraded": rifampicin_degraded, "storage_poor": storage_poor, "treatment_failure": treatment_failure, "sputum_conversion_delayed": sputum_conversion_delayed, "mdr_emergence": mdr_emergence, "death": death, "hepatotoxicity": hepatotoxicity, "dots_observed": dots_observed, "treatment_completed": treatment_completed, "lost_to_followup": lost_to_followup, "quality_tested": quality_tested, "genexpert_done": genexpert_done, "reported_to_ntp": reported_to_ntp, "any_adverse": any_adverse, } 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()