Files
MeltMind/tools/s2_pilot_prepare.py

57 lines
3.6 KiB
Python

"""Prepare an authorized non-test part once, then extract only its permitted label."""
import argparse
import json
from pathlib import Path
import sys
import time
import traceback
import numpy as np
from freeze_g1_components import sha
from s2_e1_supervisor import save
from s2_pilot_data import permitted_role,read_non_test_fill_time,validate_query_targets
import s2_preprocess_pilot as s2_preprocess
def run(root,index):
start=time.monotonic();c=json.loads((root/'contract.json').read_text());spec=c['cases'][index];case=spec['case']
manifest=json.loads((root/'role-manifest.json').read_text());role=permitted_role(manifest,case)
dest=root/'ready'/case;dest.mkdir(parents=True,exist_ok=False)
try:
if spec.get('prior_failed_preparation'):
save(dest/'failure.json',dict(case=case,role=role,status='PRIOR_MEASUREMENT_FAILURE_CARRIED_FORWARD',
prior_artifacts=spec['prior_failed_preparation'],new_scientific_attempt=False,labels_read=False))
return 3
for p,h in c['code_sha256'].items():assert sha(p)==h,p
assert sha(root/'role-manifest.json')==c['role_manifest_sha256']
if spec.get('reuse_preparation'):
source=Path(spec['reuse_preparation']);r=json.loads((source/'result.json').read_text())
assert sha(source/'result.json')==spec['reuse_result_sha256'] and r['status']=='COMPLETE'
for name,h in r['output_sha256'].items():assert sha(source/name)==h
else:
code=s2_preprocess.run(root,index)
if code:raise ValueError('Full input preparation failed; no labels extracted')
source=root/'parts'/case;r=json.loads((source/'result.json').read_text())
with np.load(source/'geometry.npz',allow_pickle=False) as z:raw_ids=z['raw_node_ids']
with np.load(source/'queries.npz',allow_pickle=False) as q:
ids=q['anchor_ids'];eligible=q['eligible_s2'];counts=q['multiplicity']
if not eligible.any():raise ValueError('No common eligible queries; drop pilot part with reason')
st=Path(c['archive']).stat();ver=c['archive_stat']
assert (st.st_size,st.st_mtime_ns,st.st_ino)==(ver['bytes'],ver['mtime_ns'],ver['inode'])
values,label_meta=read_non_test_fill_time(c['archive'],case,raw_ids,manifest)
target=validate_query_targets(values,ids,eligible,spec['t_set'])
np.savez_compressed(dest/'labels.npz',anchor_ids=ids[eligible],raw_node_ids=raw_ids[ids[eligible]],
multiplicity=counts[eligible],seconds=values[ids[eligible]],target_t_star=target)
receipt=dict(case=case,role=role,status='READY',preparation_root=str(source),preparation_result_sha256=sha(source/'result.json'),
label_provenance=label_meta,label_sha256=sha(dest/'labels.npz'),t_set=spec['t_set'],
component_id=spec['component_id'],nodes=spec['tet_nodes'],eligible_queries=int(eligible.sum()),
eligible_slots=int(counts[eligible].sum()),reused_preparation=bool(spec.get('reuse_preparation')),
wall_seconds=time.monotonic()-start,test_labels_read=False,model_fit=False,gpu=False)
save(dest/'receipt.json',receipt);print(json.dumps(receipt),flush=True);return 0
except Exception as exc:
save(dest/'failure.json',dict(case=case,role=role,status='FAILED_NO_REPLACEMENT',error_type=type(exc).__name__,message=str(exc),traceback=traceback.format_exc(),wall_seconds=time.monotonic()-start))
raise
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('index',type=int);a=p.parse_args();sys.exit(run(a.root,a.index))