STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_calibration_system.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""
3Test script for the multivariate Bayesian calibration system
4
5This script tests:
61. Calibration point addition
72. Consensus computation
83. Uncertainty evolution
94. UI updates
10"""
11
12import sys
13import os
14import time
15import logging
16
17# Add parent directory to path
18sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
19
20from scripts.channel_plotter import (
21 MultivariateBayesianCalibration,
22 CalibrationPoint,
23 EnvironmentalState,
24)
25
26logging.basicConfig(level=logging.INFO)
27logger = logging.getLogger(__name__)
28
29
31 """Test the calibration system with synthetic data"""
32
33 # Create global system
34 global_system = MultivariateBayesianCalibration(num_sensors=16, order=3)
35
36 # Test data - PT1 and PT3 measuring same pressure
37 test_pressures = [0.0, 50.0, 100.0, 200.0, 500.0, 800.0]
38
39 logger.info("=== Testing Calibration System ===")
40
41 # Simulate calibration points for PT1
42 logger.info("Adding calibration points for PT1...")
43 for i, pressure in enumerate(test_pressures):
44 voltage = 0.5 + pressure / 1000.0 # Simple linear relationship + noise
45
46 calibration_point = CalibrationPoint(
47 voltage=voltage,
48 pressure=pressure,
49 timestamp=time.time(),
50 environmental_state=EnvironmentalState(),
51 uncertainty=1.0, # 1 PSI uncertainty
52 )
53
54 global_system.multivariate_calibration_update(1, calibration_point)
55
56 logger.info(f" PT1: {pressure} PSI at {voltage:.3f}V")
57 logger.info(f" Population strength: {global_system.population_strength:.2f}")
58 logger.info(f" PT1 autonomy: {global_system.autonomy_levels[1]:.2f}")
59
60 # Simulate calibration points for PT3 (similar pressure range)
61 logger.info("\nAdding calibration points for PT3...")
62 for i, pressure in enumerate(test_pressures):
63 voltage = 0.52 + pressure / 1000.0 # Slightly different slope
64
65 calibration_point = CalibrationPoint(
66 voltage=voltage,
67 pressure=pressure,
68 timestamp=time.time(),
69 environmental_state=EnvironmentalState(),
70 uncertainty=1.0,
71 )
72
73 global_system.multivariate_calibration_update(3, calibration_point)
74
75 logger.info(f" PT3: {pressure} PSI at {voltage:.3f}V")
76 logger.info(f" Population strength: {global_system.population_strength:.2f}")
77 logger.info(f" PT3 autonomy: {global_system.autonomy_levels[3]:.2f}")
78
79 # Test consensus computation
80 logger.info("\n=== Testing Consensus ===")
81 voltages = {1: 0.8, 3: 0.82} # Both PTs at ~300 PSI
82 consensus = global_system.compute_consensus_pressure(voltages, EnvironmentalState())
83
84 logger.info(f"Consensus pressure: {consensus.pressure:.1f} PSI")
85 logger.info(f"Consensus uncertainty: {consensus.uncertainty:.2f} PSI")
86 logger.info(f"Agreement score: {consensus.agreement_score:.2f}")
87 logger.info(f"Consensus confidence: {global_system.consensus_confidence:.2f}")
88
89 # Test uncertainty inflation
90 logger.info("\n=== Testing Uncertainty Evolution ===")
91
92 # Add noisy calibration point (should inflate uncertainty)
93 noisy_point = CalibrationPoint(
94 voltage=0.8,
95 pressure=350.0, # Disagrees with consensus
96 timestamp=time.time(),
97 environmental_state=EnvironmentalState(),
98 uncertainty=1.0,
99 )
100
101 global_system.multivariate_calibration_update(1, noisy_point)
102
103 inflation_before = global_system.uncertainty_inflation_factor[1]
104 logger.info(f"PT1 uncertainty inflation after disagreement: {inflation_before:.2f}")
105
106 # Test quality history
107 logger.info("\n=== Testing Quality History ===")
108 quality_history = global_system.quality_history[1]
109 logger.info(f"PT1 quality records: {len(quality_history)}")
110
111 if quality_history:
112 latest = quality_history[-1]
113 logger.info(f"Latest RMSE: {latest['rmse']:.3f}")
114 logger.info(f"Latest MAE: {latest['mae']:.3f}")
115 logger.info(f"Latest autonomy: {latest['autonomy']:.2f}")
116
117 logger.info("\n=== Test Complete ===")
118
119 return global_system
120
121
122if __name__ == "__main__":
123 try:
125 print("\nāœ… All tests passed!")
126 except Exception as e:
127 print(f"\nāŒ Test failed: {e}")
128 import traceback
129
130 traceback.print_exc()
TOML_NODISCARD_CTOR time() noexcept=default