-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize.py
More file actions
executable file
·154 lines (127 loc) · 5.48 KB
/
visualize.py
File metadata and controls
executable file
·154 lines (127 loc) · 5.48 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
#!/usr/bin/env python3
"""
Visualization script for Resonant Monolith coherence evolution.
This script parses log output from the Resonant Monolith and plots:
- Field gradient evolution (∇Ψ) over time
- Threshold adaptation (θ) over time
- Average resonance score evolution
- Absorption rate over time
Usage:
cargo run --release 2>&1 | tee monolith.log
python3 visualize.py monolith.log
"""
import re
import sys
import matplotlib.pyplot as plt
from datetime import datetime
def parse_log(filename):
"""Parse log file and extract metrics."""
timestamps = []
gradients = []
thresholds = []
resonances = []
absorption_rates = []
with open(filename, 'r') as f:
for line in f:
# Parse timestamp
timestamp_match = re.search(r'(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z)', line)
if not timestamp_match:
continue
timestamp_str = timestamp_match.group(1)
timestamp = datetime.fromisoformat(timestamp_str.replace('Z', '+00:00'))
# Parse metrics line
metrics_match = re.search(
r'Metrics: processed=(\d+) gradient=([\d.]+) threshold=([\d.]+) avg_resonance=([\d.]+)',
line
)
if metrics_match:
timestamps.append(timestamp)
gradients.append(float(metrics_match.group(2)))
thresholds.append(float(metrics_match.group(3)))
resonances.append(float(metrics_match.group(4)))
# Parse decision stats
stats_match = re.search(
r'absorption_rate=([\d.]+)%',
line
)
if stats_match:
absorption_rates.append(float(stats_match.group(1)))
return timestamps, gradients, thresholds, resonances, absorption_rates
def plot_evolution(timestamps, gradients, thresholds, resonances, absorption_rates):
"""Plot the evolution of system metrics."""
if not timestamps:
print("No data to plot")
return
# Convert timestamps to seconds from start
start_time = timestamps[0]
time_seconds = [(t - start_time).total_seconds() for t in timestamps]
# Create figure with subplots
fig, axs = plt.subplots(2, 2, figsize=(14, 10))
fig.suptitle('Resonant Monolith - Coherence Evolution', fontsize=16, fontweight='bold')
# Plot 1: Field Gradient (∇Ψ) - Most important for convergence
axs[0, 0].plot(time_seconds, gradients, 'b-', linewidth=2, label='∇Ψ')
axs[0, 0].axhline(y=0.001, color='r', linestyle='--', label='Convergence threshold')
axs[0, 0].set_xlabel('Time (seconds)')
axs[0, 0].set_ylabel('Gradient Magnitude')
axs[0, 0].set_title('Field Gradient Evolution: ∇Ψ → 0')
axs[0, 0].legend()
axs[0, 0].grid(True, alpha=0.3)
axs[0, 0].set_yscale('log')
# Plot 2: Threshold Adaptation (θ)
axs[0, 1].plot(time_seconds, thresholds, 'g-', linewidth=2, label='θ(t)')
axs[0, 1].set_xlabel('Time (seconds)')
axs[0, 1].set_ylabel('Threshold Value')
axs[0, 1].set_title('Adaptive Threshold: dθ/dt = -λ * ∂E/∂θ')
axs[0, 1].legend()
axs[0, 1].grid(True, alpha=0.3)
# Plot 3: Average Resonance Score
axs[1, 0].plot(time_seconds, resonances, 'm-', linewidth=2, label='⟨Ψ, S(T_in)⟩')
axs[1, 0].set_xlabel('Time (seconds)')
axs[1, 0].set_ylabel('Resonance Score')
axs[1, 0].set_title('Resonance Coupling Evolution')
axs[1, 0].legend()
axs[1, 0].grid(True, alpha=0.3)
# Plot 4: Absorption Rate
if absorption_rates:
time_seconds_stats = time_seconds[:len(absorption_rates)]
axs[1, 1].plot(time_seconds_stats, absorption_rates, 'r-', linewidth=2, label='Absorption %')
axs[1, 1].set_xlabel('Time (seconds)')
axs[1, 1].set_ylabel('Absorption Rate (%)')
axs[1, 1].set_title('Packet Decision Statistics')
axs[1, 1].legend()
axs[1, 1].grid(True, alpha=0.3)
plt.tight_layout()
# Save and show
output_file = 'resonant_monolith_evolution.png'
plt.savefig(output_file, dpi=300, bbox_inches='tight')
print(f"\n✓ Visualization saved to: {output_file}")
# Print convergence statistics
if gradients:
print(f"\n=== Convergence Statistics ===")
print(f"Initial gradient: {gradients[0]:.6f}")
print(f"Final gradient: {gradients[-1]:.6f}")
print(f"Convergence ratio: {gradients[-1]/gradients[0]:.4f}")
print(f"Time to converge: {time_seconds[-1]:.2f}s")
# Find when gradient first drops below threshold
for i, g in enumerate(gradients):
if g < 0.001:
print(f"First convergence at: {time_seconds[i]:.2f}s (packet ~{i*30})")
break
plt.show()
def main():
if len(sys.argv) != 2:
print("Usage: python3 visualize.py <log_file>")
print("\nExample:")
print(" cargo run --release 2>&1 | tee monolith.log")
print(" python3 visualize.py monolith.log")
sys.exit(1)
filename = sys.argv[1]
print(f"Parsing log file: {filename}")
timestamps, gradients, thresholds, resonances, absorption_rates = parse_log(filename)
if not timestamps:
print("No metrics found in log file")
sys.exit(1)
print(f"Found {len(timestamps)} metric snapshots")
plot_evolution(timestamps, gradients, thresholds, resonances, absorption_rates)
if __name__ == '__main__':
main()