AstroFrame/tools/make_validation_sheets.py
2026-10-03 18:29:15 -07:00

33 lines
2.4 KiB
Python

"""Fixed capture-order samples, chosen independently of automatic verdicts."""
import json,sys,struct,zlib
import numpy as np
from pathlib import Path
from PIL import Image,ImageDraw,ImageFont
pdir=Path(sys.argv[1] if len(sys.argv)>1 else 'projects/Sample Survey')
p=json.loads((pdir/'project.json').read_text());sessions=sorted({f['Session'] for f in p['Frames']})
out=Path('docs/validation-sheets');out.mkdir(exist_ok=True)
font=ImageFont.truetype('C:/Windows/Fonts/consola.ttf',12)
index=[]
for number,session in enumerate(sessions):
frames=sorted([f for f in p['Frames'] if f['Session']==session],key=lambda f:f['Timestamp'])
count=16 if '1432' in session else 8
chosen=[frames[int((i+.5)*len(frames)/count)] for i in range(count)]
sheet=Image.new('RGB',(800,340*((count+3)//4)),(15,21,32));draw=ImageDraw.Draw(sheet)
for i,f in enumerate(chosen):
x=(i%4)*200;y=(i//4)*340
current=pdir/'previews'/f"{f['Id']}-display3.bin"
if current.exists():
data=current.read_bytes();_,_,_,w,h=struct.unpack('<iqqii',data[:28]);a=np.frombuffer(zlib.decompress(data[28:],-15),dtype='<f4').reshape(h,w)
center=float(np.median(a));noise=max(.001,float(np.median(np.abs(np.diff(a,axis=1))))/.953872);black=center-2.8*noise;white=max(float(np.percentile(a,99.95)),center+30*noise)
xx=np.clip((a-black)/(white-black),0,1);mid=(center-black)/(white-black);target=.18;m=mid*(target-1)/((2*target-1)*mid-target);gray=np.uint8(np.clip((m-1)*xx/((2*m-1)*xx-m),0,1)*255);im=Image.fromarray(gray).convert('RGB')
else:
data=(pdir/'previews'/f"{f['Id']}.bin").read_bytes();w,h=struct.unpack('<ii',data[:8]);im=Image.frombytes('L',(w,h),data[8:]).convert('RGB')
im.thumbnail((180,290));sheet.paste(im,(x+10+(180-im.width)//2,y+5))
suffix=Path(f['Path']).stem.split('_')[-1]
caption=f"{i+1:02d} {suffix}\n{f['Timestamp'][11:19]} UTC"
if len(sys.argv)>2 and sys.argv[2]=='decisions':caption+=f"\n{f['Automatic']} cloud {f.get('CloudCoverage',0):.0%}"
draw.multiline_text((x+6,y+297),caption,font=font,fill=(224,234,244))
index.append({'session':session,'sheet':number,'position':i+1,'file':Path(f['Path']).name})
sheet.save(out/f'session-{number}.png')
Path('docs/validation-sheets/index.json').write_text(json.dumps(index,indent=2))
print('\n'.join(f'{i}: {s}' for i,s in enumerate(sessions)))