Commit 4407a915bbb835c97a22b1e3b12045c0312319ca
1 parent
25fa0bfe
working with new vispy
Showing
7 changed files
with
162 additions
and
84 deletions
Show diff stats
GraphCanvas.py
@@ -13,6 +13,7 @@ from vispy.gloo import set_viewport, set_state, clear, set_blend_color, context | @@ -13,6 +13,7 @@ from vispy.gloo import set_viewport, set_state, clear, set_blend_color, context | ||
13 | from vispy.util.transforms import perspective, translate, rotate, scale | 13 | from vispy.util.transforms import perspective, translate, rotate, scale |
14 | import vispy.gloo.gl as glcore | 14 | import vispy.gloo.gl as glcore |
15 | from vispy import app | 15 | from vispy import app |
16 | +import copy | ||
16 | 17 | ||
17 | import numpy as np | 18 | import numpy as np |
18 | import math | 19 | import math |
@@ -91,7 +92,6 @@ class GraphCanvas(scene.SceneCanvas): | @@ -91,7 +92,6 @@ class GraphCanvas(scene.SceneCanvas): | ||
91 | self.moving = False | 92 | self.moving = False |
92 | self.moving_cluster = False | 93 | self.moving_cluster = False |
93 | self.selection = False | 94 | self.selection = False |
94 | - | ||
95 | n = 10 | 95 | n = 10 |
96 | ne = 10 | 96 | ne = 10 |
97 | #Init dummy structures | 97 | #Init dummy structures |
@@ -209,6 +209,9 @@ class GraphCanvas(scene.SceneCanvas): | @@ -209,6 +209,9 @@ class GraphCanvas(scene.SceneCanvas): | ||
209 | 209 | ||
210 | 210 | ||
211 | self.timer = app.Timer('auto', connect=self.on_timer, start=False) | 211 | self.timer = app.Timer('auto', connect=self.on_timer, start=False) |
212 | + #self.constant = app.Timer('auto', connect=self.update, start=True) | ||
213 | + self.num=0 | ||
214 | + print(self.context.config) | ||
212 | 215 | ||
213 | def on_timer(self, event): | 216 | def on_timer(self, event): |
214 | #get the temporary positions of the vertices (and edges) | 217 | #get the temporary positions of the vertices (and edges) |
@@ -285,7 +288,7 @@ class GraphCanvas(scene.SceneCanvas): | @@ -285,7 +288,7 @@ class GraphCanvas(scene.SceneCanvas): | ||
285 | self.program_s.bind(self.vbo_s) | 288 | self.program_s.bind(self.vbo_s) |
286 | self.program_e_s.bind(self.vbo_cluster_lines) | 289 | self.program_e_s.bind(self.vbo_cluster_lines) |
287 | 290 | ||
288 | - self.update() | 291 | + self.refresh() |
289 | 292 | ||
290 | """ | 293 | """ |
291 | Function that recolors vertices based on the selected statistic | 294 | Function that recolors vertices based on the selected statistic |
@@ -306,7 +309,7 @@ class GraphCanvas(scene.SceneCanvas): | @@ -306,7 +309,7 @@ class GraphCanvas(scene.SceneCanvas): | ||
306 | self.vbo = gloo.VertexBuffer(self.data) | 309 | self.vbo = gloo.VertexBuffer(self.data) |
307 | self.program.bind(self.vbo) | 310 | self.program.bind(self.vbo) |
308 | #self.program_e.bind(self.vbo) | 311 | #self.program_e.bind(self.vbo) |
309 | - self.update() | 312 | + self.refresh() |
310 | 313 | ||
311 | def update_color_buffers(self): | 314 | def update_color_buffers(self): |
312 | color = self.G.vertex_properties["RGBA"].get_2d_array(range(4)).T | 315 | color = self.G.vertex_properties["RGBA"].get_2d_array(range(4)).T |
@@ -352,7 +355,7 @@ class GraphCanvas(scene.SceneCanvas): | @@ -352,7 +355,7 @@ class GraphCanvas(scene.SceneCanvas): | ||
352 | self.vbo = gloo.VertexBuffer(self.data) | 355 | self.vbo = gloo.VertexBuffer(self.data) |
353 | self.program.bind(self.vbo) | 356 | self.program.bind(self.vbo) |
354 | #self.program_e.bind(self.vbo) | 357 | #self.program_e.bind(self.vbo) |
355 | - self.update() | 358 | + self.refresh() |
356 | 359 | ||
357 | 360 | ||
358 | """ | 361 | """ |
@@ -461,7 +464,7 @@ class GraphCanvas(scene.SceneCanvas): | @@ -461,7 +464,7 @@ class GraphCanvas(scene.SceneCanvas): | ||
461 | self.vbo_line = gloo.VertexBuffer(self.line_data) | 464 | self.vbo_line = gloo.VertexBuffer(self.line_data) |
462 | self.program.bind(self.vbo) | 465 | self.program.bind(self.vbo) |
463 | self.program_e.bind(self.vbo_line) | 466 | self.program_e.bind(self.vbo_line) |
464 | - self.update() | 467 | + self.refresh() |
465 | 468 | ||
466 | """ | 469 | """ |
467 | Sets the edge color based on the the cluster the vertices belongs to | 470 | Sets the edge color based on the the cluster the vertices belongs to |
@@ -480,10 +483,10 @@ class GraphCanvas(scene.SceneCanvas): | @@ -480,10 +483,10 @@ class GraphCanvas(scene.SceneCanvas): | ||
480 | Test function that only binds the buffer | 483 | Test function that only binds the buffer |
481 | """ | 484 | """ |
482 | def gen_vertex_vbo_minimalist(self): | 485 | def gen_vertex_vbo_minimalist(self): |
483 | - self.update() | 486 | + self.refresh() |
484 | self.vbo.set_data(self.data) | 487 | self.vbo.set_data(self.data) |
485 | self.program.bind(self.vbo) | 488 | self.program.bind(self.vbo) |
486 | - self.update() | 489 | + self.refresh() |
487 | 490 | ||
488 | """ | 491 | """ |
489 | Helper function that generates the framebuffer object that stores the vertices | 492 | Helper function that generates the framebuffer object that stores the vertices |
@@ -540,7 +543,7 @@ class GraphCanvas(scene.SceneCanvas): | @@ -540,7 +543,7 @@ class GraphCanvas(scene.SceneCanvas): | ||
540 | self.program_s.bind(self.vbo_s) | 543 | self.program_s.bind(self.vbo_s) |
541 | if DEBUG: | 544 | if DEBUG: |
542 | print(self.view) | 545 | print(self.view) |
543 | - self.update() | 546 | + self.refresh() |
544 | 547 | ||
545 | """ | 548 | """ |
546 | Helper function that creates colored "block" lines based on the edges | 549 | Helper function that creates colored "block" lines based on the edges |
@@ -898,7 +901,7 @@ class GraphCanvas(scene.SceneCanvas): | @@ -898,7 +901,7 @@ class GraphCanvas(scene.SceneCanvas): | ||
898 | # self.program_s.bind(self.vbo_s) | 901 | # self.program_s.bind(self.vbo_s) |
899 | # if DEBUG: | 902 | # if DEBUG: |
900 | # print(self.view) | 903 | # print(self.view) |
901 | -# self.update() | 904 | +# self.refresh() |
902 | 905 | ||
903 | 906 | ||
904 | """ | 907 | """ |
@@ -928,12 +931,59 @@ class GraphCanvas(scene.SceneCanvas): | @@ -928,12 +931,59 @@ class GraphCanvas(scene.SceneCanvas): | ||
928 | 931 | ||
929 | #Generate the clusters | 932 | #Generate the clusters |
930 | self.labels = nwt.Network.spectral_clustering(G,'length', n_clusters = n_c) | 933 | self.labels = nwt.Network.spectral_clustering(G,'length', n_clusters = n_c) |
934 | + bb = nwt.AABB(G) | ||
935 | + #print("FLJKKHDFLKJFDLKJFDLKJ ", m) | ||
936 | + pts = [] | ||
937 | + x, y, z = bb.project_grid(3) | ||
938 | + for i in range(3): | ||
939 | + for j in range(3): | ||
940 | + for k in range(3): | ||
941 | + pts.append(np.array([x[i], y[j], z[k]])) | ||
942 | + | ||
931 | #self.labels = nwt.Network.spectral_clustering(G,'length') | 943 | #self.labels = nwt.Network.spectral_clustering(G,'length') |
932 | 944 | ||
933 | #Add clusters as a vertex property | 945 | #Add clusters as a vertex property |
934 | G.vertex_properties["clusters"] = G.new_vertex_property("int", vals=self.labels) | 946 | G.vertex_properties["clusters"] = G.new_vertex_property("int", vals=self.labels) |
935 | num_clusters = len(np.unique(self.labels)) | 947 | num_clusters = len(np.unique(self.labels)) |
936 | self.n_c = n_c | 948 | self.n_c = n_c |
949 | + new_indices = [] | ||
950 | + pos = G.vertex_properties["p"].get_2d_array(range(3)).T | ||
951 | + | ||
952 | + #for each cluster find the average vertex position and match to closest point | ||
953 | + #in the unique grid. | ||
954 | + for i in range(n_c): | ||
955 | + point = np.sum(pos[np.argwhere(self.labels == i)], axis=0)/len(np.argwhere(self.labels == i)) | ||
956 | + d = 100000000.0 | ||
957 | + idx = -1 | ||
958 | + for j in range(len(pts)): | ||
959 | + dist = np.sqrt(np.power(pts[j][0]-point[0,0],2) + np.power(pts[j][1]-point[0,1],2) + np.power(pts[j][2]-point[0,2],2)) | ||
960 | + if dist < d: | ||
961 | + d = dist | ||
962 | + idx = j | ||
963 | + new_indices.append(idx) | ||
964 | + pts[idx] = np.array([100000000.0, 1000000000.0, 100000000.0]) | ||
965 | + #since there are more points than clusters, we need to make the indices range from | ||
966 | + #[0, n_c) | ||
967 | + j=0 | ||
968 | + unique_indices = np.array(new_indices) | ||
969 | + for i in range(n_c): | ||
970 | + idx = np.argmin(new_indices) | ||
971 | + unique_indices[idx] = j | ||
972 | + j += 1 | ||
973 | + new_indices[idx] = 100 | ||
974 | + | ||
975 | + lbl = np.zeros(self.labels.shape) | ||
976 | + for i in range(n_c): | ||
977 | + idxs = np.argwhere(self.labels == i) | ||
978 | + new_idx = np.argwhere(unique_indices == i) | ||
979 | + lbl[idxs] = unique_indices[i] | ||
980 | + | ||
981 | + self.labels = lbl | ||
982 | + G.vertex_properties["clusters"] = G.new_vertex_property("int", vals=self.labels) | ||
983 | + | ||
984 | + | ||
985 | + | ||
986 | + | ||
937 | 987 | ||
938 | #add colormap | 988 | #add colormap |
939 | G.vertex_properties["RGBA"] = nwt.Network.map_property_to_color(G, G.vertex_properties["clusters"]) | 989 | G.vertex_properties["RGBA"] = nwt.Network.map_property_to_color(G, G.vertex_properties["clusters"]) |
@@ -1072,7 +1122,7 @@ class GraphCanvas(scene.SceneCanvas): | @@ -1072,7 +1122,7 @@ class GraphCanvas(scene.SceneCanvas): | ||
1072 | self.program_s.bind(self.vbo_s) | 1122 | self.program_s.bind(self.vbo_s) |
1073 | if DEBUG: | 1123 | if DEBUG: |
1074 | print(self.view) | 1124 | print(self.view) |
1075 | - self.update() | 1125 | + self.refresh() |
1076 | 1126 | ||
1077 | """ | 1127 | """ |
1078 | Loads the data G and generates all the buffers necessary as well as performs | 1128 | Loads the data G and generates all the buffers necessary as well as performs |
@@ -1143,7 +1193,7 @@ class GraphCanvas(scene.SceneCanvas): | @@ -1143,7 +1193,7 @@ class GraphCanvas(scene.SceneCanvas): | ||
1143 | self.program_s.bind(self.vbo_s) | 1193 | self.program_s.bind(self.vbo_s) |
1144 | if DEBUG: | 1194 | if DEBUG: |
1145 | print(self.view) | 1195 | print(self.view) |
1146 | - self.update() | 1196 | + self.refresh() |
1147 | 1197 | ||
1148 | """ | 1198 | """ |
1149 | Function that changes and redraws the buffer during a resize event. | 1199 | Function that changes and redraws the buffer during a resize event. |
@@ -1165,7 +1215,20 @@ class GraphCanvas(scene.SceneCanvas): | @@ -1165,7 +1215,20 @@ class GraphCanvas(scene.SceneCanvas): | ||
1165 | if(self.subgraphs): | 1215 | if(self.subgraphs): |
1166 | self.program_e_s.draw('triangles', indices=self.index_clusters_s) | 1216 | self.program_e_s.draw('triangles', indices=self.index_clusters_s) |
1167 | self.program_s.draw('triangles', indices=self.index_s) | 1217 | self.program_s.draw('triangles', indices=self.index_s) |
1218 | + print("updated ", self.num) | ||
1219 | + self.num += 1 | ||
1220 | + #self._u | ||
1221 | + #self._update_pending = True | ||
1222 | + #app.Canvas.update(self) | ||
1223 | + #super(scene.SceneCanvas, self).update() #This forces redrawd | ||
1168 | 1224 | ||
1225 | + """ | ||
1226 | + refreshes the canvas and forces the redraw. A workaround for issue in | ||
1227 | + vispy 0.6.3 | ||
1228 | + """ | ||
1229 | + def refresh(self): | ||
1230 | + self.update() | ||
1231 | + app.Canvas.update(self) | ||
1169 | 1232 | ||
1170 | # """ | 1233 | # """ |
1171 | # A function to animate from one layout to another layout given a new G. | 1234 | # A function to animate from one layout to another layout given a new G. |
@@ -1259,7 +1322,7 @@ class GraphCanvas(scene.SceneCanvas): | @@ -1259,7 +1322,7 @@ class GraphCanvas(scene.SceneCanvas): | ||
1259 | # print(self.color_dict[tuple(color)]) | 1322 | # print(self.color_dict[tuple(color)]) |
1260 | 1323 | ||
1261 | #reset the original buffer | 1324 | #reset the original buffer |
1262 | - self.update() | 1325 | + self.refresh() |
1263 | 1326 | ||
1264 | #Return the element under the click. | 1327 | #Return the element under the click. |
1265 | if clusters == False: | 1328 | if clusters == False: |
@@ -1343,7 +1406,7 @@ class GraphCanvas(scene.SceneCanvas): | @@ -1343,7 +1406,7 @@ class GraphCanvas(scene.SceneCanvas): | ||
1343 | def update_vbo(self): | 1406 | def update_vbo(self): |
1344 | self.vbo = gloo.VertexBuffer(self.data) | 1407 | self.vbo = gloo.VertexBuffer(self.data) |
1345 | self.program.bind(self.vbo) | 1408 | self.program.bind(self.vbo) |
1346 | - self.update() | 1409 | + self.refresh() |
1347 | 1410 | ||
1348 | def update_vertex_alpha(self, vertex, alpha): | 1411 | def update_vertex_alpha(self, vertex, alpha): |
1349 | temp = self.G.vertex_properties["RGBA"][vertex] | 1412 | temp = self.G.vertex_properties["RGBA"][vertex] |
@@ -1605,7 +1668,7 @@ class GraphCanvas(scene.SceneCanvas): | @@ -1605,7 +1668,7 @@ class GraphCanvas(scene.SceneCanvas): | ||
1605 | self.program_e.bind(self.vbo_line) | 1668 | self.program_e.bind(self.vbo_line) |
1606 | #self.program.draw('points') | 1669 | #self.program.draw('points') |
1607 | self.location = event.pos | 1670 | self.location = event.pos |
1608 | - self.update() | 1671 | + self.refresh() |
1609 | elif(self.moving == True and self.moving_cluster == True): | 1672 | elif(self.moving == True and self.moving_cluster == True): |
1610 | if(len(self.c_id) < 2): | 1673 | if(len(self.c_id) < 2): |
1611 | #Project into GLSpace and get before and after move coordinates | 1674 | #Project into GLSpace and get before and after move coordinates |
@@ -1640,7 +1703,7 @@ class GraphCanvas(scene.SceneCanvas): | @@ -1640,7 +1703,7 @@ class GraphCanvas(scene.SceneCanvas): | ||
1640 | self.vbo_s = gloo.VertexBuffer(self.clusters) | 1703 | self.vbo_s = gloo.VertexBuffer(self.clusters) |
1641 | self.program_s.bind(self.vbo_s) | 1704 | self.program_s.bind(self.vbo_s) |
1642 | self.update_cluster_line_vbo() | 1705 | self.update_cluster_line_vbo() |
1643 | - self.update() | 1706 | + self.refresh() |
1644 | 1707 | ||
1645 | 1708 | ||
1646 | else: | 1709 | else: |
@@ -1660,7 +1723,7 @@ class GraphCanvas(scene.SceneCanvas): | @@ -1660,7 +1723,7 @@ class GraphCanvas(scene.SceneCanvas): | ||
1660 | self.program_s['u_view'] = self.view | 1723 | self.program_s['u_view'] = self.view |
1661 | self.program_e_s['u_view'] = self.view | 1724 | self.program_e_s['u_view'] = self.view |
1662 | self.location = event.pos | 1725 | self.location = event.pos |
1663 | - self.update() | 1726 | + self.refresh() |
1664 | 1727 | ||
1665 | """ | 1728 | """ |
1666 | Handles the mouse wheel zoom event. | 1729 | Handles the mouse wheel zoom event. |
@@ -1687,4 +1750,4 @@ class GraphCanvas(scene.SceneCanvas): | @@ -1687,4 +1750,4 @@ class GraphCanvas(scene.SceneCanvas): | ||
1687 | self.program_s['u_view'] = self.view | 1750 | self.program_s['u_view'] = self.view |
1688 | self.program_e_s['u_view'] = self.view | 1751 | self.program_e_s['u_view'] = self.view |
1689 | #print(event.delta[1]) | 1752 | #print(event.delta[1]) |
1690 | - self.update() | 1753 | + self.refresh() |
GraphWidget.py
@@ -7,7 +7,7 @@ Created on Mon Aug 5 15:42:19 2019 | @@ -7,7 +7,7 @@ Created on Mon Aug 5 15:42:19 2019 | ||
7 | """ | 7 | """ |
8 | 8 | ||
9 | from GraphCanvas import GraphCanvas | 9 | from GraphCanvas import GraphCanvas |
10 | -from pyqtgraph.Qt import QtCore, QtGui, QtWidgets | 10 | +from PyQt5 import QtCore, QtGui, QtWidgets |
11 | import network_dep as nwt | 11 | import network_dep as nwt |
12 | 12 | ||
13 | import numpy as np | 13 | import numpy as np |
@@ -19,14 +19,16 @@ Initializes the entire QTlayout and sets the mouse press events. | @@ -19,14 +19,16 @@ Initializes the entire QTlayout and sets the mouse press events. | ||
19 | These are connected to the slots such that each is processes by this class | 19 | These are connected to the slots such that each is processes by this class |
20 | and the class it wraps. | 20 | and the class it wraps. |
21 | """ | 21 | """ |
22 | -class GraphWidget(QtGui.QWidget): | 22 | +#class GraphWidget(QtGui.QWidget): |
23 | +class GraphWidget(QtWidgets.QWidget): | ||
23 | select = QtCore.pyqtSignal(list) | 24 | select = QtCore.pyqtSignal(list) |
24 | def __init__(self): | 25 | def __init__(self): |
25 | super(GraphWidget, self).__init__() | 26 | super(GraphWidget, self).__init__() |
26 | - box = QtGui.QVBoxLayout(self) | 27 | + box = QtWidgets.QVBoxLayout(self) |
27 | self.resize(500,500) | 28 | self.resize(500,500) |
28 | self.setLayout(box) | 29 | self.setLayout(box) |
29 | self.canvas = GraphCanvas() | 30 | self.canvas = GraphCanvas() |
31 | + self.setUpdatesEnabled(True) | ||
30 | #self.canvas.create_native() | 32 | #self.canvas.create_native() |
31 | box.addWidget(self.canvas.native) | 33 | box.addWidget(self.canvas.native) |
32 | self.color = True | 34 | self.color = True |
@@ -37,8 +39,9 @@ class GraphWidget(QtGui.QWidget): | @@ -37,8 +39,9 @@ class GraphWidget(QtGui.QWidget): | ||
37 | self.canvas.events.mouse_release.connect(self.on_mouse_release) | 39 | self.canvas.events.mouse_release.connect(self.on_mouse_release) |
38 | self.canvas.events.mouse_move.connect(self.on_mouse_move) | 40 | self.canvas.events.mouse_move.connect(self.on_mouse_move) |
39 | self.canvas.events.mouse_double_click.connect(self.on_mouse_double_click) | 41 | self.canvas.events.mouse_double_click.connect(self.on_mouse_double_click) |
40 | - | 42 | + #self.canvas.events. |
41 | #self.show() | 43 | #self.show() |
44 | + #self.repaint() | ||
42 | 45 | ||
43 | """ | 46 | """ |
44 | Handles the right click mouse event at the QWidget level. | 47 | Handles the right click mouse event at the QWidget level. |
@@ -49,11 +52,16 @@ class GraphWidget(QtGui.QWidget): | @@ -49,11 +52,16 @@ class GraphWidget(QtGui.QWidget): | ||
49 | The context menu level actions are also coded in this section of the code. | 52 | The context menu level actions are also coded in this section of the code. |
50 | """ | 53 | """ |
51 | def on_mouse_press(self, event): | 54 | def on_mouse_press(self, event): |
55 | + self.canvas.set_current(event) | ||
52 | if event.button == 2: | 56 | if event.button == 2: |
53 | if self.canvas.view[0][0] >= 0.0010: | 57 | if self.canvas.view[0][0] >= 0.0010: |
58 | + self.canvas.update() | ||
54 | c_id = self.canvas.get_clicked_id(event) | 59 | c_id = self.canvas.get_clicked_id(event) |
60 | + self.canvas.update() | ||
55 | else: | 61 | else: |
62 | + self.canvas.update() | ||
56 | c_id = self.canvas.get_clicked_id(event, clusters=True) | 63 | c_id = self.canvas.get_clicked_id(event, clusters=True) |
64 | + self.canvas.update() | ||
57 | #right click | 65 | #right click |
58 | if(c_id == None): | 66 | if(c_id == None): |
59 | menu = QtWidgets.QMenu(self) | 67 | menu = QtWidgets.QMenu(self) |
@@ -178,15 +186,16 @@ class GraphWidget(QtGui.QWidget): | @@ -178,15 +186,16 @@ class GraphWidget(QtGui.QWidget): | ||
178 | Handles the mouse double click event. | 186 | Handles the mouse double click event. |
179 | Pass-through function. | 187 | Pass-through function. |
180 | """ | 188 | """ |
189 | + | ||
181 | def on_mouse_double_click(self, event): | 190 | def on_mouse_double_click(self, event): |
182 | self.canvas.update_path(event) | 191 | self.canvas.update_path(event) |
183 | self.select.emit(self.canvas.get_path()) | 192 | self.select.emit(self.canvas.get_path()) |
184 | - old_pos = self.canvas.G.vertex_properties["pos"].get_2d_array(range(3)).T | ||
185 | - nG = nwt.Network.gen_new_fd_layout(self.canvas.G) | 193 | +# old_pos = self.canvas.G.vertex_properties["pos"].get_2d_array(range(3)).T |
194 | +# nG = nwt.Network.gen_new_fd_layout(self.canvas.G) | ||
186 | # def animate(self, new_G): | 195 | # def animate(self, new_G): |
187 | # | 196 | # |
188 | - new_pos = nG.vertex_properties["pos"].get_2d_array(range(3)).T | ||
189 | - self.canvas.animate(old_pos, new_pos) | 197 | +# new_pos = nG.vertex_properties["pos"].get_2d_array(range(3)).T |
198 | +# self.canvas.animate(old_pos, new_pos) | ||
190 | 199 | ||
191 | """ | 200 | """ |
192 | Handles the mouse release event. | 201 | Handles the mouse release event. |
GuiVisPy_tube.py
@@ -7,35 +7,39 @@ Created on Thu Jan 31 15:29:40 2019 | @@ -7,35 +7,39 @@ Created on Thu Jan 31 15:29:40 2019 | ||
7 | `""" | 7 | `""" |
8 | 8 | ||
9 | from vispy import app | 9 | from vispy import app |
10 | - | 10 | +import vispy |
11 | 11 | ||
12 | import network_dep as nwt | 12 | import network_dep as nwt |
13 | 13 | ||
14 | -from pyqtgraph.Qt import QtCore, QtGui, QtWidgets | ||
15 | -import pyqtgraph as pg | 14 | +#from pyqtgraph.Qt import QtCore, QtGui, QtWidgets |
15 | +from PyQt5 import QtCore, QtGui, QtWidgets | ||
16 | +#import pyqtgraph as pg | ||
16 | import numpy as np | 17 | import numpy as np |
17 | import math | 18 | import math |
18 | 19 | ||
19 | from mpl_toolkits.mplot3d import Axes3D | 20 | from mpl_toolkits.mplot3d import Axes3D |
20 | -import matplotlib | ||
21 | -import matplotlib.pyplot as plt | ||
22 | -matplotlib.use('Qt5Agg') | 21 | +#import matplotlib |
22 | +#import matplotlib.pyplot as plt | ||
23 | +#matplotlib.use('Qt5Agg') | ||
23 | 24 | ||
24 | from GraphWidget import GraphWidget | 25 | from GraphWidget import GraphWidget |
25 | from TubeWidget import TubeWidget | 26 | from TubeWidget import TubeWidget |
27 | +#vispy.use(app='egl') | ||
26 | 28 | ||
27 | 29 | ||
28 | DEBUG = False | 30 | DEBUG = False |
29 | 31 | ||
30 | - | ||
31 | #set the backend. for different versions of PyQt | 32 | #set the backend. for different versions of PyQt |
32 | app.use_app(backend_name='PyQt5') | 33 | app.use_app(backend_name='PyQt5') |
33 | 34 | ||
35 | +app.set_interactive(True) | ||
36 | + | ||
34 | #Define a top level application | 37 | #Define a top level application |
35 | -appMain = QtGui.QApplication([]) | 38 | +appMain = QtWidgets.QApplication([]) |
39 | +#QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts, True) | ||
36 | 40 | ||
37 | ##Define a toplevel Widget | 41 | ##Define a toplevel Widget |
38 | -top = QtGui.QWidget() | 42 | +top = QtWidgets.QWidget() |
39 | top.resize(900, 900) | 43 | top.resize(900, 900) |
40 | 44 | ||
41 | 45 | ||
@@ -46,7 +50,7 @@ fibers.canvas.create_native() | @@ -46,7 +50,7 @@ fibers.canvas.create_native() | ||
46 | 50 | ||
47 | #plt = hist.addPlot() | 51 | #plt = hist.addPlot() |
48 | 52 | ||
49 | -layout = QtGui.QGridLayout() | 53 | +layout = QtWidgets.QGridLayout() |
50 | graph = GraphWidget() | 54 | graph = GraphWidget() |
51 | graph.canvas.create_native() | 55 | graph.canvas.create_native() |
52 | 56 | ||
@@ -54,14 +58,15 @@ graph.connect(fibers) | @@ -54,14 +58,15 @@ graph.connect(fibers) | ||
54 | fibers.connect(graph) | 58 | fibers.connect(graph) |
55 | 59 | ||
56 | #initialize the layout | 60 | #initialize the layout |
57 | -layout.addWidget(graph, 0, 0, 2, 3) | ||
58 | -layout.addWidget(fibers, 0, 2, 2, 3) | 61 | +layout.addWidget(graph.canvas.native, 0, 0, 2, 3) |
62 | +layout.addWidget(fibers.canvas.native, 0, 2, 2, 3) | ||
59 | layout.setColumnStretch(0, 2) | 63 | layout.setColumnStretch(0, 2) |
60 | layout.setRowStretch(0, 2) | 64 | layout.setRowStretch(0, 2) |
61 | layout.setColumnStretch(2, 1) | 65 | layout.setColumnStretch(2, 1) |
62 | layout.setRowStretch(0, 1) | 66 | layout.setRowStretch(0, 1) |
63 | 67 | ||
64 | top.setLayout(layout) | 68 | top.setLayout(layout) |
69 | + | ||
65 | top.show() | 70 | top.show() |
66 | 71 | ||
67 | 72 | ||
@@ -95,8 +100,8 @@ G, bbl, bbu = load_nwt("/home/pavel/Documents/Python/GraphGuiQt/network_4.nwt") | @@ -95,8 +100,8 @@ G, bbl, bbu = load_nwt("/home/pavel/Documents/Python/GraphGuiQt/network_4.nwt") | ||
95 | 100 | ||
96 | #G, bbl, bbu = load_nwt("/home/pavel/Documents/Python/GraphGuiQt/net`````````work_test_251_1kx3_short_NEW.nwt") | 101 | #G, bbl, bbu = load_nwt("/home/pavel/Documents/Python/GraphGuiQt/net`````````work_test_251_1kx3_short_NEW.nwt") |
97 | #ret = nwt.Network.get_affinity_matrix(G, "length") | 102 | #ret = nwt.Network.get_affinity_matrix(G, "length") |
98 | -node_image = QtGui.QImage("/home/pavel/Documents/Python/GraphGuiQt/node_tex.jpg") | ||
99 | -node_tex = QtGui.QBitmap.fromImage(node_image) | 103 | +#node_image = QtGui.QImage("/home/pavel/Documents/Python/GraphGuiQt/node_tex.jpg") |
104 | +#node_tex = QtGui.QBitmap.fromImage(node_image) | ||
100 | 105 | ||
101 | if DEBUG: | 106 | if DEBUG: |
102 | print(node_tex.depth()) | 107 | print(node_tex.depth()) |
@@ -109,10 +114,12 @@ graph.canvas.set_data(G, bbl, bbu) | @@ -109,10 +114,12 @@ graph.canvas.set_data(G, bbl, bbu) | ||
109 | fibers.canvas.set_data(G, bbl, bbu, 16) | 114 | fibers.canvas.set_data(G, bbl, bbu, 16) |
110 | #fibers.draw_all(G, center, fibers, graph, node_tex) | 115 | #fibers.draw_all(G, center, fibers, graph, node_tex) |
111 | graph.set_g_view(fibers) | 116 | graph.set_g_view(fibers) |
117 | + | ||
112 | ## Start Qt event loop unless running in interactive mode. | 118 | ## Start Qt event loop unless running in interactive mode. |
113 | if __name__ == '__main__': | 119 | if __name__ == '__main__': |
114 | - import sys | ||
115 | - if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): | ||
116 | - QtGui.QApplication.instance().exec_() | ||
117 | - | 120 | + #import sys |
121 | + #if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): | ||
122 | + #QtGui.QApplication.instance().exec_() | ||
123 | + app.set_interactive(enabled=True) | ||
124 | + app.run() | ||
118 | 125 |
TubeCanvas.py
@@ -10,7 +10,7 @@ Created on Mon Aug 5 15:56:47 2019 | @@ -10,7 +10,7 @@ Created on Mon Aug 5 15:56:47 2019 | ||
10 | Class that extends the vispy SceneCanvas to draw 3D tubes | 10 | Class that extends the vispy SceneCanvas to draw 3D tubes |
11 | """ | 11 | """ |
12 | 12 | ||
13 | -from vispy import gloo, scene | 13 | +from vispy import gloo, scene, app |
14 | from vispy.gloo import set_viewport, set_state, clear, set_blend_color, context | 14 | from vispy.gloo import set_viewport, set_state, clear, set_blend_color, context |
15 | from vispy.gloo import gl | 15 | from vispy.gloo import gl |
16 | from vispy.util.transforms import perspective, translate, rotate, scale | 16 | from vispy.util.transforms import perspective, translate, rotate, scale |
@@ -47,6 +47,7 @@ class TubeDraw(scene.SceneCanvas): | @@ -47,6 +47,7 @@ class TubeDraw(scene.SceneCanvas): | ||
47 | self.cylinder_data = np.zeros(5*5, dtype=[('a_position', np.float32, 3), | 47 | self.cylinder_data = np.zeros(5*5, dtype=[('a_position', np.float32, 3), |
48 | ('a_normal', np.float32, 3), | 48 | ('a_normal', np.float32, 3), |
49 | ('a_fg_color', np.float32, 4), | 49 | ('a_fg_color', np.float32, 4), |
50 | + ('a_selection', np.float32, 1), | ||
50 | #('a_linewidth', np.float32, 1), | 51 | #('a_linewidth', np.float32, 1), |
51 | ]) | 52 | ]) |
52 | self.triangle_data = np.random.randint(size=(5, 3), low=0, | 53 | self.triangle_data = np.random.randint(size=(5, 3), low=0, |
@@ -82,6 +83,8 @@ class TubeDraw(scene.SceneCanvas): | @@ -82,6 +83,8 @@ class TubeDraw(scene.SceneCanvas): | ||
82 | self.program['u_eye'] = self.camera | 83 | self.program['u_eye'] = self.camera |
83 | self.program['u_up'] = self.up | 84 | self.program['u_up'] = self.up |
84 | self.program['u_target'] = np.asarray([0., 0., 0.], dtype=np.float32) | 85 | self.program['u_target'] = np.asarray([0., 0., 0.], dtype=np.float32) |
86 | +# self.bb = np.ones((26, 3), dtype=np.float32) | ||
87 | +# self.program['u_bb'] = self.bb | ||
85 | 88 | ||
86 | 89 | ||
87 | 90 | ||
@@ -170,6 +173,10 @@ class TubeDraw(scene.SceneCanvas): | @@ -170,6 +173,10 @@ class TubeDraw(scene.SceneCanvas): | ||
170 | self.projection = perspective(90.0, self.physical_size[0]/self.physical_size[1], 1.0, 1000.0) | 173 | self.projection = perspective(90.0, self.physical_size[0]/self.physical_size[1], 1.0, 1000.0) |
171 | self.program['u_projection'] = self.projection | 174 | self.program['u_projection'] = self.projection |
172 | 175 | ||
176 | + | ||
177 | + def refresh(self): | ||
178 | + self.update() | ||
179 | + app.Canvas.update(self) | ||
173 | #Creates a cylinder around ever segment in the microvascular network. | 180 | #Creates a cylinder around ever segment in the microvascular network. |
174 | def gen_cylinder_vbo(self, G, num_sides = 32): | 181 | def gen_cylinder_vbo(self, G, num_sides = 32): |
175 | i = 0 | 182 | i = 0 |
@@ -417,7 +424,8 @@ class TubeDraw(scene.SceneCanvas): | @@ -417,7 +424,8 @@ class TubeDraw(scene.SceneCanvas): | ||
417 | #gloo.set_depth_func('lequal') | 424 | #gloo.set_depth_func('lequal') |
418 | #clear(color='white', depth=True) | 425 | #clear(color='white', depth=True) |
419 | #print(gloo.wrappers.get_gl_configuration()) | 426 | #print(gloo.wrappers.get_gl_configuration()) |
420 | - self.update() | 427 | + self.refresh() |
428 | + | ||
421 | #print(self.canvas.edge_dict[(e[1], e[0])]) | 429 | #print(self.canvas.edge_dict[(e[1], e[0])]) |
422 | #Handles the mouse wheel event, i.e., zoom | 430 | #Handles the mouse wheel event, i.e., zoom |
423 | def on_mouse_wheel(self, event): | 431 | def on_mouse_wheel(self, event): |
@@ -456,7 +464,8 @@ class TubeDraw(scene.SceneCanvas): | @@ -456,7 +464,8 @@ class TubeDraw(scene.SceneCanvas): | ||
456 | self.program['u_eye'] = self.camera | 464 | self.program['u_eye'] = self.camera |
457 | #print(self.view) | 465 | #print(self.view) |
458 | #print(event.delta[1]) | 466 | #print(event.delta[1]) |
459 | - self.update() | 467 | + self.refresh() |
468 | + | ||
460 | 469 | ||
461 | 470 | ||
462 | 471 | ||
@@ -534,7 +543,7 @@ class TubeDraw(scene.SceneCanvas): | @@ -534,7 +543,7 @@ class TubeDraw(scene.SceneCanvas): | ||
534 | self.program['u_eye'] = self.camera | 543 | self.program['u_eye'] = self.camera |
535 | self.program['u_up'] = self.up | 544 | self.program['u_up'] = self.up |
536 | #self.program['u_LightPos'] = [self.camera[0], self.camera[1], self.camera[2]] | 545 | #self.program['u_LightPos'] = [self.camera[0], self.camera[1], self.camera[2]] |
537 | - self.update() | 546 | + self.refresh() |
538 | 547 | ||
539 | #reverts the mouse state during release. | 548 | #reverts the mouse state during release. |
540 | def on_mouse_release(self, event): | 549 | def on_mouse_release(self, event): |
TubeWidget.py
@@ -11,19 +11,20 @@ Created on Mon Aug 5 15:53:16 2019 | @@ -11,19 +11,20 @@ Created on Mon Aug 5 15:53:16 2019 | ||
11 | methods necessary to use the QT signals and slots API. | 11 | methods necessary to use the QT signals and slots API. |
12 | """ | 12 | """ |
13 | 13 | ||
14 | -from pyqtgraph.Qt import QtCore, QtGui, QtWidgets | 14 | +#from pyqtgraph.Qt import QtCore, QtGui, QtWidgets |
15 | +from PyQt5 import QtCore, QtGui, QtWidgets | ||
15 | from TubeCanvas import TubeDraw | 16 | from TubeCanvas import TubeDraw |
16 | import numpy as np | 17 | import numpy as np |
17 | #import trimesh as tm | 18 | #import trimesh as tm |
18 | 19 | ||
19 | DEBUG = False | 20 | DEBUG = False |
20 | 21 | ||
21 | -class TubeWidget(QtGui.QWidget): | 22 | +class TubeWidget(QtWidgets.QWidget): |
22 | sigUpdate = QtCore.pyqtSignal(float, float, float) | 23 | sigUpdate = QtCore.pyqtSignal(float, float, float) |
23 | #Initializes the QT wrapper class. | 24 | #Initializes the QT wrapper class. |
24 | def __init__(self): | 25 | def __init__(self): |
25 | super(TubeWidget, self).__init__() | 26 | super(TubeWidget, self).__init__() |
26 | - box = QtGui.QVBoxLayout(self) | 27 | + box = QtWidgets.QVBoxLayout(self) |
27 | self.resize(500,500) | 28 | self.resize(500,500) |
28 | self.setLayout(box) | 29 | self.setLayout(box) |
29 | self.canvas = TubeDraw() | 30 | self.canvas = TubeDraw() |
graph_shaders.py
@@ -11,7 +11,7 @@ from vispy import gloo, app, scene | @@ -11,7 +11,7 @@ from vispy import gloo, app, scene | ||
11 | #shaders for drawing nodes | 11 | #shaders for drawing nodes |
12 | 12 | ||
13 | vert = """ | 13 | vert = """ |
14 | -#version 120 | 14 | + |
15 | // Uniforms | 15 | // Uniforms |
16 | // ------------------------------------ | 16 | // ------------------------------------ |
17 | uniform float u_size; | 17 | uniform float u_size; |
@@ -58,7 +58,7 @@ void main (void) { | @@ -58,7 +58,7 @@ void main (void) { | ||
58 | """ | 58 | """ |
59 | 59 | ||
60 | frag = """ | 60 | frag = """ |
61 | -#version 120 | 61 | + |
62 | // Constants | 62 | // Constants |
63 | // ------------------------------------ | 63 | // ------------------------------------ |
64 | uniform mat4 u_model; | 64 | uniform mat4 u_model; |
@@ -90,8 +90,9 @@ void main() | @@ -90,8 +90,9 @@ void main() | ||
90 | { | 90 | { |
91 | float size = v_size + 2*(v_linewidth + 1.5*v_antialias); | 91 | float size = v_size + 2*(v_linewidth + 1.5*v_antialias); |
92 | float t = v_linewidth/2.0-v_antialias; | 92 | float t = v_linewidth/2.0-v_antialias; |
93 | - // The marker function needs to be linked with this shader | ||
94 | - float r = marker(gl_PointCoord, size); | 93 | + vec2 p = (gl_PointCoord.xy - vec2(0.5,0.5))*size; |
94 | + float r = length(p); | ||
95 | + r -= v_size/2; | ||
95 | float d = abs(r) - t; | 96 | float d = abs(r) - t; |
96 | if( r > (v_linewidth/3.0+v_antialias)) | 97 | if( r > (v_linewidth/3.0+v_antialias)) |
97 | { | 98 | { |
@@ -103,7 +104,7 @@ void main() | @@ -103,7 +104,7 @@ void main() | ||
103 | { | 104 | { |
104 | float alpha = d/v_antialias; | 105 | float alpha = d/v_antialias; |
105 | alpha = new_alpha(v_zoom_level); | 106 | alpha = new_alpha(v_zoom_level); |
106 | - gl_FragColor = mix(vec4(1,1,1,0.9), v_bg_color, max(1.0-alpha, 0.3)); | 107 | + gl_FragColor = mix(vec4(1,1,1,0.9), v_bg_color, max(1.0-alpha, 0.3)); |
107 | } | 108 | } |
108 | else | 109 | else |
109 | { | 110 | { |
@@ -132,7 +133,14 @@ void main() | @@ -132,7 +133,14 @@ void main() | ||
132 | else | 133 | else |
133 | gl_FragColor = vec4(gl_Color.rgb, max(1.0-alpha, 0.1)); | 134 | gl_FragColor = vec4(gl_Color.rgb, max(1.0-alpha, 0.1)); |
134 | else | 135 | else |
135 | - gl_FragColor = mix(v_bg_color, v_fg_color, alpha); | 136 | + { |
137 | + //gl_FragColor = mix(v_bg_color, v_fg_color, alpha); vec3 color = vec3(1.0, 0.0, 0.0); | ||
138 | + vec3 normal = normalize(vec3(p.xy, d)); | ||
139 | + vec3 direction = normalize(vec3(1.0, 1.0, 5.0)); | ||
140 | + float diffuse = max(0.0, dot(direction, normal)); | ||
141 | + float specular = pow(diffuse, 24.0); | ||
142 | + gl_FragColor = mix(vec4(max(diffuse*v_bg_color.rgb, specular*vec3(0.7)), 1.0), v_fg_color, alpha); | ||
143 | + } | ||
136 | } | 144 | } |
137 | 145 | ||
138 | 146 | ||
@@ -177,7 +185,7 @@ float new_alpha(float zoom_level) | @@ -177,7 +185,7 @@ float new_alpha(float zoom_level) | ||
177 | 185 | ||
178 | 186 | ||
179 | vs = """ | 187 | vs = """ |
180 | -#version 120 | 188 | + |
181 | 189 | ||
182 | // Uniforms | 190 | // Uniforms |
183 | // ------------------------------------ | 191 | // ------------------------------------ |
@@ -215,7 +223,7 @@ void main() { | @@ -215,7 +223,7 @@ void main() { | ||
215 | """ | 223 | """ |
216 | 224 | ||
217 | fs = """ | 225 | fs = """ |
218 | -#version 120 | 226 | + |
219 | // Varying | 227 | // Varying |
220 | // ------------------------------------ | 228 | // ------------------------------------ |
221 | varying vec4 v_fg_color; | 229 | varying vec4 v_fg_color; |
subgraph_shaders.py
@@ -8,7 +8,6 @@ Created on Mon Aug 5 15:35:04 2019 | @@ -8,7 +8,6 @@ Created on Mon Aug 5 15:35:04 2019 | ||
8 | 8 | ||
9 | #shaders for drawing supernodes | 9 | #shaders for drawing supernodes |
10 | vert_s = """ | 10 | vert_s = """ |
11 | -#version 120 | ||
12 | // Uniforms | 11 | // Uniforms |
13 | // ------------------------------------ | 12 | // ------------------------------------ |
14 | uniform mat4 u_model; | 13 | uniform mat4 u_model; |
@@ -50,7 +49,6 @@ void main (void) { | @@ -50,7 +49,6 @@ void main (void) { | ||
50 | """ | 49 | """ |
51 | 50 | ||
52 | frag_s = """ | 51 | frag_s = """ |
53 | -#version 120 | ||
54 | const float pi = 3.1415926535897932384626433832795; | 52 | const float pi = 3.1415926535897932384626433832795; |
55 | // Varyings | 53 | // Varyings |
56 | // ------------------------------------ | 54 | // ------------------------------------ |
@@ -99,6 +97,12 @@ void main() | @@ -99,6 +97,12 @@ void main() | ||
99 | if(dist < 0.25) | 97 | if(dist < 0.25) |
100 | { | 98 | { |
101 | //gl_FragColor = v_cluster_color; | 99 | //gl_FragColor = v_cluster_color; |
100 | +// vec2 p = (gl_FragCoord.xy - v_value); | ||
101 | +// vec3 normal = normalize(vec3(p.xy, dist)); | ||
102 | +// vec3 direction = normalize(vec3(1.0, 1.0, 5.0)); | ||
103 | +// float diffuse = max(0.0, dot(direction, normal)); | ||
104 | +// float specular = pow(diffuse, 24.0); | ||
105 | +// gl_FragColor = vec4(max(diffuse*v_cluster_color.rgb, specular*vec3(1.0)), 1.0); | ||
102 | gl_FragColor = vec4(v_cluster_color.rgb, n_alpha); | 106 | gl_FragColor = vec4(v_cluster_color.rgb, n_alpha); |
103 | } | 107 | } |
104 | if(dist > 0.3 && 0.55 > dist) | 108 | if(dist > 0.3 && 0.55 > dist) |
@@ -139,28 +143,6 @@ void main() | @@ -139,28 +143,6 @@ void main() | ||
139 | gl_FragColor = vec4(0.93, 0.949, 0.329, n_alpha); | 143 | gl_FragColor = vec4(0.93, 0.949, 0.329, n_alpha); |
140 | //gl_FragColor = vec4(1.0, 1.0, 0.0, n_alpha); | 144 | //gl_FragColor = vec4(1.0, 1.0, 0.0, n_alpha); |
141 | } | 145 | } |
142 | - //else | ||
143 | - //{ | ||
144 | - // discard; | ||
145 | - //} | ||
146 | - | ||
147 | -// if(angle > -pi && angle < -pi/4.0) | ||
148 | -// { | ||
149 | -// //gl_FragColor = vec4(0.32, 0.61, 0.93, n_alpha); | ||
150 | -// gl_FragColor = vec4(1.0, 0.0, 0.0, n_alpha); | ||
151 | -// } | ||
152 | -// else if(angle > -pi/4.0 && angle < 0.0) | ||
153 | -// { | ||
154 | -// gl_FragColor = vec4(0.0, 1.0, 0.0, n_alpha); | ||
155 | -// } | ||
156 | -// else if(angle > 0.0 && angle < pi/2.0) | ||
157 | -// { | ||
158 | -// gl_FragColor = vec4(0.0, 0.0, 1.0, n_alpha); | ||
159 | -// } | ||
160 | -// else if(angle > pi/2.0 && angle < pi) | ||
161 | -// { | ||
162 | -// gl_FragColor = vec4(1.0, 1.0, 0.0, n_alpha); | ||
163 | -// } | ||
164 | } | 146 | } |
165 | } | 147 | } |
166 | else | 148 | else |
@@ -200,7 +182,6 @@ float new_alpha(float zoom_level) | @@ -200,7 +182,6 @@ float new_alpha(float zoom_level) | ||
200 | """ | 182 | """ |
201 | 183 | ||
202 | vs_s = """ | 184 | vs_s = """ |
203 | -#version 120 | ||
204 | 185 | ||
205 | // Uniforms | 186 | // Uniforms |
206 | // ------------------------------------ | 187 | // ------------------------------------ |
@@ -237,7 +218,7 @@ void main() { | @@ -237,7 +218,7 @@ void main() { | ||
237 | """ | 218 | """ |
238 | 219 | ||
239 | fs_s = """ | 220 | fs_s = """ |
240 | -#version 120 | 221 | + |
241 | // Varying | 222 | // Varying |
242 | // ------------------------------------ | 223 | // ------------------------------------ |
243 | varying vec4 v_fg_color; | 224 | varying vec4 v_fg_color; |