Close custody main-review checks for named B0 units and coordinate scaling

This commit is contained in:
Frank14f committed 2026-11-15 16:12:22 +08:00
1 parent 6e4d991d78
commit 522ee84f6a
7 files changed
+233 -7

No files matched your search

+6 -2
View File
@@ -8,6 +8,8 @@ import numpy as np
from s2_custody_common import contract,checked,test_rows,prediction_inputs,expected_models,prediction_path
from s2_recovery_supervisor import save
from freeze_g1_components import sha
from s2_models.features import R2_COLUMNS
from s2_models.objectives import b0_seconds
def load_neural(m,device):
@@ -50,8 +52,10 @@ def run(root,fold):
with torch.inference_mode(),torch.autocast('cuda',enabled=False):
y=loaded[m['name']](tensors['base'],tensors['ids'],tensors['gaps'],tensors['edge'] if m['arm']=='E' else None).cpu().numpy().astype(np.float64)
elif m['kind']=='R2':y=loaded[m['name']].predict(d['x'])
else:y=d['x'][:,0].copy()
seconds=y*d['t_set']
else:
assert R2_COLUMNS[0]=='Vf'
y=d['x'][:,R2_COLUMNS.index('Vf')].copy()
seconds=b0_seconds(y,d['t_set']) if m['kind']=='B0' else y*d['t_set']
if seconds.shape!=d['ids'].shape or not np.isfinite(seconds).all():raise ValueError('Invalid prediction '+m['name'])
p=prediction_path(root,m,row['case']);p.parent.mkdir(parents=True,exist_ok=True)
if p.exists():raise FileExistsError('No prediction overwrite/retry')
+7
View File
@@ -3,6 +3,7 @@ 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):
@@ -29,6 +30,11 @@ def test_rows(c, fold):
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)
@@ -40,6 +46,7 @@ def prediction_inputs(row):
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'])
+15 -1
View File
@@ -9,6 +9,8 @@ from freeze_g1_components import sha
from s2_custody_common import checked,prediction_inputs,verify_prediction_index
from s2_custody_statistics import six_region,kendall_pairs,last_location,part_metrics,component_values,contrast
from run_s2_custody_score import decode_gate
from s2_models.features import BASE_COLUMNS,R2_COLUMNS,base_features
from s2_models.objectives import b0_seconds
def roster():
@@ -91,12 +93,24 @@ class Acceptance(unittest.TestCase):
np.save(root/'base.npy',base);np.savez(root/'r2.npz',features=x)
np.savez(root/'queries.npz',eligible_s2=[True,False,True],anchor_ids=[0,1,2],raw_node_ids=[10,11,12],
multiplicity=[2,1,3],g=np.ones((3,2)),edge_summary=np.ones((3,4)),near_wall=[True,False,False])
(root/'result.json').write_text(json.dumps(dict(base_columns=list(BASE_COLUMNS),r2=dict(columns=list(R2_COLUMNS)))))
row=dict(nodes=3,preparation=dict(preparation_root=str(root),t_set=.4,
output_sha256={p.name:sha(p) for p in root.iterdir()}))
result_sha256=sha(root/'result.json'),output_sha256={p.name:sha(p) for p in root.iterdir()}))
original=np.load;paths=[]
def audit(path,**kwargs):paths.append(Path(path).name);return original(path,**kwargs)
with patch('s2_custody_common.np.load',side_effect=audit):d=prediction_inputs(row)
self.assertEqual(set(paths),{'base.npy','r2.npz','queries.npz'});np.testing.assert_array_equal(d['raw_ids'],[10,12])
metadata=json.loads((root/'result.json').read_text());metadata['r2']['columns'][0]='wrong'
(root/'result.json').write_text(json.dumps(metadata));row['preparation']['result_sha256']=sha(root/'result.json')
with self.assertRaises(ValueError):prediction_inputs(row)
def test_named_B0_units_and_centred_coordinates_over_Rg(self):
self.assertEqual(R2_COLUMNS[0],'Vf');np.testing.assert_array_equal(b0_seconds([0,.25,1],4),[0,1,4])
x=np.array([[2.,4.,6.],[6.,10.,14.]])
base=base_features(x,[1,2],[0,1],[2,4,6],[1,2,3],2,10,1,4)
self.assertEqual(BASE_COLUMNS[:3],('x_star','y_star','z_star'))
np.testing.assert_array_equal(base[:,:3],(x-[1,2,3])/2)
self.assertEqual(last_location([1,2],[2,1],[0,1],base[:,:3]),np.linalg.norm(x[1]-x[0])/2)
def test_frozen_real_metadata_denominators_no_label_read(self):
# Only immutable JSON membership/roster metadata, never geometry or targets.