"""Synthetic data-role, moment and conservative-cost contract checks.""" import json from pathlib import Path import tempfile import unittest from unittest.mock import patch import numpy as np from phase2_h2_data import standardization,load_part,projection class Integration(unittest.TestCase): def test_history_only_peak_survives_latest_progress_regression(self): from s2_main_memory_guard import PeakRecords,VRAM_BYTES with tempfile.TemporaryDirectory() as temp: r=Path(temp);(r/'history').mkdir() (r/'history/0000.json').write_text(json.dumps(dict(status='FAILED',peak_reserved_bytes=VRAM_BYTES+1,peak_allocated_bytes=1))) (r/'progress.json').write_text(json.dumps(dict(peak_reserved_bytes=1,peak_allocated_bytes=1))) tracker=PeakRecords();self.assertTrue(tracker.scan(r)) self.assertTrue(tracker.witnesses['peak_reserved_bytes']['path'].endswith('history/0000.json')) def test_learning_only_welford_population(self): values={'a':np.arange(57,dtype=np.float64).reshape(3,19),'b':np.arange(95,dtype=np.float64).reshape(5,19)+.3} for x in values.values():x[:,2]=4 c=dict(folds=[dict(learning=['a','b'],learning_nodes=8)],parts={'a':dict(nodes=3),'b':dict(nodes=5)}) seen=[] def cache(c,case,name): self.assertEqual(name,'features');seen.append(case);return values[case] with tempfile.TemporaryDirectory() as t,patch('phase2_h2_data.cached',side_effect=cache): out=standardization(c,Path(t));allx=np.concatenate(list(values.values())) np.testing.assert_allclose(out['mean'],allx.mean(0));np.testing.assert_allclose(out['population_std'],allx.std(0),atol=1e-13) self.assertEqual(out['scale'][2],1.);self.assertEqual(seen,['a','b']);self.assertFalse(out['labels_read']) def test_test_role_refused_before_open(self): c=dict(folds=[dict(learning=['a'],validation=['b'])]) with patch('phase2_h2_data.cached',side_effect=AssertionError('must not read')): with self.assertRaises(PermissionError):load_part(c,'sealed',{},'test',Path('/nonexistent')) with self.assertRaises(PermissionError):load_part(c,'b',{},'learning',Path('/nonexistent')) def test_projection_all_five_folds_and_pilot_once(self): f=dict(fold=0,learning=['a']*2,validation=['b'],test=['c'],learning_nodes=20,learning_edges=100,validation_nodes=10,validation_edges=50,test_nodes=10,test_edges=50) c=dict(folds=[dict(f,fold=i) for i in range(5)],future_setup_reserve_seconds=300) p=projection(c,20,1,600);self.assertEqual(len(p['per_fold']),5) self.assertAlmostEqual(p['training_with_validation_seconds'],1.25*5*(200*20+20));self.assertAlmostEqual(p['total_gpu_seconds'],600+300+1.25*5*(4000+20+1)) self.assertTrue(p['fits_12h_training_2h_prediction_16h_total']);self.assertFalse(projection(c,100,1,600)['fits_12h_training_2h_prediction_16h_total']) def test_projection_uses_slowest_node_edge_part_scaling(self): f=dict(fold=0,learning=['a'],validation=['b'],test=['c'],learning_nodes=20,learning_edges=100,validation_nodes=10,validation_edges=50,test_nodes=10,test_edges=50) other=dict(f,fold=1,learning_nodes=40,learning_edges=400) p=projection(dict(folds=[f,other],future_setup_reserve_seconds=300),1,1,60) self.assertEqual(p['per_fold'][1]['learning_ratio'],4) if __name__=='__main__':unittest.main()