OpenIslet

scripts/verify_iip0042_master_multilayer_portfolio_crosswalk.py

IIP-0042 / DEC-121 (CAND-033 / EXP-030): Master Exact 2^20 Multi-Layer Portfolio

script Release v0.8.0 gen-eb955f8266035aba 881 KB · 63 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_iip0042_master_multilayer_portfolio_crosswalk.py · SHA-256: 16964a158458b5cf…

        elif roster_ok and type(raw_rid) is str and type(raw_ts) is str:
            expected_r_sha = _shield_compute_donor_roster_sha256(
                raw_rid,
                raw_ts,
                parsed_r_ids,
                predeclared_per_donor_unit_count_n=roster_scalar_n if has_r_scalar_n else None,
                predeclared_per_donor_unit_counts=parsed_r_n_map if has_r_map_n else None,
                planned_analysis_looks=1,
                single_look_fixed_horizon_plan_locked=True,
            )
            if raw_r_sha != expected_r_sha:
                roster_ok = False
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"preregistered_donor_family_roster.roster_sha256 mismatch: supplied={raw_r_sha} != "
                    f"recomputed={expected_r_sha} (tampered or unpinned donor roster / sample-size / single-look plan rejected fail-closed)",
                )

        if roster_ok and type(raw_rid) is str and type(raw_ts) is str and type(raw_r_sha) is str and r_plan_v is not None:
            roster_verified = True
            roster_id_val = raw_rid
            roster_ts_val = raw_ts
            roster_sha256_val = raw_r_sha
            roster_donor_ids = parsed_r_ids
            roster_set = frozenset(parsed_r_ids)
            roster_planned_n = r_plan_v
            roster_predeclared_n_by_donor = parsed_roster_n_by_donor
            roster_planned_looks_val = 1
            roster_single_look_locked = True

    has_top_planned = "planned_t1d_donor_family_n" in observation and observation["planned_t1d_donor_family_n"] is not None
    top_level_plan_conflict = False
    if has_top_planned:
        top_plan_val, top_plan_err = _shield_check_strict_int(
            observation["planned_t1d_donor_family_n"],
            "planned_t1d_donor_family_n",
            min_val=_SHIELD_MIN_DONORS_N,
        )
        if top_plan_err is not None or top_plan_val is None:
            top_level_plan_conflict = True
            _escalate("HOLD_INVALID_OR_MISSING_INPUT", top_plan_err or "Invalid top-level planned_t1d_donor_family_n")
        elif roster_verified and roster_planned_n is not None and top_plan_val != roster_planned_n:
            top_level_plan_conflict = True
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                f"Top-level planned_t1d_donor_family_n={top_plan_val} disagrees with "
                f"preregistered_donor_family_roster.planned_t1d_donor_family_n={roster_planned_n}",
            )

    has_primary_donors = "tcell_blockade_and_apc_cytotoxicity_donors" in observation
    has_alias_donors = "primary_t1d_donor_cohorts" in observation
    conflicting_donor_lists = (
        has_primary_donors
        and has_alias_donors
        and observation["tcell_blockade_and_apc_cytotoxicity_donors"] != observation["primary_t1d_donor_cohorts"]
    )
    if conflicting_donor_lists:
        _escalate(
            "HOLD_INVALID_OR_MISSING_INPUT",
            "Contradictory tcell_blockade_and_apc_cytotoxicity_donors and primary_t1d_donor_cohorts supplied; "
            "both excluded from biological adjudication",
        )
        donors_raw = None
    else:
        donors_raw = (
            observation.get("tcell_blockade_and_apc_cytotoxicity_donors")
            if has_primary_donors
            else observation.get("primary_t1d_donor_cohorts")
        )
    submitted_donor_records_n = len(donors_raw) if isinstance(donors_raw, list) else 0

    # Parse mandatory typed between-donor sampling & independence design (R10 B1/B2 + R11 B1 + R12 0037-B3 + R14 0039-B1 class-specific allowlist)
    _allowed_sampling_design_keys = frozenset({
        "sampling_design_class",
        "independent_donor_sampling_attested",
        "between_donor_shared_shock_controls_verified",
        "sampling_protocol_ref",
        "between_donor_independence_justification",
    })
    _allowed_between_donor_justifications_by_class: dict[str, frozenset[str]] = {
        "SIMPLE_RANDOM_OR_CONSECUTIVE_INDEPENDENT_T1D_COHORT": frozenset({
            "Simple random or consecutive independent T1D HLA-DQ donors executed across disjoint "
            "expression batches and separate assay runs with independent reagent preparations",
            "DISJOINT_BATCH_AND_RUN_INDEPENDENT_T1D_DONOR_COHORT",
        }),
        "STRATIFIED_INDEPENDENT_T1D_HLA_DQ_COHORT": frozenset({
            "STRATIFIED_DISJOINT_BATCH_AND_RUN_T1D_DONOR_COHORT",
        }),
    }
    sampling_design_obj = observation.get("donor_sampling_and_independence_design")
    donor_sampling_design_valid = False
    suppression_inference_authorized_by_sampling_design = False
    sampling_design_class_val: str | None = None
    sampling_protocol_ref_val: str | None = None
    if not isinstance(sampling_design_obj, dict):
        _escalate(
            "HOLD_INVALID_OR_MISSING_INPUT",
            "Missing mandatory typed donor_sampling_and_independence_design block "
            "(requires sampling_design_class, independent_donor_sampling_attested=True, "
            "between_donor_shared_shock_controls_verified=True, sampling_protocol_ref, "
            "between_donor_independence_justification)",
        )
    else:
        sd_ok = True
        extra_sd_keys = sorted(set(sampling_design_obj.keys()) - _allowed_sampling_design_keys)
        if extra_sd_keys:
            sd_ok = False
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                f"donor_sampling_and_independence_design contains unexpected/free-text keys {extra_sd_keys}; "
                f"strict closed typed schema required",
            )
        sd_cls = sampling_design_obj.get("sampling_design_class")
        sd_cls_valid = type(sd_cls) is str and sd_cls in _SHIELD_ALLOWED_DONOR_SAMPLING_DESIGNS
        if not sd_cls_valid:
            sd_ok = False
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                f"donor_sampling_and_independence_design.sampling_design_class must be in "
                f"{sorted(_SHIELD_ALLOWED_DONOR_SAMPLING_DESIGNS)} (got {sd_cls!r}; only "
                f"{sorted(_SHIELD_AUTHORIZING_DONOR_SAMPLING_DESIGNS)} authorizes unweighted "
                f"Clopper-Pearson population-suppression inference)",
            )
        if sampling_design_obj.get("independent_donor_sampling_attested") is not True:
            sd_ok = False
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                "donor_sampling_and_independence_design.independent_donor_sampling_attested must be literal boolean True",
            )
        if sampling_design_obj.get("between_donor_shared_shock_controls_verified") is not True:
            sd_ok = False
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                "donor_sampling_and_independence_design.between_donor_shared_shock_controls_verified must be literal boolean True",
            )
        sd_ref = sampling_design_obj.get("sampling_protocol_ref")
        if type(sd_ref) is not str or not sd_ref or sd_ref != sd_ref.strip() or not _SHIELD_EVIDENCE_REF_RE.match(sd_ref):
            sd_ok = False
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                f"donor_sampling_and_independence_design.sampling_protocol_ref must be an unpadded ref matching "
                f"{_SHIELD_EVIDENCE_REF_RE.pattern} (got {sd_ref!r})",
            )
        sd_just = sampling_design_obj.get("between_donor_independence_justification")
        allowed_just_for_cls = (
            _allowed_between_donor_justifications_by_class[sd_cls]
            if sd_cls_valid and type(sd_cls) is str
            else frozenset()
        )
        if type(sd_just) is not str or sd_just not in allowed_just_for_cls:
            sd_ok = False
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                f"donor_sampling_and_independence_design.between_donor_independence_justification "
                f"must be a strict class-specific canonical allowlisted declaration compatible with "
                f"sampling_design_class={sd_cls!r} (free-text prose and cross-class contradictions "
                f"rejected fail-closed; got {sd_just!r})",
            )
        if sd_ok and type(sd_cls) is str and type(sd_ref) is str:
            donor_sampling_design_valid = True
            sampling_design_class_val = sd_cls
            sampling_protocol_ref_val = sd_ref
            suppression_inference_authorized_by_sampling_design = bool(
                sd_cls in _SHIELD_AUTHORIZING_DONOR_SAMPLING_DESIGNS
            )

    # Parse optional documented pre-assay excluded_donor_records for 100% roster reconciliation
    excluded_records_valid = True
    valid_excluded_donor_ids: set[str] = set()
    raw_excluded_donor_ids: set[str] = set()
    if "excluded_donor_records" in observation and observation["excluded_donor_records"] is not None:
        excl_raw = observation["excluded_donor_records"]
        if not isinstance(excl_raw, list):
            excluded_records_valid = False
            _escalate("HOLD_INVALID_OR_MISSING_INPUT", "excluded_donor_records must be a list of pre-assay exclusion objects")
        else:
            for e_idx, e_rec in enumerate(excl_raw):
                if not isinstance(e_rec, dict):
                    excluded_records_valid = False
                    _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"excluded_donor_records[{e_idx}] must be an object")
                    continue
                e_did = e_rec.get("donor_id")
                if type(e_did) is str and e_did.strip():
                    raw_excluded_donor_ids.add(e_did.strip())
                if (
                    type(e_did) is not str
                    or not e_did
                    or e_did != e_did.strip()
                    or not _SHIELD_STRICT_ID_RE.match(e_did)
                ):
                    excluded_records_valid = False
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"excluded_donor_records[{e_idx}].donor_id must be an unpadded canonical ID (got {e_did!r})",
                    )
                    continue
                if e_did in valid_excluded_donor_ids:
                    excluded_records_valid = False
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"excluded_donor_records contains duplicate donor_id {e_did!r}",
                    )
                    continue
                if roster_verified and e_did not in roster_set:
                    excluded_records_valid = False
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"excluded_donor_records[{e_idx}].donor_id={e_did!r} is not in preregistered_donor_family_roster",
                    )
                    continue
                e_reason = e_rec.get("pre_assay_exclusion_reason")
                if type(e_reason) is not str or len(e_reason) < 8 or e_reason != e_reason.strip():
                    excluded_records_valid = False
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"excluded_donor_records[{e_idx}].pre_assay_exclusion_reason must be an unpadded string "
                        f"of length >= 8 (got {e_reason!r})",
                    )
                    continue
                if e_rec.get("outcome_blind_exclusion_attested") is not True:
                    excluded_records_valid = False
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"excluded_donor_records[{e_idx}].outcome_blind_exclusion_attested must be literal True",
                    )
                    continue
                valid_excluded_donor_ids.add(e_did)

    valid_donor_sup_vals: list[float] = []
    valid_donor_sup_by_id: dict[str, float] = {}
    topology_donor_batches: dict[str, set[str]] = {}
    topology_donor_runs: dict[str, set[str]] = {}
    donors_with_invalid_topology: set[str] = set()
    valid_donor_cyto_rates: list[float] = []
    valid_donor_cp_l95s_unadj: list[float] = []
    valid_donor_cp_u95s: list[float] = []
    valid_donor_k_list: list[int] = []
    valid_donor_neff_list: list[int] = []
    seen_donors: set[str] = set()
    raw_submitted_donor_ids: set[str] = set()
    conflicted_donors: set[str] = set()
    unrostered_submitted_donors: set[str] = set()
    excluded_overlap_donors: set[str] = set()
    invalid_donors_excluded: list[str] = []
    malformed_donor_rows_present = False
    _allowed_gy018_keys = frozenset({
        "independent_unit_class",
        "within_donor_units_physically_independent",
        "clustering_controls_present_and_verified",
        "shared_well_technical_reads_excluded",