scripts/wetlab_contract.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/wetlab_contract.py · SHA-256: 6bb3955530f8f05d…
or len(pilot_raw) < int(C['MIN_PILOT_SELECTION_LOTS_N'])
or any(_strict_unpadded_str_iip0029(x, 2) is None for x in pilot_raw)
):
return _hold('HOLD_WINNERS_CURSE_UNADJUSTED_MULTIWINDOW_OR_LOT_OVERLAP')
if len(set(pilot_raw)) != len(pilot_raw) or (set(pilot_raw) & set(lot_ids)):
return _hold('HOLD_WINNERS_CURSE_UNADJUSTED_MULTIWINDOW_OR_LOT_OVERLAP')
if (
not isinstance(pilot_blk_raw, (list, tuple))
or len(pilot_blk_raw) < int(C['MIN_PILOT_SELECTION_LOTS_N'])
or any(_strict_unpadded_str_iip0029(x, 2) is None for x in pilot_blk_raw)
or len(set(pilot_blk_raw)) != len(pilot_blk_raw)
):
return _hold('HOLD_WINNERS_CURSE_UNADJUSTED_MULTIWINDOW_OR_LOT_OVERLAP')
pilot_block_ids_set = {str(x) for x in pilot_blk_raw}
t_crit = student_t_ppf(0.95, float(n_lots - 1))
elif win_design == 'MULTI_WINDOW_CONFIRMATION_BONFERRONI':
if k_cand < 2:
return _hold('HOLD_INVALID_BONFERRONI_SINGLE_WINDOW_CONFIG')
t_crit = student_t_ppf(1.0 - 0.05 / float(k_cand), float(n_lots - 1))
else:
return _hold('HOLD_WINNERS_CURSE_UNADJUSTED_MULTIWINDOW_OR_LOT_OVERLAP')
# 4. Registered hormone unit, common-denominator recovery basis, and fluidics mode (Sol Fix #4):
hormone_unit = _strict_unpadded_str_iip0029(record.get('hormone_unit'), 3)
norm_denom = _strict_unpadded_str_iip0029(record.get('normalization_denominator'), 3)
recov_basis = _strict_unpadded_str_iip0029(record.get('recovery_denominator_basis'), 3)
if hormone_unit is None or hormone_unit not in C['REGISTERED_HORMONE_UNIT_SPECS'] or norm_denom is None or recov_basis is None:
return _hold('HOLD_MISSING_OR_UNREGISTERED_HORMONE_UNIT_OR_DENOMINATOR')
unit_spec = C['REGISTERED_HORMONE_UNIT_SPECS'][hormone_unit]
if norm_denom != str(unit_spec['required_denominator']) or recov_basis != norm_denom:
return _hold('HOLD_RECOVERY_DENOMINATOR_BASIS_MISMATCH')
if record.get('eta_sort_includes_sorting_and_reaggregation_losses') is not True:
return _hold('HOLD_ETA_SORT_MUST_INCLUDE_SORTING_AND_REAGGREGATION_LOSSES')
z_min_floor = float(unit_spec['z_min_floor'])
fluidics = record.get('assay_fluidics_mode')
if fluidics not in ('DYNAMIC_PERIFUSION', 'STATIC_BATCH_BENCHMARK'):
return _hold('HOLD_INVALID_ASSAY_FLUIDICS_MODE')
# 5. Source-bound per-lot records with independent differentiation_block_ids and matched 40 + 100 mmHg coverage:
lot_obs_list = record.get('lot_observations')
if not isinstance(lot_obs_list, (list, tuple)) or len(lot_obs_list) != n_lots:
return _hold('HOLD_MISSING_SOURCE_BOUND_PER_LOT_OBSERVATIONS')
seen_obs_lot_ids: list[str] = []
seen_block_ids: list[str] = []
seen_trace_ids: list[str] = []
block_by_lot: dict[str, str] = {}
eta_v: list[float] = []
basal1_v: list[float] = []
low1_v: list[float] = []
high1_v: list[float] = []
low2_v: list[float] = []
high2_v: list[float] = []
kcl_v: list[float] = []
adult_ref_z2_v: list[float] = []
stored_iu_v: list[float] = []
proins_ratio_v: list[float] = []
peak1_v: list[float] = []
tpeak1_v: list[float] = []
t50_v: list[float] = []
t90_v: list[float] = []
tau_ch_v: list[float] = []
gcg_fold_v: list[float] = []
glc_thresh_v: list[float] = []
ucn3_v: list[float] = []
counterreg_v: list[float] = []
# Matched 100 mmHg sibling vectors for DYNAMIC_PERIFUSION:
h1_100_v: list[float] = []
l1_100_v: list[float] = []
h2_100_v: list[float] = []
l2_100_v: list[float] = []
tau_100_v: list[float] = []
for item in lot_obs_list:
if not isinstance(item, (dict, MappingProxyType)):
return _hold('HOLD_INVALID_PER_LOT_OBSERVATION_RECORD')
lsk = _coerce_lot_scope_key_iip0029(item.get('lot_scope_key'))
if lsk is None:
return _hold('HOLD_PER_LOT_RECORD_MISSING_LOT_SCOPE_KEY')
if (
lsk.product_variant_id != scope_key.product_variant_id
or lsk.construct_sha256 != scope_key.construct_sha256
or lsk.donor_seedline_id != scope_key.donor_seedline_id
or lsk.mcb_id != scope_key.mcb_id
):
return _hold('HOLD_PER_LOT_SCOPE_KEY_PROVENANCE_MISMATCH')
lid = _strict_unpadded_str_iip0029(lsk.lot_id, 2)
bid = _strict_unpadded_str_iip0029(item.get('differentiation_block_id'), 2)
tid = _strict_unpadded_str_iip0029(item.get('trace_id'), 3)
po2 = _strict_finite_float_iip0029(item.get('assay_po2_mmhg'))
if lid is None or bid is None or tid is None or po2 is None or po2 not in C['ALLOWED_ASSAY_PO2_MMHG']:
return _hold('HOLD_PER_LOT_MISSING_BLOCK_TRACE_ID_OR_INVALID_ASSAY_PO2')
seen_obs_lot_ids.append(lid)
seen_block_ids.append(bid)
seen_trace_ids.append(tid)
block_by_lot[lid] = bid
vals: dict[str, float] = {}
for k in (
'eta_sort_viable_recovery',
'basal1_pre_pulse',
'low1_mean',
'high1_mean',
'low2_mean',
'high2_mean',
'kcl_mean',
'adult_islet_ref_z2',
'stored_mature_insulin_iu_implanted',
'proinsulin_to_cpeptide_content_molar_ratio',
):
fv = _strict_finite_float_iip0029(item.get(k))
if fv is None or fv < 0.0:
return _hold(f'HOLD_MISSING_OR_INVALID_PER_LOT_{k.upper()}')
vals[k] = fv
eta_v.append(vals['eta_sort_viable_recovery'])
basal1_v.append(vals['basal1_pre_pulse'])
low1_v.append(vals['low1_mean'])
high1_v.append(vals['high1_mean'])
low2_v.append(vals['low2_mean'])
high2_v.append(vals['high2_mean'])
kcl_v.append(vals['kcl_mean'])
adult_ref_z2_v.append(vals['adult_islet_ref_z2'])
stored_iu_v.append(vals['stored_mature_insulin_iu_implanted'])
proins_ratio_v.append(vals['proinsulin_to_cpeptide_content_molar_ratio'])
if fluidics == 'DYNAMIC_PERIFUSION':
# Primary trace must be boundary-matched 40 mmHg paired with a matched 100 mmHg sibling trace:
if abs(po2 - 40.0) > 1e-6:
return _hold('HOLD_DYNAMIC_PRIMARY_TRACE_MUST_BE_40MMHG_PAIRED_WITH_100MMHG_SIBLING')
# Censored short low-interval handling (Sol Fix #4):
low2_dur = _strict_finite_float_iip0029(item.get('low2_washout_duration_min'))
if low2_dur is None or low2_dur < float(C['MIN_UNCENSORED_LOW2_DURATION_MIN']) or item.get('effluent_t90_shutoff_min') is None:
return _hold('HOLD_CENSORED_SHORT_LOW_INTERVAL_SHUTOFF_UNRESOLVED')
# Per-trace tracer validity, alignment, and individual chamber ceiling <= 0.50 min:
if item.get('tracer_response_valid') is not True or item.get('tracer_time_aligned') is not True:
return _hold('HOLD_PER_TRACE_TRACER_RESPONSE_OR_TIME_ALIGNMENT_UNVERIFIED')
for k, dst in (
('peak1_instantaneous', peak1_v),
('time_to_peak1_min', tpeak1_v),
('effluent_t50_shutoff_min', t50_v),
('effluent_t90_shutoff_min', t90_v),
('inert_tracer_tau_chamber_min', tau_ch_v),
('glucagon_2mM_fold_increase', gcg_fold_v),
('glucose_threshold_mM', glc_thresh_v),
('ucn3_maturity_ratio', ucn3_v),
('alpha_counterregulatory_gain_ratio', counterreg_v),
):
fv = _strict_finite_float_iip0029(item.get(k))
if fv is None or fv <= 0.0:
return _hold('HOLD_MISSING_DYNAMIC_PERIFUSION_PEAK_SHUTOFF_TRACER_OR_GATE0M_METRIC')
dst.append(fv)
if tau_ch_v[-1] > float(C['TRACER_TAU_CHAMBER_MAX_MIN']):
return _hold('HOLD_INDIVIDUAL_CHAMBER_DISPERSION_TAU_EXCEEDED_0P50_MIN')
# Matched 100 mmHg sibling trace coverage on every independent block:
tid_100 = _strict_unpadded_str_iip0029(item.get('matched_100mmhg_trace_id'), 3)
po2_100 = _strict_finite_float_iip0029(item.get('matched_100mmhg_assay_po2_mmhg'))
tau_100 = _strict_finite_float_iip0029(item.get('matched_100mmhg_tracer_tau_chamber_min'))
h1_100 = _strict_finite_float_iip0029(item.get('matched_100mmhg_high1_mean'))
l1_100 = _strict_finite_float_iip0029(item.get('matched_100mmhg_low1_mean'))
h2_100 = _strict_finite_float_iip0029(item.get('matched_100mmhg_high2_mean'))
l2_100 = _strict_finite_float_iip0029(item.get('matched_100mmhg_low2_mean'))
if (
tid_100 is None
or po2_100 is None
or abs(po2_100 - 100.0) > 1e-6
or tau_100 is None
or tau_100 <= 0.0
or h1_100 is None or h1_100 < 0.0
or l1_100 is None or l1_100 < 0.0
or h2_100 is None or h2_100 < 0.0
or l2_100 is None or l2_100 < 0.0
or item.get('matched_100mmhg_tracer_response_valid') is not True
or item.get('matched_100mmhg_tracer_time_aligned') is not True
):
return _hold('HOLD_MISSING_MATCHED_100MMHG_OXYGEN_COVERAGE_OR_TRACER_ALIGNMENT')
if tau_100 > float(C['TRACER_TAU_CHAMBER_MAX_MIN']):
return _hold('HOLD_INDIVIDUAL_CHAMBER_DISPERSION_TAU_EXCEEDED_0P50_MIN')
seen_trace_ids.append(tid_100)
h1_100_v.append(h1_100)
l1_100_v.append(l1_100)
h2_100_v.append(h2_100)
l2_100_v.append(l2_100)
tau_100_v.append(tau_100)
expected_traces = (2 * n_lots) if fluidics == 'DYNAMIC_PERIFUSION' else n_lots
if seen_obs_lot_ids != lot_ids or len(set(seen_trace_ids)) != expected_traces:
return _hold('HOLD_PER_LOT_IDS_OR_TRACE_IDS_MISMATCH_CONFIRMATION_SET')
if len(set(seen_block_ids)) != n_lots or (pilot_block_ids_set & set(seen_block_ids)):
return _hold('HOLD_NON_INDEPENDENT_DIFFERENTIATION_BLOCKS_OR_PILOT_BLOCK_OVERLAP')
# 6. Locked UTC Timestamps, Dual Clocks & Process Mode (Sol Fix #4):
proc_mode = record.get('preparation_process_mode')
sorting_performed = record.get('sorting_performed')
if proc_mode not in ('SORTED_REAGGREGATED', 'UNSORTED_CONTROL') or type(sorting_performed) is not bool:
return _hold('HOLD_MISSING_OR_INVALID_PROCESS_SORTING_MODE')
if (proc_mode == 'SORTED_REAGGREGATED' and sorting_performed is not True) or (
proc_mode == 'UNSORTED_CONTROL' and sorting_performed is not False
):
return _hold('HOLD_SORTING_MODE_BOOLEAN_CONTRADICTION')
dt_s7_start = _parse_utc_iso_iip0029(record.get('stage7_start_utc'))
dt_sort = _parse_utc_iso_iip0029(record.get('sort_reaggregation_utc'))
dt_harvest = _parse_utc_iso_iip0029(record.get('assay_harvest_utc'))
if dt_s7_start is None or dt_sort is None or dt_harvest is None:
return _hold('HOLD_MISSING_OR_INVALID_LOCKED_CLOCK_TIMESTAMPS')
if not (dt_s7_start <= dt_sort <= dt_harvest):
return _hold('HOLD_NON_MONOTONIC_CLOCK_TIMESTAMPS')
s7_total = _strict_finite_float_iip0029(record.get('stage7_total_days'))
s7_sort = _strict_finite_float_iip0029(record.get('sort_reaggregation_stage7_day'))
post_sort = _strict_finite_float_iip0029(record.get('days_post_sort_reaggregation'))
if s7_total is None or s7_sort is None or post_sort is None:
return _hold('HOLD_MISSING_OR_INVALID_TWOCLOCK_METADATA')
if not (float(C['STAGE7_MIN_TOTAL_DAYS']) <= s7_total <= float(C['STAGE7_MAX_TOTAL_DAYS'])):
return _hold('HOLD_STAGE7_TOTAL_DAYS_OUT_OF_QUALIFIED_RANGE')
if abs((s7_total - s7_sort) - post_sort) > 1e-6:
return _hold('HOLD_TWOCLOCK_ARITHMETIC_MISMATCH')
ts_total_d = (dt_harvest - dt_s7_start).total_seconds() / 86400.0
ts_sort_d = (dt_sort - dt_s7_start).total_seconds() / 86400.0
ts_post_d = (dt_harvest - dt_sort).total_seconds() / 86400.0
tol_d = float(C['TIMESTAMP_DAY_TOLERANCE'])
if abs(ts_total_d - s7_total) > tol_d or abs(ts_sort_d - s7_sort) > tol_d or abs(ts_post_d - post_sort) > tol_d:
return _hold('HOLD_TWOCLOCK_TIMESTAMP_DAY_MISMATCH')
if proc_mode == 'SORTED_REAGGREGATED':
if s7_sort < float(C['SORTED_MIN_SORT_DAY']) or post_sort < float(C['SORTED_MIN_POST_SORT_DAYS']):
return _hold('HOLD_SORTED_CLOCK_BELOW_MINIMUM_MATURATION_DAYS')
if any(e < float(C['SORTED_ETA_MIN']) or e >= float(C['SORTED_ETA_MAX_EXCLUSIVE']) for e in eta_v):
return _hold('HOLD_IMPOSSIBLE_OR_UNQUALIFIED_SORTED_ETA_RECOVERY')
else:
if abs(post_sort) > 1e-9 or abs(s7_sort - s7_total) > 1e-9 or any(abs(e - 1.0) > 1e-9 for e in eta_v):
return _hold('HOLD_UNSORTED_CONTROL_CLOCK_OR_ETA_MISMATCH')
# 7. Stage-7 Maturation Pharmacology (ALK5i-free + T3/NAC + ZM447439 tolerance + LLOQ washout + vascular function):
if record.get('obs_alk5i_free_maturation_applied') is not True or record.get('terminal_alk5i_tgfb_inhibitor_continued') is not False:
return _hold('HOLD_PROHIBITED_OR_UNVERIFIED_ALK5I_FREE_MATURATION')
if record.get('stage7_t3_10nM_and_nac_1mM_retained') is not True:
return _hold('HOLD_STAGE7_MISSING_REQUIRED_10NM_T3_OR_1MM_NAC')
zm_regimen = record.get('post_sort_endocrine_zm447439_regimen')
zm_uM = _strict_finite_float_iip0029(record.get('post_sort_zm447439_uM'))
if zm_regimen not in ('S7W0_ONLY_0UM_POST_SORT', 'CONTINUOUS_0P5UM_BALBOA_S7') or zm_uM is None:
return _hold('HOLD_MISSING_OR_UNREGISTERED_ZM447439_COVARIATE')
if zm_regimen == 'S7W0_ONLY_0UM_POST_SORT' and abs(zm_uM) > 1e-9: