-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trust_GUI.py
267 lines (210 loc) · 10.3 KB
/
Trust_GUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""
The code below opens the trustchain database and runs the Monte Carlo PageRank algorithm on it's directed graph
determining the trustworthiness of the agents in the network.
"""
from __future__ import division
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import networkx as nx
import sys
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import numpy as np
import operator
from heapq import nlargest
import copy
class TrustGUI(QWidget):
NumButtons = ['Show All Peers', 'Show Most Trusted Peer', 'Show Trustworthy Peers']
def __init__(self, graph, main_node, page_ranks):
super(TrustGUI, self).__init__()
self.graph = graph
self.page_ranks = page_ranks
self.main_node = main_node
self.setMouseTracking(True)
self.initUI()
def initUI(self):
"""
:return:
"""
""" Set Window Parameters """
self.setGeometry(100, 100, 800, 600)
self.center()
self.setWindowTitle('Network Plot')
self.setWindowIcon(QIcon('Tribler Logo.png'))
""" Set Grid Layout """
self.grid = QGridLayout()
self.setLayout(self.grid)
""" Create Actions for GUI window """
exitAct = QAction('Exit Application', self)
exitAct.setShortcut('Ctrl+W')
exitAct.setStatusTip('Closes the Network Explorer')
exitAct.triggered.connect(QApplication.instance().quit)
""" Create Menubar at the top of the window """
menubar = QMenuBar()
self.grid.addWidget(menubar, 0, 0)
filemenu = menubar.addMenu('&File')
filemenu.addAction(exitAct)
""" Create Statusbar at the bottom of the window """
statusbar = QStatusBar()
self.grid.addWidget(statusbar, 9, 0)
statusbar.showMessage("Ready")
statusbar.show()
""" Create Vertical Box of Buttons """
buttonLayout = QVBoxLayout()
self.createVerticalGroupBox()
buttonLayout.addWidget(self.verticalGroupBox)
""" Create Canvas for Network Display and Buttons"""
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.grid.addWidget(self.canvas, 0, 1, 9, 9)
self.grid.addLayout(buttonLayout, 1, 0)
""" Create Network Display """
self.showgraph(self.main_node)
""" Show Widget """
self.show()
def createVerticalGroupBox(self):
self.verticalGroupBox = QGroupBox()
layout = QVBoxLayout()
for i in self.NumButtons:
button = QPushButton(i)
button.setObjectName(i)
layout.addWidget(button)
layout.setSpacing(10)
self.verticalGroupBox.setLayout(layout)
button.clicked.connect(self.submitCommand)
return
def submitCommand(self):
eval('self.' + str(self.sender().objectName()).replace(" ", "") + '()')
return
def ShowMostTrustedPeer(self):
self.page_ranks_of_subgraph = {node: self.page_ranks[node] for node in self.sub_graph.nodes()}
del self.page_ranks_of_subgraph[self.main_node]
self.most_trusted_node = max(self.page_ranks_of_subgraph.iteritems(), key=operator.itemgetter(1))[0]
del self.page_ranks_of_subgraph[self.most_trusted_node]
self.node_color_trusted = []
self.node_color_untrusted = []
for node in self.sub_graph.nodes():
if node in self.page_ranks_of_subgraph.keys():
if self.page_ranks[node] < 0.0001:
self.node_color_untrusted.append('red')
if 0.0001 < self.page_ranks[node] < 0.0005:
self.node_color_untrusted.append('yellow')
if self.page_ranks[node] > 0.0005:
self.node_color_untrusted.append('green')
else:
if self.page_ranks[node] < 0.0001:
self.node_color_trusted.append('red')
if 0.0001 < self.page_ranks[node] < 0.0005:
self.node_color_trusted.append('yellow')
if self.page_ranks[node] > 0.0005:
self.node_color_trusted.append('green')
self.figure.clf()
self.canvas.draw_idle()
plt.title('Tribler Network', size=15)
plt.axis('off')
nx.draw_networkx_nodes(self.sub_graph, pos=self.pos, nodelist=[self.main_node, self.most_trusted_node],
node_size=50, width=0.05, node_color=self.node_color_trusted)
nx.draw_networkx_nodes(self.sub_graph, pos=self.pos, nodelist=self.page_ranks_of_subgraph.keys(), node_size=50,
width=0.05, node_color=self.node_color_untrusted, alpha=0.1)
nx.draw_networkx_edges(self.sub_graph, pos=self.pos, edgelist=[(self.main_node, self.most_trusted_node)], width=0.2)
nx.draw_networkx_edges(self.sub_graph, pos=self.pos, edgelist=self.sub_graph.edges(), width=0.05, alpha=0.1)
def ShowTrustworthyPeers(self):
self.page_ranks_of_subgraph = {node: self.page_ranks[node] for node in self.sub_graph.nodes()}
del self.page_ranks_of_subgraph[self.main_node]
self.most_trusted_nodes = nlargest(10, self.page_ranks_of_subgraph)
for node in self.most_trusted_nodes:
del self.page_ranks_of_subgraph[node]
self.node_color_trusted = []
self.node_color_untrusted = []
for node in self.sub_graph.nodes():
if node in self.page_ranks_of_subgraph.keys():
if self.page_ranks[node] < 0.0001:
self.node_color_untrusted.append('red')
if 0.0001 < self.page_ranks[node] < 0.0005:
self.node_color_untrusted.append('yellow')
if self.page_ranks[node] > 0.0005:
self.node_color_untrusted.append('green')
else:
if self.page_ranks[node] < 0.0001:
self.node_color_trusted.append('red')
if 0.0001 < self.page_ranks[node] < 0.0005:
self.node_color_trusted.append('yellow')
if self.page_ranks[node] > 0.0005:
self.node_color_trusted.append('green')
self.figure.clf()
self.canvas.draw_idle()
plt.title('Tribler Network', size=15)
plt.axis('off')
nodelist = list(set(self.most_trusted_nodes).union({self.main_node}))
nx.draw_networkx_nodes(self.sub_graph, pos=self.pos, nodelist=nodelist,
node_size=50, width=0.05, node_color=self.node_color_trusted)
nx.draw_networkx_nodes(self.sub_graph, pos=self.pos, nodelist=self.page_ranks_of_subgraph.keys(), node_size=50,
width=0.05, node_color=self.node_color_untrusted, alpha=0.1)
#nx.
return
def ShowAllPeers(self):
self.showgraph(self.main_node)
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def showgraph(self, main_node):
main_node_neighbours = list(set(nx.DiGraph.successors(self.graph, main_node)).union(
set(nx.DiGraph.predecessors(self.graph, main_node))))
neighbours_of_neighbours = dict()
all_neighbours_of_neighbours = []
for j in range(len(main_node_neighbours)):
node_neighbours = []
node_neighbours.extend(nx.DiGraph.successors(self.graph, main_node_neighbours[j]))
node_neighbours.extend(nx.DiGraph.predecessors(self.graph, main_node_neighbours[j]))
node_neighbours.remove(main_node)
node_neighbours = list(set(node_neighbours) - set(main_node_neighbours))
for node in node_neighbours:
if node in all_neighbours_of_neighbours:
node_neighbours.remove(node)
else:
continue
all_neighbours_of_neighbours.extend(node_neighbours)
neighbours_of_neighbours[main_node_neighbours[j]] = node_neighbours
self.sub_graph = nx.DiGraph.subgraph(self.graph, list(set(main_node_neighbours).union({main_node}).union(all_neighbours_of_neighbours)))
plt.title('Tribler Network', size=15)
plt.axis('off')
self.pos = nx.spring_layout(self.sub_graph, center=np.array([0, 0]), scale=20)
self.pos[main_node] = np.array([0, 0])
angle_first_circle = (2*np.pi) / len(main_node_neighbours)
angle_second_circle = dict()
i = 0
for node in main_node_neighbours:
self.pos[node] = np.array([np.cos(angle_first_circle * i) * 20, np.sin(angle_first_circle * i) * 20])
try:
angle_second_circle[node] = angle_first_circle/len(neighbours_of_neighbours[node])
except ZeroDivisionError:
angle_second_circle[node] = None
i += 1
i = 0
for node1 in neighbours_of_neighbours.keys():
j = 0
for node2 in neighbours_of_neighbours[node1]:
# pos[node2] = np.array([np.cos(angle_second_circle[node1] * j + angle_first_circle * i - angle_first_circle / 2) * 20, np.sin(angle_second_circle[node1] * j + angle_first_circle * i - angle_first_circle / 2) * 20])
self.pos[node2] = np.array(
[np.cos(angle_second_circle[node1] * j + angle_first_circle * i - angle_first_circle / 2) * 40,
np.sin(angle_second_circle[node1] * j + angle_first_circle * i - angle_first_circle / 2) * 40])
j += 1
i += 1
self.node_color = []
for node in self.sub_graph.nodes():
if self.page_ranks[node] < 0.0001:
self.node_color.append('red')
if 0.0001 < self.page_ranks[node] < 0.0005:
self.node_color.append('yellow')
if self.page_ranks[node] > 0.0005:
self.node_color.append('green')
self.figure.clf()
self.canvas.draw_idle()
labels = {}
labels[self.main_node] = r'you'
self.pos[main_node] = np.array([0, 0])
nx.draw(self.sub_graph, with_labels=True, labels=labels, pos=self.pos, node_color=self.node_color, node_size=50, width=0.05)