56 lines
4.0 KiB
Python
56 lines
4.0 KiB
Python
"""Report fixed-route calibration residuals; freeze thresholds only after all jobs pass."""
|
|
from pathlib import Path
|
|
from collections import Counter
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import math
|
|
from g1_copy_calibration import ROOT, check_contract
|
|
from s2_e1_supervisor import save
|
|
|
|
def main():
|
|
ap=argparse.ArgumentParser();ap.add_argument('--partial',action='store_true');args=ap.parse_args()
|
|
check_contract(ROOT)
|
|
if not args.partial:
|
|
batch=json.loads((ROOT/'batch-complete.json').read_text())
|
|
assert batch['status']=='CALIBRATION_RESIDUALS_COMPLETE' and batch['failed_jobs']==0
|
|
rows=[];mirror_flags=[];negative_controls=[]
|
|
for path in sorted((ROOT/'custodian/results').glob('job-*/calib-*.json')):
|
|
r=json.loads(path.read_text());pair=r['pair'];kind=pair['kind'];out={}
|
|
assert r['mean_rg']>0 and r['rg_ratio']>=1
|
|
for route in ['physical','scale_free']:
|
|
branches={str(v['parity']):v['residual_max_two_way_area_pps_p95']/(r['mean_rg'] if route=='physical' else 1.) for v in r['routes'] if v['route']==route}
|
|
assert set(branches)=={'1','-1'} and all(math.isfinite(v) and v>=0 for v in branches.values())
|
|
positive=route=='scale_free' or kind!='scale'
|
|
out[route]={'branch_residuals':branches,'best_residual':min(branches.values()),'positive':positive,
|
|
'size_gate_pass':r['rg_ratio']<=1.05 if route=='physical' else True}
|
|
if kind=='mirror' and branches['-1']>=branches['1']:
|
|
mirror_flags.append({'pair_id':pair['id'],'route':route,'det_minus':branches['-1'],'det_plus':branches['1'],
|
|
'note':'det- did not strictly win; retain result, including possible symmetry/local optimum'})
|
|
if kind=='scale':
|
|
negative_controls.append({'pair_id':pair['id'],'rg_ratio':r['rg_ratio'],'physical_size_gate_pass':out['physical']['size_gate_pass']})
|
|
rows.append({'pair_id':pair['id'],'kind':kind,'rg_ratio':r['rg_ratio'],'routes':out,
|
|
'source_result_sha256':hashlib.sha256(path.read_bytes()).hexdigest()})
|
|
if any(r['physical_size_gate_pass'] for r in negative_controls):
|
|
save(ROOT/'public/negative-control-stop.json',{'status':'STOP','controls':negative_controls})
|
|
raise ValueError('A scaled negative control passed the physical size gate; do not generate candidates')
|
|
thresholds=None
|
|
if not args.partial:
|
|
assert len(rows)==62 and Counter(r['kind'] for r in rows)=={'rigid':20,'mirror':20,'scale':20,'natural_visual_rigid':2}
|
|
thresholds={route:3*max(r['routes'][route]['best_residual'] for r in rows if r['routes'][route]['positive']) for route in ['physical','scale_free']}
|
|
assert sum(r['routes']['physical']['positive'] for r in rows)==42
|
|
assert len(negative_controls)==20
|
|
result={'status':'PARTIAL_RESIDUAL_CHECKPOINT' if args.partial else 'CALIBRATION_THRESHOLDS_FROZEN',
|
|
'completed_pairs':len(rows),'pair_kind_counts':dict(Counter(r['kind'] for r in rows)),
|
|
'thresholds':thresholds,'threshold_ruling':'1794662380.787319','residual_distributions':rows,
|
|
'physical_scale_negative_controls':negative_controls,'mirror_branch_flags':mirror_flags,
|
|
'physical_positive_size_gate_failures':[r['pair_id'] for r in rows if r['routes']['physical']['positive'] and not r['routes']['physical']['size_gate_pass']],
|
|
'claim':'Engineering calibration on development examples; not independent cross-key recall certification',
|
|
'candidate_generation_started':False,'source_sha256':hashlib.sha256(Path(__file__).read_bytes()).hexdigest()}
|
|
dest=ROOT/'public'/('partial-residual-checkpoint.json' if args.partial else 'calibration-thresholds.json')
|
|
if dest.exists() and not args.partial:raise FileExistsError('Frozen thresholds already exist')
|
|
save(dest,result)
|
|
print(json.dumps({k:v for k,v in result.items() if k not in ['residual_distributions','physical_scale_negative_controls']},indent=2))
|
|
|
|
if __name__=='__main__':main()
|