HistogramWidget.py
4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 3 13:47:39 2020
@author: pavel
"""
from PyQt5 import QtCore, QtGui, QtWidgets
import network_dep as nwt
import vispy.plot as vp
#import vispy.scene.visuals. as Histogram
from vispy.scene.visuals import Histogram as Histogram
import numpy as np
DEBUG = False
class HistogramWidget(QtWidgets.QWidget):
def __init__(self):
super(HistogramWidget, self).__init__()
box = QtWidgets.QVBoxLayout(self)
self.resize(500,500)
self.setLayout(box)
self.canvas = vp.Fig(show=False)
self.hist = self.canvas[0, 0].histogram(np.random.rand(100))
#data = np.random.rand(100)
#self.canvas[0, 0].histogram(data)
#self.canvas = Histogram()
self.setUpdatesEnabled(True)
box.addWidget(self.canvas.native)
self.G = None
self.canvas.events.mouse_press.connect(self.on_mouse_press)
#self.canvas.events.mouse_release.connect(self.on_mouse_release)
#self.canvas.events.mouse_move.connect(self.on_mouse_move)
#self.canvas.events.mouse_double_click.connect(self.on_mouse_double_click)
self.current_color = 'clusters'
def on_mouse_press(self, event):
print("mouse_press")
if event.button == 2:
menu = QtWidgets.QMenu(self)
tmp = menu.addMenu('Histogram Metric')
edge_properties = self.G.edge_properties.keys()
edge_actions = []
for i in range(len(edge_properties)):
if(edge_properties[i] != "map" or edge_properties[i] != "RGBA"):
edge_actions.append(tmp.addAction(edge_properties[i]))
action = menu.exec_(event.native.globalPos())
for i in range(len(edge_actions)):
if action == edge_actions[i]:
self.set_data(prop=edge_properties[i])
print("mouse_press")
def on_mouse_double_click(self, event):
print("mouse_double")
def on_mouse_release(self, event):
print("mouse_release")
def on_mouse_move(self, event):
print("mouse_move")
def set_Graph(self, G):
self.G = G
#self.canvas[0, 0].histogram(np.random.rand(10))
self.set_data()
def gen_mesh(self, data, bins=10, orientation='h'):
data = np.asarray(data)
if data.ndim != 1:
raise ValueError('Only 1D data currently supported')
X, Y = (0, 1) if orientation == 'h' else (1, 0)
# do the histogramming
data, bin_edges = np.histogram(data, bins)
# construct our vertices
rr = np.zeros((3 * len(bin_edges) - 2, 3), np.float32)
rr[:, X] = np.repeat(bin_edges, 3)[1:-1]
rr[1::3, Y] = data
rr[2::3, Y] = data
bin_edges.astype(np.float32)
# and now our tris
tris = np.zeros((2 * len(bin_edges) - 2, 3), np.uint32)
offsets = 3 * np.arange(len(bin_edges) - 1,
dtype=np.uint32)[:, np.newaxis]
tri_1 = np.array([0, 2, 1])
tri_2 = np.array([2, 0, 3])
tris[::2] = tri_1 + offsets
tris[1::2] = tri_2 + offsets
return rr, tris
def set_data(self, prop = 'length'):
#self.canvas = vp.Fig(show=False)
#, xlabel=prop, ylabel='#'
#self.canvas.clear()
#self.canvas[0, 0]._configured = False
p = self.G.ep[prop].get_array().T
self.canvas.bgcolor = 'w'
rr, tris = self.gen_mesh(p)
self.hist.set_data(rr, tris, color = 'b')
#self.canvas.context.clear(color='w', depth=True)
#self.canvas.central_widget().context.clear(color='w', depth=True)
self.canvas[0, 0].xlabel.text = prop
self.canvas[0, 0].ylabel.text = '# of occurances'
self.canvas[0, 0].title.text = 'edge property distribution'
for hist, pwidget in zip([self.hist], self.canvas.plot_widgets):
#x_data = hist._line.pos[:, 0]
#y_data = hist._line.pos[:, 1]
#print(hist._bounds, hist._bounds[:, 0], hist._bounds[:, 1])
x_range = hist._bounds[0][0], hist._bounds[0][1]
y_range = hist._bounds[1][0], hist._bounds[1][1]
pwidget.view.camera.set_range(x=x_range, y=y_range)
#self.canvas[0, 0]._configured = False
#self.canvas[0, 0].histogram(p, color = 'b')
self.canvas.update()
self.canvas.show()