"""Synthetic checks for the labelled seven-part domain continuation.""" 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 s2_models.features import R2_COLUMNS from naive_ordering_math import volume_clock_curve import run_naive_domain_part as part import run_naive_domain_analysis as analysis import run_naive_domain_continuation as continuation import naive_domain_common as common class DomainChecks(unittest.TestCase): def test_cached_NaNs_preserved_without_any_archive_decode(self): self.cached_case(False) def test_domain_is_geometry_first_and_missing_tet_label_is_not_filtered(self): self.cached_case(True) def cached_case(self,missing_inside): 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,np.nan]);full[0]=np.nan if missing_inside else 0.;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','x') 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') cache=root/'original-full-cache.npz';np.savez_compressed(cache,seconds=full,retained_tet_raw_ids=np.arange(4)) c.update(continuation_indices=[0],continuation_inputs={'0':dict(label_path=str(cache),label_sha256=sha(cache),invalid_outside_tet=1)},domain_ruling='synthetic',scope_ruling='synthetic',archive='/does-not-exist') 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]): if missing_inside: with self.assertRaisesRegex(ValueError,'outside-tet NaN pattern'):part.run(root,0) return self.assertEqual(part.run(root,0),0) with self.assertRaises(FileExistsError):part.run(root,0) self.assertEqual(sha(root/'parts/part-000/output/full-node-labels.npz'),sha(cache)) r=read_json(root/'parts/part-000/output/result.json') self.assertIsNone(r['full_VTU_T_end']);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);self.assertEqual(r['decode_count'],0) curve=read_json(root/'parts/part-000/output/volume-clock-curve.json') self.assertEqual(curve['T_end_tet_domain'],.8);self.assertNotIn('full_VTU_end',curve) def test_canonical_mapping_scoring_uses_tet_end_and_preserves_order(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('{}');(root/'canonical-preparation.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_tet_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 test_exact_seven_join_reuses610_and_charges_prior_CPU(self): with tempfile.TemporaryDirectory() as temp: root=Path(temp)/'new';root.mkdir();old=Path(temp)/'old';old.mkdir() indices=[26,194,367,406,470,480,483];jobs=[];cases=[f'case-{i}' for i in range(617)] for i in range(617): passed=i not in indices;p=old/f'result-{i}.json' p.write_text(json.dumps(dict(case=cases[i],full_tet_T_end=1.,full_VTU_T_end=1.,output_sha256={}))) jobs.append(dict(index=i,worker=dict(status='PASS' if passed else 'STOPPED_OR_FAILED'),result_path=str(p) if passed else None,result_sha256=sha(p) if passed else None)) batch=old/'batch-complete.json';batch.write_text(json.dumps(dict(jobs=jobs,worker_CPU_s=641.9))) original_hash=sha(batch);audit=old/'domain.json';audit.write_text('{}') (root/'contract.json').write_text('{}');(root/'code-freeze.json').write_text('{}') c=dict(original_root=str(old),original_batch_sha256=original_hash,domain_audit=str(audit),continuation_indices=indices, supplemental_audit_CPU_s=2.4,cases=cases,limits=dict(part_cpu_seconds=180,part_wall_seconds=600,part_rss_bytes=4*2**30, part_disk_bytes=2*2**30,preparation_cpu_seconds=28800,disk_bytes=20*2**30)) attempted=[] def supervise(command,dest,name,limits,**kwargs): i=int(command[-1]);attempted.append(i);out=dest/'output';out.mkdir() (out/'result.json').write_text(json.dumps(dict(status='COMPLETE',case=cases[i],full_tet_T_end=1.,full_VTU_T_end=None,output_sha256={}))) return dict(status='PASS',cpu_seconds=1.,stop_reason=None) with patch.object(continuation,'contract',return_value=c),patch.object(continuation.os,'sched_setaffinity'),patch.object(continuation,'supervise',supervise),patch('builtins.print'): self.assertEqual(continuation.run(root),0) with self.assertRaises(FileExistsError):continuation.run(root) self.assertEqual(attempted,indices);self.assertEqual(sha(batch),original_hash) result=read_json(root/'batch-complete.json');self.assertAlmostEqual(result['worker_CPU_s'],651.3) join=read_json(root/'canonical-preparation.json');self.assertEqual(len(join['parts']),617) self.assertEqual(sum(p['domain_continuation'] for p in join['parts']),7) self.assertEqual(join['original_counts'],dict(PASS=610,failed=7)) self.assertEqual(len(common.prepared(root,c)),617) def test_scope_rejects_any_eighth_part(self): with tempfile.TemporaryDirectory() as temp: root=Path(temp);c=dict(source_sha256={},code_sha256={},continuation_indices=[26,194,367,406,470,480,483,500],scope_ruling='1794735162.417219') (root/'contract.json').write_text(json.dumps(c));(root/'code-freeze.json').write_text(json.dumps(dict(git_commit='synthetic',contract_sha256=sha(root/'contract.json')))) with self.assertRaises(ValueError):common.contract(root) if __name__=='__main__':unittest.main(verbosity=2)