"""Preselect public image IDs from file inventory without fetching annotations."""
import hashlib,json,random
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1];OUT=ROOT/'results/r46';SEED=20261207

def sha(path):return hashlib.sha256(path.read_bytes()).hexdigest()
def main():
 p=OUT/'metadata-selection.json'
 if p.exists():raise FileExistsError('Selection already exists')
 inv=json.loads((OUT/'inventory.json').read_text());names={x['name'] for x in inv['files']};eligible=sorted(n[7:-4] for n in names if n.startswith('images/') and n.endswith('.png') and 'annotations/'+n[7:] in names and 'xmls/'+n[7:-4]+'.xml' in names and n!='images/1.png')
 if len(eligible)<64:raise ValueError('Not enough paired files')
 ids=random.Random(SEED).sample(eligible,64)
 p.write_text(json.dumps({'format':'r46-metadata-selection-v1','inventory_sha256':sha(OUT/'inventory.json'),'seed':SEED,'selection':ids,
  'warning':'Sampled file IDs, not verified biological samples; one image and annotation used in source-format inspection were excluded.'},indent=2)+'\n')
 print('Frozen',len(ids),'file IDs before fetching their boxes or pixels.')
if __name__=='__main__':main()
