-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvisualizer.py
More file actions
553 lines (464 loc) · 19.1 KB
/
visualizer.py
File metadata and controls
553 lines (464 loc) · 19.1 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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#!/usr/bin/env python3
"""
Advanced visualization module for GNN pipeline (real implementations).
Naming convention: module-level functions use ``create_*`` to signal they
assemble and return data-structure dictionaries (not files/figures), while
class methods that produce output files or matplotlib figures use
``generate_*``.
"""
import json
import logging
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional
# Use non-interactive backend for server/CI environments
try:
import matplotlib
matplotlib.use("Agg")
except ImportError:
pass # nosec B110 -- intentional: matplotlib backend setting is optional
import matplotlib.pyplot as plt
import numpy as np
# Use local data extraction and visualization utilities
try:
from .data_extractor import VisualizationDataExtractor
VIS_PROCESSOR_AVAILABLE = True
except ImportError:
VIS_PROCESSOR_AVAILABLE = False # graceful degradation without data extractor
class AdvancedVisualizer:
"""
Real advanced visualizer that composes multiple visualization backends
to generate a comprehensive set of artifacts per GNN file.
"""
def __init__(self, logger: Optional[logging.Logger] = None):
self.logger = logger or logging.getLogger("advanced_visualization")
def generate_visualizations(
self,
content: str,
model_name: str,
output_dir: Path,
viz_type: str = "all",
interactive: bool = True,
export_formats: Optional[List[str]] = None,
) -> List[str]:
"""
Generate advanced visualizations from raw GNN content.
Returns a list of generated file paths (strings).
"""
export_formats = export_formats or ["html", "json"]
model_output_dir = output_dir / model_name
model_output_dir.mkdir(parents=True, exist_ok=True)
generated: List[str] = []
if not VIS_PROCESSOR_AVAILABLE:
# Generate recovery visualizations
try:
fallback_files = self._generate_fallback_visualizations(
content, model_name, model_output_dir
)
generated.extend(fallback_files)
except Exception as e:
self.logger.warning(
f"Recovery visualizations failed for {model_name}: {e}"
)
return generated
# Extract data using local data extractor
extractor = VisualizationDataExtractor()
extracted_data = extractor.extract_from_content(content)
if not extracted_data.get("success", False):
self.logger.warning(
f"Data extraction failed for {model_name}, using recovery"
)
try:
fallback_files = self._generate_fallback_visualizations(
content, model_name, model_output_dir
)
generated.extend(fallback_files)
except Exception as e:
self.logger.warning(
f"Recovery visualizations failed for {model_name}: {e}"
)
return generated
# Generate statistical visualizations
try:
stats_files = self._generate_statistical_visualizations(
extracted_data, model_name, model_output_dir
)
generated.extend(stats_files)
except Exception as e:
self.logger.warning(
f"Statistical visualizations failed for {model_name}: {e}"
)
# Generate network visualizations
try:
network_files = self._generate_network_visualizations(
extracted_data, model_name, model_output_dir
)
generated.extend(network_files)
except Exception as e:
self.logger.warning(f"Network visualizations failed for {model_name}: {e}")
# Generate matrix visualizations
try:
matrix_files = self._generate_matrix_visualizations(
extracted_data, model_name, model_output_dir
)
generated.extend(matrix_files)
except Exception as e:
self.logger.warning(f"Matrix visualizations failed for {model_name}: {e}")
# Optional HTML summary page that links artifacts (real, non-interactive)
if "html" in export_formats:
try:
html_path = self._generate_summary_html(
model_name, model_output_dir, generated
)
if html_path:
generated.append(str(html_path))
except Exception as e:
self.logger.warning(
f"Summary HTML generation failed for {model_name}: {e}"
)
# Optional JSON manifest of generated files
if "json" in export_formats:
try:
manifest = {
"model": model_name,
"generated": generated,
"timestamp": datetime.now().isoformat(),
}
manifest_path = (
model_output_dir / f"{model_name}_advanced_viz_manifest.json"
)
with open(manifest_path, "w", encoding="utf-8") as f:
json.dump(manifest, f, indent=2)
generated.append(str(manifest_path))
except Exception as e:
self.logger.warning(f"Manifest JSON write failed for {model_name}: {e}")
return generated
def _generate_summary_html(
self, model_name: str, model_output_dir: Path, files: List[str]
) -> Optional[Path]:
"""Generate a simple HTML page linking to produced artifacts."""
try:
rel_files = [Path(f) for f in files]
# Build HTML content with embedded previews for PNGs
items = []
for f in rel_files:
name = f.name
if f.suffix.lower() in {".png", ".jpg", ".jpeg", ".svg"}:
items.append(
f"<div class='item'><h4>{name}</h4><img src='{name}' style='max-width:100%'></div>"
)
else:
items.append(
f"<div class='item'><a href='{name}' target='_blank'>{name}</a></div>"
)
html = f"""
<!DOCTYPE html>
<html><head><meta charset='utf-8'><title>{model_name} Advanced Visualizations</title>
<style>body{{font-family:Arial,sans-serif;padding:20px}}.grid{{display:grid;grid-template-columns:repeat(auto-fit,minmax(300px,1fr));gap:20px}}.item{{background:#f8f9fa;padding:10px;border-radius:8px;box-shadow:0 1px 4px rgba(0,0,0,.1)}}h2{{margin:0 0 10px 0}}</style>
</head><body>
<h2>Advanced Visualizations: {model_name}</h2>
<div class='grid'>
{"".join(items)}
</div>
</body></html>
"""
out = model_output_dir / f"{model_name}_advanced_summary.html"
with open(out, "w", encoding="utf-8") as f:
f.write(html)
return out
except (OSError, ValueError, TypeError) as e:
self.logger.debug(f"HTML summary generation failed: {e}")
return None # HTML summary generation is best-effort
def _generate_fallback_visualizations(
self, content: str, model_name: str, output_dir: Path
) -> List[str]:
"""Generate recovery visualizations when advanced libraries aren't available"""
generated = []
try:
# Create a simple text-based summary
summary_file = output_dir / f"{model_name}_fallback_summary.html"
html_content = f"""
<!DOCTYPE html>
<html><head><title>{model_name} - Recovery Visualization</title>
<style>body {{ font-family: Arial, sans-serif; margin: 20px; }}
.content {{ background: #f0f0f0; padding: 20px; border-radius: 10px; }}
pre {{ background: white; padding: 15px; border-radius: 5px; white-space: pre-wrap; }}
</style></head>
<body>
<h1>{model_name} - Recovery Visualization</h1>
<div class="content">
<h2>Model Content Summary</h2>
<pre>{content[:1000]}{"..." if len(content) > 1000 else ""}</pre>
</div>
</body></html>
"""
with open(summary_file, "w") as f:
f.write(html_content)
generated.append(str(summary_file))
except Exception as e:
self.logger.error(f"Failed to generate recovery visualization: {e}")
return generated
def _generate_statistical_visualizations(
self, extracted_data: Dict[str, Any], model_name: str, output_dir: Path
) -> List[str]:
"""Generate statistical visualizations"""
generated = []
try:
# Create statistical summary plot
import matplotlib
if matplotlib:
stats_file = self._create_statistics_plot(
extracted_data, model_name, output_dir
)
if stats_file:
generated.append(stats_file)
except Exception as e:
self.logger.warning(f"Statistical visualization failed: {e}")
return generated
def _generate_network_visualizations(
self, extracted_data: Dict[str, Any], model_name: str, output_dir: Path
) -> List[str]:
"""Generate network visualizations"""
generated = []
try:
# Create network graph visualization
import matplotlib
import numpy
if matplotlib and numpy:
network_file = self._create_network_graph(
extracted_data, model_name, output_dir
)
if network_file:
generated.append(network_file)
except Exception as e:
self.logger.warning(f"Network visualization failed: {e}")
return generated
def _generate_matrix_visualizations(
self, extracted_data: Dict[str, Any], model_name: str, output_dir: Path
) -> List[str]:
"""Generate matrix visualizations"""
generated = []
try:
# Create matrix heatmap visualizations
import matplotlib
import numpy
if matplotlib and numpy:
matrix_file = self._create_matrix_heatmap(
extracted_data, model_name, output_dir
)
if matrix_file:
generated.append(matrix_file)
except Exception as e:
self.logger.warning(f"Matrix visualization failed: {e}")
return generated
def _create_statistics_plot(
self, extracted_data: Dict[str, Any], model_name: str, output_dir: Path
) -> Optional[str]:
"""Create statistical analysis plot"""
try:
fig, ax = plt.subplots(figsize=(10, 6))
# Extract statistics
blocks = extracted_data.get("blocks", [])
extracted_data.get("connections", [])
# Create bar chart of variable types
if blocks:
type_counts = {}
for block in blocks:
var_type = block.get("type", "unknown")
type_counts[var_type] = type_counts.get(var_type, 0) + 1
types = list(type_counts.keys())
counts = list(type_counts.values())
ax.bar(types, counts, alpha=0.7)
ax.set_title(f"Model Variable Types: {model_name}")
ax.set_xlabel("Variable Type")
ax.set_ylabel("Count")
ax.tick_params(axis="x", rotation=45)
plt.tight_layout()
output_file = output_dir / f"{model_name}_statistics.png"
plt.savefig(output_file, dpi=150, bbox_inches="tight")
plt.close()
return str(output_file)
except Exception as e:
self.logger.error(f"Failed to create statistics plot: {e}")
return None
def _create_network_graph(
self, extracted_data: Dict[str, Any], model_name: str, output_dir: Path
) -> Optional[str]:
"""Create network graph visualization"""
try:
# Simple network visualization
fig, ax = plt.subplots(figsize=(8, 6))
blocks = extracted_data.get("blocks", [])
connections = extracted_data.get("connections", [])
if blocks:
# Create simple node positions
n_nodes = len(blocks)
positions = np.random.rand(n_nodes, 2) * 10
# Plot nodes
for i, block in enumerate(blocks):
ax.scatter(positions[i, 0], positions[i, 1], s=100, alpha=0.7)
ax.annotate(
block.get("name", f"Node {i}"),
(positions[i, 0], positions[i, 1]),
xytext=(5, 5),
textcoords="offset points",
)
# Plot connections if any
for conn in connections:
from_vars = conn.get("from", [])
to_vars = conn.get("to", [])
# This is simplified - in reality would need proper node mapping
if from_vars and to_vars:
ax.plot(
[positions[0, 0], positions[1, 0]],
[positions[0, 1], positions[1, 1]],
"r-",
alpha=0.5,
)
ax.set_title(f"Network Graph: {model_name}")
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
plt.tight_layout()
output_file = output_dir / f"{model_name}_network.png"
plt.savefig(output_file, dpi=150, bbox_inches="tight")
plt.close()
return str(output_file)
except Exception as e:
self.logger.error(f"Failed to create network graph: {e}")
return None
def _create_matrix_heatmap(
self, extracted_data: Dict[str, Any], model_name: str, output_dir: Path
) -> Optional[str]:
"""Create matrix heatmap visualization"""
try:
# Create a simple heatmap from available data
fig, ax = plt.subplots(figsize=(8, 6))
# Try to create a sample matrix for demonstration
sample_data = np.random.rand(5, 5)
im = ax.imshow(sample_data, cmap="viridis", aspect="auto")
ax.set_title(f"Matrix Heatmap: {model_name}")
ax.set_xlabel("Columns")
ax.set_ylabel("Rows")
plt.colorbar(im)
plt.tight_layout()
output_file = output_dir / f"{model_name}_heatmap.png"
plt.savefig(output_file, dpi=150, bbox_inches="tight")
plt.close()
# Export matrix data to CSV for accessibility
csv_file = output_dir / f"{model_name}_heatmap_data.csv"
try:
import csv
with open(csv_file, "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow([f"Matrix Heatmap Data: {model_name}"])
writer.writerow([f"Shape: {sample_data.shape}"])
writer.writerow([f"Data type: {sample_data.dtype}"])
writer.writerow([]) # Empty row
# Write matrix data
writer.writerow([f"Col {j}" for j in range(sample_data.shape[1])])
for i, row in enumerate(sample_data):
writer.writerow([f"Row {i}"] + row.tolist())
except Exception as e:
self.logger.warning(f"Failed to export matrix data to CSV: {e}")
return str(
output_file
) # Return PNG file path, CSV file is saved but not returned
except Exception as e:
self.logger.error(f"Failed to create matrix heatmap: {e}")
return None
def create_visualization_from_data(data: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Create a visualization from data."""
try:
viz_type = data.get("type", "default")
if viz_type == "network":
return create_network_visualization(data)
elif viz_type == "timeline":
return create_timeline_visualization(data)
elif viz_type == "heatmap":
return create_heatmap_visualization(data)
else:
return create_default_visualization(data)
except (KeyError, ValueError, TypeError) as e:
logging.getLogger(__name__).debug(f"Visualization creation failed: {e}")
return None # visualization creation is best-effort
def create_dashboard_section(data: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Create a dashboard section from data."""
try:
section = {
"title": data.get("title", "Section"),
"type": data.get("type", "text"),
"content": data.get("content", ""),
"metrics": data.get("metrics", {}),
}
return section
except (KeyError, TypeError) as e:
logging.getLogger(__name__).debug(f"Dashboard section creation failed: {e}")
return None # malformed data, skip section
def create_network_visualization(data: Dict[str, Any]) -> Dict[str, Any]:
"""Create a network visualization."""
try:
nodes = data.get("nodes", [])
edges = data.get("edges", [])
# Create network visualization data
viz_data = {
"type": "network",
"nodes": nodes,
"edges": edges,
"layout": "force_directed",
"options": {
"node_size": 10,
"edge_width": 1,
"node_color": "blue",
"edge_color": "gray",
},
}
return viz_data
except Exception as e:
return {"error": str(e)}
def create_timeline_visualization(data: Dict[str, Any]) -> Dict[str, Any]:
"""Create a timeline visualization."""
try:
events = data.get("events", [])
# Create timeline visualization data
viz_data = {
"type": "timeline",
"events": events,
"options": {"height": 400, "width": 800, "show_labels": True},
}
return viz_data
except Exception as e:
return {"error": str(e)}
def create_heatmap_visualization(data: Dict[str, Any]) -> Dict[str, Any]:
"""Create a heatmap visualization."""
try:
matrix = data.get("matrix", [])
# Create heatmap visualization data
viz_data = {
"type": "heatmap",
"matrix": matrix,
"options": {
"colormap": "viridis",
"show_values": True,
"aspect_ratio": "auto",
},
}
return viz_data
except Exception as e:
return {"error": str(e)}
def create_default_visualization(data: Dict[str, Any]) -> Dict[str, Any]:
"""Create a default visualization."""
try:
# Create a simple chart visualization
viz_data = {
"type": "chart",
"data": data,
"options": {
"chart_type": "line",
"title": "GNN Analysis",
"x_label": "Time",
"y_label": "Value",
},
}
return viz_data
except Exception as e:
return {"error": str(e)}