47 lines
3.4 KiB
Python
47 lines
3.4 KiB
Python
"""Outcome calibration: fixed old algorithm, new sealed seed IDs, no labels."""
|
|
import json,time
|
|
from pathlib import Path
|
|
import numpy as np
|
|
from phase2_h1_worker_v2 import checked,save,sha
|
|
from phase2_b1213_features import input_geometry,cached
|
|
from gre24_features_v2 import mesh,exterior_faces,solve_eikonal,cell_flow_speed,strict_volume_cdf,nodal_gradient,spatial_moment
|
|
from o1_prepare import source_geometry
|
|
from o1_partition import readout
|
|
|
|
def recompute(x,t,dw,sources,progress=lambda *a,**kw:None):
|
|
g=mesh(x,t);faces=exterior_faces(t);timings={};tick=time.process_time()
|
|
def done(name):
|
|
nonlocal tick
|
|
now=time.process_time();timings[name]=now-tick;tick=now
|
|
progress('Di');di=solve_eikonal(g,sources,1.);done('Di')
|
|
speed,zero=cell_flow_speed(g,dw,faces);done('cell_speed')
|
|
progress('Df');df=solve_eikonal(g,sources,speed);done('Df')
|
|
if dw.max()<=0 or di.max()<=0:raise ValueError('Undefined feature normalization')
|
|
vf=strict_volume_cdf(df,g['mass']);dis=di/di.max();dws=dw/dw.max()
|
|
grads=[nodal_gradient(g,a) for a in (vf,dis,dws)];done('gradients')
|
|
progress('moments');moment,meta=spatial_moment(g,di,progress);done('moment')
|
|
features=np.column_stack([vf,grads[0],dis,grads[1],dw,dws,grads[2],moment])
|
|
if features.shape!=(len(x),19) or not np.isfinite(features).all():raise ValueError('Invalid recomputed features')
|
|
return dict(features=features,Di=di,Df=df,Vf=vf),dict(timings_cpu_seconds=timings,moment=meta,all_wall_tetrahedra=len(zero))
|
|
|
|
def prepare(root,c,index):
|
|
item=c['parts'][index];row=item['row'];dest=root/'parts'/f'{index:04d}';start=time.process_time()
|
|
x,t,raw,old=input_geometry(row);ids=np.array(item['gate_raw_ids'],dtype=np.int64);sources=np.searchsorted(raw,ids)
|
|
if not len(ids) or np.any(sources>=len(raw)) or not np.array_equal(raw[sources],ids):raise ValueError('Sealed source IDs outside tet domain')
|
|
dw=cached(row,'Dw')
|
|
def progress(stage,**kw):save(dest/'progress.json',dict(stage=stage,case=row['case'],cpu_seconds=time.process_time()-start,**kw))
|
|
arrays,meta=recompute(x,t,dw,sources,progress)
|
|
gentry=item['G'];extra=np.full((len(raw),2),np.nan)
|
|
if gentry['valid']:
|
|
cm,decoded=source_geometry(c['archive'],row['case'])
|
|
if not np.array_equal(cm['raw_nodes'],raw) or not np.array_equal(cm['points'],x):raise ValueError('Frozen G canonical identity')
|
|
with np.load(checked(gentry['partition_path'],gentry['partition_sha256']),allow_pickle=False) as z:labels=z['labels']
|
|
values=arrays['features'][:,[0,9]]
|
|
if labels.shape!=(len(cm['tets']),):raise ValueError('Frozen partition shape')
|
|
extra=readout(cm['tets'],cm['volume'],values[cm['tets']].mean(axis=1),labels,len(raw))
|
|
mass=np.bincount(cm['tets'].ravel(),weights=np.repeat(cm['volume']/4,4),minlength=len(raw))
|
|
if not np.allclose((mass[:,None]*values).sum(0),(mass[:,None]*extra).sum(0),rtol=1e-11,atol=1e-12):raise ValueError('G readout volume identity')
|
|
arrays['G_extra']=extra
|
|
for name,a in arrays.items():np.save(dest/(name+'.npy'),a,allow_pickle=False)
|
|
save(dest/'result.json',dict(status='FEATURES_COMPLETE_NO_PREDICTIONS',case=row['case'],index=index,output_sha256={name+'.npy':sha(dest/(name+'.npy')) for name in arrays},gate_raw_ids=ids.tolist(),Dw_reused_sha256=row['v5_cache']['output_sha256']['Dw.npy'],G_partition_reused=True,G_valid=gentry['valid'],new_labels_read=False,new_fits=0,cpu_seconds=time.process_time()-start,**meta))
|