-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetgraph_on_pyqt.py
163 lines (137 loc) · 6.38 KB
/
netgraph_on_pyqt.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
XKCD_COLORS = {
'cloudy blue': '#acc2d9',
'dark pastel green': '#56ae57',
'dust': '#b2996e',
'electric lime': '#a8ff04',
'fresh green': '#69d84f',
'light eggplant': '#894585',
'nasty green': '#70b23f',
'really light blue': '#d4ffff',
'tea': '#65ab7c',
'warm purple': '#952e8f',
'yellowish tan': '#fcfc81',
'cement': '#a5a391',
'dark grass green': '#388004',
'dusty teal': '#4c9085',
'grey teal': '#5e9b8a',
'macaroni and cheese': '#efb435',
'pinkish tan': '#d99b82',
'spruce': '#0a5f38',
'strong blue': '#0c06f7',
'toxic green': '#61de2a',
'windows blue': '#3778bf',
'blue blue': '#2242c7',
'blue with a hint of purple': '#533cc6',
'booger': '#9bb53c',
'bright sea green': '#05ffa6'
}
color_names=list(XKCD_COLORS.values())
import numpy as np
import sys
from matplotlib.figure import Figure
from PyQt5 import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
# netgraph is a matplotlib-based network plotting tool.
from netgraph import Graph
import networkx as nx
from networkx.algorithms.community.louvain import louvain_communities
class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=20, height=20, dpi=100):
super(MplCanvas, self).__init__(Figure(figsize=(width, height), dpi=dpi))
self.setParent(parent)
self.ax = self.figure.add_subplot(111)
######## INPUT: list of channel labels and matrix S, where S will correspond to coherence_at_mark(eeg) for our purposes.
######### OUTPUT: PyQt plot of network communities, where community memberships are represented by color.
######### See https://www.pythonguis.com/tutorials/plotting-matplotlib/ which discusses creating PyQt plots using matplotlib.
# SAMPLE MATRIX S AND LIST OF CHANNEL LABELS:
S=np.array([[0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
0, 0],
[0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,
0, 0],
[1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0],
[1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0],
[1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0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, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0,
1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1,
1, 0]])
channels=['A'+str(i) for i in range(S.shape[0])]
##### CREATE NETWORK HAVING ADJACENCY MATRIX S
G=nx.from_numpy_matrix(S)
Nc=len(channels)
channels_dict={i:channels[i] for i in range(Nc)}
###### DETERMINE COMMUNITIES USING LOUVAIN METHOD
clusters=louvain_communities(G)
###### PARTITION OF LABELS, E.G. {{A1, A4, A5}, {A8, A10, A22}, ...}
community_labels=[{channels[member] for member in comm} for comm in clusters ]
node_to_community = dict()
nodes=range(Nc)
for i in range(Nc):
for j in range(len(clusters)):
if nodes[i] in clusters[j]:
node_to_community.update({i:j})
####### ASSIGN A COLOR TO EACH COMMUNITY
community_to_color={i:color_names[i] for i in range(8)}
node_color = {node: community_to_color[community_id] for node, community_id in node_to_community.items()}
###### USE NETGRAPH TO PLOT NETWORK AND HIGHLIGHT COMMUNITIES
self_plot_instance=Graph(G,ax=self.ax,node_labels=channels_dict,node_label_fontdict=dict(size=12), node_label_offset=0.075,
node_color=node_color, node_edge_width=0, edge_alpha=0.1 ,
node_layout='community', node_layout_kwargs=dict(node_to_community=node_to_community),
edge_layout='bundled', edge_layout_kwargs=dict(k=2000),
)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.canvas = MplCanvas(self, width=10, height=10, dpi=100)
widget = QtWidgets.QWidget()
self.setCentralWidget(widget)
layout = QtWidgets.QVBoxLayout(widget)
layout.addWidget(self.canvas)
def main():
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
if __name__ == "__main__":
main()