"""Freeze a new random subset of the public image inventory before retrieval."""
import hashlib,json,random
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1];OUT=ROOT/'results/r47';SRC=ROOT/'results/r46';SEED=20261208
def h(p):return hashlib.sha256(p.read_bytes()).hexdigest()
def main():
 path=OUT/'selection.json'
 if path.exists():raise FileExistsError('Selection already exists')
 inv=json.loads((SRC/'inventory.json').read_text());names={x['name'] for x in inv['files']};excluded=set(json.loads((SRC/'metadata-selection.json').read_text())['selection'])|{'1'}
 eligible=sorted(n[7:-4] for n in names if n.startswith('images/') and n.endswith('.png') and n[7:-4] not in excluded and 'annotations/'+n[7:] in names and 'xmls/'+n[7:-4]+'.xml' in names)
 if len(eligible)<24:raise ValueError('Not enough eligible cases')
 ids=random.Random(SEED).sample(eligible,24)
 path.write_text(json.dumps({'format':'r47-selection-v1','seed':SEED,'inventory_sha256':h(SRC/'inventory.json'),'previous_selection_sha256':h(SRC/'metadata-selection.json'),'selection':ids,
    'warning':'New file IDs do not prove new independent biological samples; model pretraining overlap is unknown.'},indent=2)+'\n')
 print('Frozen 24 distinct public file IDs before annotation or image retrieval.')
if __name__=='__main__':main()
