58 lines
2.8 KiB
Python
58 lines
2.8 KiB
Python
"""Learning-only query cache. No validation/test labels or fitted selection."""
|
|
import json
|
|
from pathlib import Path
|
|
import numpy as np
|
|
from phase2_h1_worker_v2 import checked, sha, save
|
|
from phase2_h2_data import cached
|
|
from s2_custody_common import prediction_inputs
|
|
from phase3_item_c_loss import candidates
|
|
|
|
|
|
def require_learning(view, case):
|
|
f = view['folds'][0]
|
|
if case not in f['learning'] or case in f['validation'] or case in f['test']:
|
|
raise PermissionError('Location truth restricted to learning role')
|
|
|
|
|
|
def prepare_queries(view, rows, output):
|
|
output = Path(output)
|
|
output.mkdir(exist_ok=False)
|
|
manifest = {}
|
|
by_case = {r['case']: r for r in rows}
|
|
if len(by_case) != len(rows):
|
|
raise ValueError('Repeated roster case')
|
|
for case in view['folds'][0]['learning']:
|
|
require_learning(view, case) # before ANY case array access
|
|
row = by_case[case]
|
|
q = prediction_inputs(row) # existing label-free query recipe
|
|
full = cached(view, case, 'raw_ids')
|
|
idx = np.searchsorted(full, q['raw_ids'])
|
|
if np.any(idx >= len(full)) or not np.array_equal(full[idx], q['raw_ids']):
|
|
raise ValueError('Original query/full-node identity')
|
|
if q['t_set'] != view['parts'][case]['t_set']:
|
|
raise ValueError('Original process-time identity')
|
|
# The original scorer uses physical seconds with exact equality ties.
|
|
# FP32 residual reconstruction is not an acceptable true-peak source.
|
|
digest = view['parts'][case]['output_sha256']['seconds.npy']
|
|
with (output/'learning-location-target-access.jsonl').open('a') as f:
|
|
f.write(json.dumps(dict(case=case, role='learning', seconds_sha256=digest))+'\n')
|
|
seconds = cached(view, case, 'seconds')
|
|
d = candidates(full, q['raw_ids'], q['counts'], q['coordinates_over_Rg'], seconds[idx])
|
|
p = output/(case+'.npz')
|
|
np.savez(p, **d, original_raw_ids=q['raw_ids'], original_counts=q['counts'],
|
|
original_anchor_ids=q['ids'], original_near_wall=q['near_wall'],
|
|
original_coordinates_over_Rg=q['coordinates_over_Rg'])
|
|
manifest[case] = dict(path=str(p), sha256=sha(p), seconds_sha256=digest,
|
|
queries_sha256=row['preparation']['output_sha256']['queries.npz'],
|
|
unique_candidates=len(d['indices']), true_peak_raw_id=d['true_peak_raw_id'],
|
|
true_peak_ties=d['true_peak_ties'], undefined=False)
|
|
save(output/'manifest.json', manifest)
|
|
return manifest
|
|
|
|
|
|
def query_data(view, case, manifest):
|
|
require_learning(view, case)
|
|
r = manifest[case]
|
|
with np.load(checked(r['path'], r['sha256']), allow_pickle=False) as z:
|
|
return {k:z[k].copy() for k in ('indices', 'distances')}
|