'unknown_keys': 'REJECT', 'tier6b_sorted_lot': { 'minimum_untreated_plated_cells_n': IIP0031_MIN_UNTREATED_CFU_PLATED_CELLS, 'underpowered_untreated_cfu_reason': 'UNDERPOWERED_UNTREATED_CFU_CONTROL_DENOMINATOR', }, 'metadata_keys': [ 'batch_id', 'assay_id', 'product_variant_id', 'estimand_scale', 's8b_epistasis_estimand_scale', 'obs_beta_x1_x4_epistasis_scale', 'obs_beta_x3_x4_epistasis_scale', 'specimen_or_clone_id', 'specimen_or_clone_ids', 'mcb_clone_id', 'prior_variant_safety_status', 'reset_prior_safety_stop_reason', 'reset_prior_safety_stop_reason_code', 'reset_prior_safety_stop_repeat_batch_ids', 'reset_prior_safety_stop_independent_qp_approver_id', 'oos_retest_assignable_cause_code', 'adjunct_conditioning_history_arm', 'adjunct_depletion_mode', 'adjunct_sa_fhl1_status', 'adjunct_immune_responder_history', 'knockin_splice_challenger_arm', 'knockin_locus4_orientation', 's10_construct_id', 's10_construct_donor_sha256', 'current_lot_id', 's10_qualified_lot_id', 's10_hlae_floor_mode', 'b2_tyk2_recipient_genotype_stratum', 'b2_tyk2_hlac_allele', 's6_cluster_size_arm', 'p05_barrier_slab_geometry_mode', 'design_z_divider_arm', 'p05_catalase_uniprot_accession', 'cuff_bioconjugation_arm', 'cuff_serum_challenge_stratum', 'rest_ocr_coupon_arm', 'conditioning_4arm_arm', 'conditioning_washout_analyte_scope', 'track2_rescue_route', 'design_y_integrase_modality', 'design_y_silencing_modality', 'design_y_payload_arm', 'donor_seedline_id', 'device_assembly_id', 'physical_route_id', 'assay_campaign_id', 'reset_prior_safety_stop_target_category', 'submitter_id', 'contrast_b1_geometry_arm', 'contrast_b2_perfusion_and_retrieval_arm', 'contrast_b3_donor_iec_arm', 'bound_c1_c5_pair_sha256', 'tyk2_pe_modality', 'iip0026_schema_era', 'recipient_serum_pair_id', 'intact_serum_sample_id', 'igg_depleted_serum_sample_id', 'positive_adcc_control_id', 'isotype_adcc_control_id', 'arm2_depleted_sample_id', 'contemporaneous_arm3_control_sample_id', 'sister_assembly_id', 'cxcl10_tss_donor_id', 'cxcl10_tss_transcription_unit_id', 'construct_sha256', 'post_sort_endocrine_zm447439_regimen', 'stage7_start_utc', 'sort_reaggregation_utc', 'assay_harvest_utc', 'registered_mixture_id', 'locked_analysis_plan_id', 'gate0s_elevated_store_scope_matched_device_lot_id', 'background_id', 'mcb_id', 'l2_v3_donor_id', 'tier6b_presort_lineage_panel', 'tier6b_clonogenic_risk_unit', ], 'boolean_policy_keys': list(STAGE0_DECISION_TREE_BOOLEAN_KEYS), 'endpoints': endpoints, 'stage0_decision_tree_endpoints': stage0_tree_endpoints} def write_contract(root: Path | str | None = None) -> dict: root = Path(root) if root is not None else Path(__file__).resolve().parents[1] value = derive_contract(root) (root / CONTRACT_RELATIVE_PATH).write_text(json.dumps(value, indent=2) + '\n') return value def finite_decimal(value: object, name: str) -> float: if isinstance(value, bool) or not isinstance(value, (int, float, str)): raise ValueError(f'{name} must be a finite decimal number') if isinstance(value, str) and not DECIMAL.fullmatch(value.strip()): raise ValueError(f'{name} must be a nonempty decimal/scientific number') number = float(value) if not math.isfinite(number): raise ValueError(f'{name} must be finite') # IEEE signed zero is one physical observation, not a distinct replay identity. return 0.0 if number == 0.0 else number def normalize_measurements(payload: dict, *, require_observation: bool = True, root: Path | str | None = None) -> tuple[dict, dict]: """Validate endpoints, retaining explicit missingness and declared SE defaults. Batch identity/replay is an additional stateful ingestor requirement; this shared measurement check is also used by the stateless draft validator. """ if not isinstance(payload, dict): raise ValueError('Measurement payload must be an object') payload = _canonicalize_stage0_alias_keys(payload) contract = derive_contract(root) endpoints = contract['endpoints'] stage0_endpoints = contract.get('stage0_decision_tree_endpoints', []) all_endpoints = list(endpoints) + list(stage0_endpoints) bool_policy_keys = set(contract.get('boolean_policy_keys', ['require_full_5endpoint_s9_panel'])) allowed = set(contract['metadata_keys']) | bool_policy_keys | {e[k] for e in all_endpoints for k in ['meanKey', 'seKey']} unknown = payload.keys() - allowed if unknown: raise ValueError(f'Unknown measurement keys: {sorted(unknown)}') result = {} for bkey in bool_policy_keys: if bkey in payload and payload[bkey] is not None: if type(payload[bkey]) is not bool: raise ValueError(f'{bkey} must be a boolean when provided') result[bkey] = payload[bkey] mean_keys = {e['meanKey'] for e in all_endpoints} for key in contract['metadata_keys']: if key not in payload: continue if key == 'specimen_or_clone_ids': raw_map = payload[key] if not isinstance(raw_map, dict) or not raw_map: raise ValueError('specimen_or_clone_ids must be a non-empty dictionary mapping endpoint meanKey to specimen/clone ID') clean_map = {} for raw_mk, sv in raw_map.items(): mk = STAGE0_JSON_TRIGGER_KEY_ALIASES.get(raw_mk, raw_mk) if mk not in mean_keys or payload.get(mk, 'UNAVAILABLE') == 'UNAVAILABLE': raise ValueError(f'specimen_or_clone_ids key {raw_mk!r} must reference an observed endpoint meanKey') if not isinstance(sv, str) or not sv.strip() or len(sv) > 120: raise ValueError(f'specimen_or_clone_ids[{raw_mk!r}] must be a nonempty string of at most 120 characters') if mk in clean_map and clean_map[mk] != sv.strip(): raise ValueError(f'Conflicting specimen_or_clone_ids for canonical endpoint {mk!r}') clean_map[mk] = sv.strip() result[key] = clean_map elif key == 'reset_prior_safety_stop_repeat_batch_ids': raw_list = payload[key] if ( not isinstance(raw_list, list) or len(raw_list) < 2 or not all(isinstance(x, str) and x.strip() and len(x) <= 120 for x in raw_list) or len({x.strip() for x in raw_list}) < 2 ): raise ValueError('reset_prior_safety_stop_repeat_batch_ids must be a list of at least 2 distinct nonempty batch ID strings of at most 120 characters') result[key] = [x.strip() for x in raw_list] elif key in ('s8b_epistasis_estimand_scale', 'obs_beta_x1_x4_epistasis_scale', 'obs_beta_x3_x4_epistasis_scale'): if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_S8B_EPISTASIS_ESTIMAND_SCALES: raise ValueError(f'{key} must be one of {sorted(ALLOWED_S8B_EPISTASIS_ESTIMAND_SCALES)}') result[key] = payload[key].strip() elif key == 'prior_variant_safety_status': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_PRIOR_VARIANT_SAFETY_STATUSES: raise ValueError(f'prior_variant_safety_status must be one of {sorted(ALLOWED_PRIOR_VARIANT_SAFETY_STATUSES)} (AUDIT-REF)') result[key] = payload[key].strip() elif key == 'adjunct_conditioning_history_arm': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_ADJUNCT_CONDITIONING_HISTORY_ARMS: raise ValueError(f'adjunct_conditioning_history_arm must be one of {sorted(ALLOWED_ADJUNCT_CONDITIONING_HISTORY_ARMS)} (IIP-0012; DEC-091)') result[key] = payload[key].strip() elif key == 'adjunct_depletion_mode': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_ADJUNCT_DEPLETION_MODES: raise ValueError(f'adjunct_depletion_mode must be one of {sorted(ALLOWED_ADJUNCT_DEPLETION_MODES)} (IIP-0012; DEC-091)') result[key] = payload[key].strip() elif key == 'adjunct_sa_fhl1_status': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_ADJUNCT_SA_FHL1_STATUSES: raise ValueError(f'adjunct_sa_fhl1_status must be one of {sorted(ALLOWED_ADJUNCT_SA_FHL1_STATUSES)} (IIP-0012; DEC-091)') result[key] = payload[key].strip() elif key == 'adjunct_immune_responder_history': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_ADJUNCT_IMMUNE_RESPONDER_HISTORIES: raise ValueError(f'adjunct_immune_responder_history must be one of {sorted(ALLOWED_ADJUNCT_IMMUNE_RESPONDER_HISTORIES)} (IIP-0012; DEC-091)') result[key] = payload[key].strip() elif key == 'knockin_splice_challenger_arm': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_KNOCKIN_SPLICE_CHALLENGER_ARMS: raise ValueError(f'knockin_splice_challenger_arm must be one of {sorted(ALLOWED_KNOCKIN_SPLICE_CHALLENGER_ARMS)} (IIP-0015; DEC-092)') result[key] = payload[key].strip() elif key == 'knockin_locus4_orientation': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_KNOCKIN_LOCUS4_ORIENTATIONS: raise ValueError(f'knockin_locus4_orientation must be one of {sorted(ALLOWED_KNOCKIN_LOCUS4_ORIENTATIONS)} (IIP-0015; DEC-092)') result[key] = payload[key].strip() elif key == 's10_hlae_floor_mode': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_S10_HLAE_FLOOR_MODES: raise ValueError(f's10_hlae_floor_mode must be one of {sorted(ALLOWED_S10_HLAE_FLOOR_MODES)} (IIP-0016; DEC-094)') result[key] = payload[key].strip() elif key == 'b2_tyk2_recipient_genotype_stratum': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_B2_TYK2_RECIPIENT_GENOTYPE_STRATA: raise ValueError(f'b2_tyk2_recipient_genotype_stratum must be one of {sorted(ALLOWED_B2_TYK2_RECIPIENT_GENOTYPE_STRATA)} (IIP-0017; DEC-095)') result[key] = payload[key].strip() elif key == 'b2_tyk2_hlac_allele': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_B2_TYK2_HLAC_ALLELES: raise ValueError(f'b2_tyk2_hlac_allele must be one of {sorted(ALLOWED_B2_TYK2_HLAC_ALLELES)} (IIP-0017; DEC-095)') result[key] = payload[key].strip() elif key == 's6_cluster_size_arm': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_S6_CLUSTER_SIZE_ARMS: raise ValueError(f's6_cluster_size_arm must be one of {sorted(ALLOWED_S6_CLUSTER_SIZE_ARMS)} (IIP-0019; DEC-097)') result[key] = payload[key].strip() elif key == 'p05_barrier_slab_geometry_mode': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_P05_BARRIER_SLAB_GEOMETRY_MODES: raise ValueError(f'p05_barrier_slab_geometry_mode must be one of {sorted(ALLOWED_P05_BARRIER_SLAB_GEOMETRY_MODES)} (IIP-0019; DEC-097)') result[key] = payload[key].strip() elif key == 'design_z_divider_arm': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_DESIGN_Z_DIVIDER_ARMS: raise ValueError(f'design_z_divider_arm must be one of {sorted(ALLOWED_DESIGN_Z_DIVIDER_ARMS)} (IIP-0019; DEC-097)') result[key] = payload[key].strip() elif key == 'p05_catalase_uniprot_accession': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_P05_CATALASE_UNIPROT_ACCESSIONS: raise ValueError(f'p05_catalase_uniprot_accession must be one of {sorted(ALLOWED_P05_CATALASE_UNIPROT_ACCESSIONS)} (IIP-0020; DEC-099)') result[key] = payload[key].strip() elif key == 'cuff_bioconjugation_arm': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_CUFF_BIOCONJUGATION_ARMS: raise ValueError(f'cuff_bioconjugation_arm must be one of {sorted(ALLOWED_CUFF_BIOCONJUGATION_ARMS)} (IIP-0020; DEC-099)') result[key] = payload[key].strip() elif key == 'cuff_serum_challenge_stratum': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_CUFF_SERUM_CHALLENGE_STRATA: raise ValueError(f'cuff_serum_challenge_stratum must be one of {sorted(ALLOWED_CUFF_SERUM_CHALLENGE_STRATA)} (IIP-0020; DEC-099)') result[key] = payload[key].strip() elif key == 'rest_ocr_coupon_arm': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_REST_OCR_COUPON_ARMS: raise ValueError(f'rest_ocr_coupon_arm must be one of {sorted(ALLOWED_REST_OCR_COUPON_ARMS)} (IIP-0021; DEC-100)') result[key] = payload[key].strip() elif key == 'conditioning_4arm_arm': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_CONDITIONING_4ARM_ARMS: raise ValueError(f'conditioning_4arm_arm must be one of {sorted(ALLOWED_CONDITIONING_4ARM_ARMS)} (IIP-0021; DEC-100)') result[key] = payload[key].strip() elif key == 'conditioning_washout_analyte_scope': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_CONDITIONING_WASHOUT_ANALYTE_SCOPES: raise ValueError(f'conditioning_washout_analyte_scope must be one of {sorted(ALLOWED_CONDITIONING_WASHOUT_ANALYTE_SCOPES)} (IIP-0021; DEC-100)') result[key] = payload[key].strip() elif key == 'track2_rescue_route': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_TRACK2_RESCUE_ROUTES: raise ValueError(f'track2_rescue_route must be one of {sorted(ALLOWED_TRACK2_RESCUE_ROUTES)} (IIP-0022; DEC-101)') result[key] = payload[key].strip() elif key == 'design_y_integrase_modality': if not isinstance(payload[key], str) or payload[key].strip() not in ALLOWED_DESIGN_Y_INTEGRASE_MODALITIES: raise ValueError(f'design_y_integrase_modality must be one of {sorted(ALLOWED_DESIGN_Y_INTEGRASE_MODALITIES)} (IIP-0023; DEC-102)') result[key] = payload[key].strip()