"""Read all verified proposal files through the location-partition dataset interface."""
import collections,ctypes,json,os,signal
from pathlib import Path
from diatom_pilot.ude_scaleup_dataset import UDEScaleupDataset,sha256
R=Path(__file__).resolve().parents[1];O=R/'results/r112/species-scaleup';parent=os.getppid();assert ctypes.CDLL(None,use_errno=True).prctl(1,signal.SIGTERM)==0
if parent!=os.getppid():raise RuntimeError('launcher exited')
counts={};triage=collections.Counter();ids={};groups={};n=0
for part in ('training','tuning','check'):
 d=UDEScaleupDataset(part,include_unverified_masks=True);ids[part]=set();groups[part]=set()
 for i in range(len(d)):
  x=d[i];m=x['weak_mask_proposals'];assert m.shape==(3,x['image'].height,x['image'].width) and x['weak_mask_order']==('center','minus','plus') and 'unverified' in x['mask_status'] and 'unknown' in x['background_status'];ids[part].add(x['annotation_id']);groups[part].add(x['source_site_component']);triage[x['mask_triage']]+=1;n+=1
 counts[part]=len(d)
assert n==4000 and all(not ids[a]&ids[b] and not groups[a]&groups[b] for a,b in (('training','tuning'),('training','check'),('tuning','check')));mask_verify=json.loads((O/'mask-hypotheses/verify.json').read_text());assert dict(triage)==mask_verify['triage'];out={'format':'r112-scaleup-mask-reader-verify-v1','reader_sha256':sha256(R/'diatom_pilot/ude_scaleup_dataset.py'),'partition_sha256':sha256(O/'location-partitions.json'),'mask_verification_sha256':sha256(O/'mask-hypotheses/verify.json'),'counts':counts,'source_crops':n,'mask_proposals':n*3,'triage':dict(triage),'checks':'Opened every source crop and each proposal through explicit location-partition reader; source and mask hashes, dimensions, per-instance proposal order, group disjointness and unknown background checked. No biological ground truth, image-domain accuracy or sample independence established.'};(O/'mask-reader-verify.json').write_text(json.dumps(out,indent=2)+'\n');print(json.dumps(out,indent=2))
