d2 = [0.0] * 32 d2[0] += 1.0 - c2_univ for (s0, s1), wp in biv2_y.items(): w = c2_univ * wp if s0 == 0 and s1 == 0: d2[0] += w elif s0 == 1 and s1 == 0: for m4 in range(16): d2[m4] += w * d2_allo4[m4] elif s0 == 0 and s1 == 1: d2[0] += w * (1.0 - u_d2) d2[16] += w * u_d2 else: for m4 in range(16): d2[m4] += w * d2_allo4[m4] * (1.0 - u_d2) d2[m4 | 16] += w * d2_allo4[m4] * u_d2 else: biv2_a = _bivariate(c2_a / c2_u0, c2_auto / c2_u0, phi_c) p_a1_d1, p_a1_d0 = c2_u0 * biv2_a[(1, 1)], c2_u0 * biv2_a[(1, 0)] p_y1_d1, p_y1_d0 = c2_univ * biv2_y[(1, 1)], c2_univ * biv2_y[(1, 0)] require(p_a1_d1 <= p_y1_d1 + 1e-12 and p_a1_d0 <= p_y1_d0 + 1e-12, "Nested H2 firewall ordering violated") require(abs(c2_bx - c2_y) <= 1e-12, "Nested H2 firewall requires c2_bx == c2_y") q11 = p_a1_d1 / p_y1_d1 if p_y1_d1 > 0 else 0.0 q10 = p_a1_d0 / p_y1_d0 if p_y1_d0 > 0 else 0.0 d2_with_a = _frechet_couple(_frechet_joint([pa[1] / c2_a, pb[1] / c2_bx, px[1] / c2_bx], 0.0), py[1] / c2_y, phi_yx, lambda mi: bool(mi & 4)) d2_no_a = _frechet_couple(_frechet_joint([0.0, pb[1] / c2_bx, px[1] / c2_bx], 0.0), py[1] / c2_y, phi_yx, lambda mi: bool(mi & 4)) d2 = [0.0] * 32 d2[0] += 1.0 - c2_univ + c2_univ * biv2_y[(0, 0)] w01 = c2_univ * biv2_y[(0, 1)] d2[0] += w01 * (1.0 - u_d2) d2[16] += w01 * u_d2 for m4 in range(16): d2[m4] += p_y1_d0 * (q10 * d2_with_a[m4] + (1.0 - q10) * d2_no_a[m4]) mix = q11 * d2_with_a[m4] + (1.0 - q11) * d2_no_a[m4] d2[m4] += p_y1_d1 * mix * (1.0 - u_d2) d2[m4 | 16] += p_y1_d1 * mix * u_d2 c2_e = c2_univ * (1.0 - biv2_y[(0, 0)]) if abs(c3_a - c3_bx) <= 1e-12 and abs(c3_d - c3_y) <= 1e-12 and c3_bx <= c3_y + 1e-12: d3_inc = _frechet_joint([pa[2] / c3_bx, pb[2] / c3_bx, px[2] / c3_bx], 0.0) r3 = c3_bx / c3_y u_y3 = py[2] / c3_y d3_both = _frechet_couple(d3_inc, u_y3, phi_yx, lambda mi: bool(mi & 4)) d3_allo4 = [r3 * d3_both[k] for k in range(16)] d3_allo4[0] += (1.0 - r3) * (1.0 - u_y3) d3_allo4[8] += (1.0 - r3) * u_y3 d3_5 = _frechet_couple(d3_allo4, pd[2] / c3_y, phi_c, lambda m4: m4 != 0) d3 = [c3_y * x for x in d3_5] d3[0] += 1.0 - c3_y elif c3_a < c3_bx and abs(c3_bx - c3_y) <= 1e-12 and abs(c3_d - c3_y) <= 1e-12: r_a = c3_a / c3_y d5_all = _frechet_couple(_frechet_couple(_frechet_joint([pa[2] / c3_a, pb[2] / c3_y, px[2] / c3_y], 0.0), py[2] / c3_y, phi_yx, lambda mi: bool(mi & 4)), pd[2] / c3_y, phi_c, lambda m4: m4 != 0) d5_noa = _frechet_couple(_frechet_couple(_frechet_joint([0.0, pb[2] / c3_y, px[2] / c3_y], 0.0), py[2] / c3_y, phi_yx, lambda mi: bool(mi & 4)), pd[2] / c3_y, phi_c, lambda m4: m4 != 0) d3 = [c3_y * (r_a * d5_all[k] + (1.0 - r_a) * d5_noa[k]) for k in range(32)] d3[0] += 1.0 - c3_y else: raise RuntimeError(f"Unsupported H3 ceiling configuration: {(c3_a, c3_bx, c3_y, c3_d)}") c3_e = c3_y require(c4_pa <= c4_pbx + 1e-12 and c4_pbx <= c4_univ + 1e-12, "Invalid H4 ceilings") r_a4 = c4_pa / c4_pbx d_ax_with = _frechet_joint([pa[3] / c4_pa, px[3] / c4_pbx], 1.0) d_ax_no = _frechet_joint([0.0, px[3] / c4_pbx], 1.0) d_ax = [r_a4 * d_ax_with[k] + (1.0 - r_a4) * d_ax_no[k] for k in range(4)] u_b4 = pb[3] / c4_pbx d4_inc = [0.0] * 8 for m_ax in range(4): bm = (m_ax & 1) | (((m_ax >> 1) & 1) << 2) d4_inc[bm] += d_ax[m_ax] * (1.0 - u_b4) d4_inc[bm | 2] += d_ax[m_ax] * u_b4 biv4 = _bivariate(c4_pbx / c4_univ, c4_r3p / c4_univ, phi_c) d4_r3 = _frechet_joint([py[3] / c4_r3p, pd[3] / c4_r3p], 1.0) d4 = [0.0] * 32 d4[0] += 1.0 - c4_univ for (s0, s1), wp in biv4.items(): w = c4_univ * wp if s0 == 0 and s1 == 0: d4[0] += w elif s0 == 1 and s1 == 0: for m3 in range(8): d4[m3] += w * d4_inc[m3] elif s0 == 0 and s1 == 1: for mr in range(4): d4[((mr & 1) << 3) | (((mr >> 1) & 1) << 4)] += w * d4_r3[mr] else: for m3 in range(8): for mr in range(4): d4[m3 | ((mr & 1) << 3) | (((mr >> 1) & 1) << 4)] += w * d4_inc[m3] * d4_r3[mr] c4_e = c4_univ * (1.0 - biv4[(0, 0)]) dists = (d1, d2, d3, d4) for h, dh in enumerate(dists): require(abs(sum(dh) - 1.0) <= 1e-12 and all(w >= -1e-15 for w in dh), f"H{h+1} distribution invalid") for i in range(5): rec = sum(dh[m] for m in range(32) if (m & (1 << i))) require(abs(rec - arms[i][h]) <= 1e-11, f"H{h+1} arm {i} marginal mismatch: {rec} vs {arms[i][h]}") v = [0.0] * 32 v[31] = 1.0 v_h123: list[float] = [] for h, dh in enumerate(dists): vn = [0.0] * 32 for s, vs in enumerate(v): if vs: for m, w in enumerate(dh): if w: vn[s & m] += vs * w v = vn if h == 2: v_h123 = list(v) routes = ("R1", "R2", "R1", "R3P", "R3P") classes = ("UNHARDENED_A_B_X", "UNHARDENED_A_B_X", "UNHARDENED_A_B_X", "DESIGN_Y_FULLSTACK_V2", "D1B_AUTO_C_V2") compat = { "UNHARDENED_A_B_X": frozenset({"R1", "R2"}), "DESIGN_Y_FULLSTACK_V2": frozenset({"R3P"}), "D1B_AUTO_C_V2": frozenset({"R3P"}), } locked = sum(v[s] for s in range(1, 32)) mod100 = 0.0 for mc in range(1, 32): wc = v_h123[mc] if not wc: continue allowed = set() for i in range(5): if mc & (1 << i): allowed |= compat[classes[i]] for mh in range(1, 32): wh = d4[mh] if wh and any(routes[j] in allowed for j in range(5) if (mh & (1 << j))): mod100 += wc * wh a_4_marginals = [sum(dh[m] for m in range(32) if (m & 1)) for dh in dists] a_standalone = sum(v[s] for s in range(32) if (s & 1)) h2_ad_4cell = [ sum(d2[m] for m in range(32) if not (m & 1) and not (m & 16)), sum(d2[m] for m in range(32) if (m & 1) and not (m & 16)), sum(d2[m] for m in range(32) if not (m & 1) and (m & 16)), sum(d2[m] for m in range(32) if (m & 1) and (m & 16)), ] return { "ceiling_exact": c1_allo * c2_e * c3_e * c4_e, "locked_exact": locked, "modular_k100_exact": mod100, "design_a_4_marginals_exact": a_4_marginals, "design_a_standalone_exact": a_standalone, "design_a_d_h2_joint_4cell_exact": h2_ad_4cell, "p_h2_a1_d1_exact": h2_ad_4cell[3], "p_h4_a1_exact": a_4_marginals[3], } def check_item_full(item_name: str, item: dict[str, Any], scoring_panels: dict[str, dict[str, list[str]]]) -> dict[str, int]: fam = item["cell_family"] p = item["panels"] c = item["composite"] tol = 2.5e-4 counts = { "full_vec": 0, "thresh": 0, "full_genes": 0, "thresh_panels": 0, "thresh_s1_nz": 0, "thresh_s2_nz": 0, "thresh_prof_s1_nz": 0, "thresh_prof_s2_nz": 0, } sources = set() for pn, pv in p.items(): expected_n = len(scoring_panels[fam][pn]) require(pv["n_panel_genes"] == expected_n, f"{item_name}:{pn} n_panel_genes mismatch") genes_map = pv["nonzero_panel_genes"] for g in genes_map: require(g in scoring_panels[fam][pn], f"{item_name}:{pn} unknown gene {g}") src = pv["panel_genes_source"] sources.add(src) if src == "exact_full_npz_nonzero": require(pv["sub_0p05_tail_unverifiable_at_gene_level"] is False, f"{item_name}:{pn} flag mismatch") counts["full_genes"] += len(genes_map) recon_s1 = sum(d["s1_norm_rank"] for d in genes_map.values()) / expected_n recon_s2 = sum(d["s2_lfc"] for d in genes_map.values()) / expected_n recon_up = sum(1 for d in genes_map.values() if d["s1_norm_rank"] > 0.05) recon_dn = sum(1 for d in genes_map.values() if d["s1_norm_rank"] < -0.05) require(abs(recon_s1 - pv["mean_s1_norm_rank"]) <= tol, f"{item_name}:{pn} exact S1 gene->mean mismatch") require(abs(recon_s2 - pv["mean_s2_lfc"]) <= tol, f"{item_name}:{pn} exact S2 gene->mean mismatch") require(recon_up == pv["n_up"], f"{item_name}:{pn} exact n_up mismatch: {recon_up} != {pv['n_up']}") require(recon_dn == pv["n_down"], f"{item_name}:{pn} exact n_down mismatch: {recon_dn} != {pv['n_down']}") elif src == "raw_json_abs_gt_0p05_thresholded": require(pv["sub_0p05_tail_unverifiable_at_gene_level"] is True, f"{item_name}:{pn} flag mismatch") counts["thresh_panels"] += 1 tail_s1 = pv["unverifiable_sub_0p05_tail_residual_s1"] tail_s2 = pv["unverifiable_sub_0p05_tail_residual_s2"] if abs(tail_s1) > 1e-6: counts["thresh_s1_nz"] += 1 if abs(tail_s2) > 1e-6: counts["thresh_s2_nz"] += 1 recon_s1 = (sum(d["s1_norm_rank"] for d in genes_map.values()) + tail_s1) / expected_n recon_s2 = (sum(d["s2_lfc"] for d in genes_map.values()) + tail_s2) / expected_n # Raw-JSON genes are stored to 3 decimals whenever |s1|>0.05 OR |s2|>0.05; values exactly at +/-0.050 # may have been >0.050 before rounding (e.g. Baricitinib NK GZMB = 0.050), giving tight [strict, inclusive] bounds. up_lo = sum(1 for d in genes_map.values() if d["s1_norm_rank"] > 0.05) up_hi = sum(1 for d in genes_map.values() if d["s1_norm_rank"] >= 0.05) dn_lo = sum(1 for d in genes_map.values() if d["s1_norm_rank"] < -0.05) dn_hi = sum(1 for d in genes_map.values() if d["s1_norm_rank"] <= -0.05) require(abs(recon_s1 - pv["mean_s1_norm_rank"]) <= tol, f"{item_name}:{pn} thresholded S1 sum mismatch") require(abs(recon_s2 - pv["mean_s2_lfc"]) <= tol, f"{item_name}:{pn} thresholded S2 sum mismatch") require(up_lo <= pv["n_up"] <= up_hi, f"{item_name}:{pn} thresholded n_up out of [{up_lo},{up_hi}]: {pv['n_up']}") require(dn_lo <= pv["n_down"] <= dn_hi, f"{item_name}:{pn} thresholded n_down out of [{dn_lo},{dn_hi}]: {pv['n_down']}") else: raise RuntimeError(f"Unknown panel_genes_source {src!r} in {item_name}:{pn}") require(len(sources) == 1, f"Mixed panel sources within {item_name}: {sources}") if "exact_full_npz_nonzero" in sources: counts["full_vec"] = 1 else: counts["thresh"] = 1 if counts["thresh_s1_nz"] > 0: counts["thresh_prof_s1_nz"] = 1 if counts["thresh_s2_nz"] > 0: counts["thresh_prof_s2_nz"] = 1 if fam == "iec": bar = p["barrier_angio_good"]["mean_s1_norm_rank"] inf = p["inflam_coag_complement_bad"]["mean_s1_norm_rank"] mit = p["mitotic_prolif_marker"]["mean_s1_norm_rank"] tox = p["arrest_stress_apop_bad"]["mean_s1_norm_rank"] require(abs(c["vascular_rescue_score"] - round(bar - inf - 0.5 * tox, 4)) <= tol, f"{item_name} vasc") require(abs(c["pre_iec_safety_score"] - round(bar + 0.3 * mit - 1.0 * tox - 0.5 * inf, 4)) <= tol, f"{item_name} pre_iec") require(abs(c["c3_mitotic_suppression_index"] - round(-mit, 4)) <= tol, f"{item_name} c3_mit") elif fam == "beta": ident = p["identity_gsis_good"]["mean_s1_norm_rank"] apop = p["apoptosis_upr_bad"]["mean_s1_norm_rank"] imm = p["immune_chemokine_mhc_bad"]["mean_s1_norm_rank"] prol = p["prolif_ec_drift_bad"]["mean_s1_norm_rank"] surv = p["survival_protective_good"]["mean_s1_norm_rank"] exp_h3 = round(0.8 * ident + 0.4 * surv - 1.2 * apop - 1.0 * imm - 1.0 * max(0.0, prol), 4) exp_sf = round(1.2 * ident - 1.0 * max(0.0, apop) - 1.0 * max(0.0, prol) - 0.5 * max(0.0, imm), 4) require(abs(c["h3_bystander_rescue_score"] - exp_h3) <= tol, f"{item_name} h3") require(abs(c["ring1_safety_score"] - exp_sf) <= tol, f"{item_name} safety") elif fam == "macrophage": fbr = p["profibrotic_fbr_bad"]["mean_s1_norm_rank"] eff = p["efferocytosis_homeostatic_good"]["mean_s1_norm_rank"] tox = p["apop_tox_bad"]["mean_s1_norm_rank"] require(abs(c["anti_fbr_score"] - round(-1.2 * fbr + 0.8 * eff - 1.0 * max(0.0, tox), 4)) <= tol, f"{item_name} anti_fbr") elif fam == "tcell": treg = p["treg_tolerance_good"]["mean_s1_norm_rank"] eff = p["effector_autoimmune_bad"]["mean_s1_norm_rank"] require(abs(c["local_tolerance_score"] - round(1.2 * treg - 1.0 * eff, 4)) <= tol, f"{item_name} tcell")