OpenIslet

scripts/run_layer4_tissue_and_metabolic_twin.py

Layer 4: Multi-Scale Tissue, Vascular & Metabolic Digital Twin Plane (v0.8.0)

script Release v0.8.0 gen-eb955f8266035aba 713 KB · 47 sections

Use this file in any AI chat (paste-only or attachment-capable):

Download as text (.txt) Download section (.txt)

Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/run_layer4_tissue_and_metabolic_twin.py · SHA-256: 624a491d5e418a7d…

            "PMID:37476069",
            "PMID:35743287",
            "PMID:27729543",
            "PMID:8720919",
            "PMID:10656833",
            "PMID:22371586",
            "PMID:36971050",
            "PMID:26970510",
            "PMID:29288549",
            "PMID:12702875",
            "PMID:40404627",
            "PMID:19074987",
            "PMID:28489987",
            "PMID:40757665",
            "PMID:35152257",
            "PMID:24065585",
            "PMID:16897713",
            "PMID:37318185",
            "PMID:36707047",
            "PMID:35795779",
            "PMID:38603470",
            "PMID:34480863",
            "PMID:25893782",
            "PMID:31900242",
            "PMID:31177185",
        ],
        "ordered_unit_cell_multimesh_dual_band_gate": {
            "rule_definition": "min over nx in {40, 60, 80} of unrounded dual-band worst-face fv_harmonic_face_min_mmhg >= 15.0 AND min_core_po2_mmhg >= 5.0",
            "conserved_500k_ieq_cluster_count_100um": round(n_cl_100, 2),
            "conserved_500k_ieq_cluster_count_80um": round(n_cl_80, 2),
            "cluster_count_ratio_80_over_100_exact": round(n_cl_80 / n_cl_100, 6),
            "pitch_held_80um_768ieq_dose_reduction_pct": 48.8,
            "nominal_100um_350um_no_film": mm_100_nofilm,
            "layout_a_350um_barrier_100um": mm_100_350bar,
            "expanded_390um_barrier_100um": mm_100_390bar,
            "nominal_80um_350um_no_film": mm_80_nofilm,
            "layout_a_350um_barrier_80um": mm_80_350bar,
            "expanded_390um_barrier_80um": mm_80_390bar,
            "plus10pct_joint_stress_80um_no_film_nx40": uc_80_plus10_nofilm_nx40,
            "plus10pct_joint_stress_80um_layout_a_350um_barrier": mm_80_plus10_350bar,
            "lateral_half_voxel_shift_80um_layout_a_350um_nx40_mmhg": uc_80_350bar_shift_nx40["fv_harmonic_face_min_mmhg"],
            "contrast_b1_paired_effect_390um_primary_nx40_mmhg": round(
                mm_80_390bar["nx40"]["fv_harmonic_face_min_unrounded_mmhg"] - mm_100_390bar["nx40"]["fv_harmonic_face_min_unrounded_mmhg"], 4
            ),
            "contrast_b1_paired_effect_350um_sensitivity_nx40_mmhg": round(
                mm_80_350bar["nx40"]["fv_harmonic_face_min_unrounded_mmhg"] - mm_100_350bar["nx40"]["fv_harmonic_face_min_unrounded_mmhg"], 4
            ),
            "nx60_nz105_boundary_plane_symmetry_diagnostics": {
                "sphere_mask_asymmetry_voxels": mm_80_350bar["nx60"]["sphere_mask_asymmetry_voxels"],
                "lamina_boundary_asymmetry_voxels": mm_80_350bar["nx60"]["lamina_mask_asymmetry_voxels"],
                "film_boundary_asymmetry_voxels": mm_80_350bar["nx60"]["film_mask_asymmetry_voxels"],
                "near_threshold_0p95x_potency_fixed_vascular_symmetric_brackets_mmhg": [
                    sym_095_inc_fv["fv_harmonic_face_min_mmhg"],
                    sym_095_exc_fv["fv_harmonic_face_min_mmhg"],
                ],
                "near_threshold_0p95x_potency_coscaled_symmetric_brackets_mmhg": [
                    sym_095_inc_co["fv_harmonic_face_min_mmhg"],
                    sym_095_exc_co["fv_harmonic_face_min_mmhg"],
                ],
                "near_threshold_straddling_classification": "NUMERICALLY_KNIFE_EDGE_UNRESOLVED",
                "nominal_80um_layout_a_symmetric_brackets_mmhg": [
                    sym_nom80_inc["fv_harmonic_face_min_mmhg"],
                    sym_nom80_exc["fv_harmonic_face_min_mmhg"],
                ],
            },
            "layout_b_119um_lamina_95p5um_band_excluded": layout_b_119um_caught,
            "wigner_seitz_unrounded_0p497_boundary_check": {
                "unrounded_gel_only_120um_core_mmhg": round(float(bvp_0497["wigner_seitz_two_zone_fv_gel_only_supply_core_po2_unrounded_mmhg"]), 6),
                "display_rounded_2dec_mmhg": float(bvp_0497["wigner_seitz_two_zone_fv_gel_only_supply_core_po2_mmhg"]),
                "passes_unrounded_0p5mmhg_gate": bool(float(bvp_0497["wigner_seitz_two_zone_fv_gel_only_supply_core_po2_unrounded_mmhg"]) >= 0.5),
            },
        },
        "design_z_and_contrast_b3_unrounded_min_face_audit": {
            "pb30_100um_d_chronic": z_pb30_100,
            "pb30_80um_d_chronic": z_pb30_80,
            "pb30_100um_d_chronic_div3_collagen": z_pb30_100_div3,
            "pfc_5volpct_pb25_100um": z_pfc5_25_100,
            "pfc_5volpct_pb25_80um": z_pfc5_25_80,
            "pfc_5volpct_conditional_20x_solubility_reservoir": {
                "pfc_droplet_volume_cm3_in_4p6667cm3": round(pfc_5pct_vol_cm3, 4),
                "releasable_o2_40_to_15mmhg_umol": round(pfc_5pct_o2_umol, 5),
                "graft_demand_umol_s": round(graft_500k_demand_umol_s, 5),
                "standalone_buffer_seconds": round(pfc_5pct_bridge_s, 3),
            },
            "contrast_b3_donor_iec_omitted_pb30_intact_kperf_0p1358": {
                "100um": b3_pb30_intact_100,
                "80um": b3_pb30_intact_80,
            },
            "contrast_b3_donor_iec_omitted_pb30_halved_kperf_0p0679": {
                "100um": b3_pb30_halved_100,
                "80um": b3_pb30_halved_80,
            },
            "unrounded_bed_po2_cutoffs_mmhg": {
                "mode_a_mixed_100um_d_chronic_48x48x28": round(cut_100_48, 6),
                "mode_a_mixed_100um_d_chronic_72x72x42": round(cut_100_72, 6),
                "mode_a_mixed_80um_d_chronic_48x48x28": round(cut_80_48, 6),
                "mode_a_mixed_80um_d_chronic_72x72x42": round(cut_80_72, 6),
                "mode_a_mixed_100um_d_chronic_div3_collagen_48x48x28": round(cut_100_div3, 4),
                "contrast_b3_intact_kperf_0p1358_100um_48x48x28": round(cut_b3_intact_100, 6),
                "contrast_b3_intact_kperf_0p1358_80um_48x48x28": round(cut_b3_intact_80, 6),
                "contrast_b3_halved_kperf_0p0679_100um_48x48x28": round(cut_b3_halved_100, 6),
                "contrast_b3_halved_kperf_0p0679_80um_48x48x28": round(cut_b3_halved_80, 6),
            },
        },
        "gate_p05_two_timepoint_p04040_and_dual_basis_h2o2_audit": {
            "precure_ceiling_iu_333cm2": precure_cap_iu,
            "assumed_cure_yield": cure_yield_assumed,
            "postcure_t0_at_65pct_yield_under_158300u_cap_iu": postcure_at_cap_iu,
            "conditional_min_yield_pct_if_day14_retention_le_50pct": round(100.0 * (target_336h_iu / 0.50) / precure_cap_iu, 4),
            "conditional_min_yield_pct_if_day14_retention_le_49pct": round(100.0 * (target_336h_iu / 0.49) / precure_cap_iu, 4),
            "min_day14_retention_pct_at_65pct_cure_yield": round(100.0 * target_336h_iu / postcure_at_cap_iu, 4),
            "conditional_yield_qualification_note": (
                "The <93.2407% (<95.1436% at 0.70^2) cure-yield incompatibility with the 158,300 U pre-cure ceiling is "
                "conditional on assuming <=50% (<=49%) Day-14 retention; at 65% cure yield (102,895 IU post-cure), any "
                "formulation achieving >=71.7236% Day-14 retention (for example 80.0% Day-7 and 75.0% Day-14 = 77,171.25 IU) "
                "reconciles both canonical timepoints under the 158,300 U cap, whereas t_1/2 = 168 h (K2, 50.0% Day-7) fails Day 7."
            ),
            "k2_unrounded_total_mass_mg": k2_total_mass_mg,
            "k2_exact_unrounded_endotoxin_cap_eu_per_mg": min(0.10, 0.50 / k2_total_mass_mg),
            "k2_rounded_0p05505_implied_burden_eu_exceeds_0p50": round(0.05505 * k2_total_mass_mg, 7),
            "sizing_scenarios": p05_sizing_scenarios,
            "single_50um_bead_cell_pitch_um": round(side_cm * 1e4, 2),
            "single_50um_bead_lamina_volume_fraction_pct": round(100.0 * v_b / ((side_cm ** 2) * lam_cm), 4),
            "stationary_aged_repeat_burst_dual_basis_h2o2": dual_basis_h2o2,
        },
    }
