OpenIslet

scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py

IIP-0043 / DEC-122 (CAND-034 / EXP-031 — Bucket 1 Closure, Stage S3-R2 Remediated)

script Release v0.8.0 gen-eb955f8266035aba 1008 KB · 76 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/verify_iip0043_wholegraft_gate0v_and_calibration.py · SHA-256: 20706180ed1ed6a2…

    r_miss_cx = evaluate_wholegraft_gate0v_and_stress_ap1903(_seal(p_miss_cx), openislet_root=resolved_root)

    p_miss_vk = copy.deepcopy(p_miss_spec)
    p_miss_vk["gate0v_kill_stress_ap1903"]["active_or_persistent_cvb4_infection"]["beta"]["post_ap1903_viable_viral_pos_k"] = 1
    r_miss_vk = evaluate_wholegraft_gate0v_and_stress_ap1903(_seal(p_miss_vk), openislet_root=resolved_root)

    p_miss_casp = copy.deepcopy(p_miss_spec)
    p_miss_casp["gate0v_kill_stress_ap1903"]["active_or_persistent_hcmv_infection"]["beta"]["caspase9_3_activity_fold_over_vehicle"] = "1.5"
    r_miss_casp = evaluate_wholegraft_gate0v_and_stress_ap1903(_seal(p_miss_casp), openislet_root=resolved_root)

    p_miss_k_gt_n = copy.deepcopy(p_miss_spec)
    p_miss_k_gt_n["gate0v_kill_stress_ap1903"]["normoxia_baseline"]["beta"]["ap1903_24h_viable_survivors_k"] = 10001
    r_miss_k_gt_n = evaluate_wholegraft_gate0v_and_stress_ap1903(_seal(p_miss_k_gt_n), openislet_root=resolved_root)

    def _any_nested_cert_true(obj: Any) -> bool:
        stack: List[Any] = [obj]
        while stack:
            cur = stack.pop()
            if isinstance(cur, dict):
                if cur.get("exact_directional_confidence_certified") is True:
                    return True
                for v in cur.values():
                    if isinstance(v, (dict, list, tuple)):
                        stack.append(v)
            elif isinstance(cur, (list, tuple)):
                for item in cur:
                    if isinstance(item, (dict, list, tuple)):
                        stack.append(item)
        return False

    ok_r12_b1a = bool(
        r_miss_spec["verdict"] == "HOLD_INCOMPLETE_OR_UNDERPOWERED"
        and r_miss_spec["clustering_design_effect_deff"] is None
        and r_miss_spec["exact_directional_confidence_certified"] is False
        and r_miss_spec["confidence_scope"] == "UNCERTIFIED_MISSING_OR_INVALID_SAMPLING_SPEC"
        and r_miss_spec["release_authorizing"] is False
        and not _any_nested_cert_true(r_miss_spec)
        and r_bad_deff["verdict"] == "HOLD_INCOMPLETE_OR_UNDERPOWERED"
        and r_bad_deff["clustering_design_effect_deff"] is None
        and r_bad_deff["exact_directional_confidence_certified"] is False
        and r_bad_deff["confidence_scope"] == "UNCERTIFIED_MISSING_OR_INVALID_SAMPLING_SPEC"
        and r_bad_deff["release_authorizing"] is False
        and r_lt1_deff["verdict"] == "HOLD_INCOMPLETE_OR_UNDERPOWERED"
        and r_lt1_deff["clustering_design_effect_deff"] is None
        and r_lt1_deff["exact_directional_confidence_certified"] is False
        and r_miss_cx["verdict"] == "FAIL_HARD_STOP"
        and r_miss_cx["exact_directional_confidence_certified"] is False
        and r_miss_cx["release_authorizing"] is False
        and r_miss_vk["verdict"] == "FAIL_HARD_STOP"
        and r_miss_vk["exact_directional_confidence_certified"] is False
        and r_miss_casp["verdict"] == "FAIL_HARD_STOP"
        and r_miss_casp["exact_directional_confidence_certified"] is False
        and r_miss_k_gt_n["verdict"] == "FAIL_HARD_STOP"
        and r_miss_k_gt_n["exact_directional_confidence_certified"] is False
    )
    cases.append({
        "case": "r12_b1_missing_or_invalid_sampling_spec_never_defaults_deff_1_bio_fail_preserved",
        "expected_verdict": "HOLD_INCOMPLETE_OR_UNDERPOWERED",
        "actual_verdict": r_miss_spec["verdict"],
        "expected_token": "HOLD_GATE0V_MISSING_CLUSTERING_AND_INDEPENDENCE_SPEC",
        "hard_stops": r_miss_vk["hard_stops"][:2],
        "holds": r_miss_spec["holds"][:3],
        "passed": ok_r12_b1a,
    })

    # 54. R12-B1 (b): Sol's 6 -> 3 probe, mutated per_unit_counts sum mismatch, omitted per_unit_counts,
    #     and single-cell unit-matched vs multi-well conditional scopes!
    p_ref_r12 = build_passing_reference_payload(openislet_root=resolved_root)
    r_ref_r12 = evaluate_wholegraft_gate0v_and_stress_ap1903(p_ref_r12, openislet_root=resolved_root)

    p_units_3 = build_passing_reference_payload(openislet_root=resolved_root)
    p_units_3["clustering_and_independence_spec"]["independent_biological_units_n"] = 3
    r_units_3 = evaluate_wholegraft_gate0v_and_stress_ap1903(_seal(p_units_3), openislet_root=resolved_root)

    p_units_3_rebuilt = copy.deepcopy(p_units_3)
    reconcile_fixture_per_unit_counts(p_units_3_rebuilt, units_n=3)
    r_units_3_rebuilt = evaluate_wholegraft_gate0v_and_stress_ap1903(_seal(p_units_3_rebuilt), openislet_root=resolved_root)

    p_mut_part = build_passing_reference_payload(openislet_root=resolved_root)
    p_mut_part["gate0v_kill_stress_ap1903"]["normoxia_baseline"]["beta"]["per_unit_counts"]["ap1903_24h"][0]["n"] += 1
    r_mut_part = evaluate_wholegraft_gate0v_and_stress_ap1903(_seal(p_mut_part), openislet_root=resolved_root)

    p_omit_part = build_passing_reference_payload(openislet_root=resolved_root)
    p_omit_part["gate0v_b_infectious_challenge"]["pathogens"]["cvb4_e2"]["day28_persistence_by_lineage"]["beta"].pop("per_unit_counts", None)
    r_omit_part = evaluate_wholegraft_gate0v_and_stress_ap1903(_seal(p_omit_part), openislet_root=resolved_root)

    p_sc_pass = build_passing_reference_payload(
        independent_biological_units_n=6_000_000,
        independent_sampling_unit_class="INDEPENDENT_SINGLE_CELL_OR_LIMITING_DILUTION_UNIT",
        openislet_root=resolved_root,
    )
    r_sc_pass = evaluate_wholegraft_gate0v_and_stress_ap1903(p_sc_pass, openislet_root=resolved_root)

    p_sc_under = build_passing_reference_payload(
        independent_biological_units_n=50,
        independent_sampling_unit_class="INDEPENDENT_SINGLE_CELL_OR_LIMITING_DILUTION_UNIT",
        openislet_root=resolved_root,
    )
    r_sc_under = evaluate_wholegraft_gate0v_and_stress_ap1903(p_sc_under, openislet_root=resolved_root)

    ok_r12_b1b = bool(
        r_ref_r12["verdict"] == "PASS_WHOLEGRAFT_BANK_QUALIFICATION"
        and r_ref_r12["exact_directional_confidence_certified"] is True
        and r_ref_r12["confidence_scope"] == "CONDITIONAL_ON_EXTERNALLY_VALIDATED_WITHIN_WELL_INDEPENDENCE_DEFF_EQ_1"
        and r_units_3["verdict"] == "HOLD_INCOMPLETE_OR_UNDERPOWERED"
        and r_units_3["exact_directional_confidence_certified"] is False
        and r_units_3["release_authorizing"] is False
        and "HOLD_INSUFFICIENT_INDEPENDENT_BIOLOGICAL_UNITS" in r_units_3["holds"]
        and not _any_nested_cert_true(r_units_3)
        and r_units_3_rebuilt["verdict"] == "HOLD_INCOMPLETE_OR_UNDERPOWERED"
        and r_units_3_rebuilt["exact_directional_confidence_certified"] is False
        and "HOLD_INSUFFICIENT_INDEPENDENT_BIOLOGICAL_UNITS" in r_units_3_rebuilt["holds"]
        and r_mut_part["verdict"] == "HOLD_INCOMPLETE_OR_UNDERPOWERED"
        and r_mut_part["exact_directional_confidence_certified"] is False
        and r_mut_part["release_authorizing"] is False
        and "HOLD_SAMPLING_UNIT_PARTITION_MISMATCH_OR_INSUFFICIENT_UNITS" in r_mut_part["holds"]
        and not _any_nested_cert_true(r_mut_part)
        and r_omit_part["verdict"] == "HOLD_INCOMPLETE_OR_UNDERPOWERED"
        and r_omit_part["exact_directional_confidence_certified"] is False
        and "HOLD_SAMPLING_UNIT_PARTITION_MISMATCH_OR_INSUFFICIENT_UNITS" in r_omit_part["holds"]
        and r_sc_pass["verdict"] == "PASS_WHOLEGRAFT_BANK_QUALIFICATION"
        and r_sc_pass["exact_directional_confidence_certified"] is True
        and r_sc_pass["confidence_scope"] == "EXACT_INDEPENDENT_BINOMIAL_BONFERRONI_DEFF_EQ_1_UNIT_MATCHED"
        and r_sc_under["verdict"] == "HOLD_INCOMPLETE_OR_UNDERPOWERED"
        and r_sc_under["exact_directional_confidence_certified"] is False
        and "HOLD_SAMPLING_UNIT_PARTITION_MISMATCH_OR_INSUFFICIENT_UNITS" in r_sc_under["holds"]
    )
    cases.append({
        "case": "r12_b1_units_6_to_3_and_partition_mismatch_held_and_single_cell_unit_matched",
        "expected_verdict": "HOLD_INCOMPLETE_OR_UNDERPOWERED",
        "actual_verdict": r_units_3["verdict"],
        "expected_token": "HOLD_INSUFFICIENT_INDEPENDENT_BIOLOGICAL_UNITS",
        "hard_stops": [],
        "holds": r_units_3["holds"][:3],
        "passed": ok_r12_b1b,
    })

    # 55. R12-B2 (a): JSON number `1.00000000000000001` vs JSON string `"1.00000000000000001"` for
    #     `clustering_design_effect_deff` via file-based `--evaluate-lot` intake MUST BOTH fail closed
    #     identically with exit 2, HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE,
    #     exact_directional_confidence_certified=False, release_authorizing=False!
    with tempfile.TemporaryDirectory() as td_r12_b2:
        td_12 = Path(td_r12_b2)
        p_deff_str = build_passing_reference_payload(openislet_root=resolved_root)
        p_deff_str["clustering_and_independence_spec"]["clustering_design_effect_deff"] = "1.00000000000000001"
        _seal(p_deff_str)
        f_deff_str = td_12 / "deff_str.json"
        f_deff_str.write_text(json.dumps(p_deff_str), encoding="utf-8")

        p_deff_num_base = build_passing_reference_payload(openislet_root=resolved_root)
        p_deff_num_base["clustering_and_independence_spec"]["clustering_design_effect_deff"] = Decimal("1.00000000000000001")
        _seal(p_deff_num_base)
        raw_num_json = _canonical_json_dumps(p_deff_num_base)
        f_deff_num = td_12 / "deff_num.json"
        f_deff_num.write_text(raw_num_json, encoding="utf-8")

        r_ds, rc_ds = evaluate_wholegraft_gate0v_lot_file(f_deff_str, openislet_root=resolved_root)
        r_dn, rc_dn = evaluate_wholegraft_gate0v_lot_file(f_deff_num, openislet_root=resolved_root)

        # 56. R12-B2 (b): Lot file with nonzero float underflow (-1e-400) and signed-zero deff (-0.0) fail closed exit 2!
        f_lot_uf = td_12 / "lot_underflow.json"
        f_lot_uf.write_text(
            '{"qualification_tier": "BANK_QUALIFICATION", "probe_underflow": -1e-400}',
            encoding="utf-8",
        )
        r_luf, rc_luf = evaluate_wholegraft_gate0v_lot_file(f_lot_uf, openislet_root=resolved_root)

        p_deff_nz = build_passing_reference_payload(openislet_root=resolved_root)
        p_deff_nz["clustering_and_independence_spec"]["clustering_design_effect_deff"] = Decimal("-0.0")
        _seal(p_deff_nz)
        f_deff_nz = td_12 / "deff_neg_zero.json"
        f_deff_nz.write_text(
            json.dumps(p_deff_nz, default=lambda x: "-0.0").replace('"-0.0"', "-0.0"),
            encoding="utf-8",
        )
        r_dnz, rc_dnz = evaluate_wholegraft_gate0v_lot_file(f_deff_nz, openislet_root=resolved_root)

    ok_r12_b2a = bool(
        '"clustering_design_effect_deff":1.00000000000000001' in raw_num_json
        and rc_ds == 2
        and rc_dn == 2
        and r_ds["verdict"] == "HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE"
        and r_dn["verdict"] == "HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE"
        and r_ds["exact_directional_confidence_certified"] is False
        and r_dn["exact_directional_confidence_certified"] is False
        and r_ds["release_authorizing"] is False
        and r_dn["release_authorizing"] is False
        and r_ds["clustering_design_effect_deff"] == "1.00000000000000001"
        and r_dn["clustering_design_effect_deff"] == "1.00000000000000001"
    )
    cases.append({
        "case": "r12_b2_deff_json_number_vs_string_1p00000000000000001_identical_clustered_hold",
        "expected_verdict": "HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE",
        "actual_verdict": r_dn["verdict"],
        "expected_token": "HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE",
        "hard_stops": [],
        "holds": r_dn["holds"][:3],
        "passed": ok_r12_b2a,
    })

    ok_r12_b2b = bool(
        rc_luf == 2
        and r_luf["verdict"] == "HOLD_INCOMPLETE_OR_UNDERPOWERED"
        and r_luf["release_authorizing"] is False
        and "HOLD_GATE0V_AMBIGUOUS_OR_NONCANONICAL_LOT_JSON:NONZERO_FLOAT_UNDERFLOW_REJECTED" in (r_luf.get("holds") or [])
        and rc_dnz == 2
        and r_dnz["verdict"] == "HOLD_INCOMPLETE_OR_UNDERPOWERED"
        and r_dnz["exact_directional_confidence_certified"] is False
        and r_dnz["clustering_design_effect_deff"] is None
        and "INVALID_NEGATIVE_OR_SIGNED_ZERO_BOUNDARY_VALUE" in (r_dnz.get("holds") or [])
    )
    cases.append({
        "case": "r12_b2_lot_file_float_underflow_neg_1e_minus_400_and_signed_zero_rejected",
        "expected_verdict": "HOLD_INCOMPLETE_OR_UNDERPOWERED",
        "actual_verdict": r_luf["verdict"],
        "expected_token": "NONZERO_FLOAT_UNDERFLOW_REJECTED",
        "hard_stops": [],
        "holds": (r_luf.get("holds") or [])[:3],
        "passed": ok_r12_b2b,
    })

    # ------------------------------------------------------------------
    # Round-13 Final-Closure Adversarial Regressions (R13-B1, B2, B3, B4, M1, M2)
    # ------------------------------------------------------------------
    # 57. R13-B1 (a): Single-cell mode MUST NEVER bypass submitted `per_unit_counts`!
    #     Adverse k=1 survivor in a supplemental stress arm's `per_unit_counts` (both viral-pos
    #     AND AP1903 72h while aggregate=0) triggers deterministic `FAIL_HARD_STOP`, while clean
    #     unexpected `per_unit_counts` in single-cell mode fails closed with
    #     `HOLD_UNEXPECTED_PER_UNIT_COUNTS_IN_SINGLE_CELL_MODE`!
    with tempfile.TemporaryDirectory() as td_r13:
        td_13 = Path(td_r13)
        b_r13 = build_passing_reference_payload(
            lot_id="BANK-R13-001",
            mcb_id="MCB-R13-001",
            product_variant_id="ISLET1_A4_ARM1B",
            qualification_tier="BANK_QUALIFICATION",
            openislet_root=resolved_root,
        )
        brec_r13 = build_authenticated_bank_qualification_record(
            mcb_id="MCB-R13-001",
            product_variant_id="ISLET1_A4_ARM1B",
            bank_lot_id="BANK-R13-001",
            bank_payload_sha256=b_r13["payload_sha256"],
        )
        st_r13 = td_13 / "store_r13.json"
        write_record_to_trusted_qualification_store(
            st_r13, brec_r13, category="bank_qualifications", archived_payload=b_r13
        )
        p_sc_clin = build_passing_reference_payload(
            lot_id="LOT-R13-SC-001",