ArnoChen commited on
Commit
9d9b44d
Β·
1 Parent(s): 2e8b391

Reapply "Integrated the GraphML Visualizer as an optional component of LightRAG"

Browse files
README.md CHANGED
@@ -1058,6 +1058,12 @@ LightRag can be installed with API support to serve a Fast api interface to perf
1058
 
1059
  The documentation can be found [here](lightrag/api/README.md)
1060
 
 
 
 
 
 
 
1061
  ## Star History
1062
 
1063
  <a href="https://star-history.com/#HKUDS/LightRAG&Date">
 
1058
 
1059
  The documentation can be found [here](lightrag/api/README.md)
1060
 
1061
+ ## Graph viewer
1062
+ LightRag can be installed with Tools support to add extra tools like the graphml 3d visualizer.
1063
+
1064
+ The documentation can be found [here](lightrag/tools/lightrag_visualizer/README.md)
1065
+
1066
+
1067
  ## Star History
1068
 
1069
  <a href="https://star-history.com/#HKUDS/LightRAG&Date">
{extra β†’ external_bindings}/OpenWebuiTool/openwebui_tool.py RENAMED
File without changes
lightrag/__init__.py CHANGED
@@ -1,5 +1,5 @@
1
  from .lightrag import LightRAG as LightRAG, QueryParam as QueryParam
2
 
3
- __version__ = "1.1.4"
4
  __author__ = "Zirui Guo"
5
  __url__ = "https://github.com/HKUDS/LightRAG"
 
1
  from .lightrag import LightRAG as LightRAG, QueryParam as QueryParam
2
 
3
+ __version__ = "1.1.5"
4
  __author__ = "Zirui Guo"
5
  __url__ = "https://github.com/HKUDS/LightRAG"
lightrag/api/__init__.py CHANGED
@@ -1 +1 @@
1
- __api_version__ = "1.0.3"
 
1
+ __api_version__ = "1.0.4"
lightrag/api/lightrag_server.py CHANGED
@@ -557,7 +557,14 @@ class DocumentManager:
557
  def __init__(
558
  self,
559
  input_dir: str,
560
- supported_extensions: tuple = (".txt", ".md", ".pdf", ".docx", ".pptx", "xlsx"),
 
 
 
 
 
 
 
561
  ):
562
  self.input_dir = Path(input_dir)
563
  self.supported_extensions = supported_extensions
 
557
  def __init__(
558
  self,
559
  input_dir: str,
560
+ supported_extensions: tuple = (
561
+ ".txt",
562
+ ".md",
563
+ ".pdf",
564
+ ".docx",
565
+ ".pptx",
566
+ ".xlsx",
567
+ ),
568
  ):
569
  self.input_dir = Path(input_dir)
570
  self.supported_extensions = supported_extensions
extra/VisualizationTool/assets/place_font_here β†’ lightrag/tools/__init__.py RENAMED
File without changes
{extra/VisualizationTool β†’ lightrag/tools/lightrag_visualizer}/README-zh.md RENAMED
File without changes
{extra/VisualizationTool β†’ lightrag/tools/lightrag_visualizer}/README.md RENAMED
@@ -1,6 +1,22 @@
1
- # 3D GraphML Viewer
2
 
3
- An interactive 3D graph visualization tool based on Dear ImGui and ModernGL.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  ## Features
6
 
@@ -27,44 +43,32 @@ An interactive 3D graph visualization tool based on Dear ImGui and ModernGL.
27
  - **NumPy**: Numerical computations
28
  - **community**: Community detection
29
 
30
- ## Usage
31
 
32
- 1. **Launch the Program**:
33
- ```bash
34
- python -m pip install -r requirements.txt
35
- python graph_visualizer.py
36
- ```
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- 2. **Load Font**:
39
- - Place the font file `font.ttf` in the `assets` directory
40
- - Or modify the `CUSTOM_FONT` constant to use a different font file
41
-
42
- 3. **Load Graph File**:
43
- - Click the "Load GraphML" button in the interface
44
- - Select a graph file in GraphML format
45
-
46
- 4. **Interactive Controls**:
47
- - **Camera Movement**:
48
- - W: Move forward
49
- - S: Move backward
50
- - A: Move left
51
- - D: Move right
52
- - Q: Move up
53
- - E: Move down
54
- - **View Control**:
55
- - Hold right mouse button and drag to rotate view
56
- - **Node Interaction**:
57
- - Hover mouse to highlight nodes
58
- - Click to select nodes
59
-
60
- 5. **Visualization Settings**:
61
- - Adjustable via UI control panel:
62
- - Layout type
63
- - Node size
64
- - Edge width
65
- - Label visibility
66
- - Label size
67
- - Background color
68
 
69
  ## Customization Options
70
 
@@ -75,14 +79,58 @@ An interactive 3D graph visualization tool based on Dear ImGui and ModernGL.
75
  - **Label Color**: Set label color through `label_color`
76
  - **View Distance**: Control maximum label display distance with `label_culling_distance`
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  ## Performance Optimizations
79
 
80
  - Efficient graphics rendering using ModernGL
81
  - View distance culling for label display optimization
82
  - Community detection algorithms for optimized visualization of large-scale graphs
83
 
84
- ## System Requirements
85
 
86
- - Python 3.10+
87
- - Graphics card with OpenGL 3.3+ support
88
- - Supported Operating Systems: Windows/Linux/MacOS
 
 
 
 
 
 
1
+ # LightRAG 3D Graph Viewer
2
 
