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…

        ):
            if mon_oos and mon_v is not None:
                quant_sec_failed = True
                if mon_qc is False:
                    sec_valid_out_of_spec = True
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"PHYSICOCHEMICAL_OUT_OF_SPEC_MATERIAL_UNQUALIFIED_HOLD: valid measured {mon_lbl}={mon_v:.2f}% "
                        f"< {_SHIELD_MIN_MONOMER_PCT:.1f}% with consistent qc_flag=False (unqualified material input, "
                        f"not biological target FAIL)",
                    )
                else:
                    sec_input_missing_or_malformed = True
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: SEC-MALS quantitative threshold/QC "
                        f"contradiction: {mon_lbl}={mon_v:.2f}% < {_SHIELD_MIN_MONOMER_PCT:.1f}% contradicts qc_flag={mon_qc!r}",
                    )
        if pdi_oos and dls_pdi is not None:
            quant_sec_failed = True
            if ck_qc is False:
                sec_valid_out_of_spec = True
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"PHYSICOCHEMICAL_OUT_OF_SPEC_MATERIAL_UNQUALIFIED_HOLD: valid measured dls_pdi={dls_pdi:.4f} "
                    f"> {_SHIELD_MAX_DLS_PDI:.2f} with consistent cocktail qc_flag=False (unqualified material input, "
                    f"not biological target FAIL)",
                )
            else:
                sec_input_missing_or_malformed = True
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: SEC-MALS/DLS polydispersity threshold/QC "
                    f"contradiction: dls_pdi={dls_pdi:.4f} > {_SHIELD_MAX_DLS_PDI:.2f} contradicts qc_flag={ck_qc!r}",
                )
        if agg_oos:
            quant_sec_failed = True
            if c1_qc is True or c2_qc is True or ck_qc is True:
                sec_input_missing_or_malformed = True
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    "PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: SEC-MALS/DLS boolean QC flag contradiction: "
                    "qc_passed=True despite no_aggregates_or_precipitation=False",
                )
            else:
                sec_valid_out_of_spec = True
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    "PHYSICOCHEMICAL_OUT_OF_SPEC_MATERIAL_UNQUALIFIED_HOLD: valid measured aggregates or precipitation "
                    "detected (no_aggregates_or_precipitation=False; unqualified material input, not biological target FAIL)",
                )

        if c1_qc is False and not (c1_oos or agg_oos):
            sec_input_missing_or_malformed = True
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                "PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: SEC-MALS/DLS monomer QC flag for "
                "IsletSHIELD_QF09_pI2 alone is False without an out-of-spec measurement",
            )
        if c2_qc is False and not (c2_oos or agg_oos):
            sec_input_missing_or_malformed = True
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                "PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: SEC-MALS/DLS monomer QC flag for "
                "IsletSHIELD_Comp2_T006 alone is False without an out-of-spec measurement",
            )
        if ck_qc is False and not (ck_oos or pdi_oos or agg_oos):
            sec_input_missing_or_malformed = True
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                "PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: SEC-MALS/DLS 1:1 co-formulation QC flag "
                "(T006 + QF09_pI2) is False without an out-of-spec measurement",
            )

        for gen_k in ("qc_pass", "qc_passed"):
            if gen_k in sec:
                if sec[gen_k] is True and quant_sec_failed:
                    sec_input_missing_or_malformed = True
                    sec_top_qc_bad = True
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: sec_mals_dls.{gen_k}=True contradicts quantitative SEC-MALS/DLS threshold failure",
                    )
                elif sec[gen_k] is False and not quant_sec_failed:
                    sec_input_missing_or_malformed = True
                    sec_top_qc_bad = True
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: sec_mals_dls.{gen_k}=False without quantitative out-of-spec measurement",
                    )
                elif type(sec[gen_k]) is not bool:
                    sec_input_missing_or_malformed = True
                    sec_top_qc_bad = True
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: sec_mals_dls.{gen_k} must be a strict boolean",
                    )
                elif sec[gen_k] is False:
                    sec_top_qc_bad = True

        # R16 Shared Blocker #1 + 0039-M2: Mandatory per-mixture physical co-formulation SEC-MALS/DLS binding!
        # Every measured physical 1:1 co-formulation mixture MUST be explicitly registered with a unique
        # mixture_id, exact (comp1_expression_batch_id, comp2_expression_batch_id) lot pair, and its own
        # per-mixture monomer_pct (>= 95.0%), dls_pdi (<= 0.15), no_aggregates_or_precipitation=True, and qc_passed,
        # plus optional strictly validated laboratory 1:1 molar-ratio attestation fields (equimolar_1to1_molar_ratio_attested,
        # ratio_basis_attestation) documenting that 1:1 stoichiometry is laboratory-attested alongside SEC-MALS/DLS QC.
        _required_mix_keys = frozenset({
            "mixture_id",
            "comp1_expression_batch_id",
            "comp2_expression_batch_id",
            "monomer_pct",
            "dls_pdi",
            "no_aggregates_or_precipitation",
            "qc_passed",
        })
        _optional_mix_keys = frozenset({
            "equimolar_1to1_molar_ratio_attested",
            "ratio_basis_attestation",
        })
        _allowed_mix_keys = _required_mix_keys | _optional_mix_keys
        _allowed_mix_ratio_basis_tokens = frozenset({
            "LAB_ATTESTED_1TO1_MOLAR_COFORMULATION_MIXING_PROTOCOL",
            "UV_A280_EXTINCTION_VERIFIED_1TO1_MOLAR_COFORMULATION",
        })
        raw_mixtures = sec.get("qualified_coformulation_mixtures")
        candidate_passing_mixtures: dict[str, tuple[str, str]] = {}
        candidate_mixture_ratio_meta: dict[str, dict[str, Any]] = {}
        valid_mix_monomers: list[float] = []
        valid_mix_pdis: list[float] = []
        top_sec_overstated = False
        if not isinstance(raw_mixtures, list) or len(raw_mixtures) == 0:
            sec_input_missing_or_malformed = True
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                "PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: sec_mals_dls_monomer_coformulation missing "
                "mandatory non-empty qualified_coformulation_mixtures list binding each physical 1:1 mixture_id "
                "to its exact (comp1_expression_batch_id, comp2_expression_batch_id) pair and per-mixture monomer/PDI QC",
            )
        else:
            mix_id_counts: dict[str, int] = {}
            for m_rec in raw_mixtures:
                if isinstance(m_rec, dict):
                    raw_mid = m_rec.get("mixture_id")
                    if type(raw_mid) is str and raw_mid.strip():
                        mix_id_counts[raw_mid.strip()] = mix_id_counts.get(raw_mid.strip(), 0) + 1
            conflicted_mixture_ids = {k for k, cnt in mix_id_counts.items() if cnt > 1}
            for cmid in sorted(conflicted_mixture_ids):
                sec_input_missing_or_malformed = True
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: Duplicate/conflicting sec_mals_dls "
                    f"qualified_coformulation_mixtures mixture_id={cmid!r} (count={mix_id_counts[cmid]}); "
                    f"all occurrences excluded from physical co-formulation qualification",
                )
            for m_idx, m_rec in enumerate(raw_mixtures):
                mlbl = f"sec_mals_dls.qualified_coformulation_mixtures[{m_idx}]"
                if not isinstance(m_rec, dict):
                    sec_input_missing_or_malformed = True
                    _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: {mlbl} must be an object")
                    continue
                extra_mk = sorted(set(m_rec.keys()) - _allowed_mix_keys)
                missing_mk = sorted(_required_mix_keys - set(m_rec.keys()))
                mix_ok = True
                if extra_mk or missing_mk:
                    mix_ok = False
                    sec_input_missing_or_malformed = True
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: {mlbl} closed-schema key violation "
                        f"(missing={missing_mk}, extra={extra_mk})",
                    )
                raw_mid = m_rec.get("mixture_id")
                mid_s: str | None = None
                if (
                    type(raw_mid) is not str
                    or not raw_mid
                    or raw_mid != raw_mid.strip()
                    or not _SHIELD_STRICT_ID_RE.match(raw_mid)
                ):
                    mix_ok = False
                    sec_input_missing_or_malformed = True
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: {mlbl}.mixture_id must be a non-empty "
                        f"unpadded ID matching {_SHIELD_STRICT_ID_RE.pattern} (got {raw_mid!r})",
                    )
                elif raw_mid in conflicted_mixture_ids:
                    mix_ok = False
                else:
                    mid_s = raw_mid

                c1_bid = m_rec.get("comp1_expression_batch_id")
                if (
                    type(c1_bid) is not str
                    or not c1_bid
                    or c1_bid != c1_bid.strip()
                    or c1_bid not in passing_batch_constructs
                    or _SHIELD_COMP1_LEAD not in passing_batch_constructs[c1_bid]
                    or (sec_b_err is None and c1_bid not in tmp_c1_sec)
                ):
                    mix_ok = False
                    sec_input_missing_or_malformed = True
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: {mlbl}.comp1_expression_batch_id={c1_bid!r} "
                        f"must reference a declared passing {_SHIELD_COMP1_LEAD} batch covered by sec_mals_dls.expression_batch_ids",
                    )

                c2_bid = m_rec.get("comp2_expression_batch_id")
                if (
                    type(c2_bid) is not str
                    or not c2_bid
                    or c2_bid != c2_bid.strip()
                    or c2_bid not in passing_batch_constructs
                    or _SHIELD_COMP2_LEAD not in passing_batch_constructs[c2_bid]
                    or (sec_b_err is None and c2_bid not in tmp_c2_sec)
                ):
                    mix_ok = False
                    sec_input_missing_or_malformed = True
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: {mlbl}.comp2_expression_batch_id={c2_bid!r} "
                        f"must reference a declared passing {_SHIELD_COMP2_LEAD} batch covered by sec_mals_dls.expression_batch_ids",
                    )

                m_mon, m_mon_err = _shield_check_num(m_rec.get("monomer_pct"), f"{mlbl}.monomer_pct", lo=0.0, hi=100.0)
                if m_mon_err is not None:
                    mix_ok = False
                    sec_input_missing_or_malformed = True
                    _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: {m_mon_err}")
                elif m_mon is not None:
                    valid_mix_monomers.append(m_mon)

                m_pdi, m_pdi_err = _shield_check_num(m_rec.get("dls_pdi"), f"{mlbl}.dls_pdi", lo=0.0, hi=1.0)
                if m_pdi_err is not None:
                    mix_ok = False
                    sec_input_missing_or_malformed = True
                    _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: {m_pdi_err}")
                elif m_pdi is not None:
                    valid_mix_pdis.append(m_pdi)

                m_no_agg = m_rec.get("no_aggregates_or_precipitation")
                if type(m_no_agg) is not bool:
                    mix_ok = False
                    sec_input_missing_or_malformed = True
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: {mlbl}.no_aggregates_or_precipitation must be strict bool",
                    )
                m_qc = m_rec.get("qc_passed")