""" HC-ONC-007: Leukemia Synthetic Dataset — Simulation Engine XpertSystems.ai | SKU: HC-ONC-007 | Version 1.0.0 | April 2026 HIPAA-Safe Synthetic Data — No Real Patient Records Generates biologically consistent leukemia patient cohort covering: - AML, ALL, CML, CLL, APL subtypes with WHO 2022 classification - ELN 2022 cytogenetic/molecular risk stratification - Treatment protocols: 7+3, HyperCVAD, Venetoclax+AZA, ATRA+ATO, TKI - MRD response trajectory modeling - Bone marrow transplant (HSCT) outcomes calibrated to CIBMTR benchmarks Usage: python hc_onc_007_simulation_engine.py --n_patients 25000 --seed 42 --output_dir ./output """ import argparse import os import json import numpy as np import pandas as pd from scipy.stats import weibull_min from datetime import datetime, timedelta import uuid # ───────────────────────────────────────────────────────────────────────────── # CONSTANTS & CLINICAL BENCHMARKS # ───────────────────────────────────────────────────────────────────────────── SUBTYPE_DIST = { "AML": 0.35, "ALL": 0.25, "CML": 0.20, "CLL": 0.15, "APL": 0.05 } # ELN 2022 AML risk distribution (among AML+APL) ELN_RISK_DIST = { "Favorable": 0.30, "Intermediate": 0.40, "Adverse": 0.30 } # CR rates by subtype (landmark trial calibration) CR_RATE = { "AML_Favorable": 0.82, "AML_Intermediate": 0.65, "AML_Adverse": 0.45, "ALL_Pediatric": 0.95, "ALL_Adult": 0.85, "APL": 0.97, "CML": None, # CML uses MMR endpoint "CLL": None, # CLL uses IWCLL response } # 5-year OS benchmarks (Weibull lambda calibration) OS_LAMBDA = { "AML_Favorable": 60.0, "AML_Intermediate": 36.0, "AML_Adverse": 14.0, "ALL_Pediatric": 110.0, "ALL_Adult": 40.0, "APL": 88.0, "CML": 130.0, "CLL": 120.0 } # BMT acute GvHD Grade II-IV incidence (CIBMTR 2019-2023) AGVHD_RATE = {"MRD": 0.30, "MUD": 0.45, "MMUD": 0.55, "Haploidentical": 0.48, "Cord_Blood": 0.35} # NRM at 1-year (CIBMTR benchmarks) NRM_RATE = {"MRD": 0.10, "MUD": 0.17, "MMUD": 0.22, "Haploidentical": 0.18, "Cord_Blood": 0.20} INDUCTION_AML_REGIMENS = [ "7+3_Daunorubicin", "7+3_Idarubicin", "CPX-351", "Venetoclax+AZA", "Venetoclax+LDAC", "Midostaurin+7+3", "Enasidenib+AZA", "Ivosidenib+AZA", "Glasdegib+LDAC", "BSC" ] INDUCTION_ALL_REGIMENS = [ "CALGB_10403", "HyperCVAD_A_B", "BFM_Protocol", "GRAALL_2005", "UKALL14", "Blinatumomab+Chemo", "Inotuzumab_Ozogamicin+Chemo", "Dasatinib+Steroids", "Ponatinib+Chemo" ] CML_REGIMENS = [ "Imatinib_400mg", "Dasatinib_100mg", "Nilotinib_300mg_BID", "Bosutinib_400mg", "Ponatinib_45mg", "Asciminib_40mg_BID" ] CLL_REGIMENS = [ "Watch_and_Wait", "FCR", "BR", "Ibrutinib", "Acalabrutinib", "Venetoclax_Obinutuzumab", "Venetoclax_Rituximab", "Pirtobrutinib", "Lisocabtagene_Maraleucel" ] DONOR_TYPES = ["MRD", "MUD", "MMUD", "Haploidentical", "Cord_Blood"] DONOR_DIST = [0.35, 0.38, 0.12, 0.10, 0.05] CONDITIONING = { "MAC": ["BuCy", "TBI_Cy", "FluBu4", "Cy_TBI"], "RIC": ["FluBu2", "FluMel140", "TBI_Flu_200"], "NMA": ["Flu_Cy_TBI_200", "Flu_Cy"] } GVHD_PROPHYLAXIS = [ "Tacrolimus_MTX", "CsA_MTX", "PT_Cy_Tacrolimus_MMF", "PT_Cy_Alone", "ATG_CsA_MTX" ] # ───────────────────────────────────────────────────────────────────────────── # HELPER FUNCTIONS # ───────────────────────────────────────────────────────────────────────────── def clamp(val, lo, hi): return max(lo, min(hi, val)) def weibull_sample(rng, k, lam, size=1): """Sample from Weibull distribution.""" return weibull_min.rvs(k, scale=lam, size=size, random_state=rng) def choice(rng, options, probs=None): probs = np.array(probs) if probs else None if probs is not None: probs = probs / probs.sum() return rng.choice(options, p=probs) def lognorm(rng, mu, sigma, lo, hi): v = np.exp(rng.normal(mu, sigma)) return clamp(v, lo, hi) # ───────────────────────────────────────────────────────────────────────────── # MODULE 1: PATIENT DEMOGRAPHICS # ───────────────────────────────────────────────────────────────────────────── def generate_demographics(rng, n): """Generate demographic attributes.""" subtypes = rng.choice(list(SUBTYPE_DIST.keys()), size=n, p=list(SUBTYPE_DIST.values())) ages = [] for st in subtypes: if st == "ALL": # Bimodal: pediatric peak ~6yr, adult peak ~35yr if rng.random() < 0.55: # 55% pediatric ALL a = rng.normal(6, 3) else: a = rng.normal(35, 12) elif st == "CLL": a = rng.normal(68, 9) elif st == "CML": a = rng.normal(52, 14) elif st == "APL": a = rng.normal(44, 13) else: # AML a = rng.normal(62, 14) ages.append(clamp(a, 0.5, 95)) ages = np.array(ages) sex = rng.choice(["Male", "Female"], size=n, p=[0.55, 0.45]) race = rng.choice( ["White", "Black", "Hispanic", "Asian", "Other"], size=n, p=[0.65, 0.12, 0.13, 0.08, 0.02] ) ecog = rng.choice([0, 1, 2, 3, 4], size=n, p=[0.25, 0.35, 0.25, 0.12, 0.03]) return pd.DataFrame({ "patient_id": [str(uuid.UUID(int=rng.integers(0, 2**128))) for _ in range(n)], "leukemia_type": subtypes, "age_at_diagnosis": np.round(ages, 1), "pediatric_flag": (ages < 18).astype(int), "sex": sex, "race": race, "ecog_performance_status": ecog, }) # ───────────────────────────────────────────────────────────────────────────── # MODULE 2: DISEASE CHARACTERISTICS # ───────────────────────────────────────────────────────────────────────────── def generate_disease_characteristics(rng, demo): n = len(demo) rows = [] for i, row in demo.iterrows(): st = row["leukemia_type"] age = row["age_at_diagnosis"] d = {} # WHO 2022 / FAB classification if st == "AML": d["who_2022_classification"] = choice(rng, [ "AML_NPM1", "AML_FLT3_ITD", "AML_CEBPA_biallelic", "AML_NUP98", "AML_t8_21", "AML_inv16", "AML_MRC", "AML_TP53", "AML_IDH2", "AML_IDH1", "AML_NOS" ]) d["fab_classification"] = choice(rng, ["M0","M1","M2","M3","M4","M5","M6","M7"], [0.05,0.15,0.25,0.08,0.20,0.15,0.05,0.07]) elif st == "APL": d["who_2022_classification"] = "APL_PML_RARA" d["fab_classification"] = "M3" elif st == "ALL": is_ped = age < 18 d["who_2022_classification"] = choice(rng, ["B-ALL_ETV6_RUNX1", "B-ALL_Hyperdiploidy", "B-ALL_Hypodiploidy", "B-ALL_BCR_ABL1", "B-ALL_KMT2A", "B-ALL_NOS", "T-ALL_NOS", "T-ALL_ETP"], [0.25 if is_ped else 0.05, 0.25 if is_ped else 0.10, 0.02, 0.05 if is_ped else 0.25, 0.08, 0.20, 0.10, 0.05]) d["fab_classification"] = choice(rng, ["L1","L2","L3"], [0.50,0.40,0.10]) elif st == "CML": d["who_2022_classification"] = choice(rng, ["CML_CP", "CML_AP", "CML_BP_Myeloid", "CML_BP_Lymphoid"], [0.85, 0.08, 0.05, 0.02]) d["fab_classification"] = "N/A" else: # CLL d["who_2022_classification"] = choice(rng, ["CLL_IGHV_Mutated", "CLL_IGHV_Unmutated", "SLL"], [0.50, 0.40, 0.10]) d["fab_classification"] = "N/A" d["disease_phase"] = choice(rng, ["Newly_Diagnosed", "Relapsed", "Refractory", "Relapsed_Refractory", "MRD_Positive"], [0.70, 0.12, 0.08, 0.07, 0.03]) d["de_novo_vs_secondary"] = choice(rng, ["de_novo", "secondary_to_MDS", "therapy_related"], [0.80, 0.12, 0.08]) if st in ["AML","APL"] else "N/A" # CBC at diagnosis if st in ["AML","APL"]: d["blast_pct_bone_marrow"] = clamp(rng.normal(55, 20), 20, 98) d["blast_pct_peripheral_blood"] = clamp(d["blast_pct_bone_marrow"] * rng.uniform(0.3, 0.9), 0, 95) d["wbc_k_ul"] = lognorm(rng, 3.2, 1.1, 0.5, 400) elif st == "ALL": d["blast_pct_bone_marrow"] = clamp(rng.normal(75, 18), 25, 99) d["blast_pct_peripheral_blood"] = clamp(d["blast_pct_bone_marrow"] * rng.uniform(0.4, 0.95), 0, 99) d["wbc_k_ul"] = lognorm(rng, 3.5, 1.3, 0.5, 500) elif st == "CML": d["blast_pct_bone_marrow"] = clamp(rng.normal(8, 5), 0, 19) d["blast_pct_peripheral_blood"] = clamp(rng.normal(4, 3), 0, 15) d["wbc_k_ul"] = lognorm(rng, 5.0, 0.9, 20, 500) else: # CLL d["blast_pct_bone_marrow"] = "N/A" d["blast_pct_peripheral_blood"] = "N/A" d["wbc_k_ul"] = lognorm(rng, 4.2, 0.8, 5, 300) d["hemoglobin_g_dl"] = round(clamp(rng.normal(8.5, 2.1), 4.0, 16.0), 1) d["platelets_k_ul"] = round(lognorm(rng, 4.8, 0.9, 5, 800), 0) d["ldh_u_l"] = round(lognorm(rng, 6.2, 0.7, 100, 8000), 0) d["bone_marrow_cellularity_pct"] = int(clamp(rng.normal(80, 12), 40, 100)) d["auer_rods_flag"] = int(st == "AML" and rng.random() < 0.30) d["splenomegaly_flag"] = int( (st == "CML" and rng.random() < 0.55) or (st == "CLL" and rng.random() < 0.35) or (st in ["AML","ALL"] and rng.random() < 0.15) ) d["lymphadenopathy_flag"] = int( (st == "CLL" and rng.random() < 0.65) or (st == "ALL" and rng.random() < 0.40) or (st in ["AML","APL"] and rng.random() < 0.05) ) d["cns_involvement_flag"] = int( (st == "ALL" and rng.random() < 0.15) or (st == "AML" and rng.random() < 0.03) ) rows.append(d) return pd.DataFrame(rows, index=demo.index) # ───────────────────────────────────────────────────────────────────────────── # MODULE 3: CYTOGENETICS & MOLECULAR MARKERS # ───────────────────────────────────────────────────────────────────────────── def generate_molecular(rng, demo): n = len(demo) rows = [] for i, row in demo.iterrows(): st = row["leukemia_type"] age = row["age_at_diagnosis"] d = {} # Cytogenetics d["t_8_21_flag"] = int(st == "AML" and rng.random() < 0.07) d["inv_16_flag"] = int(st == "AML" and rng.random() < 0.05) d["t_15_17_pml_rara_flag"] = int(st == "APL") d["t_9_22_bcr_abl1_flag"] = int( st == "CML" or (st == "ALL" and rng.random() < (0.05 if age < 18 else 0.25)) ) d["bcr_abl1_transcript"] = ( "p210" if st == "CML" else "p190" if (st == "ALL" and d["t_9_22_bcr_abl1_flag"]) else "None" ) d["inv_3_flag"] = int(st == "AML" and rng.random() < 0.03) d["del_7_monosomy7_flag"] = int(st == "AML" and rng.random() < 0.08) d["del_5q_monosomy5_flag"] = int(st == "AML" and rng.random() < 0.07) d["complex_karyotype_flag"] = int(st == "AML" and rng.random() < 0.12) d["monosomal_karyotype_flag"] = int(st == "AML" and rng.random() < 0.08) d["t_4_11_kmt2a_flag"] = int(st == "ALL" and age < 2 and rng.random() < 0.70) d["t_12_21_etv6_runx1_flag"] = int(st == "ALL" and age < 18 and rng.random() < 0.25) d["t_1_19_tcf3_pbx1_flag"] = int(st == "ALL" and rng.random() < 0.05) d["hypodiploidy_flag"] = int(st == "ALL" and rng.random() < 0.05) d["hyperdiploidy_flag"] = int(st == "ALL" and age < 18 and rng.random() < 0.25) d["ikzf1_deletion_flag"] = int(st == "ALL" and rng.random() < 0.15) # AML molecular cbf_aml = d["t_8_21_flag"] or d["inv_16_flag"] d["npm1_mutation"] = choice(rng, ["WT", "Mutant"], [0.65, 0.35] if st == "AML" else [1.0, 0.0]) d["flt3_itd_status"] = choice(rng, ["Negative", "Low_Allelic_Ratio", "High_Allelic_Ratio"], [0.72, 0.14, 0.14] if st == "AML" else [1.0, 0.0, 0.0]) d["flt3_itd_allelic_ratio"] = ( round(lognorm(rng, -0.5, 0.7, 0.01, 10.0), 3) if d["flt3_itd_status"] != "Negative" else 0.0 ) d["flt3_itd_high_ratio"] = int(d["flt3_itd_allelic_ratio"] >= 0.5) d["flt3_tkd_d835_status"] = choice(rng, ["Negative","Positive"], [0.93, 0.07] if st == "AML" else [1.0, 0.0]) d["idh1_mutation"] = choice(rng, ["WT","R132H","R132C","R132S"], [0.92, 0.05, 0.02, 0.01] if st == "AML" else [1.0, 0.0, 0.0, 0.0]) d["idh2_mutation"] = choice(rng, ["WT","R140Q","R172K","R172M"], [0.87, 0.07, 0.04, 0.02] if st == "AML" else [1.0, 0.0, 0.0, 0.0]) d["dnmt3a_mutation"] = choice(rng, ["WT","R882H","R882C","Other"], [0.75, 0.12, 0.08, 0.05] if st == "AML" else [1.0, 0.0, 0.0, 0.0]) d["tet2_mutation"] = int(st == "AML" and rng.random() < 0.20) d["asxl1_mutation"] = int(st == "AML" and rng.random() < 0.15) d["runx1_mutation"] = int(st == "AML" and rng.random() < 0.12) d["tp53_mutation"] = int( (st == "AML" and rng.random() < 0.10) or (st == "CLL" and rng.random() < 0.10) ) d["cebpa_mutation"] = choice(rng, ["WT","Biallelic","Monoallelic"], [0.90, 0.05, 0.05] if st == "AML" else [1.0, 0.0, 0.0]) d["sf3b1_mutation"] = int(st == "AML" and rng.random() < 0.08) d["kit_mutation"] = int(cbf_aml and rng.random() < 0.25) # CLL molecular d["ighv_mutated_flag"] = int(st == "CLL" and rng.random() < 0.50) d["del_17p_flag"] = int(st == "CLL" and rng.random() < 0.08) d["del_11q_flag"] = int(st == "CLL" and rng.random() < 0.12) d["del_13q_flag"] = int(st == "CLL" and rng.random() < 0.50) d["trisomy_12_flag"] = int(st == "CLL" and rng.random() < 0.15) d["notch1_mutation_cll"] = int(st == "CLL" and rng.random() < 0.12) d["btk_c481s_flag"] = int(st == "CLL" and rng.random() < 0.05) # ibrutinib resistance # ALL molecular d["jak_stat_mutation"] = int(st == "ALL" and rng.random() < 0.12) d["notch1_fbxw7_mutation"] = int(st == "ALL" and rng.random() < 0.55) # MRD method d["mrd_method"] = choice(rng, ["Flow_Cytometry", "PCR", "NGS_MRD", "Not_Assessed"], [0.45, 0.30, 0.15, 0.10]) # ELN 2022 risk (AML/APL) if st in ["AML", "APL"]: if st == "APL" or d["t_8_21_flag"] or d["inv_16_flag"] or d["cebpa_mutation"] == "Biallelic": d["eln_2022_risk_category"] = "Favorable" elif d["tp53_mutation"] or d["complex_karyotype_flag"] or d["monosomal_karyotype_flag"] or d["asxl1_mutation"] or d["runx1_mutation"]: d["eln_2022_risk_category"] = "Adverse" elif d["flt3_itd_high_ratio"] and d["npm1_mutation"] == "WT": d["eln_2022_risk_category"] = "Adverse" elif d["npm1_mutation"] == "Mutant": d["eln_2022_risk_category"] = "Favorable" if d["flt3_itd_status"] == "Negative" else "Intermediate" else: d["eln_2022_risk_category"] = "Intermediate" elif st == "CML": d["eln_2022_risk_category"] = "N/A" d["sokal_score"] = choice(rng, ["Low","Intermediate","High"], [0.40, 0.40, 0.20]) d["eutos_score"] = choice(rng, ["Low","High"], [0.70, 0.30]) elif st == "CLL": d["eln_2022_risk_category"] = "N/A" d["cll_rai_stage"] = choice(rng, [0,1,2,3,4], [0.30,0.25,0.20,0.15,0.10]) d["cll_binet_stage"] = choice(rng, ["A","B","C"], [0.55,0.30,0.15]) else: # ALL d["eln_2022_risk_category"] = "N/A" d["nccn_all_risk"] = choice(rng, ["Standard","High","Very_High"], [0.30, 0.45, 0.25] if age >= 18 else [0.55, 0.35, 0.10]) # Fill N/A for missing fields for f in ["sokal_score","eutos_score","cll_rai_stage","cll_binet_stage","nccn_all_risk"]: if f not in d: d[f] = "N/A" rows.append(d) return pd.DataFrame(rows, index=demo.index) # ───────────────────────────────────────────────────────────────────────────── # MODULE 4: TREATMENT PROTOCOLS # ───────────────────────────────────────────────────────────────────────────── def generate_treatment(rng, demo, mol): rows = [] for i, row in demo.iterrows(): st = row["leukemia_type"] age = row["age_at_diagnosis"] ecog = row["ecog_performance_status"] m = mol.loc[i] d = {} d["treatment_intent"] = ( "Palliative" if ecog >= 3 else "Watch_and_Wait" if (st == "CLL" and m.get("cll_rai_stage", 0) in [0, 1]) else "Curative" ) if st == "AML": flt3_pos = m["flt3_itd_status"] != "Negative" or m["flt3_tkd_d835_status"] == "Positive" idh1_pos = m["idh1_mutation"] != "WT" idh2_pos = m["idh2_mutation"] != "WT" fit = ecog <= 2 and age <= 75 if not fit: d["induction_regimen"] = choice(rng, ["Venetoclax+AZA","Venetoclax+LDAC","BSC"], [0.55,0.30,0.15]) elif flt3_pos: d["induction_regimen"] = "Midostaurin+7+3" elif idh1_pos: d["induction_regimen"] = choice(rng, ["Ivosidenib+AZA","7+3_Idarubicin"], [0.40, 0.60]) elif idh2_pos: d["induction_regimen"] = choice(rng, ["Enasidenib+AZA","7+3_Idarubicin"], [0.35, 0.65]) elif m.get("eln_2022_risk_category") == "Adverse": d["induction_regimen"] = choice(rng, ["CPX-351","7+3_Idarubicin"], [0.45, 0.55]) else: d["induction_regimen"] = choice(rng, ["7+3_Daunorubicin","7+3_Idarubicin"], [0.40, 0.60]) d["consolidation_regimen"] = choice(rng, ["HiDAC_3g_m2", "Intermediate_AraC", "Azacitidine_Maintenance", "None"], [0.50, 0.25, 0.15, 0.10]) d["maintenance_flag"] = int(d["consolidation_regimen"] == "Azacitidine_Maintenance" or (flt3_pos and rng.random() < 0.50)) d["maintenance_regimen"] = "Gilteritinib" if (flt3_pos and d["maintenance_flag"]) else ( "Azacitidine" if d["maintenance_flag"] else "None") d["induction_cycles"] = int(rng.choice([1, 2], p=[0.70, 0.30])) d["consolidation_cycles"] = int(rng.choice([1, 2, 3, 4], p=[0.15, 0.35, 0.35, 0.15])) d["targeted_agent"] = ( "Midostaurin" if d["induction_regimen"] == "Midostaurin+7+3" else "Ivosidenib" if "Ivosidenib" in d["induction_regimen"] else "Enasidenib" if "Enasidenib" in d["induction_regimen"] else "Venetoclax" if "Venetoclax" in d["induction_regimen"] else "None" ) d["apl_atra_flag"] = 0 elif st == "APL": d["induction_regimen"] = choice(rng, ["ATRA_ATO_APL0406","ATRA_ATO_APML4","ATRA_Daunorubicin_Sanz"], [0.50, 0.25, 0.25]) d["apl_atra_flag"] = 1 d["consolidation_regimen"] = choice(rng, ["ATRA_ATO_Consolidation","ATRA_Chemo"], [0.60, 0.40]) d["maintenance_flag"] = int(rng.random() < 0.70) d["maintenance_regimen"] = "ATRA_6MP_MTX" if d["maintenance_flag"] else "None" d["induction_cycles"] = 1 d["consolidation_cycles"] = int(rng.choice([2, 3], p=[0.30, 0.70])) d["targeted_agent"] = "ATO+ATRA" elif st == "ALL": ph_pos = m.get("t_9_22_bcr_abl1_flag", 0) == 1 if ph_pos: d["induction_regimen"] = choice(rng, ["Dasatinib+Steroids","Ponatinib+Chemo","HyperCVAD_Dasatinib"], [0.40, 0.30, 0.30]) d["targeted_agent"] = "Ponatinib" if "Ponatinib" in d["induction_regimen"] else "Dasatinib" elif age < 18: d["induction_regimen"] = choice(rng, ["CALGB_10403","BFM_Protocol","COG_AALL0434"], [0.40, 0.35, 0.25]) d["targeted_agent"] = "None" elif m.get("cns_involvement_flag", 0): d["induction_regimen"] = "HyperCVAD_A_B" d["targeted_agent"] = "None" else: d["induction_regimen"] = choice(rng, ["CALGB_10403","HyperCVAD_A_B","GRAALL_2005","UKALL14", "Blinatumomab+Chemo","Inotuzumab_Ozogamicin+Chemo"], [0.25, 0.25, 0.15, 0.15, 0.10, 0.10]) d["targeted_agent"] = ( "Blinatumomab" if "Blinatumomab" in d["induction_regimen"] else "Inotuzumab" if "Inotuzumab" in d["induction_regimen"] else "None" ) d["consolidation_regimen"] = choice(rng, ["Consolidation_A_B_C","Maintenance_Only","None"], [0.60, 0.30, 0.10]) d["maintenance_flag"] = 1 d["maintenance_regimen"] = "6MP_MTX_Vincristine_Prednisone" d["induction_cycles"] = 1 d["consolidation_cycles"] = int(rng.choice([2, 3, 4], p=[0.25, 0.45, 0.30])) d["apl_atra_flag"] = 0 elif st == "CML": phase = m.get("who_2022_classification","CML_CP") if "BP" in str(phase): d["induction_regimen"] = choice(rng, ["Ponatinib_45mg","Dasatinib_Chemo","Asciminib"], [0.40, 0.35, 0.25]) else: d["induction_regimen"] = choice(rng, CML_REGIMENS, [0.25, 0.25, 0.20, 0.10, 0.10, 0.10]) d["targeted_agent"] = d["induction_regimen"].split("_")[0] d["tki_generation"] = ( "First" if "Imatinib" in d["induction_regimen"] else "Second" if any(x in d["induction_regimen"] for x in ["Dasatinib","Nilotinib","Bosutinib"]) else "Third" if "Ponatinib" in d["induction_regimen"] else "STAMP" ) d["consolidation_regimen"] = "N/A" d["maintenance_flag"] = 0 d["maintenance_regimen"] = "Ongoing_TKI" d["induction_cycles"] = "Continuous" d["consolidation_cycles"] = 0 d["apl_atra_flag"] = 0 else: # CLL rai = m.get("cll_rai_stage", 0) unfit = ecog >= 2 or age >= 75 if rai in [0, 1]: d["induction_regimen"] = "Watch_and_Wait" d["targeted_agent"] = "None" elif unfit: d["induction_regimen"] = choice(rng, ["Venetoclax_Obinutuzumab","Acalabrutinib","Ibrutinib"], [0.45, 0.30, 0.25]) d["targeted_agent"] = d["induction_regimen"].split("_")[0] elif m.get("del_17p_flag", 0) or m.get("tp53_mutation", 0): d["induction_regimen"] = choice(rng, ["Ibrutinib","Acalabrutinib","Venetoclax_Rituximab"], [0.40, 0.35, 0.25]) d["targeted_agent"] = d["induction_regimen"].split("_")[0] elif m.get("ighv_mutated_flag", 0): d["induction_regimen"] = choice(rng, ["FCR","Venetoclax_Obinutuzumab","Venetoclax_Rituximab"], [0.35, 0.35, 0.30]) d["targeted_agent"] = d["induction_regimen"].split("_")[0] else: d["induction_regimen"] = choice(rng, ["Ibrutinib","Acalabrutinib","Venetoclax_Obinutuzumab"], [0.35, 0.35, 0.30]) d["targeted_agent"] = d["induction_regimen"].split("_")[0] d["consolidation_regimen"] = "N/A" d["maintenance_flag"] = 0 d["maintenance_regimen"] = "Ongoing_BTKi" if "rutinib" in d["induction_regimen"] else "None" d["induction_cycles"] = int(rng.choice([6, 12, 24], p=[0.30, 0.40, 0.30])) d["consolidation_cycles"] = 0 d["apl_atra_flag"] = 0 d["total_treatment_months"] = int(clamp(rng.normal(24, 8), 6, 48)) d["clinical_trial_enrollment_flag"] = int(rng.random() < 0.25) d["trial_phase"] = choice(rng, ["Phase_I","Phase_II","Phase_III"], [0.10, 0.40, 0.50]) if d["clinical_trial_enrollment_flag"] else "None" rows.append(d) return pd.DataFrame(rows, index=demo.index) # ───────────────────────────────────────────────────────────────────────────── # MODULE 5: REMISSION & RESPONSE # ───────────────────────────────────────────────────────────────────────────── def generate_remission(rng, demo, mol, treat): rows = [] for i, row in demo.iterrows(): st = row["leukemia_type"] age = row["age_at_diagnosis"] m = mol.loc[i] t = treat.loc[i] d = {} if st in ["AML","APL"]: eln = m.get("eln_2022_risk_category","Intermediate") key = f"AML_{eln}" if st == "AML" else "APL" base_cr = CR_RATE.get(key, 0.60) d["cr_achieved_flag"] = int(rng.random() < base_cr) d["cri_achieved_flag"] = int(not d["cr_achieved_flag"] and rng.random() < 0.15) d["pr_achieved_flag"] = int(not d["cr_achieved_flag"] and not d["cri_achieved_flag"] and rng.random() < 0.10) d["time_to_cr_days"] = int(clamp(rng.normal(28, 7), 14, 60)) if d["cr_achieved_flag"] else None d["cytogenetic_remission_flag"] = int(d["cr_achieved_flag"] and rng.random() < 0.85) d["molecular_remission_flag"] = int(d["cytogenetic_remission_flag"] and rng.random() < 0.75) d["mrd_negativity_flag"] = int(d["molecular_remission_flag"] and rng.random() < 0.80) d["mrd_conversion_cycle"] = int(rng.choice([1,2,3,4], p=[0.40,0.35,0.15,0.10])) if d["mrd_negativity_flag"] else None d["hematologic_relapse_flag"] = int(d["cr_achieved_flag"] and rng.random() < (0.20 if eln == "Favorable" else 0.45 if eln == "Intermediate" else 0.65)) d["molecular_relapse_flag"] = int(d["mrd_negativity_flag"] and rng.random() < 0.25) d["time_to_relapse_months"] = round(weibull_sample(rng, 1.5, 18 if eln=="Favorable" else 12)[0], 1) if d["hematologic_relapse_flag"] else None d["relapse_site"] = choice(rng, ["Bone_Marrow","Extramedullary","CNS","Peripheral_Blood"], [0.70,0.15,0.08,0.07]) if d["hematologic_relapse_flag"] else "None" d["salvage_cr_rate"] = round(rng.uniform(0.30, 0.45) if st=="AML" else rng.uniform(0.40, 0.65), 2) if d["hematologic_relapse_flag"] else None # CML-specific (N/A) for f in ["bcr_abl1_is_pct","ccyr_achieved_flag","mmr_achieved_flag","dmr_achieved_flag","sokal_score_val","tki_resistance_mechanism"]: d[f] = "N/A" # CLL-specific (N/A) for f in ["cll_iwcll_response","bruton_resistance_flag"]: d[f] = "N/A" elif st == "ALL": is_ped = age < 18 key = "ALL_Pediatric" if is_ped else "ALL_Adult" base_cr = CR_RATE[key] d["cr_achieved_flag"] = int(rng.random() < base_cr) d["cri_achieved_flag"] = int(not d["cr_achieved_flag"] and rng.random() < 0.08) d["pr_achieved_flag"] = int(not d["cr_achieved_flag"] and not d["cri_achieved_flag"] and rng.random() < 0.05) d["time_to_cr_days"] = int(clamp(rng.normal(28, 7), 14, 56)) if d["cr_achieved_flag"] else None d["cytogenetic_remission_flag"] = int(d["cr_achieved_flag"] and rng.random() < 0.80) d["molecular_remission_flag"] = int(d["cytogenetic_remission_flag"] and rng.random() < 0.70) d["mrd_negativity_flag"] = int(d["molecular_remission_flag"] and rng.random() < 0.85) d["mrd_conversion_cycle"] = int(rng.choice([1,2,3], p=[0.45,0.35,0.20])) if d["mrd_negativity_flag"] else None d["hematologic_relapse_flag"] = int(d["cr_achieved_flag"] and rng.random() < (0.05 if is_ped else 0.40)) d["molecular_relapse_flag"] = int(d["mrd_negativity_flag"] and rng.random() < 0.20) d["time_to_relapse_months"] = round(weibull_sample(rng, 1.5, 24 if is_ped else 14)[0], 1) if d["hematologic_relapse_flag"] else None d["relapse_site"] = choice(rng, ["Bone_Marrow","CNS","Testicular","Peripheral_Blood"], [0.65,0.20,0.05,0.10]) if d["hematologic_relapse_flag"] else "None" d["salvage_cr_rate"] = round(rng.uniform(0.25, 0.50), 2) if d["hematologic_relapse_flag"] else None for f in ["bcr_abl1_is_pct","ccyr_achieved_flag","mmr_achieved_flag","dmr_achieved_flag","sokal_score_val","tki_resistance_mechanism"]: d[f] = "N/A" for f in ["cll_iwcll_response","bruton_resistance_flag"]: d[f] = "N/A" elif st == "CML": d["cr_achieved_flag"] = "N/A" d["cri_achieved_flag"] = "N/A" d["pr_achieved_flag"] = "N/A" d["time_to_cr_days"] = "N/A" d["cytogenetic_remission_flag"] = "N/A" d["molecular_remission_flag"] = "N/A" d["mrd_negativity_flag"] = "N/A" d["mrd_conversion_cycle"] = "N/A" d["hematologic_relapse_flag"] = int(rng.random() < 0.10) d["molecular_relapse_flag"] = int(rng.random() < 0.15) d["time_to_relapse_months"] = round(weibull_sample(rng, 1.8, 36)[0], 1) if d["hematologic_relapse_flag"] else None d["relapse_site"] = "Peripheral_Blood" if d["hematologic_relapse_flag"] else "None" d["salvage_cr_rate"] = "N/A" # CML molecular response tki_gen = t.get("tki_generation", "Second") mmr_base = 0.70 if tki_gen == "First" else 0.80 if tki_gen == "Second" else 0.75 d["ccyr_achieved_flag"] = int(rng.random() < (mmr_base + 0.10)) d["mmr_achieved_flag"] = int(rng.random() < mmr_base) d["dmr_achieved_flag"] = int(d["mmr_achieved_flag"] and rng.random() < 0.40) d["bcr_abl1_is_pct"] = round(lognorm(rng, -2.0, 1.5, 0.001, 100) if d["mmr_achieved_flag"] else lognorm(rng, 0.5, 1.0, 0.1, 100), 4) d["tki_resistance_mechanism"] = choice(rng, ["T315I","E255K","F317L","Compound_Mutation","None"], [0.30,0.20,0.15,0.10,0.25]) if d["molecular_relapse_flag"] else "None" for f in ["cll_iwcll_response","bruton_resistance_flag"]: d[f] = "N/A" else: # CLL d["cr_achieved_flag"] = int(rng.random() < 0.25) d["cri_achieved_flag"] = "N/A" d["pr_achieved_flag"] = int(not d["cr_achieved_flag"] and rng.random() < 0.55) d["time_to_cr_days"] = "N/A" d["cytogenetic_remission_flag"] = "N/A" d["molecular_remission_flag"] = "N/A" d["mrd_negativity_flag"] = int(d["cr_achieved_flag"] and rng.random() < 0.60) d["mrd_conversion_cycle"] = "N/A" d["hematologic_relapse_flag"] = int(rng.random() < 0.25) d["molecular_relapse_flag"] = "N/A" d["time_to_relapse_months"] = round(weibull_sample(rng, 1.4, 36)[0], 1) if d["hematologic_relapse_flag"] else None d["relapse_site"] = "Peripheral_Blood" if d["hematologic_relapse_flag"] else "None" d["salvage_cr_rate"] = "N/A" d["cll_iwcll_response"] = choice(rng, ["CR","MRD_Negative_CR","PR","SD","PD"], [0.20,0.15,0.45,0.10,0.10]) d["bruton_resistance_flag"] = int(m.get("btk_c481s_flag", 0)) for f in ["bcr_abl1_is_pct","ccyr_achieved_flag","mmr_achieved_flag","dmr_achieved_flag","sokal_score_val","tki_resistance_mechanism"]: d[f] = "N/A" rows.append(d) return pd.DataFrame(rows, index=demo.index) # ───────────────────────────────────────────────────────────────────────────── # MODULE 6: BONE MARROW TRANSPLANT # ───────────────────────────────────────────────────────────────────────────── def generate_bmt(rng, demo, mol, treat, remission): rows = [] for i, row in demo.iterrows(): st = row["leukemia_type"] age = row["age_at_diagnosis"] m = mol.loc[i] t = treat.loc[i] r = remission.loc[i] d = {} # BMT eligibility eln = m.get("eln_2022_risk_category", "Intermediate") age_eligible = age <= 75 if st == "AML": bmt_indicated = (eln in ["Intermediate","Adverse"]) and age_eligible and str(r.get("cr_achieved_flag","0")) == "1" elif st == "ALL": bmt_indicated = age_eligible and str(r.get("cr_achieved_flag","0")) == "1" elif st == "APL": bmt_indicated = str(r.get("hematologic_relapse_flag","0")) == "1" elif st == "CML": bmt_indicated = str(r.get("tki_resistance_mechanism","None")) not in ["None","N/A"] else: # CLL bmt_indicated = False d["bmt_indicated_flag"] = int(bmt_indicated) d["bmt_performed_flag"] = int(bmt_indicated and rng.random() < 0.70) if d["bmt_performed_flag"]: d["bmt_timing"] = choice(rng, ["CR1","CR2","Upfront_High_Risk","Relapsed_Refractory"], [0.55, 0.25, 0.12, 0.08]) d["bmt_type"] = choice(rng, ["Allogeneic","Autologous","Haploidentical"], [0.75, 0.15, 0.10]) if d["bmt_type"] in ["Allogeneic","Haploidentical"]: d["donor_type"] = "Haploidentical" if d["bmt_type"] == "Haploidentical" else choice(rng, ["MRD","MUD","MMUD"], [0.45, 0.42, 0.13]) d["hla_match_grade"] = "5-6/10" if d["donor_type"] == "Haploidentical" else choice(rng, ["10/10","9/10","8/10"], [0.60, 0.30, 0.10]) else: d["donor_type"] = "Autologous" d["hla_match_grade"] = "N/A" d["donor_recipient_sex_mismatch_flag"] = int(rng.random() < 0.25) d["stem_cell_source"] = choice(rng, ["Peripheral_Blood","Bone_Marrow","Cord_Blood"], [0.75, 0.20, 0.05]) age_fit = age >= 60 or m.get("eln_2022_risk_category","") == "Adverse" d["conditioning_intensity"] = choice(rng, ["Myeloablative_MAC","Reduced_Intensity_RIC","Non_Myeloablative_NMA"], [0.35, 0.50, 0.15] if age >= 55 else [0.70, 0.25, 0.05]) cond_type = d["conditioning_intensity"].split("_")[0].split("_")[0] if "Mac" in d["conditioning_intensity"] or "Myeloab" in d["conditioning_intensity"]: d["conditioning_regimen"] = choice(rng, CONDITIONING["MAC"]) elif "RIC" in d["conditioning_intensity"]: d["conditioning_regimen"] = choice(rng, CONDITIONING["RIC"]) else: d["conditioning_regimen"] = choice(rng, CONDITIONING["NMA"]) d["gvhd_prophylaxis"] = choice(rng, GVHD_PROPHYLAXIS) d["graft_cd34_x10e6_kg"] = round(lognorm(rng, 1.8, 0.4, 1.0, 15.0), 2) d["engraftment_day_neutrophil"] = int(clamp(rng.normal(14, 4), 8, 35)) d["platelet_engraftment_day"] = int(clamp(rng.normal(18, 5), 10, 45)) d["primary_graft_failure_flag"] = int(rng.random() < 0.05) d["secondary_graft_failure_flag"] = int(not d["primary_graft_failure_flag"] and rng.random() < 0.03) if d["bmt_type"] in ["Allogeneic","Haploidentical"] and not d["primary_graft_failure_flag"]: donor = d["donor_type"] agvhd_p = AGVHD_RATE.get(donor, 0.40) # PT-Cy prophylaxis reduces aGvHD in haplo if "PT_Cy" in d["gvhd_prophylaxis"]: agvhd_p *= 0.65 d["acute_gvhd_flag"] = int(rng.random() < agvhd_p) d["acute_gvhd_grade"] = choice(rng, ["None","I","II","III","IV"], [1-agvhd_p, agvhd_p*0.30, agvhd_p*0.40, agvhd_p*0.20, agvhd_p*0.10]) if d["acute_gvhd_flag"] else "None" d["acute_gvhd_organs"] = choice(rng, ["Skin","Gut","Liver","Multi_organ"], [0.45, 0.30, 0.10, 0.15]) if d["acute_gvhd_flag"] else "None" d["chronic_gvhd_flag"] = int(rng.random() < 0.40) d["chronic_gvhd_severity"] = choice(rng, ["Mild","Moderate","Severe"], [0.45, 0.35, 0.20]) if d["chronic_gvhd_flag"] else "None" d["chronic_gvhd_organs"] = choice(rng, ["Skin","Mouth","Eye","Lung","Liver","Multi"], [0.30,0.20,0.15,0.10,0.10,0.15]) if d["chronic_gvhd_flag"] else "None" nrm_p = NRM_RATE.get(donor, 0.15) d["non_relapse_mortality_flag"] = int(rng.random() < nrm_p) else: for f in ["acute_gvhd_flag","acute_gvhd_grade","acute_gvhd_organs", "chronic_gvhd_flag","chronic_gvhd_severity","chronic_gvhd_organs", "non_relapse_mortality_flag"]: d[f] = 0 if "flag" in f else "None" if f.endswith("_flag")==False else 0 # Complications is_mac = "Mac" in d["conditioning_intensity"] or "Myeloab" in d["conditioning_intensity"] d["vod_flag"] = int(rng.random() < (0.12 if is_mac else 0.04)) d["vod_severity"] = choice(rng, ["Mild","Moderate","Severe","Very_Severe"], [0.40,0.30,0.20,0.10]) if d["vod_flag"] else "None" d["cmv_reactivation_flag"] = int(rng.random() < 0.50) d["ebv_reactivation_flag"] = int(rng.random() < 0.18) d["invasive_fungal_infection_flag"] = int(rng.random() < (0.10 if is_mac else 0.04)) d["bacterial_infection_flag"] = int(rng.random() < 0.40) d["idiopathic_pneumonia_flag"] = int(rng.random() < 0.05) d["thrombotic_microangiopathy_flag"] = int(rng.random() < 0.07) d["day_100_mortality_flag"] = int( d["primary_graft_failure_flag"] or (d["vod_flag"] and d["vod_severity"] in ["Severe","Very_Severe"] and rng.random() < 0.50) or (d.get("acute_gvhd_grade","None") in ["III","IV"] and rng.random() < 0.35) or rng.random() < 0.05 ) d["day_100_chimerism_pct"] = round(clamp(rng.normal(97, 4), 80, 100), 1) if not d["primary_graft_failure_flag"] else round(clamp(rng.normal(60, 20), 10, 80), 1) d["relapse_post_bmt_flag"] = int(not d["day_100_mortality_flag"] and rng.random() < 0.28) d["time_to_relapse_post_bmt_months"] = round(weibull_sample(rng, 1.3, 14)[0], 1) if d["relapse_post_bmt_flag"] else None d["donor_lymphocyte_infusion_flag"] = int(d["relapse_post_bmt_flag"] and rng.random() < 0.45) d["second_bmt_flag"] = int(d["relapse_post_bmt_flag"] and rng.random() < 0.07) d["one_yr_os_post_bmt"] = int(not d["day_100_mortality_flag"] and not d["non_relapse_mortality_flag"]) d["two_yr_rfs_post_bmt"] = int(d["one_yr_os_post_bmt"] and not d["relapse_post_bmt_flag"] and rng.random() < 0.70) else: for f in ["bmt_timing","bmt_type","donor_type","hla_match_grade","donor_recipient_sex_mismatch_flag", "stem_cell_source","conditioning_intensity","conditioning_regimen","gvhd_prophylaxis", "graft_cd34_x10e6_kg","engraftment_day_neutrophil","platelet_engraftment_day", "primary_graft_failure_flag","secondary_graft_failure_flag", "acute_gvhd_flag","acute_gvhd_grade","acute_gvhd_organs", "chronic_gvhd_flag","chronic_gvhd_severity","chronic_gvhd_organs", "vod_flag","vod_severity","cmv_reactivation_flag","ebv_reactivation_flag", "invasive_fungal_infection_flag","bacterial_infection_flag", "idiopathic_pneumonia_flag","thrombotic_microangiopathy_flag", "day_100_mortality_flag","non_relapse_mortality_flag","day_100_chimerism_pct", "relapse_post_bmt_flag","time_to_relapse_post_bmt_months", "donor_lymphocyte_infusion_flag","second_bmt_flag", "one_yr_os_post_bmt","two_yr_rfs_post_bmt"]: d[f] = "N/A" rows.append(d) return pd.DataFrame(rows, index=demo.index) # ───────────────────────────────────────────────────────────────────────────── # MODULE 7: TOXICITY # ───────────────────────────────────────────────────────────────────────────── def generate_toxicity(rng, demo, treat): rows = [] for i, row in demo.iterrows(): st = row["leukemia_type"] t = treat.loc[i] reg = str(t.get("induction_regimen","")) d = {} d["febrile_neutropenia_flag"] = int(st in ["AML","APL","ALL"] and rng.random() < 0.85) d["induction_mortality_flag"] = int( (st == "AML" and rng.random() < 0.04) or (st == "APL" and rng.random() < 0.02) or (st == "ALL" and rng.random() < 0.02) ) d["fungal_infection_flag"] = int(rng.random() < 0.15 if st in ["AML","APL","ALL"] else rng.random() < 0.03) d["tumor_lysis_syndrome_flag"] = int( (st == "ALL" and rng.random() < 0.18) or (st == "AML" and rng.random() < 0.08) ) d["tls_cairo_bishop_grade"] = int(rng.choice([1,2,3], p=[0.50,0.35,0.15])) if d["tumor_lysis_syndrome_flag"] else 0 d["differentiation_syndrome_flag"] = int( (st == "APL" and rng.random() < 0.22) or ("IDH" in reg and rng.random() < 0.15) ) d["differentiation_syndrome_severity"] = choice(rng, ["Mild","Moderate","Severe"], [0.50,0.35,0.15]) if d["differentiation_syndrome_flag"] else "None" d["anthracycline_cardiotoxicity_flag"] = int("7+3" in reg and rng.random() < 0.08) d["lvef_post_treatment_pct"] = int(clamp(rng.normal(58, 8), 30, 75)) d["peripheral_neuropathy_flag"] = int(st == "ALL" and rng.random() < 0.30) d["hepatotoxicity_flag"] = int(rng.random() < 0.15) d["mucositis_grade"] = int(rng.choice([0,1,2,3,4], p=[0.40,0.25,0.20,0.10,0.05])) d["hemorrhagic_cystitis_flag"] = int(rng.random() < 0.08) d["qol_score_baseline_fact_leu"] = int(clamp(rng.normal(130, 22), 60, 176)) d["qol_score_end_of_induction"] = int(clamp(d["qol_score_baseline_fact_leu"] - rng.normal(20, 12), 40, 176)) rows.append(d) return pd.DataFrame(rows, index=demo.index) # ───────────────────────────────────────────────────────────────────────────── # MODULE 8: SURVIVAL OUTCOMES # ───────────────────────────────────────────────────────────────────────────── def generate_survival(rng, demo, mol, remission, bmt, tox): rows = [] for i, row in demo.iterrows(): st = row["leukemia_type"] age = row["age_at_diagnosis"] m = mol.loc[i] r = remission.loc[i] b = bmt.loc[i] tx = tox.loc[i] d = {} eln = str(m.get("eln_2022_risk_category","Intermediate")) is_ped = age < 18 induction_dead = tx.get("induction_mortality_flag", 0) bmt_dead = str(b.get("day_100_mortality_flag","0")) in ["1","True"] nrm_dead = str(b.get("non_relapse_mortality_flag","0")) in ["1","True"] if st == "AML": key = f"AML_{eln}" if eln in ["Favorable","Intermediate","Adverse"] else "AML_Intermediate" elif st == "APL": key = "APL" elif st == "ALL": key = "ALL_Pediatric" if is_ped else "ALL_Adult" elif st == "CML": key = "CML" else: key = "CLL" lam = OS_LAMBDA.get(key, 36.0) os_months = round(weibull_sample(rng, 1.4, lam)[0], 1) # Compress OS if induction or BMT mortality if induction_dead: os_months = round(rng.uniform(0.5, 3.0), 1) elif bmt_dead: bmt_os = str(b.get("one_yr_os_post_bmt","N/A")) if bmt_os == "0": os_months = min(os_months, round(rng.uniform(2, 12), 1)) d["overall_survival_months"] = os_months d["event_free_survival_months"] = round(min(os_months, weibull_sample(rng, 1.5, lam * 0.7)[0]), 1) d["leukemia_free_survival_months"] = round(min(d["event_free_survival_months"], weibull_sample(rng, 1.6, lam * 0.8)[0]), 1) d["relapse_free_survival_months"] = round(min(d["leukemia_free_survival_months"], weibull_sample(rng, 1.7, lam * 0.9)[0]), 1) if induction_dead: d["vital_status"] = "Dead_Treatment" d["cause_of_death"] = "Organ_Failure" elif bmt_dead or nrm_dead: d["vital_status"] = "Dead_Treatment" d["cause_of_death"] = choice(rng, ["GvHD","Infection","Organ_Failure"], [0.35,0.45,0.20]) elif str(r.get("hematologic_relapse_flag","0")) == "1" and os_months < lam * 0.5: d["vital_status"] = "Dead_Disease" d["cause_of_death"] = "Relapse" elif rng.random() < 0.65: d["vital_status"] = "Alive" d["cause_of_death"] = "None" else: d["vital_status"] = choice(rng, ["Dead_Disease","Dead_Other"], [0.75, 0.25]) d["cause_of_death"] = choice(rng, ["Relapse","Infection","Secondary_Malignancy","Other"], [0.60, 0.20, 0.10, 0.10]) d["second_malignancy_flag"] = int(rng.random() < 0.03) rows.append(d) return pd.DataFrame(rows, index=demo.index) # ───────────────────────────────────────────────────────────────────────────── # MRD LONGITUDINAL DATA # ───────────────────────────────────────────────────────────────────────────── def generate_mrd_longitudinal(rng, demo, remission, n_timepoints=12): """Generate monthly MRD assessments for a subset of patients.""" records = [] sample_ids = demo.sample(frac=0.40, random_state=42)["patient_id"].tolist() for pid in sample_ids: idx = demo[demo["patient_id"] == pid].index[0] st = demo.loc[idx, "leukemia_type"] r = remission.loc[idx] cr = str(r.get("cr_achieved_flag", "0")) == "1" mrd_neg = str(r.get("mrd_negativity_flag", "0")) == "1" for month in range(1, n_timepoints + 1): mrd_val = None if cr and month <= 3: mrd_val = round(lognorm(rng, -1.0, 1.2, 0.001, 10.0), 4) elif mrd_neg and month > 3: mrd_val = round(lognorm(rng, -3.5, 0.8, 0.0001, 0.05), 5) elif cr: mrd_val = round(lognorm(rng, -2.0, 1.0, 0.001, 1.0), 4) records.append({ "patient_id": pid, "leukemia_type": st, "assessment_month": month, "mrd_value_pct": mrd_val, "mrd_negative_flag": int(mrd_val is not None and mrd_val < 0.01), "assessment_method": rng.choice(["Flow_Cytometry","PCR","NGS_MRD"]), "wbc_k_ul": round(lognorm(rng, 2.3, 0.5, 1.0, 15.0), 1), "hemoglobin_g_dl": round(clamp(rng.normal(11.5, 1.8), 7.0, 16.0), 1), "platelets_k_ul": round(lognorm(rng, 5.0, 0.4, 50, 400), 0), "neutrophils_k_ul": round(lognorm(rng, 1.2, 0.6, 0.1, 12.0), 2), }) return pd.DataFrame(records) # ───────────────────────────────────────────────────────────────────────────── # MAIN # ───────────────────────────────────────────────────────────────────────────── def main(): parser = argparse.ArgumentParser(description="HC-ONC-007 Leukemia Simulation Engine") parser.add_argument("--n_patients", type=int, default=25000) parser.add_argument("--seed", type=int, default=42) parser.add_argument("--output_dir", type=str, default="./output") parser.add_argument("--format", type=str, default="csv", choices=["csv","parquet","json"]) args = parser.parse_args() os.makedirs(args.output_dir, exist_ok=True) rng = np.random.default_rng(args.seed) n = args.n_patients print(f"[HC-ONC-007] Generating {n:,} synthetic leukemia patients (seed={args.seed})...") print(" [1/8] Demographics...") demo = generate_demographics(rng, n) print(" [2/8] Disease characteristics...") disease = generate_disease_characteristics(rng, demo) print(" [3/8] Cytogenetics & molecular markers...") mol = generate_molecular(rng, demo) print(" [4/8] Treatment protocols...") treat = generate_treatment(rng, demo, mol) print(" [5/8] Remission & response...") remission = generate_remission(rng, demo, mol, treat) print(" [6/8] Bone marrow transplant outcomes...") bmt = generate_bmt(rng, demo, mol, treat, remission) print(" [7/8] Toxicity...") tox = generate_toxicity(rng, demo, treat) print(" [8/8] Survival outcomes...") survival = generate_survival(rng, demo, mol, remission, bmt, tox) # Combine primary cohort primary = pd.concat([demo, disease, mol, treat, remission, bmt, tox, survival], axis=1) primary["generation_timestamp"] = datetime.now().isoformat() primary["sku"] = "HC-ONC-007" primary["version"] = "1.0.0" primary["seed"] = args.seed out_path = os.path.join(args.output_dir, "hc_onc_007_primary_cohort.csv") primary.to_csv(out_path, index=False) print(f"\n Primary cohort: {len(primary):,} rows × {len(primary.columns)} columns → {out_path}") # MRD longitudinal print(" Generating MRD longitudinal data...") mrd_long = generate_mrd_longitudinal(rng, demo, remission) mrd_path = os.path.join(args.output_dir, "hc_onc_007_mrd_longitudinal.csv") mrd_long.to_csv(mrd_path, index=False) print(f" MRD longitudinal: {len(mrd_long):,} records → {mrd_path}") # Summary print("\n" + "="*60) print("HC-ONC-007 Generation Complete") print("="*60) print(f" Patients: {len(primary):,}") print(f" Columns: {len(primary.columns)}") print(f" Subtype distribution:") for st, cnt in primary["leukemia_type"].value_counts().items(): print(f" {st}: {cnt:,} ({cnt/len(primary)*100:.1f}%)") bmt_done = (primary["bmt_performed_flag"] == 1).sum() print(f" BMT performed: {bmt_done:,} ({bmt_done/len(primary)*100:.1f}%)") cr_rate = (primary["cr_achieved_flag"].astype(str).isin(["1","1.0"])).mean() print(f" Overall CR rate: {cr_rate*100:.1f}%") print(f" Output: {args.output_dir}/") if __name__ == "__main__": main()