File size: 8,019 Bytes
72c2af9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python3
"""
HIV Viral Load & CD4 Testing Dataset
========================================
Each record = ONE HIV viral load or CD4 test request/result.

Literature-Grounded Parameterization:
[1] WHO (2023). HIV treatment guidelines.
    - VL preferred monitoring; target <1000 copies/mL
    - CD4 for staging and OI prophylaxis
    - VL coverage SSA: ~70% of ART patients

[2] UNAIDS (2023). Global HIV statistics.
    - SSA: 25.6M PLHIV, 21M on ART
    - VL suppression: 76% of those on ART

[3] Lecher SL et al. (2021). Scale-up HIV VL monitoring.
    MMWR. DOI: 10.15585/mmwr.mm7034a2
    - VL coverage 17% to 71% (2015-2019)
    - Centralized testing: TAT weeks

[4] Jani IV et al. (2016). POC CD4 and VL testing.
    Lancet HIV. DOI: 10.1016/S2352-3018(15)00236-X
    - POC reduces loss to follow-up
    - Conventional VL TAT: 2-6 weeks
    - POC VL TAT: 1-3 hours
    - DBS for sample transport
"""
import numpy as np, pandas as pd, argparse, os

SCENARIOS = {
    'vl_accessible': {
        'exemplar': 'South Africa/Botswana/Kenya (urban)',
        'vl_coverage': 0.75,
        'cd4_available': 0.85,
        'poc_vl_available': 0.20,
        'dbs_used': 0.15,
        'conventional_vl': 0.65,
        'vl_suppression_rate': 0.80,
        'cd4_median_on_art': 450,
        'tat_vl_days': 7,
        'tat_cd4_days': 1,
        'eac_for_unsuppressed': 0.60,
        'result_returned_patient': 0.70,
        'reagent_stockout': 0.10,
    },
    'vl_limited': {
        'exemplar': 'Kenya (district)/Ghana/Tanzania/Ethiopia',
        'vl_coverage': 0.35,
        'cd4_available': 0.50,
        'poc_vl_available': 0.05,
        'dbs_used': 0.30,
        'conventional_vl': 0.25,
        'vl_suppression_rate': 0.72,
        'cd4_median_on_art': 380,
        'tat_vl_days': 21,
        'tat_cd4_days': 3,
        'eac_for_unsuppressed': 0.30,
        'result_returned_patient': 0.40,
        'reagent_stockout': 0.30,
    },
    'no_vl_access': {
        'exemplar': 'DRC/CAR/Sierra Leone/Niger (rural)',
        'vl_coverage': 0.08,
        'cd4_available': 0.15,
        'poc_vl_available': 0.01,
        'dbs_used': 0.05,
        'conventional_vl': 0.05,
        'vl_suppression_rate': 0.60,
        'cd4_median_on_art': 300,
        'tat_vl_days': 45,
        'tat_cd4_days': 14,
        'eac_for_unsuppressed': 0.10,
        'result_returned_patient': 0.15,
        'reagent_stockout': 0.55,
    },
}

