File size: 19,372 Bytes
8e874f5 | 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 | #!/usr/bin/env python3
"""
Enhanced Interactive Knowledge Graph Visualizer Module for QAFD_RAG
Creates an HTML file with interactive graph showing all nodes and relationships
with improved color schemes for different components
This module is designed to be imported by the main QAFD_RAG script.
"""
import networkx as nx
from pyvis.network import Network
import json
from pathlib import Path
def create_interactive_kg_visualization(graphml_path, output_html="knowledge_graph_interactive.html"):
"""
Create an interactive HTML visualization of the knowledge graph with enhanced colors
Args:
graphml_path (str): Path to the GraphML file
output_html (str): Output HTML file path
Returns:
bool: True if successful, False otherwise
"""
print(f"Loading graph from: {graphml_path}")
# Load the GraphML file
try:
G = nx.read_graphml(graphml_path)
print(f"β
Graph loaded successfully!")
print(f" Nodes: {G.number_of_nodes()}")
print(f" Edges: {G.number_of_edges()}")
# ADDED: Debug edges
if G.number_of_edges() > 0:
print("Sample edges:")
for i, (source, target, data) in enumerate(G.edges(data=True)):
if i < 3: # Show first 3 edges
print(f" {source} -> {target}: {data}")
else:
print("WARNING: No edges found in GraphML file!")
except Exception as e:
print(f"β Error loading graph: {e}")
return False
# Create Pyvis network with enhanced styling
net = Network(
height="900px",
width="100%",
bgcolor="#0a0a0a", # Darker background for better contrast
font_color="white",
directed=False,
notebook=False
)
# Enhanced color schemes
node_colors = {
'entity': {
'color': '#FF4757', # Bright red for entities
'border': '#FF3742',
'highlight': '#FF6B7A'
},
'chunk': {
'color': '#2ED573', # Bright green for chunks
'border': '#20BF6B',
'highlight': '#54E091'
},
'table': {
'color': '#3742FA', # Electric blue for tables
'border': '#2F3542',
'highlight': '#5352ED'
},
'column': {
'color': '#FFA726', # Orange for columns
'border': '#FF9800',
'highlight': '#FFB74D'
},
'complete_table': { # ADDED: Handle complete_table type
'color': '#3742FA', # Same as table
'border': '#2F3542',
'highlight': '#5352ED'
},
'default': {
'color': '#A4B0BE', # Light gray for others
'border': '#747D8C',
'highlight': '#DDD6FE'
}
}
# Edge color schemes
edge_colors = {
'primary_key': '#FF6B35', # Orange-red for primary key edges
'foreign_key': '#4834D4', # Purple for foreign key edges
'contains': '#00D2D3', # Cyan for containment relationships
'belongs_to': '#FF9FF3', # Pink for belongs_to relationships
'references': '#54A0FF', # Light blue for references
'has': '#5F27CD', # Deep purple for has relationships
'table_structure': '#00D2D3', # Same as contains
'default': '#57606F' # Gray for standard edges
}
print("Adding nodes with enhanced styling...")
# First, let's analyze what node types we actually have
actual_node_types = set()
entity_types = set()
for node_id, node_data in G.nodes(data=True):
node_type = node_data.get('type', 'unknown')
entity_type = node_data.get('entity_type', 'unknown')
actual_node_types.add(node_type)
entity_types.add(entity_type)
print(f"Detected 'type' attributes: {actual_node_types}")
print(f"Detected 'entity_type' attributes: {entity_types}")
for node_id, node_data in G.nodes(data=True):
# Read the entity_type directly from the data
entity_type = node_data.get('entity_type', '').lower()
node_type = node_data.get('type', '').lower()
# Use entity_type as the primary classifier
if entity_type in ['table', 'complete_table']:
detected_type = 'table'
elif entity_type == 'column':
detected_type = 'column'
elif entity_type in ['entity', 'chunk']:
detected_type = entity_type
elif node_type in ['table', 'column', 'entity', 'chunk']:
detected_type = node_type
else:
detected_type = 'default'
color_scheme = node_colors.get(detected_type, node_colors['default'])
# Create detailed hover info
title = f"<b>Node ID:</b> {node_id}<br>"
# title += f"<b>Type:</b> {detected_type.title()}<br>"
# Show entity_type if it exists
if 'entity_type' in node_data:
title += f"<b>Entity Type:</b> {node_data['entity_type']}<br>"
# Add all other attributes
for key, value in node_data.items():
if key not in ['type', 'entity_type']:
# Truncate long values for readability
display_value = str(value)[:100] + "..." if len(str(value)) > 100 else str(value)
title += f"<b>{key.title()}:</b> {display_value}<br>"
# Determine node size based on detected type
size_mapping = {
'table': 35, # Largest for tables
'column': 25, # Medium for columns
'entity': 30, # Large for entities
'chunk': 20, # Smaller for chunks
'default': 18 # Smallest for others
}
# Add node with enhanced styling
net.add_node(
str(node_id),
label=str(node_id)[:25] + ("..." if len(str(node_id)) > 25 else ""),
title=title,
color={
'background': color_scheme['color'],
'border': color_scheme['border'],
'highlight': {
'background': color_scheme['highlight'],
'border': color_scheme['border']
}
},
size=size_mapping.get(detected_type, size_mapping['default']),
font={'size': 14, 'color': 'white', 'face': 'arial'},
borderWidth=3,
shadow={'enabled': True, 'color': 'rgba(0,0,0,0.5)', 'size': 10}
)
print("Adding edges with relationship-based coloring...")
# First, analyze actual edge types
actual_edge_types = set()
for source, target, edge_data in G.edges(data=True):
# Try multiple ways to get relationship info
relationship = (
edge_data.get('relationship') or
edge_data.get('description') or
edge_data.get('keywords') or
edge_data.get('label') or
'default'
)
actual_edge_types.add(str(relationship).lower())
print(f"Detected edge types: {actual_edge_types}")
edges_added = 0
for source, target, edge_data in G.edges(data=True):
# Try to get meaningful relationship info
relationship = (
edge_data.get('relationship') or
edge_data.get('description') or
edge_data.get('keywords') or
edge_data.get('label') or
'default'
)
# Convert to string and normalize
relationship_str = str(relationship).lower()
# Determine edge color and width based on relationship type
edge_color = edge_colors['default']
edge_width = 2
# Enhanced relationship detection with keyword matching
if 'foreign_key' in relationship_str or 'references' in relationship_str:
edge_color = edge_colors['foreign_key']
edge_width = 3
elif 'primary_key' in relationship_str:
edge_color = edge_colors['primary_key']
edge_width = 4
elif 'contains' in relationship_str or 'table_structure' in relationship_str:
edge_color = edge_colors['contains']
edge_width = 3
elif 'belongs_to' in relationship_str:
edge_color = edge_colors['belongs_to']
edge_width = 2
elif 'has' in relationship_str:
edge_color = edge_colors['has']
edge_width = 2
else:
# For any other specific relationships
edge_color = '#FFD93D' # Bright yellow for other relationships
edge_width = 2
# Create detailed edge title
title = f"<b>Relationship:</b> {relationship}<br>"
title += f"<b>From:</b> {source}<br>"
title += f"<b>To:</b> {target}<br>"
# Add other edge attributes
for key, value in edge_data.items():
if key not in ['relationship', 'label', 'description', 'keywords']:
title += f"<b>{key.title()}:</b> {str(value)}<br>"
# Add edge with enhanced styling
net.add_edge(
str(source),
str(target),
label=str(relationship)[:25] if relationship != 'default' else '', # Show label only if meaningful
title=title,
color={
'color': edge_color,
'highlight': edge_color,
'hover': edge_color,
'opacity': 0.8
},
width=edge_width,
font={'size': 11, 'color': 'white'}
)
edges_added += 1
print(f"Successfully added {edges_added} edges to visualization")
# Enhanced physics and layout configuration
net.set_options("""
var options = {
"physics": {
"enabled": true,
"stabilization": {
"iterations": 300,
"updateInterval": 25
},
"barnesHut": {
"gravitationalConstant": -2000,
"centralGravity": 0.2,
"springLength": 120,
"springConstant": 0.05,
"damping": 0.15,
"avoidOverlap": 0.2
}
},
"nodes": {
"font": {
"size": 14,
"color": "white",
"face": "arial",
"strokeWidth": 2,
"strokeColor": "black"
},
"borderWidth": 3,
"shadow": {
"enabled": true,
"color": "rgba(0,0,0,0.5)",
"size": 10
},
"chosen": {
"node": true,
"label": true
}
},
"edges": {
"font": {
"size": 11,
"color": "white",
"face": "arial",
"strokeWidth": 1,
"strokeColor": "black"
},
"smooth": {
"type": "continuous",
"roundness": 0.5
},
"shadow": {
"enabled": true,
"color": "rgba(0,0,0,0.3)"
},
"chosen": {
"edge": true,
"label": true
}
},
"interaction": {
"hover": true,
"tooltipDelay": 200,
"hideEdgesOnDrag": false,
"hideNodesOnDrag": false
},
"layout": {
"improvedLayout": true,
"hierarchical": false
}
}
""")
# Generate and save the HTML
try:
print("Generating enhanced HTML visualization...")
html_string = net.generate_html()
# Add custom CSS for better styling
custom_css = """
<style>
body {
margin: 0;
padding: 20px;
background: linear-gradient(135deg, #0c0c0c 0%, #1a1a1a 100%);
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
#mynetworkid {
border: 2px solid #333;
border-radius: 10px;
box-shadow: 0 8px 32px rgba(0,0,0,0.3);
}
.legend {
position: fixed;
top: 20px;
right: 20px;
background: rgba(26, 26, 26, 0.9);
padding: 15px;
border-radius: 8px;
border: 1px solid #444;
color: white;
font-size: 12px;
max-width: 200px;
z-index: 1000;
}
.legend-item {
display: flex;
align-items: center;
margin-bottom: 8px;
}
.legend-color {
width: 16px;
height: 16px;
border-radius: 50%;
margin-right: 10px;
border: 2px solid rgba(255,255,255,0.3);
}
</style>
"""
# Add legend HTML with correct colors
legend_html = """
<div class="legend">
<h4 style="margin-top: 0; color: #fff;">Node Types</h4>
<div class="legend-item">
<div class="legend-color" style="background-color: #3742FA;"></div>
<span>Tables</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #FFA726;"></div>
<span>Columns</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #FF4757;"></div>
<span>Entities</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #2ED573;"></div>
<span>Chunks</span>
</div>
<h4 style="color: #fff; margin-bottom: 5px;">Edge Types</h4>
<div class="legend-item">
<div class="legend-color" style="background-color: #57606F; border-radius: 2px;"></div>
<span>Default</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #FF6B35; border-radius: 2px;"></div>
<span>Primary Key</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #4834D4; border-radius: 2px;"></div>
<span>Foreign Key</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #00D2D3; border-radius: 2px;"></div>
<span>Contains</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #FFD93D; border-radius: 2px;"></div>
<span>Other</span>
</div>
</div>
"""
# Insert custom styling into HTML
html_string = html_string.replace('</head>', f'{custom_css}</head>')
html_string = html_string.replace('<body>', f'<body>{legend_html}')
# Write enhanced HTML to file
with open(output_html, 'w', encoding='utf-8') as f:
f.write(html_string)
print(f"β
Enhanced interactive visualization saved as: {output_html}")
print(f"π Open this file in your browser to explore the knowledge graph!")
print(f"π¨ Features enhanced color coding for nodes and edges!")
# Print detailed statistics
print(f"\nπ Graph Statistics:")
print(f" Total nodes: {G.number_of_nodes()}")
print(f" Total edges: {G.number_of_edges()}")
print(f" Edges added to visualization: {edges_added}")
print(f" Density: {nx.density(G):.4f}")
if G.number_of_nodes() > 0:
degrees = dict(G.degree())
max_degree_node = max(degrees, key=degrees.get)
avg_degree = sum(degrees.values()) / len(degrees)
print(f" Most connected node: {max_degree_node} (degree: {degrees[max_degree_node]})")
print(f" Average degree: {avg_degree:.2f}")
# Enhanced node types breakdown with actual data attributes
node_types = {}
entity_types = {}
for node_id, data in G.nodes(data=True):
# Original type from data
original_type = data.get('type', 'unknown')
node_types[original_type] = node_types.get(original_type, 0) + 1
# Entity type from data
entity_type = data.get('entity_type', 'unknown')
entity_types[entity_type] = entity_types.get(entity_type, 0) + 1
print(f"\nπ·οΈ Node 'type' Attribute Distribution:")
for node_type, count in sorted(node_types.items()):
percentage = (count / G.number_of_nodes()) * 100
print(f" {node_type}: {count} ({percentage:.1f}%)")
print(f"\nπ Node 'entity_type' Attribute Distribution:")
for entity_type, count in sorted(entity_types.items()):
percentage = (count / G.number_of_nodes()) * 100
print(f" {entity_type}: {count} ({percentage:.1f}%)")
# Edge types breakdown
edge_types = {}
for _, _, data in G.edges(data=True):
edge_type = (
data.get('relationship') or
data.get('description') or
data.get('keywords') or
data.get('label') or
'unknown'
)
edge_types[str(edge_type)] = edge_types.get(str(edge_type), 0) + 1
print(f"\nπ Edge Types Distribution:")
for edge_type, count in sorted(edge_types.items()):
percentage = (count / G.number_of_edges()) * 100 if G.number_of_edges() > 0 else 0
print(f" {edge_type}: {count} ({percentage:.1f}%)")
return True
except Exception as e:
print(f"β Error creating visualization: {e}")
print(f"Error details: {type(e).__name__}: {str(e)}")
# Fallback approach
try:
print("Trying fallback method...")
net.show(output_html)
print(f"β
Fallback successful! File saved as: {output_html}")
return True
except Exception as e2:
print(f"β Fallback also failed: {e2}")
return False
# For standalone usage
def main():
"""
Main function for standalone usage - customize the path to your GraphML file here
"""
import argparse
parser = argparse.ArgumentParser(description="Create interactive knowledge graph visualization")
parser.add_argument("graphml_path", help="Path to the GraphML file")
parser.add_argument("--output", "-o", default="knowledge_graph_interactive.html",
help="Output HTML file name (default: knowledge_graph_interactive.html)")
args = parser.parse_args()
# Check if file exists
if not Path(args.graphml_path).exists():
print(f"β GraphML file not found: {args.graphml_path}")
return
# Create visualization
success = create_interactive_kg_visualization(args.graphml_path, args.output)
if success:
print(f"\nπ Visualization complete! Open {args.output} in your browser.")
else:
print(f"\nπ₯ Visualization failed. Check the error messages above.")
if __name__ == "__main__":
main() |