forked from BNN-UPC/RouteNet-Fermi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze_predictions.py
More file actions
213 lines (179 loc) · 7.35 KB
/
Copy pathanalyze_predictions.py
File metadata and controls
213 lines (179 loc) · 7.35 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
#!/usr/bin/env python3
"""
RouteNet-Fermi Prediction Results Analyzer
This script analyzes the prediction results stored in NPY files generated by the predict.py script.
It provides statistics and visualizations of the model's performance on different traffic models.
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from pathlib import Path
import sys
def load_predictions(npy_file):
"""Load predictions from NPY file"""
try:
predictions = np.load(npy_file)
print(f"Loaded predictions from {npy_file}")
print(f"Shape: {predictions.shape}")
print(f"Data type: {predictions.dtype}")
return predictions
except Exception as e:
print(f"Error loading {npy_file}: {e}")
return None
def analyze_predictions(predictions, traffic_model):
"""Analyze prediction results"""
print(f"\n{'='*50}")
print(f"Analysis for {traffic_model}")
print(f"{'='*50}")
if predictions is None:
return
# Basic statistics
print(f"Total samples: {len(predictions)}")
print(f"Min value: {np.min(predictions):.6f}")
print(f"Max value: {np.max(predictions):.6f}")
print(f"Mean value: {np.mean(predictions):.6f}")
print(f"Median value: {np.median(predictions):.6f}")
print(f"Std deviation: {np.std(predictions):.6f}")
# Percentiles
percentiles = [1, 5, 10, 25, 50, 75, 90, 95, 99]
print(f"\nPercentiles:")
for p in percentiles:
value = np.percentile(predictions, p)
print(f" {p:2d}th percentile: {value:.6f}")
# Check for negative or zero values
negative_count = np.sum(predictions < 0)
zero_count = np.sum(predictions == 0)
positive_count = np.sum(predictions > 0)
print(f"\nValue distribution:")
print(f" Negative values: {negative_count} ({negative_count/len(predictions)*100:.2f}%)")
print(f" Zero values: {zero_count} ({zero_count/len(predictions)*100:.2f}%)")
print(f" Positive values: {positive_count} ({positive_count/len(predictions)*100:.2f}%)")
return predictions
def create_visualizations(predictions_dict):
"""Create visualizations for the predictions"""
fig, axes = plt.subplots(2, 2, figsize=(15, 12))
fig.suptitle('RouteNet-Fermi Prediction Results Analysis', fontsize=16)
# Histogram of predictions
ax1 = axes[0, 0]
for traffic_model, predictions in predictions_dict.items():
if predictions is not None:
ax1.hist(predictions, bins=50, alpha=0.7, label=traffic_model, density=True)
ax1.set_xlabel('Predicted Delay')
ax1.set_ylabel('Density')
ax1.set_title('Distribution of Predicted Delays')
ax1.legend()
ax1.set_yscale('log')
# Box plot
ax2 = axes[0, 1]
box_data = []
labels = []
for traffic_model, predictions in predictions_dict.items():
if predictions is not None:
box_data.append(predictions)
labels.append(traffic_model)
if box_data:
ax2.boxplot(box_data, labels=labels)
ax2.set_ylabel('Predicted Delay')
ax2.set_title('Box Plot of Predicted Delays')
ax2.tick_params(axis='x', rotation=45)
# Log-scale histogram
ax3 = axes[1, 0]
for traffic_model, predictions in predictions_dict.items():
if predictions is not None and np.all(predictions > 0):
ax3.hist(np.log10(predictions), bins=50, alpha=0.7, label=traffic_model, density=True)
ax3.set_xlabel('Log10(Predicted Delay)')
ax3.set_ylabel('Density')
ax3.set_title('Log-scale Distribution of Predicted Delays')
ax3.legend()
# Cumulative distribution
ax4 = axes[1, 1]
for traffic_model, predictions in predictions_dict.items():
if predictions is not None:
sorted_pred = np.sort(predictions)
cumulative = np.arange(1, len(sorted_pred) + 1) / len(sorted_pred)
ax4.plot(sorted_pred, cumulative, label=traffic_model, linewidth=2)
ax4.set_xlabel('Predicted Delay')
ax4.set_ylabel('Cumulative Probability')
ax4.set_title('Cumulative Distribution of Predicted Delays')
ax4.legend()
ax4.set_xscale('log')
plt.tight_layout()
plt.savefig('prediction_analysis.png', dpi=300, bbox_inches='tight')
print("\n✓ Visualization saved as 'prediction_analysis.png'")
plt.show()
def compare_traffic_models(predictions_dict):
"""Compare different traffic models"""
print(f"\n{'='*60}")
print(f"TRAFFIC MODEL COMPARISON")
print(f"{'='*60}")
comparison_data = []
for traffic_model, predictions in predictions_dict.items():
if predictions is not None:
stats = {
'Traffic Model': traffic_model,
'Count': len(predictions),
'Mean': np.mean(predictions),
'Median': np.median(predictions),
'Std': np.std(predictions),
'Min': np.min(predictions),
'Max': np.max(predictions),
'Q25': np.percentile(predictions, 25),
'Q75': np.percentile(predictions, 75)
}
comparison_data.append(stats)
if comparison_data:
df = pd.DataFrame(comparison_data)
print(df.to_string(index=False, float_format='%.6f'))
# Save to CSV
df.to_csv('prediction_comparison.csv', index=False)
print(f"\n✓ Comparison data saved as 'prediction_comparison.csv'")
def main():
# Find all prediction NPY files
delay_dir = Path('traffic_models/delay')
if not delay_dir.exists():
print(f"Error: Directory {delay_dir} not found")
sys.exit(1)
npy_files = list(delay_dir.glob('predictions_*.npy'))
if not npy_files:
print(f"No prediction NPY files found in {delay_dir}")
sys.exit(1)
print(f"Found {len(npy_files)} prediction files:")
for file in npy_files:
print(f" - {file.name}")
# Load and analyze each file
predictions_dict = {}
for npy_file in npy_files:
# Extract traffic model name from filename
filename = npy_file.stem
if 'constant_bitrate' in filename:
traffic_model = 'Constant Bitrate'
elif 'autocorrelated' in filename:
traffic_model = 'Autocorrelated'
elif 'onoff' in filename:
traffic_model = 'ON-OFF'
elif 'modulated' in filename:
traffic_model = 'Modulated'
else:
traffic_model = filename.replace('predictions_delay_', '').replace('_', ' ').title()
predictions = load_predictions(npy_file)
if predictions is not None:
predictions_dict[traffic_model] = predictions
analyze_predictions(predictions, traffic_model)
# Compare traffic models
if predictions_dict:
compare_traffic_models(predictions_dict)
# Create visualizations
try:
create_visualizations(predictions_dict)
except Exception as e:
print(f"Warning: Could not create visualizations: {e}")
print("This might be due to missing matplotlib or display issues")
print(f"\n{'='*60}")
print(f"ANALYSIS COMPLETE")
print(f"{'='*60}")
print(f"Results have been saved to:")
print(f" - prediction_comparison.csv")
print(f" - prediction_analysis.png (if matplotlib is available)")
if __name__ == "__main__":
main()