Files
MeltMind/tools/s2_custody_common.py

73 lines
3.5 KiB
Python

"""Frozen evaluation inputs. This module cannot decode or load target labels."""
import json
from pathlib import Path
import numpy as np
from freeze_g1_components import sha
from s2_models.features import BASE_COLUMNS,R2_COLUMNS
def read_json(path):
return json.loads(Path(path).read_text())
def checked(path, digest):
p=Path(path)
if sha(p)!=digest: raise ValueError('SHA mismatch: '+str(p))
return p
def contract(root):
c=read_json(Path(root)/'contract.json')
for p,h in {**c['code_sha256'],**c['source_sha256']}.items(): checked(p,h)
return c
def test_rows(c, fold):
rows=read_json(checked(c['roster'],c['roster_sha256']))['parts']
return sorted((r for r in rows if r['included'] and r['assignments'][fold]['role']=='test'),key=lambda r:r['case'])
def prediction_inputs(row):
"""Only base/queries/R2 features: no training-cache or archive access."""
prep=row['preparation'];root=Path(prep['preparation_root'])
metadata=read_json(checked(root/'result.json',prep['result_sha256']))
if metadata['base_columns']!=list(BASE_COLUMNS) or tuple(BASE_COLUMNS[:3])!=('x_star','y_star','z_star'):
raise ValueError('Base coordinate column names differ from centred (x-center)/Rg contract')
if metadata['r2']['columns']!=list(R2_COLUMNS) or R2_COLUMNS[0]!='Vf':
raise ValueError('B0 requires named dimensionless relative flow volume Vf at column0')
for name in ['base.npy','queries.npz','r2.npz']:
checked(root/name,prep['output_sha256'][name])
base=np.load(root/'base.npy',allow_pickle=False)
with np.load(root/'queries.npz',allow_pickle=False) as q:
keep=q['eligible_s2']; ids=q['anchor_ids'][keep]
d=dict(ids=ids,raw_ids=q['raw_node_ids'][keep],counts=q['multiplicity'][keep],
gaps=q['g'][keep],edge=q['edge_summary'][keep],near_wall=q['near_wall'][keep])
with np.load(root/'r2.npz',allow_pickle=False) as q: x=q['features'][ids].copy()
if base.shape!=(row['nodes'],10) or x.shape!=(len(ids),20) or not len(ids): raise ValueError('Feature/query shape')
if not all(np.isfinite(a).all() for a in [base,x,d['gaps'],d['edge']]):raise ValueError('Nonfinite feature')
if not np.all(x[:,-1]==prep['t_set']):raise ValueError('Process setting mismatch')
if np.any((x[:,0]<0)|(x[:,0]>1)):raise ValueError('Dimensionless relative flow volume outside [0,1]')
if np.any(np.diff(d['raw_ids'])<=0):raise ValueError('Ascending unique original IDs required')
return dict(**d,base=base,x=x,coordinates_over_Rg=base[ids,:3],t_set=prep['t_set'])
def prediction_path(root, model, case):
return Path(root)/'predictions'/str(model['fold'])/model['name']/(case+'.npz')
def expected_models(c,fold):
models=[m for m in read_json(checked(c['models'],c['models_sha256'])) if m['fold']==fold]
models.append(dict(kind='B0',arm='B0',name=f'fold{fold}-B0',fold=fold,pool=None,seed=None))
return models
def verify_prediction_index(root,c):
root=Path(root);proof=read_json(root/'predictions-complete.json')
if proof['status']!='ALL_PREDICTIONS_FROZEN' or proof['contract_sha256']!=sha(root/'contract.json'):
raise ValueError('Predictions not frozen against current contract')
index=read_json(checked(root/'prediction-index.json',proof['prediction_index_sha256']))
expected={str(prediction_path(root,m,r['case'])) for f in range(5) for m in expected_models(c,f) for r in test_rows(c,f)}
if set(index)!=expected:raise ValueError('Missing/extra prediction files')
for p,h in index.items():checked(p,h)
return proof,index