"""Freeze all structurally matched R47 cases before fetching image pixels."""
import hashlib,json
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1];SRC=ROOT/'results/r47';OUT=ROOT/'results/r48'
def h(path):return hashlib.sha256(path.read_bytes()).hexdigest()
def main():
 path=OUT/'selection.json'
 if path.exists():raise FileExistsError('Selection already exists')
 data=json.loads((SRC/'results.json').read_text());rows=[r for r in data['records'] if r['status']=='structural_match']
 if not rows:raise ValueError('No structural cases')
 path.write_text(json.dumps({'format':'r48-selection-v1','source_audit_sha256':h(SRC/'results.json'),'selection':[{'id':r['id'],'xml_sha256':r['xml_sha256'],'mask_sha256':r['mask_sha256']} for r in rows],
  'warning':'All structural matches from preselected R47 IDs; masks have already been inspected; new source image pixels not yet fetched. Structural match does not prove biological identity or sample independence.'},indent=2)+'\n')
 print('Frozen',len(rows),'structural cases before source image retrieval.')
if __name__=='__main__':main()
