OpenIslet

scripts/verify_iip0044_foundry_v1_3_biologics.py

IIP-0044 / DEC-123 (CAND-035 / EXP-032): OpenIslet Foundry v1.3 Clean-Room

script Release v0.8.0 gen-eb955f8266035aba 619 KB · 44 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_iip0044_foundry_v1_3_biologics.py · SHA-256: 1d6c53a0817f6254…

            # evidence record bound 1:1 to canonical allowlisted unit/control tokens (ZERO free-text prose accepted!)
            pd_n: int | None = None
            gy018_just = d.get("gy018_independence_justification")
            if not isinstance(gy018_just, dict):
                donor_valid = False
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"donor[{did_c}]: missing mandatory authorization-grade gy018_independence_justification object "
                    f"(requires independent_unit_class, within_donor_units_physically_independent=True, "
                    f"clustering_controls_present_and_verified=True, shared_well_technical_reads_excluded=True, "
                    f"independence_evidence_record_id, independent_experimental_unit, predeclared_per_donor_unit_count_n, clustering_controls)",
                )
            else:
                extra_gy_keys = sorted(set(gy018_just.keys()) - _allowed_gy018_keys)
                if extra_gy_keys:
                    donor_valid = False
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"donor[{did_c}].gy018_independence_justification contains unexpected/free-text keys "
                        f"{extra_gy_keys}; strict closed typed schema required",
                    )
                unit_cls = gy018_just.get("independent_unit_class")
                unit_cls_valid = type(unit_cls) is str and unit_cls in _SHIELD_ALLOWED_GY018_UNIT_CLASSES
                if not unit_cls_valid:
                    donor_valid = False
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"donor[{did_c}].gy018_independence_justification.independent_unit_class must be in "
                        f"{sorted(_SHIELD_ALLOWED_GY018_UNIT_CLASSES)} (got {unit_cls!r})",
                    )
                for bkey in (
                    "within_donor_units_physically_independent",
                    "clustering_controls_present_and_verified",
                    "shared_well_technical_reads_excluded",
                ):
                    if gy018_just.get(bkey) is not True:
                        donor_valid = False
                        _escalate(
                            "HOLD_INVALID_OR_MISSING_INPUT",
                            f"donor[{did_c}].gy018_independence_justification.{bkey} must be literal boolean True "
                            f"(got {gy018_just.get(bkey)!r})",
                        )
                ev_rec_id = gy018_just.get("independence_evidence_record_id")
                if (
                    type(ev_rec_id) is not str
                    or not ev_rec_id
                    or ev_rec_id != ev_rec_id.strip()
                    or not _SHIELD_EVIDENCE_REF_RE.match(ev_rec_id)
                ):
                    donor_valid = False
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"donor[{did_c}].gy018_independence_justification.independence_evidence_record_id must be "
                        f"an unpadded audit ref matching {_SHIELD_EVIDENCE_REF_RE.pattern} (got {ev_rec_id!r})",
                    )
                iu_txt = gy018_just.get("independent_experimental_unit")
                allowed_units_for_cls = (
                    _SHIELD_ALLOWED_INDEPENDENT_EXPERIMENTAL_UNITS[unit_cls]
                    if unit_cls_valid and type(unit_cls) is str
                    else frozenset()
                )
                if type(iu_txt) is not str or iu_txt not in allowed_units_for_cls:
                    donor_valid = False
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"donor[{did_c}].gy018_independence_justification.independent_experimental_unit must be a "
                        f"canonical allowlisted token compatible with independent_unit_class={unit_cls!r} "
                        f"(free-text prose rejected fail-closed; got {iu_txt!r})",
                    )
                cc_txt = gy018_just.get("clustering_controls")
                allowed_ctrls_for_cls = (
                    _SHIELD_ALLOWED_CLUSTERING_CONTROLS[unit_cls]
                    if unit_cls_valid and type(unit_cls) is str
                    else frozenset()
                )
                if type(cc_txt) is not str or cc_txt not in allowed_ctrls_for_cls:
                    donor_valid = False
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"donor[{did_c}].gy018_independence_justification.clustering_controls must be a "
                        f"canonical allowlisted token compatible with independent_unit_class={unit_cls!r} "
                        f"(free-text prose rejected fail-closed; got {cc_txt!r})",
                    )
                pd_n, pd_n_err = _shield_check_strict_int(
                    gy018_just.get("predeclared_per_donor_unit_count_n"),
                    f"donor[{did_c}].gy018_independence_justification.predeclared_per_donor_unit_count_n",
                    min_val=_SHIELD_MIN_CYTO_N_EFF_PER_DONOR,
                )
                if pd_n_err is not None:
                    donor_valid = False
                    _escalate("HOLD_INVALID_OR_MISSING_INPUT", pd_n_err)
                elif raw_units_n is not None and pd_n != raw_units_n:
                    donor_valid = False
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"donor[{did_c}]: gy018_independence_justification.predeclared_per_donor_unit_count_n={pd_n} "
                        f"disagrees with raw_independent_units_n={raw_units_n}",
                    )

            n_eff_raw, neff_pos_err = _shield_check_strict_int(d.get("apc_cytotoxicity_n_eff"), f"donor[{did_c}].apc_cytotoxicity_n_eff", min_val=1)
            k_obs_raw, kobs_err = _shield_check_strict_int(d.get("apc_cytotoxicity_k_obs"), f"donor[{did_c}].apc_cytotoxicity_k_obs", min_val=0)
            if neff_pos_err is not None:
                donor_valid = False
                _escalate("HOLD_INVALID_OR_MISSING_INPUT", neff_pos_err)
            if kobs_err is not None:
                donor_valid = False
                _escalate("HOLD_INVALID_OR_MISSING_INPUT", kobs_err)

            if n_eff_raw is not None and raw_units_n is not None and n_eff_raw != raw_units_n:
                donor_valid = False
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"donor[{did_c}]: GY-018 denominator mismatch — apc_cytotoxicity_n_eff={n_eff_raw} != raw_independent_units_n={raw_units_n}",
                )

            if n_eff_raw is not None and n_eff_raw < _SHIELD_MIN_CYTO_N_EFF_PER_DONOR:
                donor_valid = False
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"donor[{did_c}]: apc_cytotoxicity_n_eff={n_eff_raw} < mandatory minimum {_SHIELD_MIN_CYTO_N_EFF_PER_DONOR}",
                )

            if n_eff_raw is not None and k_obs_raw is not None and k_obs_raw > n_eff_raw:
                donor_valid = False
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"donor[{did_c}]: apc_cytotoxicity_k_obs={k_obs_raw} > apc_cytotoxicity_n_eff={n_eff_raw}",
                )

            # R16 Shared Blocker #3 + Pre-Dedup Raw-Row Reconciliation (Pass 1c): Exclude any donor flagged
            # for fixed-horizon sample-size mismatch across raw submitted rows from biological adjudication!
            if roster_verified and did_c in roster_sample_size_plan_mismatched_donors:
                donor_valid = False

            # Gate ALL binomial Clopper-Pearson & caller CI calculations strictly on valid eligible rostered T1D donors!
            if donor_valid and n_eff_raw is not None and k_obs_raw is not None and sup_v is not None:
                rate_pct = 100.0 * k_obs_raw / float(n_eff_raw)
                cp_l95, cp_u95 = _clopper_pearson_one_sided_95_pct(k_obs_raw, n_eff_raw, alpha=0.05)

                # Two-sided validation of optional caller-supplied per-donor cytotoxicity intervals (<= 0.05 pp tolerance)
                rep_obs_raw = d.get("apc_cytotoxicity_observed_pct")
                rep_l95_raw = d.get("apc_cytotoxicity_l95_pct")
                rep_u95_raw = d.get("apc_cytotoxicity_u95_pct")
                rep_l95: float | None = None
                rep_u95: float | None = None
                if rep_obs_raw is not None:
                    rep_obs, ro_err = _shield_check_num(rep_obs_raw, f"donor[{did_c}].apc_cytotoxicity_observed_pct", lo=0.0, hi=100.0)
                    if ro_err is not None:
                        donor_valid = False
                        _escalate("HOLD_INVALID_OR_MISSING_INPUT", ro_err)
                    elif rep_obs is not None and abs(rep_obs - rate_pct) > _SHIELD_CYTO_CI_TOL_PP:
                        donor_valid = False
                        _escalate(
                            "HOLD_INVALID_OR_MISSING_INPUT",
                            f"donor[{did_c}]: reported observed_pct={rep_obs:.3f}% disagrees with raw counts {k_obs_raw}/{n_eff_raw} ({rate_pct:.3f}%)",
                        )
                if rep_l95_raw is not None:
                    rep_l95, rl_err = _shield_check_num(rep_l95_raw, f"donor[{did_c}].apc_cytotoxicity_l95_pct", lo=0.0, hi=100.0)
                    if rl_err is not None:
                        donor_valid = False
                        _escalate("HOLD_INVALID_OR_MISSING_INPUT", rl_err)
                if rep_u95_raw is not None:
                    rep_u95, ru_err = _shield_check_num(rep_u95_raw, f"donor[{did_c}].apc_cytotoxicity_u95_pct", lo=0.0, hi=100.0)
                    if ru_err is not None:
                        donor_valid = False
                        _escalate("HOLD_INVALID_OR_MISSING_INPUT", ru_err)
                if rep_l95 is not None and rep_u95 is not None and rep_l95 > rep_u95:
                    donor_valid = False
                    _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"donor[{did_c}]: inverted cytotoxicity CI L95={rep_l95} > U95={rep_u95}")
                if rep_l95 is not None:
                    if k_obs_raw == 0 and rep_l95 > 0.0:
                        donor_valid = False
                        _escalate(
                            "HOLD_INVALID_OR_MISSING_INPUT",
                            f"donor[{did_c}]: inconsistent cytotoxicity CI (k_obs=0 / 0% observed with positive L95={rep_l95:.3f}%)",
                        )
                    elif rep_l95 > rate_pct + 1e-6 or abs(rep_l95 - cp_l95) > _SHIELD_CYTO_CI_TOL_PP:
                        donor_valid = False
                        _escalate(
                            "HOLD_INVALID_OR_MISSING_INPUT",
                            f"donor[{did_c}]: reported L95={rep_l95:.3f}% disagrees with exact Clopper-Pearson L95={cp_l95:.3f}% (two-sided tol {_SHIELD_CYTO_CI_TOL_PP:.2f} pp)",
                        )
                if rep_u95 is not None and (rep_u95 + 1e-6 < rate_pct or abs(rep_u95 - cp_u95) > _SHIELD_CYTO_CI_TOL_PP):
                    donor_valid = False
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"donor[{did_c}]: reported U95={rep_u95:.3f}% disagrees with exact Clopper-Pearson U95={cp_u95:.3f}% (two-sided tol {_SHIELD_CYTO_CI_TOL_PP:.2f} pp)",
                    )

                if donor_valid and assay_run_c is not None:
                    referenced_batches.update(tmp_ref_d)
                    cocktail_comp1_batches.update(tmp_ck1_d)
                    cocktail_comp2_batches.update(tmp_ck2_d)
                    valid_donor_sup_vals.append(sup_v)
                    valid_donor_sup_by_id[did_c] = sup_v
                    valid_donor_cyto_rates.append(rate_pct)
                    valid_donor_cp_l95s_unadj.append(cp_l95)
                    valid_donor_cp_u95s.append(cp_u95)
                    valid_donor_k_list.append(k_obs_raw)
                    valid_donor_neff_list.append(n_eff_raw)
                else:
                    invalid_donors_excluded.append(did_c)
            else:
                invalid_donors_excluded.append(did_c)

    # 100% pre-registered donor roster accounting on ALL raw submitted donor IDs
    missing_roster_donor_ids: list[str] = []
    if roster_verified:
        accounted_roster_ids = (raw_submitted_donor_ids & roster_set) | (valid_excluded_donor_ids & roster_set)
        missing_roster_donor_ids = sorted(roster_set - accounted_roster_ids)
        if missing_roster_donor_ids:
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                f"Incomplete pre-registered donor roster reconciliation: {len(missing_roster_donor_ids)} of "
                f"{len(roster_set)} rostered donors omitted without submitted or pre-assay-excluded records "
                f"({missing_roster_donor_ids}); authorization-grade SHIELD verdict blocked",
            )

    expected_submitted_roster_ids = (roster_set - valid_excluded_donor_ids) if roster_verified else frozenset()
    donor_topology_complete_for_suppression = bool(
        roster_verified
        and len(donors_with_invalid_topology & roster_set) == 0
        and all(
            did_k in topology_donor_batches
            and len(topology_donor_batches[did_k]) >= 1
            and did_k in topology_donor_runs
            and len(topology_donor_runs[did_k]) >= 1
            for did_k in expected_submitted_roster_ids
        )
    )
    if roster_verified and not donor_topology_complete_for_suppression:
        _escalate(
            "HOLD_INVALID_OR_MISSING_INPUT",
            f"Incomplete or corrupted donor batch/run dependency topology across submitted rostered donors "
            f"(invalid_topology_donors={sorted(donors_with_invalid_topology & roster_set)}); "
            f"cluster-level suppression inference withheld fail-closed while preserving independent SPR and "
            f"fixed-roster Bonferroni cytotoxicity FAIL",
        )

    bonferroni_family_denominator_n: int | None = len(roster_donor_ids) if roster_verified else None
    authorization_grade_fwer_bound_valid = bool(
        roster_verified
        and roster_single_look_locked
        and roster_planned_looks_val == 1
        and len(roster_sample_size_plan_mismatched_donors) == 0
        and not top_level_plan_conflict
        and not conflicting_donor_lists
        and not malformed_donor_rows_present
        and excluded_records_valid
        and len(excluded_overlap_donors) == 0