#!/usr/bin/env python3 """ Literature-Informed Laboratory Reagent Supply Dataset ======================================================= Each record = ONE lab reagent/consumable observation at ONE facility for ONE quarterly reporting period. Sources: [1] WHO SARA. Laboratory service readiness indicators. Haematology, biochemistry, microbiology reagent availability. [2] Lancet Commission on Diagnostics (2021). 47% of population in LMICs lack access to diagnostics. 1% of health spending on dx. [3] ASLM (African Society for Laboratory Medicine). Lab strengthening. [4] PEPFAR lab network. GeneXpert, viral load, EID reagent supply. """ import numpy as np import pandas as pd import argparse import os LAB_REAGENTS = [ ('glucose_strips', 'biochemistry', 'POC_strip', False, 0.15), ('haemoglobin_reagent', 'haematology', 'reagent_liquid', False, 0.20), ('urinalysis_dipstick', 'urinalysis', 'POC_strip', False, 0.08), ('blood_grouping_antisera', 'blood_bank', 'reagent_liquid', True, 2.50), ('RPR_syphilis_test', 'serology', 'rapid_test', False, 0.40), ('hepatitis_B_rapid', 'serology', 'rapid_test', False, 0.60), ('hepatitis_C_rapid', 'serology', 'rapid_test', False, 0.80), ('GeneXpert_TB_cartridge', 'molecular', 'cartridge', False, 9.98), ('GeneXpert_VL_cartridge', 'molecular', 'cartridge', False, 12.00), ('GeneXpert_EID_cartridge', 'molecular', 'cartridge', False, 15.00), ('malaria_microscopy_giemsa', 'parasitology', 'stain', False, 0.05), ('gram_stain_kit', 'microbiology', 'stain_kit', False, 0.30), ('blood_culture_bottles', 'microbiology', 'culture_media', True, 3.00), ('Mueller_Hinton_agar', 'microbiology', 'culture_media', True, 1.50), ('CD4_reagent_cartridge', 'immunology', 'cartridge', True, 5.00), ('chemistry_analyzer_reagent', 'biochemistry', 'reagent_pack', True, 25.00), ('CBC_reagent_pack', 'haematology', 'reagent_pack', True, 15.00), ('pregnancy_test_hCG', 'POC', 'rapid_test', False, 0.20), ] STOCKOUT_CAUSES = [ 'procurement_delay', 'supplier_backorder', 'cold_chain_break', 'equipment_down_no_reagent_use', 'expired_stock', 'funding_gap', 'quantification_error', 'distribution_delay', 'import_clearance', 'single_source_dependency', 'quality_rejection', ] SCENARIOS = { 'reference_laboratory': { 'facility_level': 'reference_lab', 'has_molecular': True, 'has_culture': True, 'has_chemistry_analyzer': True, 'has_cold_storage': True, 'staff_lab_scientists': 8, 'base_reagent_availability': 0.82, 'base_molecular_availability': 0.70, 'stockout_6m_rate': 0.22, 'mean_stockout_days': 12, 'tests_per_month': 3000, }, 'district_laboratory': { 'facility_level': 'district_lab', 'has_molecular': True, 'has_culture': False, 'has_chemistry_analyzer': True, 'has_cold_storage': False, 'staff_lab_scientists': 3, 'base_reagent_availability': 0.58, 'base_molecular_availability': 0.40, 'stockout_6m_rate': 0.48, 'mean_stockout_days': 25, 'tests_per_month': 800, }, 'health_centre_lab': { 'facility_level': 'health_centre', 'has_molecular': False, 'has_culture': False, 'has_chemistry_analyzer': False, 'has_cold_storage': False, 'staff_lab_scientists': 1, 'base_reagent_availability': 0.32, 'base_molecular_availability': 0.02, 'stockout_6m_rate': 0.72, 'mean_stockout_days': 45, 'tests_per_month': 150, }, } def generate_dataset(n=10000, seed=42, scenario='district_laboratory'): rng = np.random.default_rng(seed) sc = SCENARIOS[scenario] records = [] n_reagents = len(LAB_REAGENTS) for idx in range(n): rec = {'id': idx + 1} rec['facility_level'] = sc['facility_level'] rec['facility_id'] = f"LAB_{rng.integers(1, 200):04d}" rec['region_type'] = rng.choice(['urban', 'peri_urban', 'rural'], p=[0.10, 0.20, 0.70] if scenario == 'health_centre_lab' else ([0.50, 0.30, 0.20] if scenario == 'reference_laboratory' else [0.25, 0.35, 0.40])) rec['lab_staff_count'] = max(1, int(rng.poisson(sc['staff_lab_scientists']))) rec['has_molecular_platform'] = 1 if sc['has_molecular'] else ( 1 if rng.random() < 0.03 else 0) rec['has_culture_capability'] = 1 if sc['has_culture'] else ( 1 if rng.random() < 0.02 else 0) rec['has_chemistry_analyzer'] = 1 if sc['has_chemistry_analyzer'] else ( 1 if rng.random() < 0.05 else 0) rec['has_cold_storage'] = 1 if sc['has_cold_storage'] else ( 1 if rng.random() < 0.10 else 0) rec['equipment_functional'] = 1 if rng.random() < ( 0.80 if scenario == 'reference_laboratory' else (0.55 if scenario == 'district_laboratory' else 0.25)) else 0 r_idx = idx % n_reagents reagent = LAB_REAGENTS[r_idx] rec['reagent_name'] = reagent[0] rec['department'] = reagent[1] rec['format'] = reagent[2] rec['cold_chain_required'] = 1 if reagent[3] else 0 rec['unit_cost_usd'] = reagent[4] rec['year'] = rng.choice([2021, 2022, 2023, 2024], p=[0.15, 0.25, 0.30, 0.30]) rec['quarter'] = rng.choice([1, 2, 3, 4]) if rec['department'] == 'molecular': base_avail = sc['base_molecular_availability'] elif rec['cold_chain_required'] and not rec['has_cold_storage']: base_avail = sc['base_reagent_availability'] * 0.5 else: base_avail = sc['base_reagent_availability'] rec['available_on_survey_day'] = 1 if rng.random() < np.clip(base_avail, 0.02, 0.98) else 0 rec['stocked_out_in_last_6m'] = 1 if rng.random() < np.clip( sc['stockout_6m_rate'] + (0.10 if rec['cold_chain_required'] else 0), 0.02, 0.95) else 0 rec['stockout_days_last_6m'] = 0 if rec['stocked_out_in_last_6m']: rec['stockout_days_last_6m'] = max(1, min(180, int(rng.exponential(sc['mean_stockout_days'] * 0.7)))) rec['stockout_cause'] = 'not_applicable' if rec['stocked_out_in_last_6m']: if scenario == 'health_centre_lab': cause_p = [0.08, 0.05, 0.05, 0.08, 0.10, 0.12, 0.10, 0.20, 0.05, 0.08, 0.09] elif scenario == 'district_laboratory': cause_p = [0.12, 0.10, 0.08, 0.10, 0.08, 0.10, 0.10, 0.12, 0.08, 0.08, 0.04] else: cause_p = [0.10, 0.15, 0.05, 0.08, 0.08, 0.08, 0.08, 0.08, 0.12, 0.12, 0.06] rec['stockout_cause'] = rng.choice(STOCKOUT_CAUSES, p=cause_p) rec['tests_performed_month'] = max(0, int(rng.poisson( sc['tests_per_month'] / n_reagents))) rec['tests_not_done_no_reagent'] = 0 if not rec['available_on_survey_day']: rec['tests_not_done_no_reagent'] = max(0, int(rng.exponential( rec['tests_performed_month'] * 0.3 + 5))) rec['sample_referred_out'] = 0 if not rec['available_on_survey_day'] and rng.random() < ( 0.50 if scenario == 'reference_laboratory' else (0.25 if scenario == 'district_laboratory' else 0.05)): rec['sample_referred_out'] = 1 rec['turnaround_time_days'] = max(0, round(rng.exponential( 1 if scenario == 'reference_laboratory' else (3 if scenario == 'district_laboratory' else 7)), 1)) rec['months_of_stock'] = max(0, round(rng.normal( 3.5 if scenario == 'reference_laboratory' else (1.5 if scenario == 'district_laboratory' else 0.4), 1.2), 1)) rec['order_fill_rate_pct'] = round(np.clip(rng.normal( 78 if scenario == 'reference_laboratory' else (52 if scenario == 'district_laboratory' else 25), 15), 5, 100), 1) rec['expired_reagent_found'] = 1 if rng.random() < ( 0.08 if scenario == 'reference_laboratory' else (0.18 if scenario == 'district_laboratory' else 0.30)) else 0 rec['report_submitted'] = 1 if rng.random() < ( 0.90 if scenario == 'reference_laboratory' else (0.55 if scenario == 'district_laboratory' else 0.15)) else 0 records.append(rec) df = pd.DataFrame(records) print(f"\n{'='*65}") print(f"Laboratory Reagent Supply — {scenario} (n={n}, seed={seed})") print(f"{'='*65}") print(f" Available: {df['available_on_survey_day'].mean()*100:.1f}%") print(f" Stocked out 6m: {df['stocked_out_in_last_6m'].mean()*100:.1f}%") print(f" Equipment functional: {df['equipment_functional'].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'lab_{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', 'lab_district_laboratory.csv'), index=False)