# === END_IIP_0025_HURDLE4_SPATIAL_OXYGEN_CATALASE_AND_CHALLENGERS ===


# === BEGIN_IIP_0028_MEASURED_POTENCY_DEC084_MIXTURE_AND_GLUCAGON_AUDIT ===
def _betacf_l4_iip_0028(a: float, b: float, x: float) -> float:
    max_it = 300
    eps = 3.0e-15
    fpmin = 1.0e-300
    qab = a + b
    qap = a + 1.0
    qam = a - 1.0
    c = 1.0
    d = 1.0 - qab * x / qap
    if abs(d) < fpmin:
        d = fpmin
    d = 1.0 / d
    h = d
    for m in range(1, max_it + 1):
        m2 = 2 * m
        aa = m * (b - m) * x / ((qam + m2) * (a + m2))
        d = 1.0 + aa * d
        if abs(d) < fpmin:
            d = fpmin
        c = 1.0 + aa / c
        if abs(c) < fpmin:
            c = fpmin
        d = 1.0 / d
        h *= d * c
        aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2))
        d = 1.0 + aa * d
        if abs(d) < fpmin:
            d = fpmin
        c = 1.0 + aa / c
        if abs(c) < fpmin:
            c = fpmin
        d = 1.0 / d
        del_h = d * c
        h *= del_h
        if abs(del_h - 1.0) < eps:
            break
    return h


