"""Reviewer-requested cache replay; no model fitting or additional language queries. The R2 rule remains primary. All sweeps are post-review diagnostics. GT is used only by score(), never by projection(). Each threshold sweep has a matched native comparator. Rejected expressions contribute exactly zero paired delta. """ from pathlib import Path import argparse, collections, hashlib, json, os, time import numpy as np from PIL import Image from scipy import ndimage import phase1_repair as p1 from r2_repair import scope_safe ROOT=Path(os.getenv('REFSEG_ROOT',Path(__file__).resolve().parents[1])) BASE=dict(anchor_margin=.05,target_margin=.05,geometry=.03,size_factor=1.,threshold_offset=0.) CONFIGS={'primary':BASE.copy()} for key,values in [('anchor_margin',[.02,.10]),('target_margin',[.02,.10]),('geometry',[.01,.05]),('size_factor',[.5,2.]),('threshold_offset',[-.05,.05])]: for v in values:CONFIGS[key+'_'+str(v)]={**BASE,key:v} def components(prob,t,factor): lab,n=ndimage.label(prob>=t,structure=np.ones((3,3),int));h,w=prob.shape sizes=np.bincount(lab.ravel());out=[];minimum=max(1,int(max(16,int(.0002*h*w))*factor)) for i,sl in enumerate(ndimage.find_objects(lab),1): if sl is None or sizes[i]1 else 0);trace['target_margin']=tm return finish(base,'target_ambiguous') if tm1 else 0);trace['anchor_margin']=am if am=cfg['geometry']];trace['eligible_components']=len(allowed) if not allowed:return finish(base,'relation_unsatisfied') tm=allowed[0]['confidence']-(allowed[1]['confidence'] if len(allowed)>1 else 0);trace['target_margin']=tm if not q.plural and tm0 gt=gtcache[r['ref_id']];allres={};tr=None;primary_mask=None for nameconf,cfg in CONFIGS.items(): threshold=native_t+cfg['threshold_offset'];cs={} for key in keys: ck=(key,threshold,cfg['size_factor']) if ck not in cc:cc[ck]=components(maps[key],threshold,cfg['size_factor']) cs[key]=cc[ck] shape=maps[q.original].shape;base=p1.union(cs[q.original],shape) pred,trace=projection(q,cs,shape,cfg) allres[nameconf]=dict(frozen=score(base,gt,r['target_count']==0),sfap=score(pred,gt,r['target_count']==0),changed=bool((base!=pred).any())) if nameconf=='primary':tr=trace;primary_mask=pred.copy() else:allres[nameconf]['different_from_primary']=bool((primary_mask!=pred).any()) row={k:r[k] for k in ['id','scene_id','split','query','target_count']} row.update(model=a.model,trace=tr,configs=allres) out.write(json.dumps(row)+'\n');count+=1 out.flush() if idx%10==0:print(json.dumps(dict(image=idx,images=len(paths),records=count,seconds=time.time()-t0)),flush=True) meta=dict(status='COMPLETE',records=count,images=len(paths),seconds=time.time()-t0,args=vars(a),configs=CONFIGS,job_id=os.getenv('SLURM_JOB_ID')) output.with_suffix('.meta.json').write_text(json.dumps(meta,indent=2));print(json.dumps(meta),flush=True) if __name__=='__main__':main()