77 lines
4.8 KiB
Python
77 lines
4.8 KiB
Python
"""One whole S1 fold; numerical functions are the unchanged reviewed scorer."""
|
|
from dataclasses import asdict,is_dataclass
|
|
import hashlib,json,resource,sys,time
|
|
from pathlib import Path
|
|
import numpy as np
|
|
from s1_scoring import score_fold,query_bootstrap_counts
|
|
from s1_scoring_io import load_part
|
|
|
|
|
|
def sha(path):
|
|
h=hashlib.sha256()
|
|
with Path(path).open('rb') as f:
|
|
for block in iter(lambda:f.read(1024*1024),b''):h.update(block)
|
|
return h.hexdigest()
|
|
|
|
|
|
def read(p):return json.loads(Path(p).read_text())
|
|
def save(p,value):Path(p).write_text(json.dumps(value,indent=2,allow_nan=False)+'\n')
|
|
|
|
|
|
def pack_tree(value,arrays):
|
|
if is_dataclass(value):return pack_tree(asdict(value),arrays)
|
|
if isinstance(value,np.ndarray):
|
|
key=f'array_{len(arrays):06d}';arrays[key]=value;return {'__array__':key}
|
|
if isinstance(value,np.generic):return pack_tree(value.item(),arrays)
|
|
if isinstance(value,float) and not np.isfinite(value):return {'__nonfinite__':'nan' if np.isnan(value) else ('inf' if value>0 else '-inf')}
|
|
if isinstance(value,dict):
|
|
if any(isinstance(k,tuple) for k in value):return {'__key_value_pairs__':[[list(k),pack_tree(v,arrays)] for k,v in value.items()]}
|
|
return {str(k):pack_tree(v,arrays) for k,v in value.items()}
|
|
if isinstance(value,(list,tuple)):return [pack_tree(v,arrays) for v in value]
|
|
return value
|
|
|
|
|
|
def unpack_tree(value,arrays):
|
|
if isinstance(value,dict):
|
|
if set(value)=={'__array__'}:return arrays[value['__array__']]
|
|
if set(value)=={'__nonfinite__'}:return float(value['__nonfinite__'])
|
|
if set(value)=={'__key_value_pairs__'}:return {tuple(k):unpack_tree(v,arrays) for k,v in value['__key_value_pairs__']}
|
|
return {k:unpack_tree(v,arrays) for k,v in value.items()}
|
|
if isinstance(value,list):return [unpack_tree(v,arrays) for v in value]
|
|
return value
|
|
|
|
|
|
def main():
|
|
start=time.monotonic();out=Path(sys.argv[1]);index=int(sys.argv[2]);c=read(out/'execution_contract.json');fold=c['folds'][index]
|
|
if index!=fold['fold_index']:raise ValueError('Fold index mismatch')
|
|
dest=out/'folds'/f'{index:02d}';dest.mkdir(exist_ok=False)
|
|
save(dest/'started.json',dict(index=index,contract_sha256=sha(out/'execution_contract.json'),unix=time.time()))
|
|
for p,h in c['code_sha256'].items():
|
|
if sha(p)!=h:raise ValueError('Execution code changed: '+p)
|
|
for p,h in c['input_sha256'].items():
|
|
if sha(p)!=h:raise ValueError('Frozen metadata changed: '+p)
|
|
if not read(Path(c['o2_root'])/'review_release.json')['process_hold_cleared']:raise ValueError('O2 release missing')
|
|
parts=[load_part(spec,Path.cwd(),c['o2_root']) for spec in c['panel']]
|
|
test=parts[fold['test_panel_index']];training=[parts[i] for i in fold['train_panel_indices']]
|
|
if test.source!=fold['test_source'] or test.case!=fold['test_case']:raise ValueError('Test identity changed')
|
|
save(dest/'progress.json',dict(stage='inputs_verified_starting_scoring',elapsed_seconds=time.monotonic()-start))
|
|
r=score_fold(training,test)
|
|
if r['status']!='PASS':raise RuntimeError('Noncomparable fold: '+r['status'])
|
|
expected=c['query_plan'][index]
|
|
for key in ['logical_knn_queries','tie_radius_queries']:
|
|
expected_key='logical_8NN_queries' if key=='logical_knn_queries' else key
|
|
if r[key]!=expected[expected_key]:raise ValueError('Query count differs: '+key)
|
|
r['test_nodes']=dict(case=test.case,source=test.source,ids=test.ids,features=test.features,counts=test.counts,eligible=test.eligible,near=test.near,draw_to_unique=test.draw_to_unique,positive_mass_fraction=test.positive_mass_fraction,bootstrap_counts=query_bootstrap_counts(test))
|
|
save(dest/'progress.json',dict(stage='scoring_complete_serializing',elapsed_seconds=time.monotonic()-start,logical_knn_queries=r['logical_knn_queries']))
|
|
arrays={};structure=pack_tree(r,arrays);np.savez_compressed(dest/'arrays.npz',**arrays);save(dest/'structure.json',structure)
|
|
result=dict(status='COMPLETE',fold_index=index,test_case=test.case,test_source=test.source,
|
|
logical_knn_queries=r['logical_knn_queries'],tie_radius_queries=r['tie_radius_queries'],expected_logical_knn_queries=expected['logical_8NN_queries'],query_counts_match=True,
|
|
tree_builds=len(r['nearest']),tie_candidate_evaluations=sum(v['tie_candidate_evaluations'] for v in r['nearest'].values()),
|
|
n_ref=int(test.eligible.sum()),training_distinct_references=len(r['physical_reference_ids']),eligible_draw_count=int(test.counts[test.eligible].sum()),
|
|
wall_seconds=time.monotonic()-start,peak_rss_bytes=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss*1024,
|
|
contract_sha256=sha(out/'execution_contract.json'),output_sha256={n:sha(dest/n) for n in ['structure.json','arrays.npz']},training=False)
|
|
save(dest/'result.json',result);print(json.dumps(result),flush=True)
|
|
|
|
|
|
if __name__=='__main__':main()
|