"""Hash and geometry/query identity preflight, with zero target-value reads.""" import argparse from pathlib import Path import time import numpy as np from r4bc_common import (contract, checked, read, sha, save, exact, fingerprint, query_inputs, tet_indices, R2_COLUMNS, InputViolation) def run(root): start=time.process_time(); c=contract(root); out=root/'input-preflight'; out.mkdir(exist_ok=False) index={}; records=[] def check(p,h): checked(p,h);index[str(p)]=h;return Path(p) for row in c['parts']: case=row['case'];p=row['preparation'];v=row['v5_cache'];s=Path(p['preparation_root']);t=Path(v['output']) old=read(check(s/'result.json',p['result_sha256']));new=read(check(t/'result.json',v['result_sha256'])) if old['case']!=case or new['case']!=case or new['status']!='PASS':raise InputViolation('Preparation case/status') if old['r2']['columns']!=list(R2_COLUMNS) or new['features']['columns']!=list(R2_COLUMNS[:-1]):raise InputViolation('Feature columns') if p['t_set']!=v['t_scale_seconds'] or new['t_scale_seconds']!=p['t_set']:raise InputViolation('t_set provenance') for name in ('queries.npz','r2.npz','geometry.npz','base.npy'): check(s/name,p['output_sha256'][name]) # Hash target files as bytes only; no target values are opened in this phase. for name in ('features.npy','raw_ids.npy','target.npy','seconds.npy'): check(t/name,v['output_sha256'][name]) check(row['label']['path'],row['label']['sha256']) q=query_inputs(row);iq=tet_indices(row,q) if len(iq)!=p['eligible_unique']:raise InputViolation('Query count differs from manifest') r4a=read(check(row['r4a']['result'],row['r4a']['result_sha256'])) check(row['r4a']['predictions'],row['r4a']['predictions_sha256']) if not r4a['cache_identity_verified'] or r4a['case']!=case:raise InputViolation('R4a cache identity unavailable') with np.load(s/'geometry.npz',allow_pickle=False) as z: raw=np.load(t/'raw_ids.npy',allow_pickle=False) exact(z['raw_node_ids'],raw,'full raw IDs') for key,field in [('points_raw','mesh_x'),('tetra','mesh_t')]: if fingerprint(z[key])!=r4a['fingerprints'][field]:raise InputViolation('Geometry/frame fingerprint') exact(z['gate_location_raw'],new['geometry']['inlet_config']['inlet_location'],'gate metadata') for key,field in [('raw_ids','query_raw_ids'),('counts','query_counts')]: if fingerprint(q[key])!=r4a['fingerprints'][field]:raise InputViolation('Query identity fingerprint') with np.load(s/'r2.npz',allow_pickle=False) as z: x=z['features'];h=x[q['ids']] if x.shape!=(row['nodes'],20) or not np.isfinite(h).all():raise InputViolation('Historical query features') exact(h[:,-1],np.full(len(h),p['t_set']),'historical process column') x=np.load(t/'features.npy',mmap_mode='r',allow_pickle=False) if x.shape!=(row['nodes'],19) or not np.isfinite(x).all():raise InputViolation('Tet feature coverage') for oldpred in row['historical_predictions'].values(): with np.load(check(oldpred['path'],oldpred['sha256']),allow_pickle=False) as z: exact(z['raw_ids'],q['raw_ids'],'historical prediction IDs') exact(z['counts'],q['counts'],'historical prediction counts') if not np.isfinite(z['seconds']).all():raise InputViolation('Nonfinite historical prediction') records.append(dict(case=case,nodes=row['nodes'],query_rows=len(iq),geometry_identity=True, historical_dtype=str(h.dtype),tet_dtype=str(x.dtype),targets_values_read=False)) save(out/'progress.json',dict(parts=len(records),total=len(c['parts']),cpu_seconds=time.process_time()-start)) save(root/'input-freeze.json',dict(contract_sha256=sha(root/'contract.json'),files=index,parts=records, label_values_read=False,new_decodes=0,new_features=0)) save(out/'result.json',dict(status='COMPLETE',parts=len(records),files=len(index), input_freeze_sha256=sha(root/'input-freeze.json'),cpu_seconds=time.process_time()-start, target_values_read=False,model_fit=False)) if __name__=='__main__': p=argparse.ArgumentParser();p.add_argument('root',type=Path);a=p.parse_args();run(a.root)