import json,tempfile,unittest from pathlib import Path from unittest.mock import patch import numpy as np import phase2_e2_geometry as e from phase2_e2_geometry_queue import cost_projection from test_o1_partition import box from o1_partition import canonical_mesh,graph,Metis,readout from phase2_h1_worker_v2 import sha class E2GeometryTests(unittest.TestCase): def test_one_invalid_arm_does_not_filter_valid_arms(self): mesh=canonical_mesh(*box());g=graph(mesh);labels,ret=Metis().partition(g,mesh['raw_cells'],0);n=len(mesh['points']);f=np.zeros((n,19));f[:,0]=mesh['points'][:,0]/8;f[:,9]=.5;values=f[:,[0,9]][mesh['tets']].mean(1);extra=readout(mesh['tets'],mesh['volume'],values,labels,n) with tempfile.TemporaryDirectory() as tmp: root=Path(tmp);dest=root/'parts/0000';dest.mkdir(parents=True);np.save(root/'G.npy',extra);np.savez(root/'G-partition.npz',labels=labels);(root/'G.json').write_text(json.dumps(dict(status='GEOMETRY_COMPLETE'))) row=dict(case='00000001_synthetic',component_id='s',nodes=n);c=dict(parts=[row],archive='not-read',O1_cache={},G_sources={row['case']:dict(result=str(root/'G.json'),result_sha256=sha(root/'G.json'),extra=str(root/'G.npy'),extra_sha256=sha(root/'G.npy'),partition=str(root/'G-partition.npz'),partition_sha256=sha(root/'G-partition.npz'))}) with patch.object(e,'source_geometry',return_value=(mesh,[])),patch.object(e,'input_geometry',return_value=(mesh['points'],mesh['tets'],mesh['raw_nodes'],{})),patch.object(e,'cached',side_effect=lambda row,key:f if key=='features' else np.ones(n)),patch.object(e,'Metis') as m: m.return_value.partition.side_effect=[(labels,ret),(np.zeros(len(labels),np.int32),ret)];e.field(root,c,0);self.assertEqual(m.return_value.partition.call_count,2) r=json.loads((dest/'result.json').read_text());self.assertTrue(r['arms']['G']['valid']);self.assertTrue(r['arms']['T']['valid']);self.assertFalse(r['arms']['C']['valid']);self.assertTrue(np.isnan(np.load(dest/'C/extra.npy')).all());np.testing.assert_array_equal(np.load(dest/'T/extra.npy'),extra);self.assertTrue(r['comparisons']['T-G']['bitwise_equal']) def test_cost_includes_all_ten_and_future_stages(self):self.assertEqual(cost_projection(160,10,100,200),1510) def test_invalid_cost_inputs_rejected(self): for a in [(50,10,100,200),(160,0,100,200),(160,101,100,200)]: with self.assertRaises(ValueError):cost_projection(*a) if __name__=='__main__':unittest.main()