99 lines
6.2 KiB
Python
99 lines
6.2 KiB
Python
"""Single scientific-v2 preparation: three seed-0 calls, geometry only."""
|
|
import argparse
|
|
from pathlib import Path
|
|
import os
|
|
import time
|
|
import traceback
|
|
|
|
import numpy as np
|
|
from scipy.spatial import QhullError
|
|
|
|
from o1_prepare import contract,load_inputs,difference
|
|
from o1_partition import (Metis,GeometryFailure,InputViolation,sha,graph,type_fields,
|
|
shuffle_type,partition_metrics,readout,interface_difference)
|
|
from o1_v2 import face_weights,reweighted_graph,triple_match,array_sha
|
|
from s2_recovery_supervisor import save
|
|
|
|
|
|
def prepare(root,index):
|
|
root=Path(root);c=contract(root);row=c['parts'][index]
|
|
dest=root/'parts'/row['case']/'output';dest.mkdir(parents=True,exist_ok=False)
|
|
start=time.process_time();wall=time.monotonic();stage='INPUTS';calls=0;arms={}
|
|
def progress(**kw):
|
|
save(dest/'progress.json',dict(stage=stage,case=row['case'],pid=os.getpid(),
|
|
cpu_seconds=time.process_time()-start,wall_seconds=time.monotonic()-wall,partition_calls=calls,**kw))
|
|
try:
|
|
progress();mesh,features,queries,provenance=load_inputs(c,row)
|
|
save(dest/'input-provenance.json',provenance)
|
|
stage='V2_TYPE_FACE_WEIGHTS';progress()
|
|
g=graph(mesh);z,b=type_fields(features,mesh['tets'])
|
|
shuffled,permutation=shuffle_type(z,mesh['volume'],mesh['raw_cells'],row['numeric_case'])
|
|
np.save(dest/'shuffle-permutation.npy',permutation)
|
|
weights,weight_diagnostics,rank_arrays=face_weights(g,z,shuffled)
|
|
gg=reweighted_graph(g,weights['G'])
|
|
if not np.array_equal(gg['adjwgt'],g['adjwgt']):raise InputViolation('G quantization differs from fixed v1 code')
|
|
np.savez_compressed(dest/'graph.npz',**{k:g[k] for k in ['xadj','adjncy','vwgt','adjwgt','edges','faces_raw','area']},
|
|
raw_nodes=mesh['raw_nodes'],raw_cells=mesh['raw_cells'],volume=mesh['volume'])
|
|
np.savez_compressed(dest/'face-weights.npz',**weights,**rank_arrays)
|
|
save(dest/'face-weight-diagnostics.json',dict(**weight_diagnostics,
|
|
graph_sha256=sha(dest/'graph.npz'),arrays_sha256=sha(dest/'face-weights.npz'),
|
|
shuffle_sha256=sha(dest/'shuffle-permutation.npy')))
|
|
engine=Metis();labels_by_arm={};metrics={}
|
|
for arm in ['G','T','C']:
|
|
before=time.process_time();stage='V2_GENERATE_'+arm;progress()
|
|
ag=reweighted_graph(g,weights[arm]);pre={k:array_sha(ag[k]) for k in ['xadj','adjncy','vwgt','adjwgt']}
|
|
rec=dict(arm=arm,seed=0,input_sha256=pre)
|
|
try:
|
|
calls+=1;labels,returned=engine.partition(ag,mesh['raw_cells'],0)
|
|
if pre!={k:array_sha(ag[k]) for k in pre}:raise InputViolation('METIS modified pinned input arrays')
|
|
rec.update(returned)
|
|
np.savez_compressed(dest/(arm+'-partition.npz'),labels=labels)
|
|
rec.update(partition_sha256=sha(dest/(arm+'-partition.npz')),canonical_label_sha256=array_sha(labels),
|
|
physical_fractions=(np.bincount(labels,weights=mesh['volume'],minlength=8)/mesh['volume'].sum()).tolist())
|
|
labels_by_arm[arm]=labels
|
|
metrics[arm]=partition_metrics(mesh,g,labels);rec.update(status='PASS',metrics=metrics[arm])
|
|
except (GeometryFailure,QhullError) as error:
|
|
rec.update(status='INELIGIBLE',reason=type(error).__name__+': '+str(error))
|
|
rec['cpu_seconds']=time.process_time()-before;arms[arm]=rec
|
|
save(dest/'arm-results.json',arms)
|
|
matching=triple_match(metrics) if len(metrics)==3 else None
|
|
qualified=bool(matching and matching['passed'])
|
|
diagnostics={};pairs=[('T','G'),('T','C'),('G','C')]
|
|
if len(labels_by_arm)==3:
|
|
diagnostics={a+'/'+b:dict(interface=interface_difference(labels_by_arm[a],labels_by_arm[b],g)) for a,b in pairs}
|
|
fields={}
|
|
if len(metrics)==3:
|
|
stage='V2_READOUT';progress()
|
|
for arm in ['G','T','C']:
|
|
fields[arm]=readout(mesh['tets'],mesh['volume'],b,labels_by_arm[arm],len(features))
|
|
if not np.isfinite(fields[arm]).all():raise GeometryFailure('NONFINITE_READOUT')
|
|
np.save(dest/(arm+'-extra.npy'),fields[arm])
|
|
qi=queries['indices']
|
|
for a,bb in pairs:
|
|
diagnostics[a+'/'+bb].update(float64=difference(fields[a][qi],fields[bb][qi]),
|
|
float32=difference(fields[a][qi].astype(np.float32),fields[bb][qi].astype(np.float32)))
|
|
np.savez_compressed(dest/'queries.npz',**queries)
|
|
reason=None if qualified else ('V2_TRIPLE_MATCH_FAIL' if matching else 'V2_ARM_GEOMETRY_FAIL')
|
|
result=dict(status='PASS' if qualified else 'INELIGIBLE',reason=reason,case=row['case'],
|
|
protocol='O1-scientific-v2-area-rank-face-weights',nodes=len(features),tets=len(mesh['tets']),faces=len(g['area']),
|
|
partition_calls=calls,seed=0,seeds={a:0 for a in arms},arms=arms,matching=matching,diagnostics=diagnostics,
|
|
weight_diagnostics=weight_diagnostics,input_provenance_sha256=sha(dest/'input-provenance.json'),
|
|
output_sha256={p.name:sha(p) for p in dest.iterdir() if p.suffix in ['.npz','.npy']},
|
|
cpu_seconds=time.process_time()-start,wall_seconds=time.monotonic()-wall,
|
|
labels_decoded=[],label_cache_opened=False,model_fit=False,original_features_unchanged=True)
|
|
save(dest/'result.json',result);stage='COMPLETE';progress();return 0
|
|
except GeometryFailure as error:
|
|
save(dest/'result.json',dict(status='INELIGIBLE',reason=str(error),case=row['case'],stage=stage,
|
|
protocol='O1-scientific-v2-area-rank-face-weights',partition_calls=calls,arms=arms,tets=row['tets'],
|
|
cpu_seconds=time.process_time()-start,labels_decoded=[],label_cache_opened=False,model_fit=False))
|
|
return 0
|
|
except Exception as error:
|
|
save(dest/'failure.json',dict(type=type(error).__name__,message=str(error),stage=stage,global_stop=True,
|
|
traceback=traceback.format_exc(),labels_decoded=[]))
|
|
return 1
|
|
|
|
|
|
if __name__=='__main__':
|
|
p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('index',type=int)
|
|
a=p.parse_args();raise SystemExit(prepare(a.root,a.index))
|