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…

                            else _run_single_pouch_pde_regime(
                                regime_id=f"IIP0002_REF_{law.upper()}_T50_{int(t50_h)}H_KP_{kp:.4f}_NZ{nz_ref}",
                                f_chem_initial=0.45,
                                spatial_mode="central",
                                cao2_half_life_h=68.0,
                                catalase_present=True,
                                iec_max_perfusion_fraction=0.97,
                                boundary_po2_mmhg=40.0,
                                inosculation_t50_h=t50_h,
                                demand_multiplier_extra=1.0,
                                release_kinetics_law=law,
                                nz_override=nz_ref,
                                k_perf_max_s_override=kp,
                                _assess_numerical_gate=False,
                            )
                        )
                        ws_ref = r_ref["wigner_seitz_120um_cluster_pericellular_shell_audit_v80_001"]
                        mesh_receipts[f"nz_{nz_ref}"] = {
                            "nz_nodes": nz_ref,
                            "dz_um": round(350.0 / (nz_ref - 1), 4),
                            "min_slab_po2_unrounded_mmhg": float(r_ref["min_slab_po2_unrounded_mmhg"]),
                            "gel_only_120um_surface_po2_unrounded_mmhg": float(
                                ws_ref["two_zone_fv_gel_only_supply_surface_po2_unrounded_mmhg"]
                            ),
                            "gel_only_120um_core_po2_unrounded_mmhg": float(
                                ws_ref["two_zone_fv_gel_only_supply_120um_core_po2_unrounded_mmhg"]
                            ),
                            "uniform_120um_core_po2_unrounded_mmhg": float(
                                ws_ref["two_zone_fv_uniform_supply_120um_core_po2_unrounded_mmhg"]
                            ),
                            "passes_15mmHg_slab_po2_gate": bool(
                                r_ref["required_120um_oxygen_solvers_converged"] and r_ref["min_slab_po2_unrounded_mmhg"] >= 15.0
                            ),
                            "passes_strict_2p0mmHg_gel_only_120um_core_gate": bool(
                                r_ref["required_120um_oxygen_solvers_converged"]
                                and r_ref["min_slab_po2_unrounded_mmhg"] >= 15.0
                                and ws_ref["two_zone_fv_gel_only_supply_120um_core_po2_unrounded_mmhg"] >= 2.0
                                and r_ref["strict_nonoxygen_gates_passed"]
                            ),
                        }
                    near_threshold_refinement_rows.append(
                        {
                            "release_kinetics_law": law,
                            "target_curve_midpoint_t50_h": t50_h,
                            "coupled_internal_vascular_exchange_kp_s_inv": kp,
                            "within_0p10mmHg_of_gate": bool(
                                abs(slab_val - 15.0) <= 0.10
                                or abs(gel_core_val - 2.0) <= 0.10
                                or abs(gel_core_val - 0.5) <= 0.10
                                or abs(uni_core_val - 2.0) <= 0.10
                            ),
                            "mesh_refinement_nz_51_101_201": mesh_receipts,
                            "gate_stability_across_51_101_201": bool(
                                len({m["passes_15mmHg_slab_po2_gate"] for m in mesh_receipts.values()}) == 1
                                and len({m["passes_strict_2p0mmHg_gel_only_120um_core_gate"] for m in mesh_receipts.values()}) == 1
                            ),
                        }
                    )

    # Exact zero-vessel lower bound (v_perf(t) == 0 identically, k_p = 0) vs legacy v_perf(0) = 0.01 decay control:
    zero_vessel_controls: Dict[str, Any] = {}
    for law in release_laws:
        r_zero_exact = _run_single_pouch_pde_regime(
            regime_id=f"IIP0002_{law.upper()}_EXACT_ZERO_VESSEL_BOUND_KP_0",
            f_chem_initial=0.45,
            spatial_mode="central",
            cao2_half_life_h=68.0,
            catalase_present=True,
            iec_max_perfusion_fraction=0.0,
            boundary_po2_mmhg=40.0,
            inosculation_t50_h=36.0,
            demand_multiplier_extra=1.0,
            release_kinetics_law=law,
            k_perf_max_s_override=0.0,
            zero_internal_vascular_state=True,
            _include_hourly_slab_po2_vector=True,
            _assess_numerical_gate=False,
        )
        r_zero_legacy_v0_0p01 = _run_single_pouch_pde_regime(
            regime_id=f"IIP0002_{law.upper()}_LEGACY_V0_0P01_KP_0",
            f_chem_initial=0.45,
            spatial_mode="central",
            cao2_half_life_h=68.0,
            catalase_present=True,
            iec_max_perfusion_fraction=0.0,
            boundary_po2_mmhg=40.0,
            inosculation_t50_h=36.0,
            demand_multiplier_extra=1.0,
            release_kinetics_law=law,
            k_perf_max_s_override=0.0,
            zero_internal_vascular_state=False,
            _include_hourly_slab_po2_vector=True,
            _assess_numerical_gate=False,
        )
        exact_row = _summarize_regime_row(
            r_zero_exact,
            law=law,
            t50_h=None,
            kp=0.0,
            timing_scope="exact_zero_vessel_lower_bound",
            include_cluster_trajectory=True,
        )
        joint_grid_rows.append(exact_row)
        r_zero_legacy_min_ph = r_zero_legacy_v0_0p01.get(
            "closed_pocket_metabolic_acidification_min_ph_unrounded",
            r_zero_legacy_v0_0p01["closed_pocket_metabolic_acidification_min_ph_v80_095"],
        )
        r_zero_exact_min_ph = r_zero_exact.get(
            "closed_pocket_metabolic_acidification_min_ph_unrounded",
            r_zero_exact["closed_pocket_metabolic_acidification_min_ph_v80_095"],
        )
        zero_vessel_controls[law] = {
            "exact_zero_internal_vascular_exchange_vperf_identically_zero": exact_row,
            "legacy_kp0_with_vperf0_0p01_decay_comparator": {
                "min_slab_po2_unrounded_mmhg": r_zero_legacy_v0_0p01["min_slab_po2_unrounded_mmhg"],
                "min_closed_pocket_acidic_ph_unrounded": r_zero_legacy_min_ph,
                "hour0_closed_pocket_acidic_ph_difference_from_exact_vperf0": round(
                    abs(
                        r_zero_exact["trajectory_0h_to_336h"][0]["min_closed_pocket_acidic_ph"]
                        - r_zero_legacy_v0_0p01["trajectory_0h_to_336h"][0]["min_closed_pocket_acidic_ph"]
                    ),
                    6,
                ),
                "global_336h_min_acidic_ph_difference_from_exact_vperf0": abs(
                    r_zero_exact_min_ph - r_zero_legacy_min_ph
                ),
            },
        }

    def _find_row(law: str, t50_h: float, kp: float) -> Dict[str, Any]:
        return next(
            r
            for r in joint_grid_rows
            if r["release_kinetics_law"] == law
            and r["target_curve_midpoint_t50_h"] is not None
            and math.isclose(float(r["target_curve_midpoint_t50_h"]), t50_h, rel_tol=1e-9)
            and math.isclose(float(r["coupled_internal_vascular_exchange_kp_s_inv"]), kp, rel_tol=1e-9)
        )

    fo_36_014 = _find_row("first_order", 36.0, 0.1400)
    sc_36_014 = _find_row("shrinking_core", 36.0, 0.1400)
    fo_40_014 = _find_row("first_order", 40.0, 0.1400)
    sc_40_014 = _find_row("shrinking_core", 40.0, 0.1400)
    fo_56_014 = _find_row("first_order", 56.0, 0.1400)
    sc_56_014 = _find_row("shrinking_core", 56.0, 0.1400)
    fo_60_014 = _find_row("first_order", 60.0, 0.1400)
    sc_60_014 = _find_row("shrinking_core", 60.0, 0.1400)
    fo_36_00944 = _find_row("first_order", 36.0, 0.0944)
    zv_fo_exact = zero_vessel_controls["first_order"]["exact_zero_internal_vascular_exchange_vperf_identically_zero"]
    zv_sc_exact = zero_vessel_controls["shrinking_core"]["exact_zero_internal_vascular_exchange_vperf_identically_zero"]
    diff_36_slab = fo_36_014["min_slab_po2_unrounded_mmhg"] - sc_36_014["min_slab_po2_unrounded_mmhg"]
    diff_60_slab = fo_60_014["min_slab_po2_unrounded_mmhg"] - sc_60_014["min_slab_po2_unrounded_mmhg"]

    return {
        "proposal_id": "IIP-0002",
        "title": "Test delayed and reduced vascular oxygen support together",
        "layer": "Layer 4",
        "test_gate": "Test 5",
        "open_question_id": "OQ-005",
        "open_question_title": "Does the oxygen supply hold up under pessimistic assumptions?",
        "status": "accepted_in_open_draft",
        "credit": (
            "Marissa Famularo (ORCID 0009-0001-8032-2215, GitHub @MarissaFamularo), IIP-0002, "
            "https://github.com/OpenIslet/openislet/issues/2. AI tools as she reported them: ChatGPT, GPT-6 Astra Pro "
            "(self-reported, never verified)."
        ),
        "exploratory_engineering_scope_note": (
            "Exploratory engineering sensitivity grid characterizing the combined early-window interaction of vascular "
            "inosculation timing and internal vascular exchange across both CaO2 release laws. "
            "Neither cited paper calibrates the chosen timing values, oxygen-transfer coefficients, or their joint distribution. "
            "Grid corners are exploratory model-dependence stress settings, not experimentally calibrated worst cases or clinical probabilities."
        ),
        "timing_axis_specification": {
            "timing_axis_label": "target_curve_midpoint_t50_h",
            "target_logistic_curve_equation": "v_target(t) = v_max / (1 + exp(-(t - t50) / 8.0)), v_max = 0.97",
            "lag_ode_equation": "dv_perf/dt = (v_target(t) - v_perf(t)) / 3.0, v_perf(0) = 0.01 (or v_perf(t) == 0 in exact zero-vessel bound)",
            "realized_half_plateau_definition": "Time t (hours) at which realized v_perf(t) reaches 0.5 * v_max = 0.485",
            "realized_minus_target_midpoint_lag_hours": round(realized_t50_map[36.0] - 36.0, 4),
            "target_to_realized_half_plateau_hours_map": {
                f"{int(t50)}h": round(realized_t50_map[t50], 4) for t50 in timings_t50_h
            },
            "hypothetical_stress_test_midpoints_hours": [96.0, 168.0],
        },
        "coupled_vascular_exchange_axis_disclosure": {
            "capacity_axis_label": "coupled_internal_vascular_exchange_coefficient_kp_s_inv",
            "effective_time_dependent_rate": "k_perf_s(t) = k_p * v_perf(t)",
            "coupled_processes_controlled_by_kp": [
                "Internal vascular O2 exchange: +k_perf_s(t) * (P_boundary - p(z, t)) in the slab O2 reaction-diffusion BVP (acts as an O2 source when local p < P_boundary = 40 mmHg, and as an O2 sink wherever local chemical O2 release raises p > 40 mmHg)",
                "Interstitial H2O2 vascular washout: -k_perf_s(t) * [H2O2](z, t) in _solve_peroxide_slab_bvp (removing H2O2 before catalase disproportionation reduces local chemical O2 generation)",
                "Interstitial OH- / alkaline species clearance: -k_perf_s(t) * [OH-](z, t) in the Henderson-Hasselbalch pH BVP",
            ],
            "interpretation_note": (
                "Because k_perf_s(t) = k_p * v_perf(t) simultaneously governs O2 exchange, H2O2 washout (which reduces catalase-mediated "
                "chemical O2 yield), and OH- clearance, and becomes an O2 sink wherever local pO2 exceeds the 40 mmHg host boundary, "
                "k_p is reported as a coupled internal vascular-exchange parameter rather than a pure oxygen-delivery coefficient."
            ),
        },
        "design_geometry_and_gy004_oxygen_demand_scope": {
            "reference_architecture_evaluated": "LEAD_CENTRAL_LAMINA_PDMS_CAO2_F045_PLUS_CATALASE (350 um total slab thickness, 119 um central lamina [a = 0.34], uniform conservative cellular demand, 120 um cluster diameter [R = 60 um], 2,500 IEQ/cm^2 across 200 cm^2 [7.00 cm^3], D_O2,gel = 9.40254e-6 cm^2/s)",
            "cao2_inventory_preserved_across_release_laws": {
                "f_chem_initial": 0.45,
                "cao2_initial_molar_inventory_mmol": 10.981,
                "cao2_initial_mass_mg_in_7cm3_slab": 791.5,
                "cao2_hydrolysis_half_life_hours": 68.0,
            },
            "gy004_oxygen_consumption_consistency": {
                "graveyard_entry": "GY-004 (RNLS knockout excluded from lead backbone due to ~1.18x basal OCR elevation)",
                "cell_intrinsic_ocr_multiplier_all_rows": 1.0,
                "vmax_islet_pure_mol_cm3_s": VMAX_ISLET_PURE_MOL_CM3_S,
                "iec_stromal_metabolic_overhead_multiplier": 1.15,
                "cross_design_transferability_caveat": (
                    "All rows hold intrinsic cellular oxygen demand fixed at 1.0x baseline V_max (RNLS-WT, consistent with GY-004) "
                    "plus the 1.15x vascular/stromal demand factor in the 350 um / 119 um-lamina / 120 um-cluster uniform-demand reference slab. "
                    "Numerical outputs do not automatically transfer to Design A Layout B (outer_annulus demand), Design B (GY-004 RNLS-KO 1.18x OCR comparator), "
                    "or Design X (100 um thinned central lamina with 80-100 um micro-islet diameter cap)."
                ),
            },
        },
        "release_law_direction_flip_and_gate_brackets_at_kp_0p14": {
            "direction_flip_explanation": (
                "Under matched initial CaO2 inventory (10.981 mmol, 791.5 mg) and 68.0 h half-time, reaction-controlled shrinking-core "
                "(m/m0)^(2/3) release is lower than first-order release around the early 13-18 h trough when t50 = 36 h "
                f"(min slab pO2 {fo_36_014['min_slab_po2_mmhg']:.3f} mmHg [FO, t={fo_36_014['time_of_min_slab_po2_hours']:.0f} h, gel-only core {fo_36_014['gel_only_120um_core_po2_mmhg']:.3f} mmHg] "
                f"vs {sc_36_014['min_slab_po2_mmhg']:.3f} mmHg [SC, t={sc_36_014['time_of_min_slab_po2_hours']:.0f} h, gel-only core {sc_36_014['gel_only_120um_core_po2_mmhg']:.3f} mmHg]), "
                "but retains slightly higher mid-trajectory chemical O2 release around the 36-39 h trough when inosculation is delayed to t50 = 60 h "
                f"(min slab pO2 {fo_60_014['min_slab_po2_mmhg']:.3f} mmHg [FO, t={fo_60_014['time_of_min_slab_po2_hours']:.0f} h, gel-only core {fo_60_014['gel_only_120um_core_po2_mmhg']:.3f} mmHg] "
                f"vs {sc_60_014['min_slab_po2_mmhg']:.3f} mmHg [SC, t={sc_60_014['time_of_min_slab_po2_hours']:.0f} h, gel-only core {sc_60_014['gel_only_120um_core_po2_mmhg']:.3f} mmHg]), "
                f"flipping the sign of (first_order - shrinking_core) between 36 h ({diff_36_slab:+.3f} mmHg slab) and 60 h ({diff_60_slab:+.3f} mmHg slab)."
            ),
            "strict_2p0mmHg_gel_only_core_gate_flip_at_kp_0p14": {
                "first_order_flips_between_t50_hours": [36.0, 40.0],
                "first_order_t50_36h_gel_only_core_unrounded_mmhg": fo_36_014["gel_only_120um_core_po2_unrounded_mmhg"],
                "first_order_t50_40h_gel_only_core_unrounded_mmhg": fo_40_014["gel_only_120um_core_po2_unrounded_mmhg"],
                "shrinking_core_already_below_2p0mmHg_at_t50_36h": True,
                "shrinking_core_t50_36h_gel_only_core_unrounded_mmhg": sc_36_014["gel_only_120um_core_po2_unrounded_mmhg"],
                "shrinking_core_t50_40h_gel_only_core_unrounded_mmhg": sc_40_014["gel_only_120um_core_po2_unrounded_mmhg"],
            },
            "slab_15mmHg_gate_flip_at_kp_0p14": {
                "first_order_flips_between_t50_hours": [56.0, 60.0],
                "first_order_t50_56h_min_slab_unrounded_mmhg": fo_56_014["min_slab_po2_unrounded_mmhg"],
                "first_order_t50_60h_min_slab_unrounded_mmhg": fo_60_014["min_slab_po2_unrounded_mmhg"],
                "shrinking_core_flips_between_t50_hours": [56.0, 60.0],
                "shrinking_core_t50_56h_min_slab_unrounded_mmhg": sc_56_014["min_slab_po2_unrounded_mmhg"],
                "shrinking_core_t50_60h_min_slab_unrounded_mmhg": sc_60_014["min_slab_po2_unrounded_mmhg"],
            },
            "near_threshold_36h_kp_0p0944_unrounded_check": {
                "target_curve_midpoint_t50_h": 36.0,
                "coupled_internal_vascular_exchange_kp_s_inv": 0.0944,
                "nz51_min_slab_po2_unrounded_mmhg": fo_36_00944["min_slab_po2_unrounded_mmhg"],
                "shortfall_below_15mmHg_at_nz51_mmhg": round(15.0 - fo_36_00944["min_slab_po2_unrounded_mmhg"], 6),