def _reg_inc_beta_l4_iip_0028(a: float, b: float, x: float) -> float:
    if x <= 0.0:
        return 0.0
    if x >= 1.0:
        return 1.0
    lbeta = math.lgamma(a) + math.lgamma(b) - math.lgamma(a + b)
    bt = math.exp(a * math.log(x) + b * math.log(1.0 - x) - lbeta)
    if x < (a + 1.0) / (a + b + 2.0):
        return bt * _betacf_l4_iip_0028(a, b, x) / a
    return 1.0 - bt * _betacf_l4_iip_0028(b, a, 1.0 - x) / b


def _student_t_ppf_l4_iip_0028(p: float, df: float) -> float:
    lo, hi = -40.0, 40.0
    for _ in range(100):
        mid = 0.5 * (lo + hi)
        if mid == 0.0:
            cdf_v = 0.5
        else:
            xv = df / (df + mid * mid)
            ib = 0.5 * _reg_inc_beta_l4_iip_0028(0.5 * df, 0.5, xv)
            cdf_v = 1.0 - ib if mid > 0.0 else ib
        if cdf_v < p:
            lo = mid
        else:
            hi = mid
    return 0.5 * (lo + hi)


def _one_sided_paired_t_power_l4_iip_0028(effect_over_sd: float, n_blocks: int, alpha: float = 0.05) -> float:
    df = float(n_blocks - 1)
    t_crit = _student_t_ppf_l4_iip_0028(1.0 - alpha, df)
    ncp = effect_over_sd * math.sqrt(float(n_blocks))
    v_max = max(df + 14.0 * math.sqrt(2.0 * df), 80.0)
    n_steps = 800
    h = v_max / n_steps

    def _integrand(v: float) -> float:
        if v <= 0.0:
            return 0.0
        log_f = (0.5 * df - 1.0) * math.log(v) - 0.5 * v - 0.5 * df * math.log(2.0) - math.lgamma(0.5 * df)
        z = t_crit * math.sqrt(v / df) - ncp
        return math.exp(log_f) * 0.5 * math.erfc(z / math.sqrt(2.0))

    acc = _integrand(1e-12) + _integrand(v_max)
    for k_i in range(1, n_steps):
        acc += (4.0 if (k_i % 2 == 1) else 2.0) * _integrand(k_i * h)
    return acc * h / 3.0


def solve_dual_band_unit_cell_iip_0028(
    load_ieq: float,
    diam_um: float,
    buf_um: float = 20.0,
    endo_ocr_mult: float = 1.097,
    vasc_load_equiv_ieq: float | None = None,
    vasc_ocr_mult: float = 1.097,
    standoff_phys_um: float = 15.0,
    pb_mmhg: float = 35.0,
    nx_c: int = 40,
    nz_c: int = 70,
) -> Dict[str, Any]:
    """
    Live 3D FVM dual-band worst-face unit-cell solver evaluating top and bottom harmonic
    interface minima across measured-potency DEC-084 mixture loadings (IIP-0028; DEC-106).
    """
    if vasc_load_equiv_ieq is None:
        vasc_load_equiv_ieq = load_ieq
    slab_um, lam_um = 350.0, 100.0
    p_krogh_chronic = ALPHA_BUNSEN_MOL_CM3_MMHG * 1.50e-5
    p_krogh_islet = ALPHA_BUNSEN_MOL_CM3_MMHG * D_O2_ISLET_TISSUE_CM2_S
    rad_cm = 0.5 * diam_um * 1e-4
    vol_sph = (4.0 / 3.0) * math.pi * (rad_cm ** 3)
    n_face = 0.5 * ((load_ieq * VOL_1_IEQ_CM3) / vol_sph)
    side_cm = math.sqrt(1.0 / n_face)
    hex_pitch_cm = math.sqrt(2.0 / (math.sqrt(3.0) * n_face))
    slab_cm = slab_um * 1e-4
    lam_half_cm = 0.5 * lam_um * 1e-4
    buf_cm = buf_um * 1e-4
    band_inner_z = lam_half_cm + buf_cm
    band_outer_z = 0.5 * slab_cm