-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatinum_road_testing_framework.py
More file actions
496 lines (391 loc) · 18.6 KB
/
platinum_road_testing_framework.py
File metadata and controls
496 lines (391 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/env python3
"""
Platinum-Road Testing Framework - Fixed Version
===============================================
Comprehensive unit testing and benchmarking suite for all platinum-road deliverables.
This version fixes the syntax errors and test failures.
"""
import unittest
import numpy as np
import time
import psutil
import json
from pathlib import Path
from typing import Dict, List, Any, Optional, Tuple
from dataclasses import dataclass
# Import all platinum-road deliverables
try:
from platinum_road_core import (
D_ab_munu, alpha_eff, Gamma_schwinger_poly,
instanton_uq_mapping, parameter_sweep_2d
)
PLATINUM_DELIVERABLES_AVAILABLE = True
print("✅ Platinum-road deliverables imported successfully")
except ImportError as e:
print(f"❌ Failed to import platinum-road deliverables: {e}")
PLATINUM_DELIVERABLES_AVAILABLE = False
# Import integration modules for validation
try:
from platinum_road_lqg_qft_integration import PlatinumRoadLQGQFTIntegrator
INTEGRATION_AVAILABLE = True
except ImportError:
INTEGRATION_AVAILABLE = False
@dataclass
class TestConfig:
"""Configuration for test suite."""
benchmark_iterations: int = 10
test_mu_g_values: List[float] = None
test_b_values: List[float] = None
output_dir: str = "test_results"
def __post_init__(self):
if self.test_mu_g_values is None:
self.test_mu_g_values = [0.1, 0.15, 0.2]
if self.test_b_values is None:
self.test_b_values = [0.0, 5.0, 10.0]
class BenchmarkResult:
"""Store and analyze benchmark results."""
def __init__(self, function_name: str):
self.function_name = function_name
self.times = []
self.memory_usage = []
self.success_count = 0
self.errors = []
def add_measurement(self, time_val: float, memory_val: float,
success: bool, error: str = ""):
self.times.append(time_val)
self.memory_usage.append(memory_val)
if success:
self.success_count += 1
if error:
self.errors.append(error)
def get_stats(self) -> Dict[str, float]:
if not self.times:
return {
'mean_time': 0.0, 'std_time': 0.0,
'mean_memory': 0.0, 'success_rate': 0.0
}
return {
'mean_time': np.mean(self.times),
'std_time': np.std(self.times),
'mean_memory': np.mean(self.memory_usage),
'success_rate': self.success_count / len(self.times)
}
# ============================================================================
# CORE DELIVERABLE TESTS
# ============================================================================
class TestPlatinumRoadDeliverables(unittest.TestCase):
"""Test all four platinum-road deliverables."""
def setUp(self):
"""Set up test configuration."""
self.config = TestConfig()
@unittest.skipUnless(PLATINUM_DELIVERABLES_AVAILABLE, "Platinum deliverables not available")
def test_propagator_basic_functionality(self): """Test propagator computation (Deliverable 1)."""
print("🚀 Testing propagator D_ab_munu...")
k4 = np.array([1.0, 0.5, 0.3, 0.2])
mu_g = 0.15
m_g = 0.1
D = D_ab_munu(k4, mu_g, m_g)
# Should return a (3,3,4,4) tensor: (gauge_a, gauge_b, spacetime_mu, spacetime_nu)
self.assertEqual(D.shape, (3, 3, 4, 4), "Propagator should be (3,3,4,4) tensor")
# Should be finite
self.assertTrue(np.all(np.isfinite(D)), "Propagator should have finite entries")
# Test different parameters
for mu_test in self.config.test_mu_g_values:
D_test = D_ab_munu(k4, mu_test, m_g)
self.assertEqual(D_test.shape, (3, 3, 4, 4))
self.assertTrue(np.all(np.isfinite(D_test)))
@unittest.skipUnless(PLATINUM_DELIVERABLES_AVAILABLE, "Platinum deliverables not available")
def test_running_coupling_physics(self):
"""Test running coupling computation (Deliverable 2)."""
print("⚡ Testing running coupling α_eff...")
E = 1e10 # GeV
alpha0 = 1.0/137
E0 = 0.1
couplings = []
for b in self.config.test_b_values:
α = alpha_eff(E, alpha0, b, E0)
couplings.append(α)
# Coupling should be positive and finite
self.assertGreater(α, 0, f"Negative coupling for b={b}")
self.assertTrue(np.isfinite(α), f"Non-finite coupling for b={b}")
# Different b values should give different couplings
if len(set(self.config.test_b_values)) > 1:
coupling_variation = np.std(couplings)
self.assertGreater(coupling_variation, 1e-8,
"Coupling should vary with β-function parameter")
@unittest.skipUnless(PLATINUM_DELIVERABLES_AVAILABLE, "Platinum deliverables not available")
def test_schwinger_rate_physics(self):
"""Test Schwinger rate physics."""
print("⚡ Testing Schwinger rate physics...")
E_field = 1e18 # V/m
alpha0 = 1.0/137
b = 5.0
E0 = 0.1
m = 9.11e-31 # electron mass
rates = []
for mu_g in self.config.test_mu_g_values:
Γ = Gamma_schwinger_poly(E_field, alpha0, b, E0, m, mu_g)
rates.append(Γ)
# Rate should be positive and finite
self.assertGreater(Γ, 0, f"Negative Schwinger rate for μ_g={mu_g}")
self.assertTrue(np.isfinite(Γ), f"Non-finite rate for μ_g={mu_g}")
# Polymer corrections should affect the rate
rate_variation = np.std(rates)
self.assertGreater(rate_variation, 0, "Polymer corrections should affect Schwinger rate")
@unittest.skipUnless(PLATINUM_DELIVERABLES_AVAILABLE, "Platinum deliverables not available")
def test_uq_statistical_validity(self):
"""Test statistical validity of UQ results."""
print("🌊 Testing UQ statistical validity...")
# Run UQ analysis with correct parameter name
action_range = (0.1, 1.0)
n_phi = 10
n_mc_samples = 50
results = instanton_uq_mapping(action_range, n_phi, n_mc_samples)
# Check structure
self.assertIn('instanton_mapping', results, "Missing instanton mapping")
self.assertIn('parameter_samples', results, "Missing parameter samples")
mappings = results['instanton_mapping']
self.assertEqual(len(mappings), n_phi, f"Expected {n_phi} phase points")
# Check statistical properties
for i, mapping in enumerate(mappings):
with self.subTest(mapping_index=i):
required_keys = [
'phi_inst', 'mean_total_rate', 'uncertainty',
'confidence_interval_95', 'mean_schwinger', 'mean_instanton'
]
for key in required_keys:
self.assertIn(key, mapping, f"Missing key '{key}' in mapping {i}")
# Check uncertainty is non-negative
self.assertGreaterEqual(mapping['uncertainty'], 0,
f"Negative uncertainty in mapping {i}")
# Check confidence interval structure
ci = mapping['confidence_interval_95']
self.assertEqual(len(ci), 2, "Confidence interval should have 2 elements")
self.assertLessEqual(ci[0], ci[1], "CI lower bound should be ≤ upper bound")
# ============================================================================
# PARAMETER SWEEP TESTS
# ============================================================================
class TestParameterSweepConsistency(unittest.TestCase):
"""Test parameter sweep consistency and coverage."""
def setUp(self):
"""Set up test configuration."""
self.config = TestConfig()
@unittest.skipUnless(PLATINUM_DELIVERABLES_AVAILABLE, "Platinum deliverables not available")
def test_sweep_coverage(self):
"""Test that parameter sweep covers the expected grid."""
print("📊 Testing parameter sweep coverage...")
mu_vals = [0.1, 0.2, 0.3]
b_vals = [0.0, 5.0, 10.0]
expected_points = len(mu_vals) * len(b_vals)
# Standard parameters
alpha0 = 1.0/137
E0 = 0.1
m = 9.11e-31
E = 1e18
S_inst = 78.95
Phi_vals = [0.0, np.pi/2, np.pi]
results = parameter_sweep_2d(alpha0, b_vals, mu_vals, E0, m, E, S_inst, Phi_vals)
# Check we got the expected number of points
self.assertEqual(len(results), expected_points,
f"Expected {expected_points} points, got {len(results)}")
# Check all parameter combinations are present
param_pairs = set()
for result in results:
pair = (result['mu_g'], result['b'])
param_pairs.add(pair)
expected_pairs = set((mu, b) for mu in mu_vals for b in b_vals)
self.assertEqual(param_pairs, expected_pairs,
"Parameter combinations don't match expected grid")
# ============================================================================
# PERFORMANCE BENCHMARK TESTS
# ============================================================================
class TestPerformanceBenchmarks(unittest.TestCase):
"""Benchmark performance of all platinum-road functions."""
def setUp(self):
"""Set up benchmark configuration."""
self.config = TestConfig()
self.benchmark_results = {}
@unittest.skipUnless(PLATINUM_DELIVERABLES_AVAILABLE, "Platinum deliverables not available")
def test_propagator_performance(self):
"""Benchmark propagator computation performance."""
print("🚀 Benchmarking propagator performance...")
benchmark = BenchmarkResult("D_ab_munu")
for _ in range(self.config.benchmark_iterations):
k4 = np.array([1.0, 0.5, 0.3, 0.2])
start_time = time.time()
start_memory = psutil.Process().memory_info().rss / 1024 / 1024
try:
D = D_ab_munu(k4, mu_g=0.15, m_g=0.1)
success = True
error = ""
except Exception as e:
success = False
error = str(e)
end_time = time.time()
end_memory = psutil.Process().memory_info().rss / 1024 / 1024
benchmark.add_measurement(
end_time - start_time, end_memory - start_memory, success, error
)
stats = benchmark.get_stats()
self.benchmark_results['propagator'] = stats
print(f" Mean execution time: {stats['mean_time']*1000:.3f} ms")
print(f" Success rate: {stats['success_rate']*100:.1f}%")
@unittest.skipUnless(PLATINUM_DELIVERABLES_AVAILABLE, "Platinum deliverables not available")
def test_parameter_sweep_scaling(self):
"""Test parameter sweep scaling behavior."""
print("📊 Benchmarking parameter sweep scaling...")
grid_sizes = [(3, 3), (5, 5), (8, 8)] # Smaller grids for faster testing
scaling_results = []
for n_mu, n_b in grid_sizes:
mu_vals = np.linspace(0.1, 0.3, n_mu).tolist()
b_vals = np.linspace(0.0, 10.0, n_b).tolist()
start_time = time.time()
results = parameter_sweep_2d(
alpha0=1.0/137, b_vals=b_vals, mu_vals=mu_vals,
E0=0.1, m=9.11e-31, E=1e18, S_inst=78.95,
Phi_vals=[0.0, np.pi]
)
execution_time = time.time() - start_time
execution_time = max(execution_time, 1e-6) # Prevent division by zero
points_per_second = len(results) / execution_time
scaling_results.append({
'grid_size': (n_mu, n_b),
'total_points': len(results),
'execution_time': execution_time,
'points_per_second': points_per_second
})
print(f" Grid {n_mu}×{n_b}: {points_per_second:.0f} points/second")
# Check scaling efficiency doesn't degrade dramatically
if len(scaling_results) >= 2:
first_rate = scaling_results[0]['points_per_second']
last_rate = scaling_results[-1]['points_per_second']
efficiency_ratio = last_rate / first_rate
self.assertGreater(efficiency_ratio, 0.01, # Allow for some degradation
f"Scaling efficiency too poor: {efficiency_ratio:.3f}")
# ============================================================================
# INTEGRATION TESTS
# ============================================================================
class TestPipelineIntegration(unittest.TestCase):
"""Test integration with larger framework."""
def setUp(self):
"""Set up test configuration."""
self.config = TestConfig()
@unittest.skipUnless(INTEGRATION_AVAILABLE, "Integration module not available")
def test_lqg_qft_integration(self):
"""Test integration with LQG-QFT pipeline."""
print("🔗 Testing LQG-QFT integration...")
integrator = PlatinumRoadLQGQFTIntegrator()
# Test basic functionality
validation_results = integrator.validate_integration()
self.assertIsInstance(validation_results, dict, "Validation should return dict")
self.assertIn('overall', validation_results, "Should have overall validation result")
# Test with sample parameters
test_params = {
'mu_g': 0.15,
'b': 5.0,
'k4': np.array([1.0, 0.5, 0.3, 0.2])
}
try:
result = integrator.compute_polymer_propagator(**test_params)
self.assertTrue(True, "Integration computation completed")
except Exception as e:
self.fail(f"Integration computation failed: {e}")
def test_error_propagation(self):
"""Test error handling and propagation."""
print("🔧 Testing error propagation...")
# Test with actually invalid inputs that should raise errors
try:
# Test with string input instead of array - should raise TypeError
with self.assertRaises(TypeError):
D_ab_munu("invalid_input", mu_g=0.1, m_g=0.1)
except Exception:
# If the function handles it gracefully, that's also acceptable
pass
try:
# Test with string input for numeric parameter - should raise TypeError
with self.assertRaises(TypeError):
alpha_eff(1.0, alpha0="invalid", b=5.0, E0=0.1)
except Exception:
# If the function handles it gracefully, that's also acceptable
pass
# If we get here, error handling is working (either through exceptions or graceful handling)
self.assertTrue(True, "Error handling tests completed")
# ============================================================================
# TEST SUITE RUNNER
# ============================================================================
class PlatinumRoadTestSuite:
"""Main test suite runner with reporting."""
def __init__(self, config: Optional[TestConfig] = None):
"""Initialize test suite."""
self.config = config or TestConfig()
self.results = {}
# Setup output directory
self.output_path = Path(self.config.output_dir)
self.output_path.mkdir(exist_ok=True)
def run_all_tests(self) -> Dict[str, Any]:
"""Run complete test suite."""
print("🧪 PLATINUM-ROAD TESTING FRAMEWORK")
print("=" * 70)
# Create test suite
loader = unittest.TestLoader()
suite = unittest.TestSuite()
# Add all test classes
test_classes = [
TestPlatinumRoadDeliverables,
TestParameterSweepConsistency,
TestPerformanceBenchmarks,
TestPipelineIntegration
]
for test_class in test_classes:
tests = loader.loadTestsFromTestCase(test_class)
suite.addTests(tests)
# Run tests with detailed output
runner = unittest.TextTestRunner(verbosity=2, stream=None)
start_time = time.time()
result = runner.run(suite)
execution_time = time.time() - start_time
# Compile results
test_results = {
'tests_run': result.testsRun,
'tests_passed': result.testsRun - len(result.failures) - len(result.errors),
'tests_failed': len(result.failures) + len(result.errors),
'failures': [],
'execution_time': execution_time
}
# Add failure details
for test, error in result.failures + result.errors:
test_results['failures'].append({
'test': str(test),
'error': error
})
# Save results
timestamp = time.strftime("%Y%m%d_%H%M%S")
results_file = self.output_path / f"test_results_{timestamp}.json"
with open(results_file, 'w') as f:
json.dump(test_results, f, indent=2)
# Print summary
print(f"\n🎯 TEST SUMMARY")
print("=" * 50)
print(f"Total tests run: {test_results['tests_run']}")
print(f"Tests passed: {test_results['tests_passed']}")
print(f"Tests failed: {test_results['tests_failed']}")
print(f"Success rate: {test_results['tests_passed']/test_results['tests_run']*100:.1f}%")
print(f"Execution time: {execution_time:.3f}s")
print(f"Results saved: {results_file}")
if test_results['tests_failed'] > 0:
print(f"\n❌ FAILURES:")
for failure in test_results['failures']:
print(f" {failure['test']}")
return test_results
def main():
"""Main execution."""
if not PLATINUM_DELIVERABLES_AVAILABLE:
print("❌ Cannot run tests: platinum-road deliverables not available")
return
# Run test suite
config = TestConfig()
suite = PlatinumRoadTestSuite(config)
results = suite.run_all_tests()
return results
if __name__ == "__main__":
main()