STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_human_input_system.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""
3Test the proper Bayesian multivariate system with human inputs as ground truth
4"""
5
6import sys
7import os
8
9sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10
11import numpy as np
12import time
13import logging
14from scripts.channel_plotter import (
15 MultivariateBayesianCalibration,
16 CalibrationPoint,
17 EnvironmentalState,
18)
19
20# Setup logging
21logging.basicConfig(level=logging.INFO, format="%(levelname)s:%(name)s:%(message)s")
22logger = logging.getLogger(__name__)
23
24
26 """Test that human inputs are treated as ground truth"""
27 logger.info("=== Testing Human Input as Ground Truth ===")
28
29 # Create system
30 system = MultivariateBayesianCalibration(num_sensors=2, order=3)
31
32 # Test 1: Add human input (should have strong update)
33 logger.info("\n--- Test 1: Human Input (Ground Truth) ---")
34 human_point = CalibrationPoint(
35 voltage=0.5,
36 pressure=0.0,
37 timestamp=time.time(),
38 environmental_state=EnvironmentalState(),
39 uncertainty=0.01,
40 )
41 human_point.is_human_input = True # Mark as human input
42
43 system.multivariate_calibration_update(0, human_point)
44
45 logger.info(f"After 1 human input:")
46 logger.info(f" PT0 autonomy: {system.autonomy_levels[0]:.3f}")
47 logger.info(f" PT0 confidence: {system.confidence_scores[0]:.3f}")
48 logger.info(f" Consensus confidence: {system.consensus_confidence:.3f}")
49 logger.info(f" Population strength: {system.population_strength:.3f}")
50
51 # Test 2: Add more human inputs (should rapidly increase confidence)
52 logger.info("\n--- Test 2: Multiple Human Inputs ---")
53 for i, (v, p) in enumerate([(0.6, 50), (0.7, 100), (0.8, 200)]):
54 human_point = CalibrationPoint(
55 voltage=v,
56 pressure=p,
57 timestamp=time.time(),
58 environmental_state=EnvironmentalState(),
59 uncertainty=0.01,
60 )
61 human_point.is_human_input = True
62
63 system.multivariate_calibration_update(0, human_point)
64
65 logger.info(f"After {i+2} human inputs:")
66 logger.info(f" PT0 autonomy: {system.autonomy_levels[0]:.3f}")
67 logger.info(f" PT0 confidence: {system.confidence_scores[0]:.3f}")
68 logger.info(f" Consensus confidence: {system.consensus_confidence:.3f}")
69
70 # Test 3: Add consensus input (should have moderate update)
71 logger.info("\n--- Test 3: Consensus Input (Auto-detected) ---")
72 consensus_point = CalibrationPoint(
73 voltage=0.9,
74 pressure=300,
75 timestamp=time.time(),
76 environmental_state=EnvironmentalState(),
77 uncertainty=1.0, # Higher uncertainty
78 )
79 consensus_point.is_consensus = True # Mark as consensus
80
81 system.multivariate_calibration_update(0, consensus_point)
82
83 logger.info(f"After 1 consensus input:")
84 logger.info(f" PT0 autonomy: {system.autonomy_levels[0]:.3f}")
85 logger.info(f" PT0 confidence: {system.confidence_scores[0]:.3f}")
86 logger.info(f" Consensus confidence: {system.consensus_confidence:.3f}")
87
88 # Test 4: Test PT1 with human inputs (should benefit from population learning)
89 logger.info("\n--- Test 4: PT1 with Human Inputs (Population Learning) ---")
90 for i, (v, p) in enumerate([(0.52, 0), (0.62, 50), (0.72, 100)]):
91 human_point = CalibrationPoint(
92 voltage=v,
93 pressure=p,
94 timestamp=time.time(),
95 environmental_state=EnvironmentalState(),
96 uncertainty=0.01,
97 )
98 human_point.is_human_input = True
99
100 system.multivariate_calibration_update(1, human_point)
101
102 logger.info(f"After {i+1} human inputs on PT1:")
103 logger.info(f" PT1 autonomy: {system.autonomy_levels[1]:.3f}")
104 logger.info(f" PT1 confidence: {system.confidence_scores[1]:.3f}")
105 logger.info(f" Consensus confidence: {system.consensus_confidence:.3f}")
106 logger.info(f" Population strength: {system.population_strength:.3f}")
107
108 # Test 5: Check final confidence levels
109 logger.info("\n--- Final Results ---")
110 logger.info(
111 f"PT0 - Autonomy: {system.autonomy_levels[0]:.3f}, Confidence: {system.confidence_scores[0]:.3f}"
112 )
113 logger.info(
114 f"PT1 - Autonomy: {system.autonomy_levels[1]:.3f}, Confidence: {system.confidence_scores[1]:.3f}"
115 )
116 logger.info(f"System - Consensus Confidence: {system.consensus_confidence:.3f}")
117 logger.info(f"System - Population Strength: {system.population_strength:.3f}")
118
119 # Expected results:
120 # - Human inputs should rapidly increase confidence (40% per point)
121 # - Autonomy should grow significantly (50% per human point)
122 # - Consensus confidence should be high due to human inputs
123 # - Population strength should grow with each update
124
125 expected_pt0_autonomy = 4 * 0.5 # 4 human points * 50% each = 200% max = 100%
126 expected_pt0_confidence = 4 * 0.4 # 4 human points * 40% each = 160% max = 100%
127 expected_consensus = 7 * 0.15 # 7 total human points * 15% each = 105% max = 80%
128
129 logger.info(f"\nExpected vs Actual:")
130 logger.info(
131 f"PT0 Autonomy: Expected ~{expected_pt0_autonomy:.1f}, Actual {system.autonomy_levels[0]:.3f}"
132 )
133 logger.info(
134 f"PT0 Confidence: Expected ~{expected_pt0_confidence:.1f}, Actual {system.confidence_scores[0]:.3f}"
135 )
136 logger.info(
137 f"Consensus Confidence: Expected ~{expected_consensus:.1f}, Actual {system.consensus_confidence:.3f}"
138 )
139
140 # Check if results are reasonable (updated expectations)
141 success = True
142 if system.autonomy_levels[0] < 0.5:
143 logger.error(f"PT0 autonomy too low: {system.autonomy_levels[0]:.3f} < 0.5")
144 success = False
145 else:
146 logger.info(f"✅ PT0 autonomy good: {system.autonomy_levels[0]:.3f}")
147
148 if system.confidence_scores[0] < 0.5:
149 logger.error(f"PT0 confidence too low: {system.confidence_scores[0]:.3f} < 0.5")
150 success = False
151 else:
152 logger.info(f"✅ PT0 confidence good: {system.confidence_scores[0]:.3f}")
153
154 if system.consensus_confidence < 0.4:
155 logger.error(
156 f"Consensus confidence too low: {system.consensus_confidence:.3f} < 0.4"
157 )
158 success = False
159 else:
160 logger.info(f"✅ Consensus confidence good: {system.consensus_confidence:.3f}")
161
162 if system.population_strength < 0.8:
163 logger.error(
164 f"Population strength too low: {system.population_strength:.3f} < 0.8"
165 )
166 success = False
167 else:
168 logger.info(f"✅ Population strength good: {system.population_strength:.3f}")
169
170 if success:
171 logger.info(
172 "✅ All tests passed! Human inputs properly treated as ground truth."
173 )
174 else:
175 logger.error("❌ Tests failed! System not properly adapting to human inputs.")
176
177 return success
178
179
180if __name__ == "__main__":
TOML_NODISCARD_CTOR time() noexcept=default