"""Verify and replay site-component partition assignment for 4,000 development crops."""
import collections,hashlib,json,tempfile
from pathlib import Path
import r112_scaleup_partition as partitioner
R=Path(__file__).resolve().parents[1];O=R/'results/r112/species-scaleup';sha=lambda p:hashlib.sha256(Path(p).read_bytes()).hexdigest();s=json.loads((O/'selection.json').read_text());a=json.loads((O/'assets.json').read_text());v=json.loads((O/'verify.json').read_text());p=json.loads((O/'location-partitions.json').read_text());assert p['selection_sha256']==v['selection_sha256']==sha(O/'selection.json') and p['assets_sha256']==v['assets_sha256']==sha(O/'assets.json');assert len(p['records'])==len(s['records'])==len(a['records'])==4000;source={r['annotation_id']:r for r in s['records']};assets={r['annotation_id']:r for r in a['records']};assert len(source)==len(assets)==4000;seen=set();groups=collections.defaultdict(set);classes=collections.defaultdict(collections.Counter);counts=collections.Counter()
for r in p['records']:
 ident=r['annotation_id'];assert ident not in seen and ident in source and ident in assets;seen.add(ident);x=source[ident];y=assets[ident];assert x['exposure_status']=='prior_development' and r['source_species']==x['source_species']==y['source_species'] and r['site_component']==x['site_component']==y['source_site_component'] and r['partition'] in ('training','tuning','check') and p['group_partition'][r['site_component']]==r['partition'];groups[r['partition']].add(r['site_component']);classes[r['source_species']][r['partition']]+=1;counts[r['partition']]+=1
assert seen==set(source)==set(assets) and all(not groups[a]&groups[b] for a,b in (('training','tuning'),('training','check'),('tuning','check'))) and dict(counts)==p['counts'] and {k:len(v) for k,v in groups.items()}==p['component_counts'] and len(classes)==p['source_name_strings']==80
for name in classes:
 assert dict(classes[name])==p['per_name'][name] and sum(classes[name].values())==50 and classes[name]['training']>=20 and classes[name]['tuning']>=3 and classes[name]['check']>=3
with tempfile.TemporaryDirectory(dir='/tmp',prefix='r112-partition-replay-') as tmp:
 d=Path(tmp);(d/'selection.json').symlink_to((O/'selection.json').resolve());(d/'assets.json').symlink_to((O/'assets.json').resolve());partitioner.O=d;partitioner.main();assert sha(d/'location-partitions.json')==sha(O/'location-partitions.json')
result={'format':'r112-scaleup-partition-verify-v1','partition_sha256':sha(O/'location-partitions.json'),'selection_sha256':sha(O/'selection.json'),'assets_sha256':sha(O/'assets.json'),'script_sha256':sha(Path(__file__)),'counts':dict(counts),'site_component_counts':{k:len(v) for k,v in groups.items()},'source_name_strings':len(classes),'replay_byte_identical':True,'checks':'All 4,000 unique IDs, prior-development statuses, component membership, class counts and disjoint component sets checked; metadata-only assignment replayed. Source names, sample independence, model exposure and masks are not scientifically validated.'};(O/'partition-verify.json').write_text(json.dumps(result,indent=2)+'\n');print(json.dumps({k:result[k] for k in ('counts','site_component_counts','source_name_strings','replay_byte_identical')},indent=2))
