-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization_vispy.py
More file actions
60 lines (49 loc) · 2.21 KB
/
visualization_vispy.py
File metadata and controls
60 lines (49 loc) · 2.21 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
# visualization_vispy.py
import vispy
from vispy import app, scene
from vispy.visuals import transforms
import networkx as nx
import numpy as np
class VispyVisualization(app.Canvas):
def __init__(self, tree, settings):
app.Canvas.__init__(self, title='Evolution 3D Visualizer with VisPy', keys='interactive')
self.tree = tree
self.settings = settings
# Create a 3D scene
self.view = scene.SceneCanvas(keys='interactive', size=(800, 600), show=True)
self.view.camera = scene.cameras.TurntableCamera(up='z', fov=60)
# Add a grid
grid = self.view.central_widget.add_grid()
# Create a scatter plot for nodes
self.node_positions = np.array([self.tree.nodes[node]['pos'] for node in self.tree.nodes()])
self.node_colors = np.array([self.get_color_for_attribute(self.tree.nodes[node].get('attribute', 'default')) for node in self.tree.nodes()])
self.scatter = scene.visuals.Markers()
self.scatter.set_data(self.node_positions, face_color=self.node_colors, size=5)
self.view.add(self.scatter)
# Create a line plot for edges
edges = []
edge_colors = []
for edge in self.tree.edges():
start_pos = self.tree.nodes[edge[0]]['pos']
end_pos = self.tree.nodes[edge[1]]['pos']
edges.append([start_pos, end_pos])
edge_colors.append([0.5, 0.5, 0.5, 1.0]) # RGBA color for edges
self.edge_segments = np.array(edges)
self.lines = scene.visuals.Line(pos=self.edge_segments.reshape(-1, 3), connect='segments', color=edge_colors)
self.view.add(self.lines)
# Show the canvas
self.show()
def get_color_for_attribute(self, attr):
color_map = {
'attribute_value_1': [1.0, 0.0, 0.0, 1.0], # Red
'attribute_value_2': [0.0, 1.0, 0.0, 1.0], # Green
'default': [0.5, 0.5, 0.5, 1.0], # Gray
}
return color_map.get(attr, color_map['default'])
def on_draw(self, event):
gloo.clear('white')
self.view.draw()
if __name__ == '__main__':
# Assume 'tree' and 'settings' are already defined
vis = VispyVisualization(tree, settings)
vispy.app.run()