141 lines
7.7 KiB
Python
141 lines
7.7 KiB
Python
"""O1 scientific-v2 acceptance: weighting, quantization, eligibility and panel gates."""
|
|
import copy
|
|
import json
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
import numpy as np
|
|
from scipy.sparse import csr_matrix
|
|
|
|
from o1_partition import (quantize,canonical_mesh,graph,Metis,InputViolation,sha)
|
|
from o1_v2 import area_midrank,common_scale_quantize,face_weights,reweighted_graph,triple_match,feasibility
|
|
from o1_v2_prepare import prepare
|
|
from run_o1_v2 import validate_runtime_contract,panel_checkpoint
|
|
from test_o1_partition import box
|
|
from test_o1_preparation import fixture
|
|
|
|
|
|
def panel_fixture(eligible,strong):
|
|
parts=[dict(case=str(i),component_id='g'+str(i)) for i in range(60)]
|
|
result={p['case']:dict(status='PASS' if i<eligible else 'INELIGIBLE',diagnostics={
|
|
'T/G':{'interface':{'value':.2 if i<strong else 0.}},
|
|
'T/C':{'interface':{'value':.2 if i<strong else 0.}}}) for i,p in enumerate(parts)}
|
|
return parts,result,[p['case'] for p in parts]
|
|
|
|
|
|
class V2Acceptance(unittest.TestCase):
|
|
def test_area_midrank_ties_and_weighted_q90(self):
|
|
ranks,q=area_midrank([0,1,1,2],[1,2,1,6])
|
|
np.testing.assert_allclose(ranks,[.05,.25,.25,.7],rtol=0,atol=1e-15)
|
|
self.assertEqual(q,2)
|
|
self.assertEqual(area_midrank([1,2],[9,1])[1],1)
|
|
|
|
def test_constant_true_field_makes_all_integer_weights_identical(self):
|
|
mesh=canonical_mesh(*box());g=graph(mesh);z=np.full((len(mesh['tets']),3),.125)
|
|
w,d,_=face_weights(g,z,z[::-1])
|
|
self.assertEqual(d['g_p'],0)
|
|
np.testing.assert_array_equal(w['G'],w['T']);np.testing.assert_array_equal(w['G'],w['C'])
|
|
self.assertEqual(d['integer_equal_G_area_fraction'],{'T':1.,'C':1.})
|
|
|
|
def test_common_scale_is_exact_G_and_not_arm_renormalized(self):
|
|
area=np.array([1.,3.]);g,_=quantize(area)
|
|
np.testing.assert_array_equal(common_scale_quantize(area,area.sum()),g)
|
|
half=common_scale_quantize(.5*area,area.sum())
|
|
self.assertTrue(np.all(half<g))
|
|
self.assertFalse(np.array_equal(half,quantize(.5*area)[0]))
|
|
|
|
def test_shared_true_strength_and_larger_jump_cheaper(self):
|
|
g={'edges':np.array([[0,1],[1,2],[2,3]]),'area':np.array([1.,2.,3.])}
|
|
z=np.array([[0.,0,0],[.001,0,0],[.003,0,0],[.006,0,0]])
|
|
shuffled=z[[0,3,1,2]]
|
|
w,d,a=face_weights(g,z,shuffled)
|
|
self.assertAlmostEqual(d['g_p'],.003/.05)
|
|
np.testing.assert_allclose(a['factor_C'],np.exp(-np.log(4)*d['g_p']*a['shuffled_rank']**2),rtol=0,atol=0)
|
|
self.assertTrue(np.all(np.diff(a['factor_T'])<0))
|
|
self.assertTrue(np.all(a['factor_T']>=.25));self.assertTrue(np.all(a['factor_C']<=1))
|
|
|
|
def test_csr_symmetry_and_vwgt_unchanged(self):
|
|
mesh=canonical_mesh(*box());g=graph(mesh)
|
|
weights=np.arange(len(g['edges']),dtype=np.int32)+1
|
|
ag=reweighted_graph(g,weights)
|
|
a=csr_matrix((ag['adjwgt'],ag['adjncy'],ag['xadj']),shape=(len(mesh['tets']),)*2)
|
|
self.assertEqual((a-a.T).nnz,0)
|
|
for key in ['xadj','adjncy','vwgt']:np.testing.assert_array_equal(ag[key],g[key])
|
|
|
|
def test_max_min_shape_gate_symmetric_at_125(self):
|
|
base=dict(fractions=[.125]*8,cut_area=8.,diameters=[2.]*8)
|
|
metrics={'G':base,'T':dict(base,cut_area=10.,diameters=[2.5]*8),'C':base}
|
|
a=triple_match(metrics);self.assertTrue(a['passed']);self.assertFalse(a['old_five_percent_matching_pass'])
|
|
metrics['T']['cut_area']=10.000001
|
|
self.assertFalse(triple_match(metrics)['passed'])
|
|
|
|
def test_original_volume_pair_tolerance_retained(self):
|
|
base=dict(fractions=[.125]*8,cut_area=8.,diameters=[2.]*8)
|
|
changed=[.12]+[.125]*6+[.13]
|
|
self.assertFalse(triple_match({'G':base,'T':dict(base,fractions=changed),'C':base})['passed'])
|
|
|
|
def test_panel_exact_80_and_strict_70(self):
|
|
for eligible,strong,expected in [(48,34,True),(47,47,False),(50,35,False),(50,36,True),(0,0,False)]:
|
|
p,r,ids=panel_fixture(eligible,strong);v=feasibility(p,r,ids)
|
|
self.assertEqual(v['passed'],expected,(eligible,strong))
|
|
|
|
def test_component_equal_weight_prevents_large_group_dominance(self):
|
|
p,r,ids=panel_fixture(60,45)
|
|
for row in p[:40]:row['component_id']='large'
|
|
v=feasibility(p,r,ids)
|
|
self.assertTrue(v['part_strict_intervention_pass']);self.assertFalse(v['component_gate_pass'])
|
|
self.assertEqual(v['total_components'],21);self.assertEqual(v['strong_paired_components'],6)
|
|
|
|
def test_panel_component_requires_every_panel_member_eligible(self):
|
|
p,r,ids=panel_fixture(60,60);p[0]['component_id']=p[1]['component_id']='shared'
|
|
r['0']['status']='INELIGIBLE'
|
|
p.append(dict(case='outside',component_id='shared'))
|
|
v=feasibility(p,r,ids);g=next(x for x in v['component_rows'] if x['component_id']=='shared')
|
|
self.assertFalse(g['paired']);self.assertEqual(g['non_panel_cases'],['outside'])
|
|
|
|
def test_full_gate_keeps_209_original_denominator(self):
|
|
p,r,ids=panel_fixture(60,60)
|
|
with self.assertRaises(InputViolation):feasibility(p,r,ids,full=True)
|
|
|
|
def test_real_runtime_contract_schema_with_scientific_v2_identity(self):
|
|
c=json.loads(Path('artifacts/o1-preparation-continuation-2026-11-17/contract.json').read_text())
|
|
c['panel_indices']=c['dispatch'][:60]
|
|
c['scientific_spec_sha256']='632688217e59dd134922245e524ff1c0f63933be64600be21379f7aa74947a5f'
|
|
self.assertTrue(validate_runtime_contract(c))
|
|
c['panel_indices']=list(reversed(c['panel_indices']))
|
|
with self.assertRaises(InputViolation):validate_runtime_contract(c)
|
|
|
|
def test_panel_checkpoint_uses_all_60_not_success_only(self):
|
|
with tempfile.TemporaryDirectory() as name:
|
|
p,r,ids=panel_fixture(48,34);records=[]
|
|
for i,case in enumerate(ids):
|
|
path=Path(name)/(case+'.json');path.write_text(json.dumps(r[case]))
|
|
records.append(dict(index=i,case=case,worker=dict(status='PASS'),result_path=str(path),result_sha256=sha(path)))
|
|
c=dict(parts=p,panel_indices=list(range(60)))
|
|
v=panel_checkpoint(c,records);self.assertTrue(v['passed']);self.assertEqual(v['total_parts'],60)
|
|
with self.assertRaises(InputViolation):panel_checkpoint(c,records[:48])
|
|
|
|
def test_three_seed_zero_calls_and_no_labels_end_to_end(self):
|
|
with tempfile.TemporaryDirectory() as name:
|
|
root=Path(name);c,row,x,t=fixture(root);calls=[];original=Metis.partition;load=np.load
|
|
def partition(self,g,raw,seed):
|
|
calls.append(seed);return original(self,g,raw,seed)
|
|
def tracked(path,*args,**kwargs):
|
|
if any(x in str(path) for x in ['target.npy','seconds.npy','Fill_time']):raise AssertionError('No labels')
|
|
return load(path,*args,**kwargs)
|
|
before=sha(Path(row['v5_cache']['output'])/'features.npy')
|
|
with patch.object(Metis,'partition',partition),patch('numpy.load',side_effect=tracked):
|
|
self.assertEqual(prepare(root,0),0)
|
|
out=root/'parts'/row['case']/'output';result=json.loads((out/'result.json').read_text())
|
|
self.assertEqual(calls,[0,0,0]);self.assertEqual(result['partition_calls'],3)
|
|
self.assertEqual(result['status'],'PASS');self.assertEqual(result['weight_diagnostics']['g_p'],0)
|
|
self.assertFalse(result['label_cache_opened']);self.assertFalse(result['model_fit'])
|
|
for pair in ['T/G','T/C','G/C']:self.assertEqual(result['diagnostics'][pair]['interface']['value'],0)
|
|
self.assertEqual(sha(Path(row['v5_cache']['output'])/'features.npy'),before)
|
|
with self.assertRaises(FileExistsError):prepare(root,0)
|
|
|
|
|
|
if __name__=='__main__':unittest.main(verbosity=2)
|