def generate_dataset(n=10000, seed=42, scenario='vl_limited'):
    rng = np.random.default_rng(seed)
    sc = SCENARIOS[scenario]
    records = []
    for idx in range(n):
        rec = {'id': idx + 1}
        rec['age'] = int(np.clip(rng.normal(38, 12), 1, 80))
        rec['sex'] = rng.choice(['male','female'], p=[0.38, 0.62])
        rec['pregnant'] = 1 if (rec['sex']=='female' and 15<=rec['age']<=45 and rng.random()<0.08) else 0
        rec['facility_level'] = rng.choice(['tertiary','district','health_centre','clinic'],
            p=[0.10, 0.25, 0.35, 0.30])
        rec['on_art'] = 1 if rng.random() < 0.85 else 0
        rec['art_duration_months'] = int(rng.exponential(36)) if rec['on_art'] else 0
        rec['art_regimen'] = rng.choice(['TLD','TLE','AZT_based','PI_based','other'],
            p=[0.40, 0.25, 0.15, 0.10, 0.10]) if rec['on_art'] else 'none'

        # Test type ordered
        rec['vl_ordered'] = 1 if rng.random() < sc['vl_coverage'] else 0
        rec['cd4_ordered'] = 1 if rng.random() < sc['cd4_available'] else 0

        # VL testing
        tier = 1.3 if rec['facility_level'] in ['tertiary','district'] else 0.7
        if rec['vl_ordered']:
            rec['vl_platform'] = rng.choice(['conventional_central','poc_vl','dbs_referred'],
                p=[sc['conventional_vl']*tier/(sc['conventional_vl']*tier+sc['poc_vl_available']+sc['dbs_used']+0.01),
                   sc['poc_vl_available']/(sc['conventional_vl']*tier+sc['poc_vl_available']+sc['dbs_used']+0.01),
                   (sc['dbs_used']+0.01)/(sc['conventional_vl']*tier+sc['poc_vl_available']+sc['dbs_used']+0.01)])
            if rec['on_art']:
                suppressed = rng.random() < sc['vl_suppression_rate']
                if suppressed:
                    rec['vl_result'] = int(np.clip(rng.exponential(50), 0, 999))
                else:
                    rec['vl_result'] = int(np.clip(rng.lognormal(np.log(5000), 1.5), 1000, 500000))
            else:
                rec['vl_result'] = int(np.clip(rng.lognormal(np.log(50000), 1.2), 1000, 1000000))
            rec['vl_suppressed'] = 1 if rec['vl_result'] < 1000 else 0
            rec['vl_tat_days'] = round(max(0.1, rng.lognormal(
                np.log(0.1 if rec['vl_platform']=='poc_vl' else sc['tat_vl_days']), 0.5)), 1)
        else:
            rec['vl_platform'] = 'not_done'
            rec['vl_result'] = np.nan
            rec['vl_suppressed'] = np.nan
            rec['vl_tat_days'] = np.nan

        # CD4 testing
        if rec['cd4_ordered']:
            rec['cd4_platform'] = rng.choice(['flow_cytometry','poc_cd4','manual'],
                p=[0.40, 0.30, 0.30] if sc['cd4_available']>0.5 else [0.15, 0.25, 0.60])
            if rec['on_art']:
                rec['cd4_result'] = int(np.clip(rng.normal(sc['cd4_median_on_art'], 180), 10, 1500))
            else:
                rec['cd4_result'] = int(np.clip(rng.lognormal(np.log(200), 0.7), 5, 800))
            rec['cd4_below_200'] = 1 if rec['cd4_result'] < 200 else 0
            rec['cd4_tat_days'] = round(max(0.1, rng.lognormal(np.log(sc['tat_cd4_days']), 0.5)), 1)
        else:
            rec['cd4_platform'] = 'not_done'
            rec['cd4_result'] = np.nan
            rec['cd4_below_200'] = np.nan
            rec['cd4_tat_days'] = np.nan

        # Clinical actions
        rec['result_returned'] = 1 if rng.random() < sc['result_returned_patient'] else 0
        rec['eac_provided'] = 1 if (rec['vl_ordered'] and rec.get('vl_suppressed')==0 and rng.random() < sc['eac_for_unsuppressed']) else 0
        rec['regimen_switch'] = 1 if (rec['vl_ordered'] and rec.get('vl_suppressed')==0 and rng.random() < 0.15) else 0
        rec['oi_prophylaxis'] = 1 if (rec['cd4_ordered'] and rec.get('cd4_below_200')==1 and rng.random() < 0.65) else 0

        # Quality
        rec['reagent_stockout'] = 1 if rng.random() < sc['reagent_stockout'] else 0
        rec['sample_rejected'] = 1 if rng.random() < 0.04 else 0
        rec['test_not_done_stockout'] = 1 if (rec['reagent_stockout'] and rng.random() < 0.40) else 0
        rec['specimen_transport_issue'] = 1 if (rec.get('vl_platform') in ['dbs_referred','conventional_central'] and rng.random() < 0.12) else 0

        rec['year'] = rng.choice([2019,2020,2021,2022,2023], p=[0.12,0.18,0.20,0.25,0.25])
        records.append(rec)

    df = pd.DataFrame(records)
    print(f"\n{'='*60}\nHIV Viral Load & CD4 — {scenario} ({sc['exemplar']})")
    print(f"  VL ordered: {df['vl_ordered'].mean()*100:.1f}% | CD4 ordered: {df['cd4_ordered'].mean()*100:.1f}%")
    vl_done = df[df['vl_ordered']==1]
    if len(vl_done)>0: print(f"  VL suppressed: {vl_done['vl_suppressed'].mean()*100:.1f}% | VL TAT median: {vl_done['vl_tat_days'].median():.1f}d")
    print(f"  Result returned: {df['result_returned'].mean()*100:.1f}% | Stockout: {df['reagent_stockout'].mean()*100:.1f}%")
    return df

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--all-scenarios', action='store_true')
    parser.add_argument('--n', type=int, default=10000)
    parser.add_argument('--seed', type=int, default=42)
    args = parser.parse_args()
    os.makedirs('data', exist_ok=True)
    if args.all_scenarios:
        for sc in SCENARIOS:
            df = generate_dataset(n=args.n, seed=args.seed, scenario=sc)
            df.to_csv(os.path.join('data', f'hiv_vl_cd4_{sc}.csv'), index=False)
            print(f"  -> Saved\n")
    else:
        df = generate_dataset(n=args.n, seed=args.seed)
        df.to_csv(os.path.join('data', 'hiv_vl_cd4_vl_limited.csv'), index=False)