"""Record relationships to the earlier 80-name local development split."""
import collections,json
from pathlib import Path
from r112_scaleup_synthetic_compare import sha
R=Path(__file__).resolve().parents[1];O=R/'results/r112/species-breadth';D=R/'results/r112/species-scaleup';p=json.loads((O/'selection.json').read_text());a=json.loads((O/'assets.json').read_text());v=json.loads((O/'verify.json').read_text());split=json.loads((D/'location-partitions.json').read_text());assert v['selection_sha256']==sha(O/'selection.json') and v['assets_sha256']==sha(O/'assets.json');parts=split['group_partition'];oldids={x['annotation_id'] for x in split['records']};records=[];counts=collections.Counter();components=collections.defaultdict(set);names=collections.defaultdict(set)
for x in a['records']:
 part=parts.get(x['source_site_component'],'unassigned');counts[part]+=1;components[part].add(x['source_site_component']);names[part].add(x['source_species']);records.append({'annotation_id':x['annotation_id'],'source_name':x['source_species'],'source_site_component':x['source_site_component'],'old_80_name_location_partition':part,'same_annotation_as_old_pool':x['annotation_id'] in oldids})
assert len(records)==1096 and sum(x['same_annotation_as_old_pool'] for x in records)==0;assert set(counts)=={'training','tuning','check'}
report={'format':'r112-breadth-prior-split-exposure-v1','source_assets_sha256':sha(O/'assets.json'),'source_verify_sha256':sha(O/'verify.json'),'prior_80_partition_sha256':sha(D/'location-partitions.json'),'counts':dict(counts),'distinct_components':{k:len(x) for k,x in components.items()},'distinct_new_source_names':{k:len(x) for k,x in names.items()},'same_annotation_ids_as_old_80_pool':0,'records':records,'warning':'The 1096 breadth crops were selected and extracted after the 80-name split and include 164 crops from its 17 check components and 148 from its tuning components. The earlier 566 check crop pixels were not opened here, but location components are exposed. Training on the full breadth pool would leak those components into evaluation using the earlier split; do not use that split to claim location-unseen results. Even training-component-only breadth data cannot make the previously used or development-exposed material an independent final benchmark. Site components are conservative proxies, not confirmed biological independence.'};(O/'prior-split-exposure.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps({k:v for k,v in report.items() if k!='records'},indent=2))
