"""Posthoc audit of cross-plane suppression against the same toy targets."""
import hashlib,json
from pathlib import Path
import numpy as np
import r38_synthetic_crossings as base
ROOT=Path(__file__).resolve().parents[1];OUT=ROOT/'results/l46'
def sha(p):return hashlib.sha256(p.read_bytes()).hexdigest()
def main():
 source=OUT/'results.json';records=json.loads(source.read_text())['records'];rows=[]
 for row in records:
  path=OUT/row['archive']
  with np.load(path,allow_pickle=False) as f:
   n,h,w=map(int,f['candidate_shape']);raw=np.unpackbits(f['candidate_bits'])[:n*h*w].reshape(n,h,w).astype(bool);refs=f['full_masks']
  variants={}
  for threshold in (None,.8,.95):
   kept=[]
   for m in raw:
    if threshold is None or all(base.iou(m,q)<threshold for q in kept):kept.append(m)
   key='none' if threshold is None else str(threshold);variants[key]={'candidates':len(kept),'matching':base.assign(refs,kept,.5)}
  if variants['0.8']['matching']!=row['automatic_scores']['multi_plane']['all']:raise ValueError('Original suppression results do not reproduce')
  rows.append({'pair':row['pair'],'kind':row['kind'],'archive_sha256':sha(path),'variants':variants})
 (OUT/'suppression-audit.json').write_text(json.dumps({'format':'l46-suppression-audit-v1','results_sha256':sha(source),'script_sha256':sha(Path(__file__)),'records':rows,'warning':'Posthoc sensitivity on the same synthetic scenes, full pool only; no real-object validation.'},indent=2)+'\n');print('Verified full-pool matching under no suppression, .8 and .95 on six synthetic scenes.')
if __name__=='__main__':main()
