#!/usr/bin/env python3 """ Literature-Informed Blood Bank Supply Management Dataset ========================================================= Each record = ONE blood product observation at ONE facility for ONE monthly reporting period. Sources: [1] WHO (2022). Blood safety and availability. SSA collects ~5 units per 1000 population vs 30+ in HICs. Only 58% of countries meet 100% voluntary non-remunerated blood donation (VNRD). [2] Lancet (2019). Blood transfusion in SSA. Chronic shortage, high rates of family/replacement donors, limited screening. [3] Africa CDC. Blood safety. Transfusion-transmissible infections (TTI) screening gaps: HIV, HBV, HCV, syphilis. [4] WHO AFRO. National Blood Transfusion Services. Centralised vs hospital-based collection. Cold chain for blood products. """ import numpy as np import pandas as pd import argparse import os BLOOD_PRODUCTS = [ ('whole_blood', 'red_cells', 35, 42, 40.00), ('packed_RBC', 'red_cells', 35, 42, 55.00), ('fresh_frozen_plasma', 'plasma', -25, 365, 45.00), ('platelet_concentrate', 'platelets', 22, 5, 60.00), ('cryoprecipitate', 'plasma_derived', -25, 365, 50.00), ('group_O_neg_emergency', 'red_cells', 35, 42, 65.00), ] BLOOD_GROUPS = ['O_pos', 'O_neg', 'A_pos', 'A_neg', 'B_pos', 'B_neg', 'AB_pos', 'AB_neg'] BG_PROBS_SSA = [0.47, 0.04, 0.21, 0.02, 0.19, 0.02, 0.04, 0.01] SHORTAGE_CAUSES = [ 'insufficient_donors', 'TTI_positive_discard', 'cold_chain_break', 'expired_units', 'demand_surge_trauma', 'demand_surge_obstetric', 'collection_campaign_failed', 'reagent_stockout_crossmatch', 'power_outage_storage', 'transport_delay', 'seasonal_low_donation', ] SCENARIOS = { 'national_blood_centre': { 'facility_level': 'national_blood_centre', 'collection_type': 'VNRD_centralised', 'has_component_separation': True, 'has_NAT_testing': True, 'has_blood_bank_fridge': True, 'has_platelet_agitator': True, 'collection_per_1000_pop': 5.0, 'tti_screening_complete': 0.95, 'units_available_rate': 0.75, 'expiry_wastage_rate': 0.12, 'shortage_rate': 0.25, 'mean_shortage_days': 8, 'crossmatch_turnaround_hours': 2, }, 'district_hospital_bb': { 'facility_level': 'district_hospital', 'collection_type': 'family_replacement', 'has_component_separation': False, 'has_NAT_testing': False, 'has_blood_bank_fridge': True, 'has_platelet_agitator': False, 'collection_per_1000_pop': 2.5, 'tti_screening_complete': 0.70, 'units_available_rate': 0.45, 'expiry_wastage_rate': 0.20, 'shortage_rate': 0.55, 'mean_shortage_days': 20, 'crossmatch_turnaround_hours': 6, }, 'rural_hospital_bb': { 'facility_level': 'rural_hospital', 'collection_type': 'emergency_only', 'has_component_separation': False, 'has_NAT_testing': False, 'has_blood_bank_fridge': False, 'has_platelet_agitator': False, 'collection_per_1000_pop': 0.5, 'tti_screening_complete': 0.30, 'units_available_rate': 0.15, 'expiry_wastage_rate': 0.30, 'shortage_rate': 0.82, 'mean_shortage_days': 45, 'crossmatch_turnaround_hours': 24, }, } def generate_dataset(n=10000, seed=42, scenario='district_hospital_bb'): rng = np.random.default_rng(seed) sc = SCENARIOS[scenario] records = [] n_prod = len(BLOOD_PRODUCTS) for idx in range(n): rec = {'id': idx + 1} rec['facility_level'] = sc['facility_level'] rec['facility_id'] = f"BB_{rng.integers(1, 150):04d}" rec['region_type'] = rng.choice(['urban', 'peri_urban', 'rural'], p=[0.05, 0.15, 0.80] if scenario == 'rural_hospital_bb' else ([0.55, 0.25, 0.20] if scenario == 'national_blood_centre' else [0.20, 0.35, 0.45])) rec['collection_type'] = sc['collection_type'] rec['has_component_separation'] = 1 if sc['has_component_separation'] else 0 rec['has_NAT_testing'] = 1 if sc['has_NAT_testing'] else 0 rec['has_blood_bank_fridge'] = 1 if sc['has_blood_bank_fridge'] else ( 1 if rng.random() < 0.15 else 0) rec['has_platelet_agitator'] = 1 if sc['has_platelet_agitator'] else 0 rec['blood_bank_staff_count'] = max(1, int(rng.poisson( 8 if scenario == 'national_blood_centre' else (3 if scenario == 'district_hospital_bb' else 1)))) prod_idx = idx % n_prod prod = BLOOD_PRODUCTS[prod_idx] rec['product_name'] = prod[0] rec['product_category'] = prod[1] rec['storage_temp_C'] = prod[2] rec['shelf_life_days'] = prod[3] rec['unit_cost_usd'] = prod[4] rec['blood_group'] = rng.choice(BLOOD_GROUPS, p=BG_PROBS_SSA) rec['year'] = rng.choice([2021, 2022, 2023, 2024], p=[0.15, 0.25, 0.30, 0.30]) rec['month'] = rng.integers(1, 13) rec['units_collected_month'] = max(0, int(rng.poisson( 200 if scenario == 'national_blood_centre' else (40 if scenario == 'district_hospital_bb' else 5)))) rec['units_requested_month'] = max(0, int(rng.poisson( 250 if scenario == 'national_blood_centre' else (50 if scenario == 'district_hospital_bb' else 8)))) rec['units_crossmatched'] = max(0, min(rec['units_collected_month'], int(rng.poisson(rec['units_requested_month'] * 0.8)))) rec['units_transfused'] = max(0, min(rec['units_crossmatched'], int(rec['units_crossmatched'] * rng.normal(0.75, 0.10)))) rec['tti_screened_pct'] = round(np.clip( rng.normal(sc['tti_screening_complete'], 0.10), 0.10, 1.0), 2) rec['tti_positive_pct'] = round(np.clip(rng.normal( 5.0 if scenario == 'national_blood_centre' else (8.0 if scenario == 'district_hospital_bb' else 12.0), 3.0), 0.5, 30.0), 1) rec['units_discarded_tti'] = max(0, int( rec['units_collected_month'] * rec['tti_positive_pct'] / 100)) rec['units_expired'] = max(0, int( rec['units_collected_month'] * rng.normal( sc['expiry_wastage_rate'], 0.05))) rec['available_on_survey_day'] = 1 if rng.random() < sc['units_available_rate'] else 0 rec['shortage_in_last_month'] = 1 if rng.random() < sc['shortage_rate'] else 0 rec['shortage_days_last_month'] = 0 if rec['shortage_in_last_month']: rec['shortage_days_last_month'] = max(1, min(30, int(rng.exponential(sc['mean_shortage_days'] * 0.5)))) rec['shortage_cause'] = 'not_applicable' if rec['shortage_in_last_month']: if scenario == 'rural_hospital_bb': cause_p = [0.25, 0.05, 0.10, 0.08, 0.10, 0.12, 0.08, 0.05, 0.08, 0.05, 0.04] elif scenario == 'district_hospital_bb': cause_p = [0.20, 0.08, 0.08, 0.10, 0.10, 0.10, 0.08, 0.08, 0.06, 0.06, 0.06] else: cause_p = [0.12, 0.10, 0.05, 0.10, 0.12, 0.10, 0.10, 0.10, 0.05, 0.06, 0.10] rec['shortage_cause'] = rng.choice(SHORTAGE_CAUSES, p=cause_p) rec['crossmatch_turnaround_hours'] = max(0.5, round( rng.exponential(sc['crossmatch_turnaround_hours'] * 0.5), 1)) rec['transfusion_reactions_month'] = max(0, int(rng.poisson( 0.5 if scenario == 'national_blood_centre' else (0.3 if scenario == 'district_hospital_bb' else 0.1)))) rec['maternal_deaths_no_blood'] = 0 if rec['shortage_in_last_month']: rec['maternal_deaths_no_blood'] = max(0, int(rng.poisson( 0.3 if scenario == 'national_blood_centre' else (0.5 if scenario == 'district_hospital_bb' else 0.2)))) rec['surgical_cancellations_no_blood'] = 0 if rec['shortage_in_last_month']: rec['surgical_cancellations_no_blood'] = max(0, int(rng.exponential( 3 if scenario == 'national_blood_centre' else (2 if scenario == 'district_hospital_bb' else 0.5)))) rec['cold_chain_maintained'] = 1 if rec['has_blood_bank_fridge'] and rng.random() < ( 0.90 if scenario == 'national_blood_centre' else (0.60 if scenario == 'district_hospital_bb' else 0.20)) else 0 rec['report_submitted'] = 1 if rng.random() < ( 0.90 if scenario == 'national_blood_centre' else (0.50 if scenario == 'district_hospital_bb' else 0.12)) else 0 records.append(rec) df = pd.DataFrame(records) print(f"\n{'='*65}") print(f"Blood Bank Supply — {scenario} (n={n}, seed={seed})") print(f"{'='*65}") print(f" Available: {df['available_on_survey_day'].mean()*100:.1f}%") print(f" Shortage in month: {df['shortage_in_last_month'].mean()*100:.1f}%") print(f" TTI screened: {df['tti_screened_pct'].mean()*100:.1f}%") print(f" Expiry wastage: {df['units_expired'].sum()}/{df['units_collected_month'].sum()} units") 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'blood_{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', 'blood_district_hospital_bb.csv'), index=False)