Files
MeltMind/tools/test_o1_preparation.py

131 lines
7.0 KiB
Python

"""Synthetic mixed-VTU ingestion and complete 32-seed preparation acceptance."""
import base64
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
from xml.etree import ElementTree as ET
from zipfile import ZipFile
import zlib
import numpy as np
from test_o1_partition import box
from o1_partition import sha, InputViolation
from o1_prepare import prepare, source_geometry, load_inputs
from run_o1_prepare import cost_check
from gre24_features import COLUMNS
def save(path, value):
Path(path).write_text(json.dumps(value))
def array(parent, name, values, dtype, components=1):
a = np.asarray(values)
data = a.tobytes(); compressed = zlib.compress(data)
header = np.array([1,len(data),len(data),len(compressed)],dtype='<u4').tobytes()
e=ET.SubElement(parent,'DataArray',Name=name,type=dtype,format='binary',NumberOfComponents=str(components))
e.text=(base64.b64encode(header)+base64.b64encode(compressed)).decode()
def fixture(root):
x,t,_,_=box();case='00000003_synthetic_step_002'
tree=ET.Element('VTKFile',byte_order='LittleEndian',compressor='vtkZLibDataCompressor',header_type='UInt32')
piece=ET.SubElement(ET.SubElement(tree,'UnstructuredGrid'),'Piece',NumberOfPoints=str(len(x)),NumberOfCells=str(len(t)+1))
array(ET.SubElement(piece,'Points'),'Points',x.astype('<f8'),'Float64',3)
cells=ET.SubElement(piece,'Cells')
# A non-tet first cell forces actual VTU tet IDs to start at 1.
array(cells,'connectivity',np.r_[t[0,:3],t.ravel()].astype('<i8'),'Int64')
array(cells,'offsets',np.r_[3,3+4*np.arange(1,len(t)+1)].astype('<i8'),'Int64')
array(cells,'types',np.r_[5,np.full(len(t),10)].astype('u1'),'UInt8')
poison=ET.SubElement(ET.SubElement(piece,'PointData'),'DataArray',Name='Fill_time',type='Float64',format='binary')
poison.text='THIS MUST NEVER BE DECODED'
archive=root/'source.zip'
with ZipFile(archive,'w') as z:z.writestr(case+'_sim.vtu',ET.tostring(tree))
pr=root/'old';vr=root/'v5';pr.mkdir();vr.mkdir()
np.savez(pr/'geometry.npz',points_raw=x,tetra=t[::-1,::-1],raw_node_ids=np.arange(len(x)))
queries=np.array([1,3,8]);counts=np.array([4,2,1])
np.savez(pr/'queries.npz',eligible_s2=np.ones(3,dtype=bool),raw_node_ids=queries,multiplicity=counts)
features=np.zeros((len(x),19));features[:,0]=x[:,0]/8
np.save(vr/'features.npy',features);np.save(vr/'raw_ids.npy',np.arange(len(x)))
save(pr/'result.json',dict(case=case,status='PASS'))
save(vr/'result.json',dict(case=case,status='PASS',t_scale_seconds=.4,features=dict(columns=list(COLUMNS))))
row=dict(case=case,numeric_case=3,tets=len(t),nodes=len(x),component_id='synthetic',
preparation=dict(preparation_root=str(pr),t_set=.4,result_sha256=sha(pr/'result.json'),
output_sha256={p.name:sha(p) for p in pr.glob('*.npz')}),
v5_cache=dict(output=str(vr),t_scale_seconds=.4,result_sha256=sha(vr/'result.json'),
output_sha256={p.name:sha(p) for p in vr.glob('*.npy')}))
st=archive.stat();c=dict(parts=[row],archive=str(archive),archive_stat=dict(bytes=st.st_size,mtime_ns=st.st_mtime_ns,inode=st.st_ino),
source_sha256={},code_sha256={})
save(root/'contract.json',c)
return c,row,x,t
class PreparationAcceptance(unittest.TestCase):
def setUp(self):
self.temp=tempfile.TemporaryDirectory();self.root=Path(self.temp.name)
self.c,self.row,self.x,self.t=fixture(self.root)
def tearDown(self):self.temp.cleanup()
def test_mixed_cell_ids_and_no_pointdata_decode(self):
mesh,decoded=source_geometry(self.c['archive'],self.row['case'])
np.testing.assert_array_equal(mesh['raw_cells'],np.arange(1,len(self.t)+1))
self.assertEqual(decoded,['Points','connectivity','offsets','types'])
np.testing.assert_array_equal(mesh['points'],self.x)
def test_field_section_before_geometry_refused(self):
p=self.root/'bad.zip'
xml='<VTKFile byte_order="LittleEndian" compressor="vtkZLibDataCompressor"><PointData/></VTKFile>'
with ZipFile(p,'w') as z:z.writestr(self.row['case']+'_sim.vtu',xml)
with self.assertRaises(InputViolation):source_geometry(p,self.row['case'])
def test_full_32_seed_prepare_without_any_labels_or_model(self):
before=sha(Path(self.row['v5_cache']['output'])/'features.npy')
original=np.load;opened=[]
def tracked(path,*a,**kw):
opened.append(str(path))
if any(name in str(path) for name in ['target.npy','seconds.npy','label-index']):
raise AssertionError('Label read forbidden')
return original(path,*a,**kw)
with patch('numpy.load',side_effect=tracked):
self.assertEqual(prepare(self.root,0),0)
dest=self.root/'parts'/self.row['case']/'output'
result=json.loads((dest/'result.json').read_text())
self.assertEqual(result['status'],'PASS');self.assertEqual(result['candidate_calls'],32)
self.assertFalse(result['model_fit']);self.assertFalse(result['label_cache_opened'])
self.assertEqual(result['labels_decoded'],[])
self.assertEqual(sha(Path(self.row['v5_cache']['output'])/'features.npy'),before)
self.assertEqual(len(list(dest.glob('partition-*.npz'))),32)
self.assertEqual(result['seeds'],dict(G=0,T=0,C=0))
with self.assertRaises(FileExistsError):prepare(self.root,0)
def test_frozen_feature_mutation_is_global_input_failure(self):
p=Path(self.row['v5_cache']['output'])/'features.npy'
with p.open('ab') as f:f.write(b'changed')
self.assertEqual(prepare(self.root,0),1)
failure=json.loads((self.root/'parts'/self.row['case']/'output/failure.json').read_text())
self.assertTrue(failure['global_stop']);self.assertEqual(failure['type'],'InputViolation')
def test_input_alignment_preserves_queries_and_whole_features(self):
mesh,f,q,provenance=load_inputs(self.c,self.row)
np.testing.assert_array_equal(q['raw_ids'],[1,3,8]);np.testing.assert_array_equal(q['counts'],[4,2,1])
np.testing.assert_array_equal(f[:,0],self.x[:,0]/8)
self.assertEqual(provenance['labels_decoded'],[])
def test_cost_checkpoint_no_complete_library_is_not_extrapolated(self):
p=self.root/'measurement.json';save(p,dict(status='INELIGIBLE',candidate_calls=1))
c=dict(first_ten=[0],dispatch=[0,1],parts=[{'tets':100},{'tets':1000}],
initial_preparation_cpu_seconds=60,limits=dict(preparation_cpu_seconds=43200))
records=[dict(index=0,case='synthetic',result_path=str(p),result_sha256=sha(p),worker=dict(status='PASS',cpu_seconds=5))]
self.assertFalse(cost_check(c,records)['passed'])
save(p,dict(status='PASS',candidate_calls=32));records[0]['result_sha256']=sha(p)
result=cost_check(c,records);self.assertTrue(result['passed'])
self.assertEqual(result['total_projected_cpu_seconds'],115)
c['limits']['preparation_cpu_seconds']=100
self.assertFalse(cost_check(c,records)['passed'])
if __name__=='__main__':unittest.main(verbosity=2)