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…

            "enable_low_g_sst_clamp": True,
            "enable_constitutive_sst_basal_suppression": True,
            "enable_sst_suprabasal_gain_attenuation": True,
            "enable_sst_calcineurin_shutoff_accel": False,
        },
        {
            "graft_id": "OAT_4_REMOVE_ONLY_ALPHA_GCG_COUNTERREGULATION",
            "description": "Ablates only graft alpha-cell low-glucose glucagon counter-regulation while retaining all delta-cell SST paracrine brake sub-mechanisms",
            "k05_gsis_mM": 7.2,
            "hill_gsis": 4.2,
            "has_ucn3_delta_sst_brake": True,
            "has_alpha_gcg_counterregulation": False,
            "enable_low_g_sst_clamp": True,
            "enable_constitutive_sst_basal_suppression": True,
            "enable_sst_suprabasal_gain_attenuation": True,
            "enable_sst_calcineurin_shutoff_accel": True,
        },
        {
            "graft_id": "OAT_5_PURE_DELTA_SST_ABLATION_RETAINING_ALPHA_GCG",
            "description": "Ablates all delta-cell SST paracrine sub-mechanisms (force_zero_sst) while retaining graft alpha-cell glucagon counter-regulation (60% beta : 30% alpha : 0% functional delta)",
            "beta_cell_fraction": 0.60,
            "alpha_cell_fraction": 0.30,
            "delta_cell_fraction": 0.0,
            "k05_gsis_mM": 7.2,
            "hill_gsis": 4.2,
            "has_ucn3_delta_sst_brake": False,
            "has_alpha_gcg_counterregulation": True,
            "enable_low_g_sst_clamp": False,
            "enable_constitutive_sst_basal_suppression": False,
            "enable_sst_suprabasal_gain_attenuation": False,
            "enable_sst_calcineurin_shutoff_accel": False,
        },
    ]
    oat_ablations = [_run_single_metabolic_ode_model(spec, include_trajectory=False) for spec in oat_specs]

    # Dedicated low-glucose (<4.2 mM) intensive exercise/fasting challenge verifying OAT_1 & OAT_1A (<4.2 mM clamp; AUDIT-REF & 098):
    low_g_lead = _run_single_metabolic_ode_model(
        graft_models[0], include_trajectory=False, low_glucose_challenge_mode=True
    )
    low_g_oat1 = _run_single_metabolic_ode_model(
        oat_specs[0], include_trajectory=False, low_glucose_challenge_mode=True
    )
    low_g_oat1a_beta_only = _run_single_metabolic_ode_model(
        oat_specs[1], include_trajectory=False, low_glucose_challenge_mode=True
    )

    # Zero-SST state verification (AUDIT-REF): when S(t) == 0, all SST-attributed branches collapse to SST-null
    zero_sst_lead = _run_single_metabolic_ode_model(
        {**graft_models[0], "has_alpha_gcg_counterregulation": False},
        include_trajectory=False,
        force_zero_sst=True,
    )
    pure_sst_ablation_retaining_alpha = next(
        a for a in oat_ablations if a["graft_id"] == "OAT_5_PURE_DELTA_SST_ABLATION_RETAINING_ALPHA_GCG"
    )
    beta_only_ref = simulations[1]

    # Null-graft (v=0) and partial-dose (v=0.05, 0.10, 0.25, 0.50, 0.60, 1.0) T1D control table (AUDIT-REF & AUDIT-REF):
    viable_dose_response_controls = []
    for v_test in [0.0, 0.05, 0.10, 0.25, 0.50, 0.60, 1.0]:
        r_v = _run_single_metabolic_ode_model(
            graft_models[0],
            include_trajectory=False,
            viable_endocrine_fraction=v_test,
            _integrator=_ORIGINAL_RK4_INTEGRATE if v_test == 0.60 else None,
        )
        viable_dose_response_controls.append(
            {
                "viable_endocrine_fraction": v_test,
                "equivalent_viable_ieq_dose": int(round(500000 * v_test)),
                "mean_glucose_mM": r_v["mean_glucose_mM"],
                "min_glucose_mM": r_v["min_glucose_mM"],
                "max_postprandial_glucose_mM": r_v["max_postprandial_glucose_mM"],
                "time_in_tight_range_3p9_to_10p0_mM_pct": r_v["time_in_tight_range_3p9_to_10p0_mM_pct"],
                "passes_clinical_glycemic_safety_targets": r_v["passes_clinical_glycemic_safety_targets"],
            }
        )
    r_v060_beta_eq = next(row for row in viable_dose_response_controls if row["viable_endocrine_fraction"] == 0.60)
    r_v100_ref = next(row for row in viable_dose_response_controls if row["viable_endocrine_fraction"] == 1.0)

    # Live 9-state 2-stage Erlang extravascular insulin transport lag sweep (AUDIT-REF):
    # S_ins -> I_isf_1 -> I_isf_2 -> I_p with tau_tr = T / 2 (washout t_50 = 1.678347 * tau_tr = 0.8391735 * T):
    erlang_lag_grid_min = [0.0, 8.0, 9.5332, 11.54, 16.0, 20.0]
    erlang_std_sweep = {}
    erlang_low_g_sweep = {}
    for t_lag in erlang_lag_grid_min:
        r_std_lag = (
            simulations[0]
            if t_lag <= 0.0
            else _run_single_metabolic_ode_model(
                graft_models[0],
                include_trajectory=False,
                extravascular_insulin_lag_total_min=t_lag,
                _integrator=_ORIGINAL_RK4_INTEGRATE,
                _skip_shutoff_half_time=True,
            )
        )
        r_lowg_lag = (
            low_g_lead
            if t_lag <= 0.0
            else _run_single_metabolic_ode_model(
                graft_models[0],
                include_trajectory=False,
                low_glucose_challenge_mode=True,
                extravascular_insulin_lag_total_min=t_lag,
                _integrator=_ORIGINAL_RK4_INTEGRATE,
                _skip_shutoff_half_time=True,
            )
        )
        erlang_std_sweep[t_lag] = {
            "total_transport_lag_T_min": t_lag,
            "per_stage_tau_tr_min": round(0.5 * t_lag, 4),
            "washout_t50_min": round(0.8391735 * t_lag, 4),
            "mean_glucose_mM": r_std_lag["mean_glucose_mM"],
            "min_glucose_mM": r_std_lag["min_glucose_mM"],
            "min_glucose_0p25min_unrounded_mM": round(
                float(r_std_lag["full_5761_step_0p25min_grid_verification_v80_025"]["min_glucose_0p25min_unrounded_mM"]), 4
            ),
            "max_postprandial_glucose_mM": r_std_lag["max_postprandial_glucose_mM"],
            "time_in_tight_range_3p9_to_10p0_mM_pct": r_std_lag["time_in_tight_range_3p9_to_10p0_mM_pct"],
            "passes_clinical_glycemic_safety_targets": r_std_lag["passes_clinical_glycemic_safety_targets"],
        }
        erlang_low_g_sweep[t_lag] = {
            "total_transport_lag_T_min": t_lag,
            "per_stage_tau_tr_min": round(0.5 * t_lag, 4),
            "washout_t50_min": round(0.8391735 * t_lag, 4),
            "mean_glucose_mM": r_lowg_lag["mean_glucose_mM"],
            "min_glucose_mM": r_lowg_lag["min_glucose_mM"],
            "min_glucose_0p25min_unrounded_mM": round(
                float(r_lowg_lag["full_5761_step_0p25min_grid_verification_v80_025"]["min_glucose_0p25min_unrounded_mM"]), 4
            ),
            "max_postprandial_glucose_mM": r_lowg_lag["max_postprandial_glucose_mM"],
            "time_in_tight_range_3p9_to_10p0_mM_pct": r_lowg_lag["time_in_tight_range_3p9_to_10p0_mM_pct"],
            "passes_3p50mM_hypoglycemia_floor": bool(
                float(r_lowg_lag["full_5761_step_0p25min_grid_verification_v80_025"]["min_glucose_0p25min_unrounded_mM"]) >= 3.50
            ),
        }

    # Common basal and shutoff constants sensitivity (AUDIT-REF):
    common_constants_sensitivity = [
        _run_single_metabolic_ode_model(gm, include_trajectory=False, force_common_basal_and_shutoff=True)
        for gm in graft_models
    ]

    # Paracrine-preserved (local 1.0x intra-cluster UCN3/SST stoichiometry) vs systemic-export-scaled (v in {0.60, 0.80, 1.00})
    # Erlang lag sweep across T in {0.0, 5.179, 9.5332, 11.54} min (S55R2-S2-01):
    paracrine_preserved_sweep = []
    for v_sys in (0.60, 0.80, 1.00):
        for t_lag_test in (0.0, 5.179, 9.5332, 11.54):
            r_pp_std = _run_single_metabolic_ode_model(
                graft_models[0],
                include_trajectory=False,
                viable_endocrine_fraction=v_sys,
                extravascular_insulin_lag_total_min=t_lag_test,
                local_paracrine_preserved_systemic_scale=True,
                _integrator=_ORIGINAL_RK4_INTEGRATE,
                _skip_shutoff_half_time=True,
            )
            r_pp_lowg = _run_single_metabolic_ode_model(
                graft_models[0],
                include_trajectory=False,
                viable_endocrine_fraction=v_sys,
                low_glucose_challenge_mode=True,
                extravascular_insulin_lag_total_min=t_lag_test,
                local_paracrine_preserved_systemic_scale=True,
                _integrator=_ORIGINAL_RK4_INTEGRATE,
                _skip_shutoff_half_time=True,
            )
            paracrine_preserved_sweep.append(
                {
                    "systemic_export_fraction_v": v_sys,
                    "local_paracrine_stoichiometry": 1.0,
                    "total_transport_lag_T_min": t_lag_test,
                    "washout_t50_min": round(0.8391735 * t_lag_test, 4),
                    "standard_protocol_tir_pct": r_pp_std["time_in_tight_range_3p9_to_10p0_mM_pct"],
                    "standard_protocol_min_glucose_mM": r_pp_std["min_glucose_mM"],
                    "standard_protocol_max_postprandial_glucose_mM": r_pp_std["max_postprandial_glucose_mM"],
                    "low_glucose_challenge_min_glucose_0p25min_unrounded_mM": round(
                        float(r_pp_lowg["full_5761_step_0p25min_grid_verification_v80_025"]["min_glucose_0p25min_unrounded_mM"]), 4
                    ),
                    "low_glucose_challenge_passes_3p50mM_floor": bool(
                        float(r_pp_lowg["full_5761_step_0p25min_grid_verification_v80_025"]["min_glucose_0p25min_unrounded_mM"]) >= 3.50
                    ),
                }
            )

    # Coupled Part A PDE viability -> Part B Metabolic ODE qualification (AUDIT-REF):
    # Each component response fraction (core pO2, slab pO2, H2O2, pH, and 336h dynamic viability) is capped
    # individually at 1.0 so surplus in one metric never masks a deficit in another!
    coupled_pde_ode_table = []
    if pde_regimes:
        for pr in pde_regimes:
            audit = pr.get("wigner_seitz_120um_cluster_pericellular_shell_audit_v80_001", {})
            prescribed_diam = float(pr.get("prescribed_cluster_diameter_um", 120.0))
            prescribed_audit = (
                pr.get("wigner_seitz_prescribed_cluster_pericellular_shell_audit_dec064", audit)
                if not math.isclose(prescribed_diam, 120.0, rel_tol=1e-9)
                else audit
            )
            conservative_core = prescribed_audit.get(
                "two_zone_fv_gel_only_supply_core_po2_unrounded_mmhg",
                prescribed_audit.get("two_zone_fv_gel_only_supply_120um_core_po2_unrounded_mmhg"),
            )
            conservative_surf = prescribed_audit.get(
                "two_zone_fv_gel_only_supply_surface_po2_unrounded_mmhg",
                prescribed_audit.get("two_zone_fv_gel_only_supply_surface_po2_mmhg"),
            )
            audit_100 = pr.get("wigner_seitz_100um_dxb_cluster_pericellular_shell_audit_dec064", {})
            audit_80 = pr.get("wigner_seitz_80um_cluster_pericellular_shell_audit_dec064", {})
            surf_100 = float(audit_100.get("two_zone_fv_gel_only_supply_surface_po2_unrounded_mmhg", 0.0) or 0.0)
            surf_80 = float(audit_80.get("two_zone_fv_gel_only_supply_surface_po2_unrounded_mmhg", 0.0) or 0.0)
            core_p = float(conservative_core if conservative_core is not None else
                           pr.get("min_120um_cluster_core_po2_unrounded_mmhg", pr["min_120um_cluster_core_po2_mmhg"]))
            surf_p = float(conservative_surf) if conservative_surf is not None else 0.0
            slab_p = float(pr.get("min_slab_po2_unrounded_mmhg", pr["min_slab_po2_across_14_days_mmhg"]))
            h2o2_max = float(pr.get("max_interstitial_h2o2_unrounded_uM", pr["max_interstitial_h2o2_uM"]))
            ph_max = float(pr.get("max_interstitial_ph_unrounded", pr["max_interstitial_ph"]))
            f_dyn = float(pr.get("coupled_dynamic_14d_viable_endocrine_fraction_unrounded",
                                 pr.get("coupled_dynamic_14d_viable_endocrine_fraction_v80_066", 1.0)))
            f_core = min(1.0, max(0.0, core_p / 2.0))
            f_slab = min(1.0, max(0.0, slab_p / 15.0))
            f_gsis_surf = min(1.0, max(0.0, surf_p / 15.0))
            f_gsis_surf_100 = min(1.0, max(0.0, surf_100 / 15.0))
            f_gsis_surf_80 = min(1.0, max(0.0, surf_80 / 15.0))
            f_o2 = min(1.0, f_core * f_slab)
            f_h2o2 = min(1.0, math.exp(-max(0.0, h2o2_max - 5.0) / 12.0))
            f_ph = min(1.0, math.exp(-max(0.0, ph_max - 7.60) / 0.35))
            f_viable = max(0.0, min(1.0, f_dyn, f_o2 * f_h2o2 * f_ph))
            ode_coupled = _run_single_metabolic_ode_model(
                graft_models[0], include_trajectory=False, viable_endocrine_fraction=f_viable, source_pde_regime_id=pr["regime_id"]
            )
            f_viable_surf_gsis = max(0.0, min(1.0, f_viable * f_gsis_surf))
            ode_surf_gsis_coupled = _run_single_metabolic_ode_model(
                graft_models[0],
                include_trajectory=False,
                viable_endocrine_fraction=f_viable_surf_gsis,
                source_pde_regime_id=pr["regime_id"],
                _integrator=_ORIGINAL_RK4_INTEGRATE,
                _skip_shutoff_half_time=True,
            )
            f_viable_surf_gsis_phys_v060 = max(0.0, min(1.0, 0.60 * f_viable * f_gsis_surf))
            ode_surf_gsis_phys_v060_coupled = _run_single_metabolic_ode_model(
                graft_models[0],
                include_trajectory=False,
                viable_endocrine_fraction=f_viable_surf_gsis_phys_v060,
                source_pde_regime_id=pr["regime_id"],
                local_paracrine_preserved_systemic_scale=True,
                _integrator=_ORIGINAL_RK4_INTEGRATE,
                _skip_shutoff_half_time=True,
            )