3
+ An interactive 3D graph visualization tool included in the LightRAG package for visualizing and analyzing RAG (Retrieval-Augmented Generation) graphs and other graph structures.
4
+
5
+ ![image](https://github.com/user-attachments/assets/b0d86184-99fc-468c-96ed-c611f14292bf)
6
+
7
+ ## Installation
8
+
9
+ ### Quick Install
10
+ ```bash
11
+ pip install lightrag-hku[tools] # Install with visualization tool only
12
+ # or
13
+ pip install lightrag-hku[api,tools] # Install with both API and visualization tools
14
+ ```
15
+
16
+ ## Launch the Viewer
17
+ ```bash
18
+ lightrag-viewer
19
+ ```
20
 
21
  ## Features
22
 
 
43
  - **NumPy**: Numerical computations
44
  - **community**: Community detection
45
 
46
+ ## Interactive Controls
47
 
48
+ ### Camera Movement
49
+ - W: Move forward
50
+ - S: Move backward
51
+ - A: Move left
52
+ - D: Move right
53
+ - Q: Move up
54
+ - E: Move down
55
+
56
+ ### View Control
57
+ - Hold right mouse button and drag to rotate view
58
+
59
+ ### Node Interaction
60
+ - Hover mouse to highlight nodes
61
+ - Click to select nodes
62
+
63
+ ## Visualization Settings
64
 
65
+ Adjustable via UI control panel:
66
+ - Layout type
67
+ - Node size
68
+ - Edge width
69
+ - Label visibility
70
+ - Label size
71
+ - Background color
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  ## Customization Options
74
 
 
79
  - **Label Color**: Set label color through `label_color`
80
  - **View Distance**: Control maximum label display distance with `label_culling_distance`
81
 
82
+ ## System Requirements
83
+
84
+ - Python 3.9+
85
+ - Graphics card with OpenGL 3.3+ support
86
+ - Supported Operating Systems: Windows/Linux/MacOS
87
+
88
+ ## Troubleshooting
89
+
90
+ ### Common Issues
91
+
92
+ 1. **Command Not Found**
93
+ ```bash
94
+ # Make sure you installed with the 'tools' option
95
+ pip install lightrag-hku[tools]
96
+
97
+ # Verify installation
98
+ pip list | grep lightrag-hku
99
+ ```
100
+
101
+ 2. **ModernGL Initialization Failed**
102
+ ```bash
103
+ # Check OpenGL version
104
+ glxinfo | grep "OpenGL version"
105
+
106
+ # Update graphics drivers if needed
107
+ ```
108
+
109
+ 3. **Font Loading Issues**
110
+ - The required fonts are included in the package
111
+ - If issues persist, check your graphics drivers
112
+
113
+ ## Usage with LightRAG
114
+
115
+ The viewer is particularly useful for:
116
+ - Visualizing RAG knowledge graphs
117
+ - Analyzing document relationships
118
+ - Exploring semantic connections
119
+ - Debugging retrieval patterns
120
+
121
  ## Performance Optimizations
122
 
123
  - Efficient graphics rendering using ModernGL
124
  - View distance culling for label display optimization
125
  - Community detection algorithms for optimized visualization of large-scale graphs
126
 
127
+ ## Support
128
 
129
+ - GitHub Issues: [LightRAG Repository](https://github.com/URL-to-lightrag)
130
+ - Documentation: [LightRAG Docs](https://URL-to-docs)
131
+
132
+ ## License
133
+
134
+ This tool is part of LightRAG and is distributed under the MIT License. See `LICENSE` for more information.
135
+
136
+ Note: This visualization tool is an optional component of the LightRAG package. Install with the [tools] option to access the viewer functionality.
extra/VisualizationTool/graph_visualizer.py β†’ lightrag/tools/lightrag_visualizer/__init__.py RENAMED
@@ -1,6 +1,6 @@
1
  """
2
  3D GraphML Viewer using Dear ImGui and ModernGL
3
- Author: LoLLMs, ArnoChen
4
  Description: An interactive 3D GraphML viewer using imgui_bundle and ModernGL
5
  Version: 2.0
6
  """
@@ -8,6 +8,18 @@ Version: 2.0
8
  from typing import Optional, Tuple, Dict, List
9
  import numpy as np
10
  import networkx as nx
 
 
 
 
 
 
 
 
 
 
 
 
11
  import moderngl
12
  from imgui_bundle import imgui, immapp, hello_imgui
13
  import community
 
1
  """
2
  3D GraphML Viewer using Dear ImGui and ModernGL
3
+ Author: ParisNeo, ArnoChen
4
  Description: An interactive 3D GraphML viewer using imgui_bundle and ModernGL
5
  Version: 2.0
6
  """
 
8
  from typing import Optional, Tuple, Dict, List
9
  import numpy as np
10
  import networkx as nx
11
+ import pipmaster as pm
12
+
13
+ # Added automatic libraries install using pipmaster
14
+ if not pm.is_installed("moderngl"):
15
+ pm.install("moderngl")
16
+ if not pm.is_installed("imgui_bundle"):
17
+ pm.install("imgui_bundle")
18
+ if not pm.is_installed("pyglm"):
19
+ pm.install("pyglm")
20
+ if not pm.is_installed("python-louvain"):
21
+ pm.install("python-louvain")
22
+
23
  import moderngl
24
  from imgui_bundle import imgui, immapp, hello_imgui
25
  import community
lightrag/tools/lightrag_visualizer/assets/place_font_here ADDED
File without changes
lightrag/tools/lightrag_visualizer/graph_visualizer.py DELETED
@@ -1,1214 +0,0 @@
1
- """
2
- 3D GraphML Viewer using Dear ImGui and ModernGL
3
- Author: LoLLMs, ArnoChen
4
- Description: An interactive 3D GraphML viewer using imgui_bundle and ModernGL
5
- Version: 2.0
6
- """
7
-
8
- from typing import Optional, Tuple, Dict, List
9
- import numpy as np
10
- import networkx as nx
11
- import moderngl
12
- from imgui_bundle import imgui, immapp, hello_imgui
13
- import community
14
- import glm
15
- import tkinter as tk
16
- from tkinter import filedialog
17
- import traceback
18
- import colorsys
19
- import os
20
-
21
- CUSTOM_FONT = "font.ttf"
22
-
23
- DEFAULT_FONT_ENG = "Geist-Regular.ttf"
24
- DEFAULT_FONT_CHI = "SmileySans-Oblique.ttf"
25
-
26
-
27
- class Node3D:
28
- """Class representing a 3D node in the graph"""
29
-
30
- def __init__(
31
- self, position: glm.vec3, color: glm.vec3, label: str, size: float, idx: int
32
- ):
33
- self.position = position
34
- self.color = color
35
- self.label = label
36
- self.size = size
37
- self.idx = idx
38
-
39
-
40
- class GraphViewer:
41
- """Main class for 3D graph visualization"""
42
-
43
- def __init__(self):
44
- self.glctx = None # ModernGL context
45
- self.graph: Optional[nx.Graph] = None
46
- self.nodes: List[Node3D] = []
47
- self.id_node_map: Dict[str, Node3D] = {}
48
- self.communities = None
49
- self.community_colors = None
50
-
51
- # Window dimensions
52
- self.window_width = 1280
53
- self.window_height = 720
54
-
55
- # Camera parameters
56
- self.position = glm.vec3(0.0, -10.0, 0.0) # Initial camera position
57
- self.front = glm.vec3(0.0, 1.0, 0.0) # Direction camera is facing
58
- self.up = glm.vec3(0.0, 0.0, 1.0) # Up vector
59
- self.yaw = 90.0 # Horizontal rotation (around Z axis)
60
- self.pitch = 0.0 # Vertical rotation
61
- self.move_speed = 0.05
62
- self.mouse_sensitivity = 0.15
63
-
64
- # Graph visualization settings
65
- self.layout_type = "Spring"
66
- self.node_scale = 0.2
67
- self.edge_width = 0.5
68
- self.show_labels = True
69
- self.label_size = 2
70
- self.label_color = (1.0, 1.0, 1.0, 1.0)
71
- self.label_culling_distance = 10.0
72
- self.available_layouts = ("Spring", "Circular", "Shell", "Random")
73
- self.background_color = (0.05, 0.05, 0.05, 1.0)
74
-
75
- # Mouse interaction
76
- self.last_mouse_pos = None
77
- self.mouse_pressed = False
78
- self.mouse_button = -1
79
- self.first_mouse = True
80
-
81
- # File dialog state
82
- self.show_load_error = False
83
- self.error_message = ""
84
-
85
- # Selection state
86
- self.selected_node: Optional[Node3D] = None
87
- self.highlighted_node: Optional[Node3D] = None
88
-
89
- # Node id map
90
- self.node_id_fbo = None
91
- self.node_id_texture = None
92
- self.node_id_depth = None
93
- self.node_id_texture_np: np.ndarray = None
94
-
95
- # Static data
96
- self.sphere_data = create_sphere()
97
-
98
- # Initialization flag
99
- self.initialized = False
100
-
101
- def setup(self):
102
- self.setup_render_context()
103
- self.setup_shaders()
104
- self.setup_buffers()
105
- self.initialized = True
106
-
107
- def handle_keyboard_input(self):
108
- """Handle WASD keyboard input for camera movement"""
109
- io = imgui.get_io()
110
-
111
- if io.want_capture_keyboard:
112
- return
113
-
114
- # Calculate camera vectors
115
- right = glm.normalize(glm.cross(self.front, self.up))
116
-
117
- # Get movement direction from WASD keys
118
- if imgui.is_key_down(imgui.Key.w): # Forward
119
- self.position += self.front * self.move_speed * 0.1
120
- if imgui.is_key_down(imgui.Key.s): # Backward
121
- self.position -= self.front * self.move_speed * 0.1
122
- if imgui.is_key_down(imgui.Key.a): # Left
123
- self.position -= right * self.move_speed * 0.1
124
- if imgui.is_key_down(imgui.Key.d): # Right
125
- self.position += right * self.move_speed * 0.1
126
- if imgui.is_key_down(imgui.Key.q): # Up
127
- self.position += self.up * self.move_speed * 0.1
128
- if imgui.is_key_down(imgui.Key.e): # Down
129
- self.position -= self.up * self.move_speed * 0.1
130
-
131
- def handle_mouse_interaction(self):
132
- """Handle mouse interaction for camera control and node selection"""
133
- if (
134
- imgui.is_any_item_active()
135
- or imgui.is_any_item_hovered()
136
- or imgui.is_any_item_focused()
137
- ):
138
- return
139
-
140
- io = imgui.get_io()
141
- mouse_pos = (io.mouse_pos.x, io.mouse_pos.y)
142
- if (
143
- mouse_pos[0] < 0
144
- or mouse_pos[1] < 0
145
- or mouse_pos[0] >= self.window_width
146
- or mouse_pos[1] >= self.window_height
147
- ):
148
- return
149
-
150
- # Handle first mouse input
151
- if self.first_mouse:
152
- self.last_mouse_pos = mouse_pos
153
- self.first_mouse = False
154
- return
155
-
156
- # Handle mouse movement for camera rotation
157
- if self.mouse_pressed and self.mouse_button == 1: # Right mouse button
158
- dx = self.last_mouse_pos[0] - mouse_pos[0]
159
- dy = self.last_mouse_pos[1] - mouse_pos[1] # Reversed for intuitive control
160
-
161
- dx *= self.mouse_sensitivity
162
- dy *= self.mouse_sensitivity
163
-
164
- self.yaw += dx
165
- self.pitch += dy
166
-
167
- # Limit pitch to avoid flipping
168
- self.pitch = np.clip(self.pitch, -89.0, 89.0)
169
-
170
- # Update front vector
171
- self.front = glm.normalize(
172
- glm.vec3(
173
- np.cos(np.radians(self.yaw)) * np.cos(np.radians(self.pitch)),
174
- np.sin(np.radians(self.yaw)) * np.cos(np.radians(self.pitch)),
175
- np.sin(np.radians(self.pitch)),
176
- )
177
- )
178
-
179
- if not imgui.is_window_hovered():
180
- return
181
-
182
- if io.mouse_wheel != 0:
183
- self.move_speed += io.mouse_wheel * 0.05
184
- self.move_speed = np.max([self.move_speed, 0.01])
185
-
186
- # Handle mouse press/release
187
- for button in range(3):
188
- if imgui.is_mouse_clicked(button):
189
- self.mouse_pressed = True
190
- self.mouse_button = button
191
- if button == 0 and self.highlighted_node: # Left click for selection
192
- self.selected_node = self.highlighted_node
193
-
194
- if imgui.is_mouse_released(button) and self.mouse_button == button:
195
- self.mouse_pressed = False
196
- self.mouse_button = -1
197
-
198
- # Handle node hovering
199
- if not self.mouse_pressed:
200
- hovered = self.find_node_at((int(mouse_pos[0]), int(mouse_pos[1])))
201
- self.highlighted_node = hovered
202
-
203
- # Update last mouse position
204
- self.last_mouse_pos = mouse_pos
205
-
206
- def update_layout(self):
207
- """Update the graph layout"""
208
- pos = nx.spring_layout(
209
- self.graph,
210
- dim=3,
211
- pos={
212
- node_id: list(node.position)
213
- for node_id, node in self.id_node_map.items()
214
- },
215
- k=2.0,
216
- iterations=100,
217
- weight=None,
218
- )
219
-
220
- # Update node positions
221
- for node_id, position in pos.items():
222
- self.id_node_map[node_id].position = glm.vec3(position)
223
- self.update_buffers()
224
-
225
- def render_node_details(self):
226
- """Render node details window"""
227
- if self.selected_node and imgui.begin("Node Details"):
228
- imgui.text(f"ID: {self.selected_node.label}")
229
-
230
- if self.graph:
231
- node_data = self.graph.nodes[self.selected_node.label]
232
- imgui.text(f"Type: {node_data.get('type', 'default')}")
233
-
234
- degree = self.graph.degree[self.selected_node.label]
235
- imgui.text(f"Degree: {degree}")
236
-
237
- for key, value in node_data.items():
238
- if key != "type":
239
- imgui.text(f"{key}: {value}")
240
- if value and imgui.is_item_hovered():
241
- imgui.set_tooltip(str(value))
242
-
243
- imgui.separator()
244
-
245
- connections = self.graph[self.selected_node.label]
246
- if connections:
247
- imgui.text("Connections:")
248
- keys = next(iter(connections.values())).keys()
249
- if imgui.begin_table(
250
- "Connections",
251
- len(keys) + 1,
252
- imgui.TableFlags_.borders
253
- | imgui.TableFlags_.row_bg
254
- | imgui.TableFlags_.resizable
255
- | imgui.TableFlags_.hideable,
256
- ):
257
- imgui.table_setup_column("Node")
258
- for key in keys:
259
- imgui.table_setup_column(key)
260
- imgui.table_headers_row()
261
-
262
- for neighbor, edge_data in connections.items():
263
- imgui.table_next_row()
264
- imgui.table_set_column_index(0)
265
- if imgui.selectable(str(neighbor), True)[0]:
266
- # Select neighbor node
267
- self.selected_node = self.id_node_map[neighbor]
268
- self.position = self.selected_node.position - self.front
269
- for idx, key in enumerate(keys):
270
- imgui.table_set_column_index(idx + 1)
271
- value = str(edge_data.get(key, ""))
272
- imgui.text(value)
273
- if value and imgui.is_item_hovered():
274
- imgui.set_tooltip(value)
275
- imgui.end_table()
276
-
277
- imgui.end()
278
-
279
- def setup_render_context(self):
280
- """Initialize ModernGL context"""
281
- self.glctx = moderngl.create_context()
282
- self.glctx.enable(moderngl.DEPTH_TEST | moderngl.CULL_FACE)
283
- self.glctx.clear_color = self.background_color
284
-
285
- def setup_shaders(self):
286
- """Setup vertex and fragment shaders for node and edge rendering"""
287
- # Node shader program
288
- self.node_prog = self.glctx.program(
289
- vertex_shader="""
290
- #version 330
291
-
292
- uniform mat4 mvp;
293
- uniform vec3 camera;
294
- uniform int selected_node;
295
- uniform int highlighted_node;
296
- uniform float scale;
297
-
298
- in vec3 in_position;
299
- in vec3 in_instance_position;
300
- in vec3 in_instance_color;
301
- in float in_instance_size;
302
-
303
- out vec3 frag_color;
304
- out vec3 frag_normal;
305
- out vec3 frag_view_dir;
306
-
307
- void main() {
308
- vec3 pos = in_position * in_instance_size * scale + in_instance_position;
309
- gl_Position = mvp * vec4(pos, 1.0);
310
-
311
- frag_normal = normalize(in_position);
312
- frag_view_dir = normalize(camera - pos);
313
-
314
- if (selected_node == gl_InstanceID) {
315
- frag_color = vec3(1.0, 0.5, 0.0);
316
- }
317
- else if (highlighted_node == gl_InstanceID) {
318
- frag_color = vec3(1.0, 0.8, 0.2);
319
- }
320
- else {
321
- frag_color = in_instance_color;
322
- }
323
- }
324
- """,
325
- fragment_shader="""
326
- #version 330
327
-
328
- in vec3 frag_color;
329
- in vec3 frag_normal;
330
- in vec3 frag_view_dir;
331
-
332
- out vec4 outColor;
333
-
334
- void main() {
335
- // Edge detection based on normal-view angle
336
- float edge = 1.0 - abs(dot(frag_normal, frag_view_dir));
337
-
338
- // Create sharp outline
339
- float outline = smoothstep(0.8, 0.9, edge);
340
-
341
- // Mix the sphere color with outline
342
- vec3 final_color = mix(frag_color, vec3(0.0), outline);
343
-
344
- outColor = vec4(final_color, 1.0);
345
- }
346
- """,
347
- )
348
-
349
- # Edge shader program with wide lines using geometry shader
350
- self.edge_prog = self.glctx.program(
351
- vertex_shader="""
352
- #version 330
353
-
354
- uniform mat4 mvp;
355
-
356
- in vec3 in_position;
357
- in vec3 in_color;
358
-
359
- out vec3 v_color;
360
- out vec4 v_position;
361
-
362
- void main() {
363
- v_position = mvp * vec4(in_position, 1.0);
364
- gl_Position = v_position;
365
- v_color = in_color;
366
- }
367
- """,
368
- geometry_shader="""
369
- #version 330
370
-
371
- layout(lines) in;
372
- layout(triangle_strip, max_vertices = 4) out;
373
-
374
- uniform float edge_width;
375
- uniform vec2 viewport_size;
376
-
377
- in vec3 v_color[];
378
- in vec4 v_position[];
379
- out vec3 g_color;
380
- out float edge_coord;
381
-
382
- void main() {
383
- // Get the two vertices of the line
384
- vec4 p1 = v_position[0];
385
- vec4 p2 = v_position[1];
386
-
387
- // Perspective division
388
- vec4 p1_ndc = p1 / p1.w;
389
- vec4 p2_ndc = p2 / p2.w;
390
-
391
- // Calculate line direction in screen space
392
- vec2 dir = normalize((p2_ndc.xy - p1_ndc.xy) * viewport_size);
393
- vec2 normal = vec2(-dir.y, dir.x);
394
-
395
- // Calculate half width based on screen space
396
- float half_width = edge_width * 0.5;
397
- vec2 offset = normal * (half_width / viewport_size);
398
-
399
- // Emit vertices with proper depth
400
- gl_Position = vec4(p1_ndc.xy + offset, p1_ndc.z, 1.0);
401
- gl_Position *= p1.w; // Restore perspective
402
- g_color = v_color[0];
403
- edge_coord = 1.0;
404
- EmitVertex();
405
-
406
- gl_Position = vec4(p1_ndc.xy - offset, p1_ndc.z, 1.0);
407
- gl_Position *= p1.w;
408
- g_color = v_color[0];
409
- edge_coord = -1.0;
410
- EmitVertex();
411
-
412
- gl_Position = vec4(p2_ndc.xy + offset, p2_ndc.z, 1.0);
413
- gl_Position *= p2.w;
414
- g_color = v_color[1];
415
- edge_coord = 1.0;
416
- EmitVertex();
417
-
418
- gl_Position = vec4(p2_ndc.xy - offset, p2_ndc.z, 1.0);
419
- gl_Position *= p2.w;
420
- g_color = v_color[1];
421
- edge_coord = -1.0;
422
- EmitVertex();
423
-
424
- EndPrimitive();
425
- }
426
- """,
427
- fragment_shader="""
428
- #version 330
429
-
430
- in vec3 g_color;
431
- in float edge_coord;
432
-
433
- out vec4 fragColor;
434
-
435
- void main() {
436
- // Edge outline parameters
437
- float outline_width = 0.2; // Width of the outline relative to edge
438
- float edge_softness = 0.1; // Softness of the edge
439
- float edge_dist = abs(edge_coord);
440
-
441
- // Calculate outline
442
- float outline_factor = smoothstep(1.0 - outline_width - edge_softness,
443
- 1.0 - outline_width,
444
- edge_dist);
445
-
446
- // Mix edge color with outline (black)
447
- vec3 final_color = mix(g_color, vec3(0.0), outline_factor);
448
-
449
- // Calculate alpha for anti-aliasing
450
- float alpha = 1.0 - smoothstep(1.0 - edge_softness, 1.0, edge_dist);
451
-
452
- fragColor = vec4(final_color, alpha);
453
- }
454
- """,
455
- )
456
-
457
- # Id framebuffer shader program
458
- self.node_id_prog = self.glctx.program(
459
- vertex_shader="""
460
- #version 330
461
-
462
- uniform mat4 mvp;
463
- uniform float scale;
464
-
465
- in vec3 in_position;
466
- in vec3 in_instance_position;
467
- in float in_instance_size;
468
-
469
- out vec3 frag_color;
470
-
471
- vec3 int_to_rgb(int value) {
472
- float R = float((value >> 16) & 0xFF);
473
- float G = float((value >> 8) & 0xFF);
474
- float B = float(value & 0xFF);
475
- // normalize to [0, 1]
476
- return vec3(R / 255.0, G / 255.0, B / 255.0);
477
- }
478
-
479
- void main() {
480
- vec3 pos = in_position * in_instance_size * scale + in_instance_position;
481
- gl_Position = mvp * vec4(pos, 1.0);
482
- frag_color = int_to_rgb(gl_InstanceID);
483
- }
484
- """,
485
- fragment_shader="""
486
- #version 330
487
- in vec3 frag_color;
488
- out vec4 outColor;
489
- void main() {
490
- outColor = vec4(frag_color, 1.0);
491
- }
492
- """,
493
- )
494
-
495
- def setup_buffers(self):
496
- """Setup vertex buffers for nodes and edges"""
497
- # We'll create these when loading the graph
498
- self.node_vbo = None
499
- self.node_color_vbo = None
500
- self.node_size_vbo = None
501
- self.edge_vbo = None
502
- self.edge_color_vbo = None
503
- self.node_vao = None
504
- self.edge_vao = None
505
- self.node_id_vao = None
506
- self.sphere_pos_vbo = None
507
- self.sphere_index_buffer = None
508
-
509
- def load_file(self, filepath: str):
510
- """Load a GraphML file with error handling"""
511
- try:
512
- # Clear existing data
513
- self.id_node_map.clear()
514
- self.nodes.clear()
515
- self.selected_node = None
516
- self.highlighted_node = None
517
- self.setup_buffers()
518
-
519
- # Load new graph
520
- self.graph = nx.read_graphml(filepath)
521
- self.calculate_layout()
522
- self.update_buffers()
523
- self.show_load_error = False
524
- self.error_message = ""
525
- except Exception as _:
526
- self.show_load_error = True
527
- self.error_message = traceback.format_exc()
528
- print(self.error_message)
529
-
530
- def calculate_layout(self):
531
- """Calculate 3D layout for the graph"""
532
- if not self.graph:
533
- return
534
-
535
- # Detect communities for coloring
536
- self.communities = community.best_partition(self.graph)
537
- num_communities = len(set(self.communities.values()))
538
- self.community_colors = generate_colors(num_communities)
539
-
540
- # Calculate layout based on selected type
541
- if self.layout_type == "Spring":
542
- pos = nx.spring_layout(
543
- self.graph, dim=3, k=2.0, iterations=100, weight=None
544
- )
545
- elif self.layout_type == "Circular":
546
- pos_2d = nx.circular_layout(self.graph)
547
- pos = {node: np.array((x, 0.0, y)) for node, (x, y) in pos_2d.items()}
548
- elif self.layout_type == "Shell":
549
- # Group nodes by community for shell layout
550
- comm_lists = [[] for _ in range(num_communities)]
551
- for node, comm in self.communities.items():
552
- comm_lists[comm].append(node)
553
- pos_2d = nx.shell_layout(self.graph, comm_lists)
554
- pos = {node: np.array((x, 0.0, y)) for node, (x, y) in pos_2d.items()}
555
- else: # Random
556
- pos = {node: np.random.rand(3) * 2 - 1 for node in self.graph.nodes()}
557
-
558
- # Scale positions
559
- positions = np.array(list(pos.values()))
560
- if len(positions) > 0:
561
- scale = 10.0 / max(1.0, np.max(np.abs(positions)))
562
- pos = {node: coords * scale for node, coords in pos.items()}
563
-
564
- # Calculate degree-based sizes
565
- degrees = dict(self.graph.degree())
566
- max_degree = max(degrees.values()) if degrees else 1
567
- min_degree = min(degrees.values()) if degrees else 1
568
-
569
- idx = 0
570
- # Create nodes with community colors
571
- for node_id in self.graph.nodes():
572
- position = glm.vec3(pos[node_id])
573
- color = self.get_node_color(node_id)
574
-
575
- # Normalize sizes between 0.5 and 2.0
576
- size = 1.0
577
- if max_degree != min_degree:
578
- # Normalize and scale size
579
- normalized = (degrees[node_id] - min_degree) / (max_degree - min_degree)
580
- size = 0.5 + normalized * 1.5
581
-
582
- if node_id in self.id_node_map:
583
- node = self.id_node_map[node_id]
584
- node.position = position
585
- node.base_color = color
586
- node.color = color
587
- node.size = size
588
- else:
589
- node = Node3D(position, color, str(node_id), size, idx)
590
- self.id_node_map[node_id] = node
591
- self.nodes.append(node)
592
- idx += 1
593
-
594
- self.update_buffers()
595
-
596
- def get_node_color(self, node_id: str) -> glm.vec3:
597
- """Get RGBA color based on community"""
598
- if self.communities and node_id in self.communities:
599
- comm_id = self.communities[node_id]
600
- color = self.community_colors[comm_id]
601
- return color
602
- return glm.vec3(0.5, 0.5, 0.5)
603
-
604
- def update_buffers(self):
605
- """Update vertex buffers with current node and edge data using batch rendering"""
606
- if not self.graph:
607
- return
608
-
609
- # Update node buffers
610
- node_positions = []
611
- node_colors = []
612
- node_sizes = []
613
-
614
- for node in self.nodes:
615
- node_positions.append(node.position)
616
- node_colors.append(node.color) # Only use RGB components
617
- node_sizes.append(node.size)
618
-
619
- if node_positions:
620
- node_positions = np.array(node_positions, dtype=np.float32)
621
- node_colors = np.array(node_colors, dtype=np.float32)
622
- node_sizes = np.array(node_sizes, dtype=np.float32)
623
-
624
- self.node_vbo = self.glctx.buffer(node_positions.tobytes())
625
- self.node_color_vbo = self.glctx.buffer(node_colors.tobytes())
626
- self.node_size_vbo = self.glctx.buffer(node_sizes.tobytes())
627
- self.sphere_pos_vbo = self.glctx.buffer(self.sphere_data[0].tobytes())
628
- self.sphere_index_buffer = self.glctx.buffer(self.sphere_data[1].tobytes())
629
-
630
- self.node_vao = self.glctx.vertex_array(
631
- self.node_prog,
632
- [
633
- (self.sphere_pos_vbo, "3f", "in_position"),
634
- (self.node_vbo, "3f /i", "in_instance_position"),
635
- (self.node_color_vbo, "3f /i", "in_instance_color"),
636
- (self.node_size_vbo, "f /i", "in_instance_size"),
637
- ],
638
- index_buffer=self.sphere_index_buffer,
639
- index_element_size=4,
640
- )
641
- self.node_vao.instances = len(self.nodes)
642
-
643
- self.node_id_vao = self.glctx.vertex_array(
644
- self.node_id_prog,
645
- [
646
- (self.sphere_pos_vbo, "3f", "in_position"),
647
- (self.node_vbo, "3f /i", "in_instance_position"),
648
- (self.node_size_vbo, "f /i", "in_instance_size"),
649
- ],
650
- index_buffer=self.sphere_index_buffer,
651
- index_element_size=4,
652
- )
653
- self.node_id_vao.instances = len(self.nodes)
654
-
655
- # Update edge buffers
656
- edge_positions = []
657
- edge_colors = []
658
-
659
- for edge in self.graph.edges():
660
- start_node = self.id_node_map[edge[0]]
661
- end_node = self.id_node_map[edge[1]]
662
-
663
- edge_positions.append(start_node.position)
664
- edge_colors.append(start_node.color)
665
-
666
- edge_positions.append(end_node.position)
667
- edge_colors.append(end_node.color)
668
-
669
- if edge_positions:
670
- edge_positions = np.array(edge_positions, dtype=np.float32)
671
- edge_colors = np.array(edge_colors, dtype=np.float32)
672
-
673
- self.edge_vbo = self.glctx.buffer(edge_positions.tobytes())
674
- self.edge_color_vbo = self.glctx.buffer(edge_colors.tobytes())
675
-
676
- self.edge_vao = self.glctx.vertex_array(
677
- self.edge_prog,
678
- [
679
- (self.edge_vbo, "3f", "in_position"),
680
- (self.edge_color_vbo, "3f", "in_color"),
681
- ],
682
- )
683
-
684
- def update_view_proj_matrix(self):
685
- """Update view matrix based on camera parameters"""
686
- self.view_matrix = glm.lookAt(
687
- self.position, self.position + self.front, self.up
688
- )
689
-
690
- aspect_ratio = self.window_width / self.window_height
691
- self.proj_matrix = glm.perspective(
692
- glm.radians(60.0), # FOV
693
- aspect_ratio, # Aspect ratio
694
- 0.001, # Near plane
695
- 1000.0, # Far plane
696
- )
697
-
698
- def find_node_at(self, screen_pos: Tuple[int, int]) -> Optional[Node3D]:
699
- """Find the node at a specific screen position"""
700
- if (
701
- self.node_id_texture_np is None
702
- or self.node_id_texture_np.shape[1] != self.window_width
703
- or self.node_id_texture_np.shape[0] != self.window_height
704
- or screen_pos[0] < 0
705
- or screen_pos[1] < 0
706
- or screen_pos[0] >= self.window_width
707
- or screen_pos[1] >= self.window_height
708
- ):
709
- return None
710
-
711
- x = screen_pos[0]
712
- y = self.window_height - screen_pos[1] - 1
713
- pixel = self.node_id_texture_np[y, x]
714
-
715
- if pixel[3] == 0:
716
- return None
717
-
718
- R = int(round(pixel[0] * 255))
719
- G = int(round(pixel[1] * 255))
720
- B = int(round(pixel[2] * 255))
721
- index = (R << 16) | (G << 8) | B
722
-
723
- if index > len(self.nodes):
724
- return None
725
- return self.nodes[index]
726
-
727
- def is_node_visible_at(self, screen_pos: Tuple[int, int], node_idx: int) -> bool:
728
- """Check if a node exists at a specific screen position"""
729
- node = self.find_node_at(screen_pos)
730
- return node is not None and node.idx == node_idx
731
-
732
- def render_settings(self):
733
- """Render settings window"""
734
- if imgui.begin("Graph Settings"):
735
- # Layout type combo
736
- changed, value = imgui.combo(
737
- "Layout",
738
- self.available_layouts.index(self.layout_type),
739
- self.available_layouts,
740
- )
741
- if changed:
742
- self.layout_type = self.available_layouts[value]
743
- self.calculate_layout() # Recalculate layout when changed
744
-
745
- # Node size slider
746
- changed, value = imgui.slider_float("Node Scale", self.node_scale, 0.01, 10)
747
- if changed:
748
- self.node_scale = value
749
-
750
- # Edge width slider
751
- changed, value = imgui.slider_float("Edge Width", self.edge_width, 0, 20)
752
- if changed:
753
- self.edge_width = value
754
-
755
- # Show labels checkbox
756
- changed, value = imgui.checkbox("Show Labels", self.show_labels)
757
-
758
- if changed:
759
- self.show_labels = value
760
-
761
- if self.show_labels:
762
- # Label size slider
763
- changed, value = imgui.slider_float(
764
- "Label Size", self.label_size, 0.5, 10.0
765
- )
766
- if changed:
767
- self.label_size = value
768
-
769
- # Label color picker
770
- changed, value = imgui.color_edit4(
771
- "Label Color",
772
- self.label_color,
773
- imgui.ColorEditFlags_.picker_hue_wheel,
774
- )
775
- if changed:
776
- self.label_color = (value[0], value[1], value[2], value[3])
777
-
778
- # Label culling distance slider
779
- changed, value = imgui.slider_float(
780
- "Label Culling Distance", self.label_culling_distance, 0.1, 100.0
781
- )
782
- if changed:
783
- self.label_culling_distance = value
784
-
785
- # Background color picker
786
- changed, value = imgui.color_edit4(
787
- "Background Color",
788
- self.background_color,
789
- imgui.ColorEditFlags_.picker_hue_wheel,
790
- )
791
- if changed:
792
- self.background_color = (value[0], value[1], value[2], value[3])
793
-
794
- imgui.end()
795
-
796
- def save_node_id_texture_to_png(self, filename):
797
- # Convert to a PIL Image and save as PNG
798
- from PIL import Image
799
-
800
- scaled_array = self.node_id_texture_np * 255
801
- img = Image.fromarray(
802
- scaled_array.astype(np.uint8),
803
- "RGBA",
804
- )
805
- img = img.transpose(method=Image.FLIP_TOP_BOTTOM)
806
- img.save(filename)
807
-
808
- def render_id_map(self, mvp: glm.mat4):
809
- """Render an offscreen id map where each node is drawn with a unique id color."""
810
- # Lazy initialization of id framebuffer
811
- if self.node_id_texture is not None:
812
- if (
813
- self.node_id_texture.width != self.window_width
814
- or self.node_id_texture.height != self.window_height
815
- ):
816
- self.node_id_fbo = None
817
- self.node_id_texture = None
818
- self.node_id_texture_np = None
819
- self.node_id_depth = None
820
-
821
- if self.node_id_texture is None:
822
- self.node_id_texture = self.glctx.texture(
823
- (self.window_width, self.window_height), components=4, dtype="f4"
824
- )
825
- self.node_id_depth = self.glctx.depth_renderbuffer(
826
- size=(self.window_width, self.window_height)
827
- )
828
- self.node_id_fbo = self.glctx.framebuffer(
829
- color_attachments=[self.node_id_texture],
830
- depth_attachment=self.node_id_depth,
831
- )
832
- self.node_id_texture_np = np.zeros(
833
- (self.window_height, self.window_width, 4), dtype=np.float32
834
- )
835
-
836
- # Bind the offscreen framebuffer
837
- self.node_id_fbo.use()
838
- self.glctx.clear(0, 0, 0, 0)
839
-
840
- # Render nodes
841
- if self.node_id_vao:
842
- self.node_id_prog["mvp"].write(mvp.to_bytes())
843
- self.node_id_prog["scale"].write(np.float32(self.node_scale).tobytes())
844
- self.node_id_vao.render(moderngl.TRIANGLES)
845
-
846
- # Revert to default framebuffer
847
- self.glctx.screen.use()
848
- self.node_id_texture.read_into(self.node_id_texture_np.data)
849
-
850
- def render(self):
851
- """Render the graph"""
852
- # Clear screen
853
- self.glctx.clear(*self.background_color, depth=1)
854
-
855
- if not self.graph:
856
- return
857
-
858
- # Enable blending for transparency
859
- self.glctx.enable(moderngl.BLEND)
860
- self.glctx.blend_func = moderngl.SRC_ALPHA, moderngl.ONE_MINUS_SRC_ALPHA
861
-
862
- # Update view and projection matrices
863
- self.update_view_proj_matrix()
864
- mvp = self.proj_matrix * self.view_matrix
865
-
866
- # Render edges first (under nodes)
867
- if self.edge_vao:
868
- self.edge_prog["mvp"].write(mvp.to_bytes())
869
- self.edge_prog["edge_width"].value = (
870
- float(self.edge_width) * 2.0
871
- ) # Double the width for better visibility
872
- self.edge_prog["viewport_size"].value = (
873
- float(self.window_width),
874
- float(self.window_height),
875
- )
876
- self.edge_vao.render(moderngl.LINES)
877
-
878
- # Render nodes
879
- if self.node_vao:
880
- self.node_prog["mvp"].write(mvp.to_bytes())
881
- self.node_prog["camera"].write(self.position.to_bytes())
882
- self.node_prog["selected_node"].write(
883
- np.int32(self.selected_node.idx).tobytes()
884
- if self.selected_node
885
- else np.int32(-1).tobytes()
886
- )
887
- self.node_prog["highlighted_node"].write(
888
- np.int32(self.highlighted_node.idx).tobytes()
889
- if self.highlighted_node
890
- else np.int32(-1).tobytes()
891
- )
892
- self.node_prog["scale"].write(np.float32(self.node_scale).tobytes())
893
- self.node_vao.render(moderngl.TRIANGLES)
894
-
895
- self.glctx.disable(moderngl.BLEND)
896
-
897
- # Render id map
898
- self.render_id_map(mvp)
899
-
900
- def render_labels(self):
901
- # Render labels if enabled
902
- if self.show_labels and self.nodes:
903
- # Save current font scale
904
- original_scale = imgui.get_font_size()
905
-
906
- self.update_view_proj_matrix()
907
- mvp = self.proj_matrix * self.view_matrix
908
-
909
- for node in self.nodes:
910
- # Project node position to screen space
911
- pos = mvp * glm.vec4(
912
- node.position[0], node.position[1], node.position[2], 1.0
913
- )
914
-
915
- # Check if node is behind camera
916
- if pos.w > 0 and pos.w < self.label_culling_distance:
917
- screen_x = (pos.x / pos.w + 1) * self.window_width / 2
918
- screen_y = (-pos.y / pos.w + 1) * self.window_height / 2
919
-
920
- if self.is_node_visible_at(
921
- (int(screen_x), int(screen_y)), node.idx
922
- ):
923
- # Set font scale
924
- imgui.set_window_font_scale(float(self.label_size) * node.size)
925
-
926
- # Calculate label size
927
- label_size = imgui.calc_text_size(node.label)
928
-
929
- # Adjust position to center the label
930
- screen_x -= label_size.x / 2
931
- screen_y -= label_size.y / 2
932
-
933
- # Set text color with calculated alpha
934
- imgui.push_style_color(imgui.Col_.text, self.label_color)
935
-
936
- # Draw label using ImGui
937
- imgui.set_cursor_pos((screen_x, screen_y))
938
- imgui.text(node.label)
939
-
940
- # Restore text color
941
- imgui.pop_style_color()
942
-
943
- # Restore original font scale
944
- imgui.set_window_font_scale(original_scale)
945
-
946
- def reset_view(self):
947
- """Reset camera view to default"""
948
- self.position = glm.vec3(0.0, -10.0, 0.0)
949
- self.front = glm.vec3(0.0, 1.0, 0.0)
950
- self.yaw = 90.0
951
- self.pitch = 0.0
952
-
953
-
954
- def generate_colors(n: int) -> List[glm.vec3]:
955
- """Generate n distinct colors using HSV color space"""
956
- colors = []
957
- for i in range(n):
958
- # Use golden ratio to generate well-distributed hues
959
- hue = (i * 0.618033988749895) % 1.0
960
- # Fixed saturation and value for vibrant colors
961
- saturation = 0.8
962
- value = 0.95
963
- # Convert HSV to RGB
964
- rgb = colorsys.hsv_to_rgb(hue, saturation, value)
965
- # Add alpha channel
966
- colors.append(glm.vec3(rgb))
967
- return colors
968
-
969
-
970
- def show_file_dialog() -> Optional[str]:
971
- """Show a file dialog for selecting GraphML files"""
972
- root = tk.Tk()
973
- root.withdraw() # Hide the main window
974
- file_path = filedialog.askopenfilename(
975
- title="Select GraphML File",
976
- filetypes=[("GraphML files", "*.graphml"), ("All files", "*.*")],
977
- )
978
- root.destroy()
979
- return file_path if file_path else None
980
-
981
-
982
- def create_sphere(sectors: int = 32, rings: int = 16) -> Tuple:
983
- """
984
- Creates a sphere.
985
- """
986
- R = 1.0 / (rings - 1)
987
- S = 1.0 / (sectors - 1)
988
-
989
- # Use those names as normals and uvs are part of the API
990
- vertices_l = [0.0] * (rings * sectors * 3)
991
- # normals_l = [0.0] * (rings * sectors * 3)
992
- uvs_l = [0.0] * (rings * sectors * 2)
993
-
994
- v, n, t = 0, 0, 0
995
- for r in range(rings):
996
- for s in range(sectors):
997
- y = np.sin(-np.pi / 2 + np.pi * r * R)
998
- x = np.cos(2 * np.pi * s * S) * np.sin(np.pi * r * R)
999
- z = np.sin(2 * np.pi * s * S) * np.sin(np.pi * r * R)
1000
-
1001
- uvs_l[t] = s * S
1002
- uvs_l[t + 1] = r * R
1003
-
1004
- vertices_l[v] = x
1005
- vertices_l[v + 1] = y
1006
- vertices_l[v + 2] = z
1007
-
1008
- t += 2
1009
- v += 3
1010
- n += 3
1011
-
1012
- indices = [0] * rings * sectors * 6
1013
- i = 0
1014
- for r in range(rings - 1):
1015
- for s in range(sectors - 1):
1016
- indices[i] = r * sectors + s
1017
- indices[i + 1] = (r + 1) * sectors + (s + 1)
1018
- indices[i + 2] = r * sectors + (s + 1)
1019
-
1020
- indices[i + 3] = r * sectors + s
1021
- indices[i + 4] = (r + 1) * sectors + s
1022
- indices[i + 5] = (r + 1) * sectors + (s + 1)
1023
- i += 6
1024
-
1025
- vbo_vertices = np.array(vertices_l, dtype=np.float32)
1026
- vbo_elements = np.array(indices, dtype=np.uint32)
1027
-
1028
- return (vbo_vertices, vbo_elements)
1029
-
1030
-
1031
- def draw_text_with_bg(
1032
- text: str,
1033
- text_pos: imgui.ImVec2Like,
1034
- text_size: imgui.ImVec2Like,
1035
- bg_color: int,
1036
- ):
1037
- imgui.get_window_draw_list().add_rect_filled(
1038
- (text_pos[0] - 5, text_pos[1] - 5),
1039
- (text_pos[0] + text_size[0] + 5, text_pos[1] + text_size[1] + 5),
1040
- bg_color,
1041
- 3.0,
1042
- )
1043
- imgui.set_cursor_pos(text_pos)
1044
- imgui.text(text)
1045
-
1046
-
1047
- def main():
1048
- """Main application entry point"""
1049
- viewer = GraphViewer()
1050
-
1051
- show_fps = True
1052
- text_bg_color = imgui.IM_COL32(0, 0, 0, 100)
1053
-
1054
- def gui():
1055
- if not viewer.initialized:
1056
- viewer.setup()
1057
- # # Change the theme
1058
- # tweaked_theme = hello_imgui.get_runner_params().imgui_window_params.tweaked_theme
1059
- # tweaked_theme.theme = hello_imgui.ImGuiTheme_.darcula_darker
1060
- # hello_imgui.apply_tweaked_theme(tweaked_theme)
1061
-
1062
- viewer.window_width = int(imgui.get_window_width())
1063
- viewer.window_height = int(imgui.get_window_height())
1064
-
1065
- # Handle keyboard and mouse input
1066
- viewer.handle_keyboard_input()
1067
- viewer.handle_mouse_interaction()
1068
-
1069
- style = imgui.get_style()
1070
- window_bg_color = style.color_(imgui.Col_.window_bg.value)
1071
-
1072
- window_bg_color.w = 0.8
1073
- style.set_color_(imgui.Col_.window_bg.value, window_bg_color)
1074
-
1075
- # Main control window
1076
- imgui.begin("Graph Controls")
1077
-
1078
- if imgui.button("Load GraphML"):
1079
- filepath = show_file_dialog()
1080
- if filepath:
1081
- viewer.load_file(filepath)
1082
-
1083
- # Show error message if loading failed
1084
- if viewer.show_load_error:
1085
- imgui.push_style_color(imgui.Col_.text, (1.0, 0.0, 0.0, 1.0))
1086
- imgui.text(f"Error loading file: {viewer.error_message}")
1087
- imgui.pop_style_color()
1088
-
1089
- imgui.separator()
1090
-
1091
- # Camera controls help
1092
- imgui.text("Camera Controls:")
1093
- imgui.bullet_text("Hold Right Mouse - Look around")
1094
- imgui.bullet_text("W/S - Move forward/backward")
1095
- imgui.bullet_text("A/D - Move left/right")
1096
- imgui.bullet_text("Q/E - Move up/down")
1097
- imgui.bullet_text("Left Mouse - Select node")
1098
- imgui.bullet_text("Wheel - Change the movement speed")
1099
-
1100
- imgui.separator()
1101
-
1102
- # Camera settings
1103
- _, viewer.move_speed = imgui.slider_float(
1104
- "Movement Speed", viewer.move_speed, 0.01, 2.0
1105
- )
1106
- _, viewer.mouse_sensitivity = imgui.slider_float(
1107
- "Mouse Sensitivity", viewer.mouse_sensitivity, 0.01, 0.5
1108
- )
1109
-
1110
- imgui.separator()
1111
-
1112
- imgui.begin_horizontal("buttons")
1113
-
1114
- if imgui.button("Reset Camera"):
1115
- viewer.reset_view()
1116
-
1117
- if imgui.button("Update Layout") and viewer.graph:
1118
- viewer.update_layout()
1119
-
1120
- # if imgui.button("Save Node ID Texture"):
1121
- # viewer.save_node_id_texture_to_png("node_id_texture.png")
1122
-
1123
- imgui.end_horizontal()
1124
-
1125
- imgui.end()
1126
-
1127
- # Render node details window if a node is selected
1128
- viewer.render_node_details()
1129
-
1130
- # Render graph settings window
1131
- viewer.render_settings()
1132
-
1133
- # Render FPS
1134
- if show_fps:
1135
- imgui.set_window_font_scale(1)
1136
- fps_text = f"FPS: {hello_imgui.frame_rate():.1f}"
1137
- text_size = imgui.calc_text_size(fps_text)
1138
- cursor_pos = (10, viewer.window_height - text_size.y - 10)
1139
- draw_text_with_bg(fps_text, cursor_pos, text_size, text_bg_color)
1140
-
1141
- # Render highlighted node ID
1142
- if viewer.highlighted_node:
1143
- imgui.set_window_font_scale(1)
1144
- node_text = f"Node ID: {viewer.highlighted_node.label}"
1145
- text_size = imgui.calc_text_size(node_text)
1146
- cursor_pos = (
1147
- viewer.window_width - text_size.x - 10,
1148
- viewer.window_height - text_size.y - 10,
1149
- )
1150
- draw_text_with_bg(node_text, cursor_pos, text_size, text_bg_color)
1151
-
1152
- window_bg_color.w = 0
1153
- style.set_color_(imgui.Col_.window_bg.value, window_bg_color)
1154
-
1155
- # Render labels
1156
- viewer.render_labels()
1157
-
1158
- def custom_background():
1159
- if viewer.initialized:
1160
- viewer.render()
1161
-
1162
- runner_params = hello_imgui.RunnerParams()
1163
- runner_params.app_window_params.window_geometry.size = (
1164
- viewer.window_width,
1165
- viewer.window_height,
1166
- )
1167
- runner_params.app_window_params.window_title = "3D GraphML Viewer"
1168
- runner_params.callbacks.show_gui = gui
1169
- runner_params.callbacks.custom_background = custom_background
1170
-
1171
- def load_font():
1172
- # You will need to provide it yourself, or use another font.
1173
- font_filename = CUSTOM_FONT
1174
-
1175
- io = imgui.get_io()
1176
- io.fonts.tex_desired_width = 4096 # Larger texture for better CJK font quality
1177
- font_size_pixels = 14
1178
- asset_dir = os.path.join(os.path.dirname(__file__), "assets")
1179
-
1180
- # Try to load custom font
1181
- if not os.path.isfile(font_filename):
1182
- font_filename = os.path.join(asset_dir, font_filename)
1183
- if os.path.isfile(font_filename):
1184
- custom_font = io.fonts.add_font_from_file_ttf(
1185
- filename=font_filename,
1186
- size_pixels=font_size_pixels,
1187
- glyph_ranges_as_int_list=io.fonts.get_glyph_ranges_chinese_full(),
1188
- )
1189
- io.font_default = custom_font
1190
- return
1191
-
1192
- # Load default fonts
1193
- io.fonts.add_font_from_file_ttf(
1194
- filename=os.path.join(asset_dir, DEFAULT_FONT_ENG),
1195
- size_pixels=font_size_pixels,
1196
- )
1197
-
1198
- font_config = imgui.ImFontConfig()
1199
- font_config.merge_mode = True
1200
-
1201
- io.font_default = io.fonts.add_font_from_file_ttf(
1202
- filename=os.path.join(asset_dir, DEFAULT_FONT_CHI),
1203
- size_pixels=font_size_pixels,
1204
- font_cfg=font_config,
1205
- glyph_ranges_as_int_list=io.fonts.get_glyph_ranges_chinese_full(),
1206
- )
1207
-
1208
- runner_params.callbacks.load_additional_fonts = load_font
1209
-
1210
- immapp.run(runner_params)
1211
-
1212
-
1213
- if __name__ == "__main__":
1214
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
{extra/VisualizationTool β†’ lightrag/tools/lightrag_visualizer}/requirements.txt RENAMED
File without changes
setup.py CHANGED
@@ -62,6 +62,16 @@ def read_api_requirements():
62
  return api_deps
63
 
64
 
 
 
 
 
 
 
 
 
 
 
65
  metadata = retrieve_metadata()
66
  long_description = read_long_description()
67
  requirements = read_requirements()
@@ -97,10 +107,12 @@ setuptools.setup(
97
  },
98
  extras_require={
99
  "api": read_api_requirements(), # API requirements as optional
 
100
  },
101
  entry_points={
102
  "console_scripts": [
103
  "lightrag-server=lightrag.api.lightrag_server:main [api]",
 
104
  ],
105
  },
106
  )
 
62
  return api_deps
63
 
64
 
65
+ def read_extra_requirements():
66
+ api_deps = []
67
+ try:
68
+ with open("./lightrag/extra/VisualizationTool/requirements.txt") as f:
69
+ api_deps = [line.strip() for line in f if line.strip()]
70
+ except FileNotFoundError:
71
+ print("Warning: API requirements.txt not found.")
72
+ return api_deps
73
+
74
+
75
  metadata = retrieve_metadata()
76
  long_description = read_long_description()
77
  requirements = read_requirements()
 
107
  },
108
  extras_require={
109
  "api": read_api_requirements(), # API requirements as optional
110
+ "tools": read_extra_requirements(), # API requirements as optional
111
  },
112
  entry_points={
113
  "console_scripts": [
114
  "lightrag-server=lightrag.api.lightrag_server:main [api]",
115
+ "lightrag-viewer=lightrag.tools.lightrag_visualizer:main [tools]",
116
  ],
117
  },
118
  )