"""Fixed7650 paired component equivalence; array/record only, no data I/O.""" from collections import defaultdict import numpy as np from r4bc_common import paired_difference from s2_models.objectives import relative_l2 from s2_custody_statistics import last_location METRICS={'relative_l2':.001,'last_location':.010} def two_metrics(pred,truth,query): p=np.asarray(pred);t=np.asarray(truth) if p.shape!=t.shape or p.ndim!=1 or not len(p) or not np.isfinite(p).all() or not np.isfinite(t).all():raise ValueError('Invalid metric arrays') return dict(relative_l2=relative_l2(p,t,query['counts'],np.ones(len(t),bool)),last_location=dict(value=last_location(t,p,query['raw_ids'],query['coordinates_over_Rg']),reason=None)) def summarize(rows,original_components): if len({r['case'] for r in rows})!=len(rows):raise ValueError('Duplicate part') result={};all_components=set(original_components) for arm in ['A','G']: result[arm]={} for metric,band in METRICS.items(): paired=defaultdict(list);excluded=[] for r in rows: if r['component_id'] not in all_components:raise ValueError('Unknown component') new=None if r['label_gate'] is None else r['label_gate'][arm][metric]['value'] old=None if r['LOG_gate'] is None else r['LOG_gate'][arm][metric]['value'] if new is None or old is None:excluded.append(r['case']);continue if not np.isfinite([new,old]).all():raise ValueError('Nonfinite metric must not be silently dropped') paired[r['component_id']].append((float(new),float(old))) means={g:np.mean(v,axis=0) for g,v in paired.items()} new={g:float(v[0]) for g,v in means.items()};old={g:float(v[1]) for g,v in means.items()} ci=paired_difference(new,old,seed=2026111408,replicates=2000) passes=ci['low'] is not None and ci['high'] is not None and ci['low']>=-band and ci['high']<=band result[arm][metric]=dict(label_minus_LOG=ci,label_mean=float(np.mean(list(new.values()))) if new else None,LOG_mean=float(np.mean(list(old.values()))) if old else None,band=[-band,band],passed=passes,paired_parts=sum(map(len,paired.values())),excluded_parts=excluded,component_part_counts={g:len(v) for g,v in paired.items()},excluded_components=sorted(all_components-set(paired)),excluded_component_count=len(all_components-set(paired))) passed=all(v['passed'] for arm in result.values() for v in arm.values()) return dict(decision='PASS_OUTCOME_EQUIVALENCE' if passed else 'FAIL_OUTCOME_EQUIVALENCE',accepted_for_v2=passed,comparisons=result,interpretation='Acceptance within fixed bands; a failed gate is not proof of non-equivalence or failure of every gate-dependent method')