Files
MeltMind/tools/test_naive_ordering.py

191 lines
14 KiB
Python

"""Synthetic mathematical/custody/queue checks; no real dataset decode or solve."""
import io
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
from zipfile import ZipFile
import numpy as np
from freeze_g1_components import sha
from s2_custody_common import read_json
from naive_ordering_math import cdf_interval,pps_cdf,training_clock,squared_wall_distance,volume_clock_curve
import run_naive_ordering_part as part
import run_naive_ordering_queue as queue
import run_naive_ordering_analysis as analysis
from s2_models.features import R2_COLUMNS
class Checks(unittest.TestCase):
def test_CDF_strict_and_right_ties_mass_counted_once(self):
left,right=cdf_interval([3,1,3],[2,1,1])
np.testing.assert_array_equal(left,[.25,0,.25]);np.testing.assert_array_equal(right,[1,.25,1])
a,b=pps_cdf([3,1,3],[2,1,1]);np.testing.assert_array_equal(a,left)
expanded=np.repeat([3,1,3],[2,1,1]);el,_=pps_cdf(expanded,np.ones(4))
np.testing.assert_array_equal(el,[.25,.25,0,.25])
def test_invalid_PPS_or_empty_CDF_rejected(self):
for s,w in [([],[]),([1],[0]),([np.nan],[1]),([1],[np.inf])]:
with self.assertRaises(ValueError):cdf_interval(s,w)
with self.assertRaises(ValueError):pps_cdf([1,2],[.2,.8])
def test_conditional_CDF_differs_under_full_constant_flow(self):
full,_=cdf_interval([1,2,3,4],[1,1,1,1]);masked,_=pps_cdf([1,2],[1,1])
np.testing.assert_array_equal(full[:2],[0,.25]);np.testing.assert_array_equal(masked,[0,.5])
alpha=.5
self.assertTrue(np.all((full[:2]>=alpha*masked)&(full[:2]<=alpha*masked+1-alpha)))
# K/2000 alone is not total-cavity eligible mass when M+ is only0.4.
self.assertEqual(.4*1000/2000,.2)
def test_c_is_component_part_equal_learning_only(self):
def row(k,g,role):return dict(case=k,component_id=g,included=True,assignments=[dict(role=role,learning_pools=['100'])],preparation=dict(t_set=2.))
rows=[row('a','A','learning'),row('b','A','learning'),row('c','C','learning'),row('v','V','validation'),row('t','T','test')]
c,access=training_clock(rows,0,100,dict(a=2.,b=6.,c=10.))
self.assertEqual(c,3.5);self.assertEqual({r['case'] for r in access},set('abc'))
with self.assertRaises(KeyError):training_clock(rows,0,100,dict(a=2.,b=6.))
def test_Dw2_is_square_of_midpoint_not_mean_of_squares(self):
out,source,excluded=squared_wall_distance([[0,0,0],[1,0,0],[2,0,0]],[[0,1],[1,2]],[0,2,4],[0,0,0])
np.testing.assert_allclose(out,[0,1,1+1/9]);self.assertEqual(source,0);self.assertEqual(excluded,0)
with self.assertRaisesRegex(ValueError,'Unreachable'):
squared_wall_distance([[0,0,0],[1,0,0],[2,0,0]],[[0,1],[1,2]],[0,0,2],[0,0,0])
def test_order_and_final_ties_preserved(self):
s=np.array([-3.,1.,1.,9.]);ids=np.array([4,2,1,7]);left,_=pps_cdf(s,[1,2,3,1]);mapped=3.7*left
self.assertEqual(ids[s==s.max()].min(),ids[mapped==mapped.max()].min())
np.testing.assert_array_equal(np.sign(s[:,None]-s),np.sign(mapped[:,None]-mapped))
def test_curve_records_full_end_distinct_from_tet_max(self):
d=volume_clock_curve([0,.4,.8],[1,1,1],1.2,2.)
self.assertEqual(d['tet_end'],.8);self.assertEqual(d['full_VTU_end'],1.2)
self.assertEqual(d['seconds'][-1],.8);self.assertAlmostEqual(d['over_full_end'][-1],2/3)
def test_one_full_decode_cache_identity_and_no_duplicate_attempt(self):
with tempfile.TemporaryDirectory() as temp:
root=Path(temp);source=root/'source';source.mkdir();(root/'parts/part-000').mkdir(parents=True)
x=np.array([[0,0,0],[1,0,0],[0,1,0],[0,0,1.]])
edges=np.array([[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]])
np.savez(source/'geometry.npz',points_raw=x,edges=edges,node_volume_raw=np.ones(4),raw_node_ids=np.arange(4),gate_location_raw=x[0])
features=np.zeros((4,20));features[:,8]=[0,1,1,1]
np.savez(source/'r2.npz',features=features,Df=[0,2,2,2],Vf=[0,.25,.25,.25])
np.savez(source/'queries.npz',eligible_s2=[True]*3,anchor_ids=[1,2,3],raw_node_ids=[1,2,3],multiplicity=[1,2,1])
(source/'result.json').write_text(json.dumps(dict(r2=dict(columns=list(R2_COLUMNS)),draw_slots=4)))
full=np.array([0.,.2,.4,.8,1.2]);old=root/'old.npz';np.savez(old,seconds=full[[1,2,3]],raw_ids=[1,2,3],counts=[1,2,1])
labels=root/'labels.json';labels.write_text(json.dumps([dict(case='case',path=str(old),sha256=sha(old))]))
archive=root/'archive.zip'
with ZipFile(archive,'w') as z:z.writestr('case_sim.vtu','<VTKFile byte_order="LittleEndian" compressor="vtkZLibDataCompressor"><UnstructuredGrid><Piece NumberOfPoints="5"><PointData><DataArray Name="Fill_time">x</DataArray></PointData></Piece></UnstructuredGrid></VTKFile>')
st=archive.stat();(root/'contract.json').write_text('{}')
c=dict(archive=str(archive),archive_stat=dict(bytes=st.st_size,mtime_ns=st.st_mtime_ns,inode=st.st_ino),label_index=str(labels),label_index_sha256=sha(labels),latest_ruling='synthetic')
prep=dict(preparation_root=str(source),t_set=1.,output_sha256={n:sha(source/n) for n in ['geometry.npz','r2.npz','queries.npz']},result_sha256=sha(source/'result.json'))
row=dict(case='case',component_id='A',preparation=prep)
with patch.object(part,'contract',return_value=c),patch.object(part,'rows',return_value=[row]),patch.object(part,'decode_array',return_value=full) as decode:
self.assertEqual(part.run(root,0),0)
with self.assertRaises(FileExistsError):part.run(root,0)
self.assertEqual(decode.call_count,1)
r=read_json(root/'parts/part-000/output/result.json')
self.assertEqual(r['full_VTU_T_end'],1.2);self.assertEqual(r['full_tet_T_end'],.8);self.assertEqual(r['query_T_max'],.8)
self.assertEqual(r['baseline_Dw_Vf_max_abs_difference'],0.)
self.assertEqual(r['raw_nodes'],5);self.assertEqual(r['tet_nodes'],4)
def test_neural_route_keeps_three_seeds_and_original_missing_mask(self):
roster=[dict(case='a',component_id='A',included=True,nodes=10,exposure=dict(development_exposed=False),assignments=[dict(fold=0,role='test')])]
def row(arm,seed):return dict(case='a',arm=arm,pool=100,seed=seed,**{k:dict(value=.2,reason=None) for k in ['relative_l2','near_wall','kendall_tau_b','last_location']})
data=[row('R2',None),row('REORDER_N',0),row('REORDER_N',1)]
tables,contrasts,_=analysis.reports(data,roster,{'a'})
r=next(x for x in contrasts if x['subset']=='all' and x['pool']==100 and x['candidate']=='REORDER_N')
self.assertEqual(r['components'],0)
data.append(row('REORDER_N',2));_,contrasts,_=analysis.reports(data,roster,{'a'})
r=next(x for x in contrasts if x['subset']=='all' and x['pool']==100 and x['candidate']=='REORDER_N')
self.assertEqual(r['components'],1)
def test_synthetic_mapping_scoring_and_exact_manifest_end_to_end(self):
from s2_custody_common import prediction_path
from s2_custody_statistics import part_metrics
with tempfile.TemporaryDirectory() as temp:
root=Path(temp);old=root/'old';old.mkdir();case='00000001_case'
(root/'contract.json').write_text('{}');(root/'code-freeze.json').write_text('{}');(root/'batch-complete.json').write_text('{}')
s2=old/'contract.json';s2.write_text('{}')
row=dict(case=case,component_id='A',included=True,nodes=3,exposure=dict(development_exposed=False),
assignments=[dict(fold=f,role='test' if f==0 else 'learning',learning_pools=['25','50','100']) for f in range(5)])
roster=root/'roster.json';roster.write_text(json.dumps(dict(parts=[row])))
truth=np.array([1.,2.,4.]);raw=np.arange(3);counts=np.array([1,2,1]);left=np.array([0.,1/3,2/3]);right=left+1/3
d=dict(ids=raw,raw_ids=raw,counts=counts,t_set=4.,near_wall=np.array([True,False,True]),coordinates_over_Rg=np.array([[0,0,0],[1,0,0],[2,0,0]]))
prepared=root/'part';prepared.mkdir()
np.savez(prepared/'full-domain-maps.npz',raw_ids=raw,truth_left=left,truth_right=right,df1_left=left,df1_right=right,df2_left=left,df2_right=right)
np.savez(prepared/'full-node-labels.npz',seconds=truth)
(prepared/'volume-clock-curve.json').write_text(json.dumps(volume_clock_curve(truth,np.ones(3),4.,4.)))
models=[]
for pool in [25,50,100]:
for arm in ['R2','N','E']:
for seed in ([None] if arm=='R2' else [0,1,2]):
models.append(dict(fold=0,pool=pool,arm=arm,seed=seed,name=f'{arm}-{pool}-{seed}'))
models.append(dict(fold=0,pool=None,arm='B0',seed=None,name='B0'))
index={};scores=[]
for m in models:
pred=4*left if m['arm']=='B0' else np.array([.9,2.2,3.7])
p=prediction_path(old,m,case);p.parent.mkdir(parents=True,exist_ok=True);np.savez(p,seconds=pred,raw_ids=raw,counts=counts)
index[str(p)]=sha(p)
scores.append(dict(case=case,component_id='A',fold=0,pool=m['pool'],arm=m['arm'],seed=m['seed'],
**part_metrics(pred,truth,counts,d['near_wall'],raw,d['coordinates_over_Rg'],1)))
pi=old/'index.json';pi.write_text(json.dumps(index));sp=old/'scores.json';sp.write_text(json.dumps(scores))
lp=old/'labels.npz';np.savez(lp,seconds=truth,raw_ids=raw,counts=counts)
li=old/'labels.json';li.write_text(json.dumps([dict(case=case,path=str(lp),sha256=sha(lp))]))
ex=root/'exposure.json';ex.write_text(json.dumps([dict(parts=[case])]))
c=dict(roster=str(roster),S2_contract=str(s2),S2_contract_sha256=sha(s2),prediction_index=str(pi),prediction_index_sha256=sha(pi),S2_root=str(old),
original_part_scores=str(sp),original_part_scores_sha256=sha(sp),label_index=str(li),label_index_sha256=sha(li),exposure_membership=str(ex),exposure_membership_sha256=sha(ex))
prep={case:(dict(full_VTU_T_end=4.,estimated_alpha=.6),prepared)}
with patch.object(analysis,'contract',return_value=c),patch.object(analysis,'prepared',return_value=prep),patch.object(analysis,'rows',return_value=[row]),patch.object(analysis,'prediction_inputs',return_value=d),patch.object(analysis,'expected_models',return_value=models),patch.object(analysis,'training_clock',return_value=(1.1,[])):
self.assertEqual(analysis.make_maps(root),0)
self.assertEqual(analysis.score_maps(root),0)
result=read_json(root/'scores/result.json');self.assertEqual(result['prediction_rows'],39);self.assertEqual(result['reorder_invariant_rows'],24)
mapped=read_json(root/'prediction-index.json');mapped.pop(next(iter(mapped)))
with self.assertRaisesRegex(ValueError,'Missing/extra'):analysis.validate_manifest(c,mapped)
def simulated_queue(self,root,fail_first=False,missing=False):
c=dict(cases=['a','b','c'],coordinator_cpus=[28,29],limits=dict(workers=4,part_cpu_seconds=180,preparation_cpu_seconds=28800,disk_bytes=2**30))
(root/'contract.json').write_text('{}');(root/'code-freeze.json').write_text('{}');spawned=[]
class Process:
def __init__(self,index):self.index=index;self.pid=800000+index;self.polls=0
def poll(self):
self.polls+=1
if self.polls<2:return None
if missing:return 1
failed=fail_first and self.index==0;dest=root/'parts'/f'part-{self.index:03d}'
(dest/'guard-result.json').write_text(json.dumps(dict(index=self.index,case=c['cases'][self.index],worker=dict(status='STOPPED_OR_FAILED' if failed else 'PASS',cpu_seconds=.1),guard_CPU_s=.01,result_path=None,result_sha256=None)))
return 1 if failed else 0
def wait(self):self.polls=2;return self.poll()
def spawn(command,**kwargs):
i=int(command[-3]);p=Process(i);spawned.append(p);return p
with patch.object(queue,'contract',return_value=c),patch.object(queue.os,'sched_setaffinity'),patch.object(queue.subprocess,'Popen',spawn),patch.object(queue,'tree_usage',return_value=(0,1,1)),patch.object(queue.time,'sleep'),patch.object(queue,'terminate'),patch('builtins.print'):
result=queue.run(root)
return result,spawned
def test_independent_part_queue_once_only_and_no_redecode_restart(self):
with tempfile.TemporaryDirectory() as temp:
root=Path(temp);code,spawned=self.simulated_queue(root)
self.assertEqual(code,0);self.assertEqual(len(spawned),3)
self.assertEqual(read_json(root/'batch-complete.json')['passed'],3)
self.assertTrue(read_json(root/'first-part-cost-check.json')['continue_within_cap'])
with patch.object(queue,'contract',return_value=dict(cases=[],limits={})),patch.object(queue.subprocess,'Popen') as spawn:
with self.assertRaises(FileExistsError):queue.run(root)
spawn.assert_not_called()
def test_failed_first_part_and_infrastructure_receipt_stop(self):
for missing in [False,True]:
with tempfile.TemporaryDirectory() as temp:
root=Path(temp);code,spawned=self.simulated_queue(root,fail_first=not missing,missing=missing)
self.assertEqual(code,1);self.assertEqual(len(spawned),1)
r=read_json(root/'batch-complete.json');self.assertEqual(r['status'],'NAIVE_PREPARATION_INCOMPLETE')
if missing:self.assertFalse(r['accounting_complete'])
def test_global_monitor_failure_prevents_any_dispatch(self):
with tempfile.TemporaryDirectory() as temp:
root=Path(temp)
with patch.object(queue.Monitor,'sample',side_effect=lambda op,fn,previous:(previous,False)):
code,spawned=self.simulated_queue(root)
self.assertEqual(code,1);self.assertEqual(len(spawned),0)
self.assertEqual(read_json(root/'batch-complete.json')['stop_reason'],'GLOBAL_MONITOR_ACCOUNTING_ERROR')
if __name__=='__main__':unittest.main(verbosity=2)