102 lines
9.0 KiB
Python
102 lines
9.0 KiB
Python
"""E2 label-free T/C, original G reused; no cross-arm eligibility filter."""
|
|
import argparse,json,time,traceback
|
|
from pathlib import Path
|
|
import numpy as np
|
|
from scipy.spatial import QhullError
|
|
from phase2_h1_worker_v2 import checked,sha,save
|
|
from phase2_fixed_io import BASE
|
|
from phase2_b1213_features import input_geometry,cached,g_metrics
|
|
from o1_prepare import source_geometry,difference
|
|
from o1_partition import graph,Metis,type_fields,shuffle_type,readout,diameter,interface_difference,GeometryFailure,InputViolation
|
|
from o1_v2 import face_weights,reweighted_graph,array_sha
|
|
|
|
def read(p):return json.loads(Path(p).read_text())
|
|
def config(root):
|
|
c=read(root/'contract.json')
|
|
if c['approval']!='1794907827.062989' or len(c['parts'])!=552:raise PermissionError('E2 fixed552 scope')
|
|
for p,h in {**c['code_sha256'],**c['source_sha256']}.items():checked(BASE/p,h)
|
|
st=Path(c['archive']).stat();old=c['archive_stat']
|
|
if (st.st_size,st.st_mtime_ns,st.st_ino)!=(old['bytes'],old['mtime_ns'],old['inode']):raise InputViolation('Archive identity')
|
|
return c
|
|
|
|
def descriptor(mesh,g,labels,metrics):
|
|
ds=[];hulls=[]
|
|
for k in range(8):
|
|
nodes=np.unique(mesh['tets'][labels==k]);d,h=diameter(mesh['points'][nodes]);ds.append(d);hulls.append(h)
|
|
return dict(cut_area=metrics['cut_area'],diameters=sorted(ds),rms_diameter=float(np.sqrt(np.mean(np.asarray(ds)**2))),fractions=metrics['fractions'],hull_vertices=hulls)
|
|
|
|
def field(root,c,index):
|
|
row=c['parts'][index];dest=root/'parts'/f'{index:04d}';start=time.process_time();arms={};fields={};labels_by={};calls=0;G=c['G_sources'][row['case']]
|
|
gr=read(checked(G['result'],G['result_sha256']));fields['G']=np.load(checked(G['extra'],G['extra_sha256']),allow_pickle=False);arms['G']=dict(valid=gr['status']=='GEOMETRY_COMPLETE',status=gr['status'],reason=gr.get('reason'),source_result_sha256=G['result_sha256'],source_extra_sha256=G['extra_sha256'],partition_calls=0)
|
|
try:
|
|
mesh,decoded=source_geometry(c['archive'],row['case']);x,t,raw,old=input_geometry(row)
|
|
if not np.array_equal(mesh['points'],x) or not np.array_equal(mesh['raw_nodes'],raw):raise InputViolation('Raw geometry/node identity')
|
|
key=lambda a:np.lexsort(tuple(a[:,i] for i in [3,2,1,0]));a=np.sort(t,axis=1);b=mesh['tets']
|
|
if not np.array_equal(a[key(a)],b[key(b)]):raise InputViolation('Canonical tetra membership')
|
|
features=cached(row,'features');g=graph(mesh);z,values=type_fields(features,mesh['tets']);shuffled,perm=shuffle_type(z,mesh['volume'],mesh['raw_cells'],int(row['case'].split('_')[0]));weights,wd,ranks=face_weights(g,z,shuffled)
|
|
if not np.array_equal(reweighted_graph(g,weights['G'])['adjwgt'],g['adjwgt']):raise InputViolation('G integer weights changed')
|
|
np.savez_compressed(dest/'geometry-evidence.npz',permutation=perm,raw_nodes=raw,raw_cells=mesh['raw_cells'],volume=mesh['volume'],T_face_integer=weights['T'],C_face_integer=weights['C'])
|
|
common={k:array_sha(g[k]) for k in ['xadj','adjncy','vwgt','adjwgt']};save(dest/'input-evidence.json',dict(graph_fingerprints=common,type_sha256=array_sha(z),shuffled_type_sha256=array_sha(shuffled),readout_values_sha256=array_sha(values),weight_diagnostics=wd,decoded_geometry_arrays=decoded,labels_read=False))
|
|
if G.get('partition'):
|
|
with np.load(checked(G['partition'],G['partition_sha256']),allow_pickle=False) as f:labels_by['G']=f['labels'].copy()
|
|
if arms['G']['valid']:
|
|
if 'G' not in labels_by:raise InputViolation('Missing frozen valid G partition')
|
|
gm=g_metrics(mesh,g,labels_by['G'])
|
|
if not gm['passed']:raise InputViolation('Frozen G eligibility changed')
|
|
reproduced=readout(mesh['tets'],mesh['volume'],values,labels_by['G'],len(raw))
|
|
if not np.array_equal(reproduced,fields['G']):raise InputViolation('Frozen G readout differs')
|
|
try:arms['G']['descriptor']=descriptor(mesh,g,labels_by['G'],gm)
|
|
except (GeometryFailure,QhullError) as exc:arms['G']['descriptor_error']=type(exc).__name__+': '+str(exc)
|
|
engine=Metis();cache=c['O1_cache'].get(row['case']);previous=None
|
|
if cache:
|
|
previous=read(checked(cache['result'],cache['result_sha256']))
|
|
if previous.get('protocol')!='O1-scientific-v2-area-rank-face-weights':raise InputViolation('Only original O1v2 cache')
|
|
with np.load(checked(cache['graph'],previous['output_sha256']['graph.npz']),allow_pickle=False) as f:
|
|
for k,v in [('raw_nodes',raw),('raw_cells',mesh['raw_cells']),('volume',mesh['volume'])]:
|
|
if not np.array_equal(f[k],v):raise InputViolation('Cached canonical graph identity')
|
|
dw=cached(row,'Dw')
|
|
for arm in ['T','C']:
|
|
before=time.process_time();d=dest/arm;d.mkdir();rec=dict(valid=False,partition_calls=0,seed=0,source='new_seed0');ag=reweighted_graph(g,weights[arm]);fp={k:array_sha(ag[k]) for k in common}
|
|
try:
|
|
if previous and arm in previous.get('arms',{}):
|
|
prior=previous['arms'][arm]
|
|
if prior['input_sha256']!=fp or prior['seed']!=0:raise InputViolation('Cached T/C fingerprint differs')
|
|
rec.update(source='REUSED_O1V2_SEED0',source_result_sha256=cache['result_sha256'])
|
|
if 'partition_sha256' not in prior:raise GeometryFailure('REUSED_ORIGINAL_PARTITION_FAILURE: '+prior.get('reason','unknown'))
|
|
if prior['options']!=engine.options(0).tolist():raise InputViolation('Cached METIS options')
|
|
with np.load(checked(Path(cache['result']).parent/(arm+'-partition.npz'),prior['partition_sha256']),allow_pickle=False) as f:labels=f['labels'].copy()
|
|
returned=dict(options=prior['options'],returncode=prior['returncode'],integer_cut=prior['integer_cut'])
|
|
else:
|
|
calls+=1;rec['partition_calls']=1;labels,returned=engine.partition(ag,mesh['raw_cells'],0)
|
|
if fp!={k:array_sha(ag[k]) for k in fp}:raise InputViolation('METIS mutated input')
|
|
labels_by[arm]=labels;np.savez_compressed(d/'partition.npz',labels=labels);rec.update(returned,partition_sha256=sha(d/'partition.npz'),input_fingerprints=fp)
|
|
metrics=g_metrics(mesh,g,labels);rec['metrics']=metrics
|
|
if not metrics['passed']:raise GeometryFailure('SINGLE_ARM_BALANCE_OR_CONNECTIVITY_FAIL')
|
|
if not np.isfinite(dw).all() or dw.max()<=0:raise GeometryFailure('DW_NORMALIZATION_UNDEFINED')
|
|
fields[arm]=readout(mesh['tets'],mesh['volume'],values,labels,len(raw))
|
|
if not np.isfinite(fields[arm]).all():raise InputViolation('Readout nonfinite')
|
|
mass=np.bincount(mesh['tets'].ravel(),weights=np.repeat(mesh['volume']/4,4),minlength=len(raw));before_mass=(mass[:,None]*features[:,[0,9]]).sum(0);after_mass=(mass[:,None]*fields[arm]).sum(0)
|
|
if not np.allclose(before_mass,after_mass,rtol=1e-11,atol=1e-12):raise InputViolation('Readout mass identity')
|
|
rec.update(valid=True,status='GEOMETRY_COMPLETE',reason=None,mass_integral_before=before_mass.tolist(),mass_integral_after=after_mass.tolist())
|
|
try:rec['descriptor']=descriptor(mesh,g,labels,metrics)
|
|
except (GeometryFailure,QhullError) as exc:rec['descriptor_error']=type(exc).__name__+': '+str(exc)
|
|
except GeometryFailure as exc:fields[arm]=np.full((row['nodes'],2),np.nan);rec.update(status='SCIENTIFIC_MISSING',reason=str(exc))
|
|
np.save(d/'extra.npy',fields[arm],allow_pickle=False);rec.update(extra_sha256=sha(d/'extra.npy'),cpu_seconds=time.process_time()-before);arms[arm]=rec;save(d/'result.json',dict(case=row['case'],component_id=row['component_id'],**rec));save(dest/'progress.json',dict(arms=list(arms),partition_calls=calls,cpu_seconds=time.process_time()-start))
|
|
comparisons={}
|
|
for a,b in [('T','G'),('T','C'),('C','G')]:
|
|
rec=dict(both_valid=arms[a]['valid'] and arms[b]['valid'])
|
|
if a in labels_by and b in labels_by:rec['interface']=interface_difference(labels_by[a],labels_by[b],g)
|
|
if rec['both_valid']:
|
|
rec['readout_difference']=difference(fields[a],fields[b]);rec['bitwise_equal']=bool(np.array_equal(fields[a],fields[b]));rec['float32_equal']=bool(np.array_equal(fields[a].astype(np.float32),fields[b].astype(np.float32)))
|
|
comparisons[a+'-'+b]=rec
|
|
save(dest/'result.json',dict(status='PREPARATION_COMPLETE',case=row['case'],index=index,arms=arms,comparisons=comparisons,partition_calls=calls,cpu_seconds=time.process_time()-start,labels_read=False,output_sha256={str(p.relative_to(dest)):sha(p) for p in dest.rglob('*') if p.is_file() and p.name!='progress.json'}))
|
|
except GeometryFailure as exc:
|
|
for arm in ['T','C']:
|
|
d=dest/arm;d.mkdir(exist_ok=True);np.save(d/'extra.npy',np.full((row['nodes'],2),np.nan),allow_pickle=False);arms[arm]=dict(valid=False,status='SCIENTIFIC_MISSING',reason=str(exc),extra_sha256=sha(d/'extra.npy'));save(d/'result.json',dict(case=row['case'],component_id=row['component_id'],**arms[arm]))
|
|
save(dest/'result.json',dict(status='PREPARATION_COMPLETE',case=row['case'],index=index,arms=arms,comparisons={},partition_calls=calls,cpu_seconds=time.process_time()-start,labels_read=False,shared_applicability_failure=str(exc)))
|
|
if __name__=='__main__':
|
|
p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('index',type=int);a=p.parse_args();c=config(a.root)
|
|
try:field(a.root,c,a.index)
|
|
except BaseException as exc:
|
|
save(a.root/'parts'/f'{a.index:04d}'/'failure.json',dict(type=type(exc).__name__,reason=str(exc),traceback=traceback.format_exc(),labels_read=False));raise
|