Kossisoroyce's picture
Upload folder using huggingface_hub
35f52bf verified
"""Generate synthetic chemical poisoning & toxicology dataset for SSA.
Research-based parameterization:
- BMC Public Health (2023): Child/adolescent pesticide poisoning mortality
in SA; street pesticides (aldicarb, organophosphates) sold illegally.
- Frontiers (2023): Children poisoning in LMICs - 4x higher mortality;
medications, pesticides, kerosene, household chemicals.
- StatPearls: Organophosphate toxicity highest in agricultural developing
nations with less stringent regulations.
- Beyond Pesticides (2024): Dozens of children died in SA from
unregulated pesticide use in communities.
- WHO: Poisoning causes ~45,000 deaths/yr in Africa.
"""
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 = {
"agricultural_pesticide": {
"setting_probs": {"rural_farm": 0.50, "peri_urban": 0.30, "urban": 0.20},
"agent_probs": {"organophosphate": 0.35, "carbamate": 0.20, "pyrethroid": 0.15,
"herbicide": 0.15, "rodenticide": 0.10, "fungicide": 0.05},
"intent_probs": {"accidental_occupational": 0.40, "accidental_child": 0.20,
"intentional_self_harm": 0.25, "intentional_other": 0.05, "unknown": 0.10},
"mortality_rate": 0.08,
"child_pct": 0.25,
"ppe_use_pct": 0.12,
"poison_centre_access": 0.05,
"antidote_available": 0.30,
},
"household_chemical_urban": {
"setting_probs": {"urban_informal": 0.40, "urban_formal": 0.30, "peri_urban": 0.30},
"agent_probs": {"kerosene_paraffin": 0.30, "bleach_caustic": 0.20,
"medication_overdose": 0.20, "rat_poison_street": 0.15,
"traditional_medicine": 0.10, "other_chemical": 0.05},
"intent_probs": {"accidental_child": 0.40, "accidental_adult": 0.15,
"intentional_self_harm": 0.30, "intentional_other": 0.05, "unknown": 0.10},
"mortality_rate": 0.05,
"child_pct": 0.45,
"ppe_use_pct": 0.0,
"poison_centre_access": 0.10,
"antidote_available": 0.40,
},
"industrial_occupational": {
"setting_probs": {"industrial": 0.45, "mining": 0.25, "construction": 0.15, "urban": 0.15},
"agent_probs": {"solvent_hydrocarbon": 0.25, "heavy_metal_compound": 0.20,
"acid_alkali": 0.15, "gas_fume": 0.20,
"pesticide_industrial": 0.10, "other_industrial": 0.10},
"intent_probs": {"accidental_occupational": 0.65, "accidental_other": 0.15,
"intentional_self_harm": 0.10, "intentional_other": 0.02, "unknown": 0.08},
"mortality_rate": 0.06,
"child_pct": 0.05,
"ppe_use_pct": 0.20,
"poison_centre_access": 0.08,
"antidote_available": 0.35,
},
}
SCENARIO_FILES = {
"agricultural_pesticide": "poisoning_agricultural.csv",
"household_chemical_urban": "poisoning_household.csv",
"industrial_occupational": "poisoning_industrial.csv",
}
ROUTES = {"ingestion": 0.50, "dermal": 0.20, "inhalation": 0.20, "injection": 0.02, "ocular": 0.08}
SEVERITY = {"mild": 0.35, "moderate": 0.35, "severe": 0.20, "fatal": 0.10}
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"])
is_child = int(rng.random() < params["child_pct"])
age = int(np.clip(rng.normal(3, 1.5) if is_child else rng.normal(32, 12), 0, 70))
sex = rng.choice(["male", "female"], p=[0.55, 0.45])
agent = _choice(rng, params["agent_probs"])
intent = _choice(rng, params["intent_probs"])
route = _choice(rng, ROUTES)
time_to_presentation_hrs = float(np.clip(rng.lognormal(np.log(3), 0.8), 0.5, 72))
delayed_presentation = int(time_to_presentation_hrs > 6)
severity = _choice(rng, SEVERITY)
if intent == "intentional_self_harm":
severity = rng.choice(["moderate", "severe", "fatal"], p=[0.30, 0.45, 0.25])
# Clinical features
gi_symptoms = int(route == "ingestion" and rng.random() < 0.70)
respiratory_distress = int(route == "inhalation" and rng.random() < 0.50)
cholinergic_crisis = int(agent in ("organophosphate", "carbamate") and rng.random() < 0.45)
seizures = int(severity in ("severe", "fatal") and rng.random() < 0.15)
altered_consciousness = int(severity in ("severe", "fatal") and rng.random() < 0.30)
chemical_burn = int(agent in ("acid_alkali", "bleach_caustic") and rng.random() < 0.40)
aspiration_pneumonia = int(agent == "kerosene_paraffin" and rng.random() < 0.25)
# Management
ppe_use = int(rng.random() < params["ppe_use_pct"])
decontamination = int(rng.random() < 0.40)
activated_charcoal = int(route == "ingestion" and time_to_presentation_hrs < 2 and rng.random() < 0.30)
antidote_available = int(rng.random() < params["antidote_available"])
antidote_given = int(antidote_available and severity in ("moderate", "severe", "fatal") and rng.random() < 0.70)
atropine_given = int(cholinergic_crisis and rng.random() < 0.60)
icu_admission = int(severity in ("severe", "fatal") and rng.random() < 0.40)
ventilator = int(icu_admission and rng.random() < 0.30)
poison_centre_consulted = int(rng.random() < params["poison_centre_access"])
referred_higher = int(severity in ("severe", "fatal") and rng.random() < 0.50)
# Outcomes
died = int(severity == "fatal" and rng.random() < params["mortality_rate"] * 10)
sequelae = int(severity in ("severe", "fatal") and not died and rng.random() < 0.15)
hospital_days = int(np.clip(
rng.poisson(1 if severity == "mild" else 3 if severity == "moderate" else 7), 0, 30))
# Prevention
safe_storage = int(rng.random() < 0.20)
child_proof_container = int(is_child and rng.random() < 0.05)
labelled_container = int(rng.random() < 0.30)
pesticide_regulation = int(rng.random() < 0.15)
record = {
"record_id": f"{name[:3].upper()}-{idx:05d}",
"scenario": name,
"year": year,
"setting": setting,
"age": age,
"sex": sex,
"is_child": is_child,
"agent": agent,
"intent": intent,
"route": route,
"time_to_presentation_hrs": round(time_to_presentation_hrs, 1),
"delayed_presentation": delayed_presentation,
"severity": severity,
"gi_symptoms": gi_symptoms,
"respiratory_distress": respiratory_distress,
"cholinergic_crisis": cholinergic_crisis,
"seizures": seizures,
"altered_consciousness": altered_consciousness,
"chemical_burn": chemical_burn,
"aspiration_pneumonia": aspiration_pneumonia,
"ppe_use": ppe_use,
"decontamination": decontamination,
"activated_charcoal": activated_charcoal,
"antidote_available": antidote_available,
"antidote_given": antidote_given,
"atropine_given": atropine_given,
"icu_admission": icu_admission,
"ventilator": ventilator,
"poison_centre_consulted": poison_centre_consulted,
"referred_higher": referred_higher,
"died": died,
"sequelae": sequelae,
"hospital_days": hospital_days,
"safe_storage": safe_storage,
"child_proof_container": child_proof_container,
"labelled_container": labelled_container,
"pesticide_regulation": pesticide_regulation,
}
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()