"""Exercise location guards and every unverified source proposal through the reader."""
import collections,json
from pathlib import Path
from diatom_pilot.ude_breadth_dataset import UDEBreadthDataset
from diatom_pilot.ude_scaleup_dataset import sha256
R=Path(__file__).resolve().parents[1];O=R/'results/r112/species-breadth';results={}
for bad in ('test','training',''):
 try:UDEBreadthDataset(bad);raise AssertionError('invalid scope accepted')
 except ValueError:pass
for scope in ('prior_training_components','all_exposed_development'):
 d=UDEBreadthDataset(scope,include_unverified_masks=True);triage=collections.Counter();parts=collections.Counter();ids=set();names=set()
 for i in range(len(d)):
  r=d[i];m=r['weak_mask_proposals'];assert r['annotation_id'] not in ids and m.shape==(3,r['image'].height,r['image'].width) and m.dtype.name=='bool' and r['background_status'].startswith('unknown') and 'unverified' in r['mask_status'];ids.add(r['annotation_id']);names.add(r['source_name']);triage[r['mask_triage']]+=1;parts[r['prior_80_location_partition']]+=1
 results[scope]={'crops':len(d),'source_names':len(names),'prior_location_parts':dict(parts),'triage':dict(triage)}
assert results['prior_training_components']['prior_location_parts']=={'training':784} and results['all_exposed_development']['prior_location_parts']=={'training':784,'check':164,'tuning':148};out={'format':'r112-species-breadth-reader-verify-v1','reader_sha256':sha256(R/'diatom_pilot/ude_breadth_dataset.py'),'source_verify_sha256':sha256(O/'verify.json'),'mask_verify_sha256':sha256(O/'mask-hypotheses/verify.json'),'exposure_sha256':sha256(O/'prior-split-exposure.json'),'scopes':results,'checks':'Both explicit scopes exercised for every crop and all three unverified masks, including grayscale/mask shape, hashes and unknown background; invalid scope names rejected. Does not validate mask correctness, names, independence or biological realism.'};(O/'reader-verify.json').write_text(json.dumps(out,indent=2)+'\n');print(json.dumps(out,indent=2))
