"""Read every scale-up development crop through the explicit-partition guard."""
import collections,ctypes,hashlib,json,os,signal
from pathlib import Path
import numpy as np
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')
ids={};groups={};counts={};names={}
for part in ('training','tuning','check'):
 d=UDEScaleupDataset(part);ids[part]=set();groups[part]=set();classes=set()
 for i in range(len(d)):
  x=d[i];assert x['partition']==part and x['image'].mode=='L' and x['mask_status']=='no verified instance mask' and 'unknown' in x['background_status'];ids[part].add(x['annotation_id']);groups[part].add(x['source_site_component']);classes.add(x['source_name'])
 counts[part]=len(d);names[part]=len(classes)
assert all(not ids[a]&ids[b] and not groups[a]&groups[b] for a,b in (('training','tuning'),('training','check'),('tuning','check'))) and sum(counts.values())==4000 and set(names.values())=={80};out={'format':'r112-scaleup-reader-verify-v1','reader_sha256':sha256(R/'diatom_pilot/ude_scaleup_dataset.py'),'partition_sha256':sha256(O/'location-partitions.json'),'counts':counts,'site_component_counts':{k:len(v) for k,v in groups.items()},'source_names':names,'checks':'Every crop opened, grayscale mode and source hash checked, explicit partitions and disjoint selected location components checked; no model masks consumed. Does not validate biological independence, source identity, taxon concepts or masks.'};(O/'reader-verify.json').write_text(json.dumps(out,indent=2)+'\n');print(json.dumps(out,indent=2))
