-
Notifications
You must be signed in to change notification settings - Fork 5
/
graphicDisplayGlobalVarAndFunctions.py
221 lines (174 loc) · 6.68 KB
/
graphicDisplayGlobalVarAndFunctions.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
# global variables and functions for graphic display management
# to be imported with
#import graphicDisplayGlobalVarAndFunctions as gvf
# useful links
#labels and colors in networkX
# https://networkx.github.io/documentation/latest/examples/drawing/labels_and_colors.html
# look also at
# https://www.wakari.io/sharing/bundle/nvikram/Basics%20of%20Networkx
# Matplotlib colors
# http://matplotlib.org/api/colors_api.html
# html colors
# http://www.w3schools.com/html/html_colornames.asp
# in this module the try/except structures are not cotrolled for debug
# these try/except constucts, indeed, are not intended to control user errors,
# but a regular flow of inputs
import networkx as nx
import matplotlib.pyplot as plt
import commonVar as common
# the base: creating the graph (and copying its address in a common variable
# to have the possibility of direct interaction with the graph when
# the program is finished, as the common space is imported also in the main
# program
def createGraph():
global colors, pos
common.g = nx.DiGraph() # directed graph, instead of nx.Graph()
colors = {}
pos = {}
common.g_labels = {}
common.g_edge_labels = {} # copy the address of the labels of the edges
# searching tools
def findNodesFromSector(sector):
nodeList = []
for aNode in common.g.nodes():
if common.g.nodes[aNode]['sector'] == sector:
nodeList.append(aNode)
return nodeList
def createEdge(a, b):
# implicitly directed, due to the use of DiGraph
if a is None or b is None:
print("Internal error, attempt to create an edge with a node defined None")
exit(0)
try:
common.g[a][b]['weight'] = 1 + common.g[a][b]['weight']
except BaseException:
common.g.add_edge(a, b)
common.g[a][b]['weight'] = 1
if a != b:
# verifying the presence of the edge in the other direction
try:
otherW = common.g[b][a]['weight']
common.g_edge_labels[a, b] = "w.s %d and %d" % (
common.g[a][b]['weight'], otherW)
common.g_edge_labels[b, a] = ""
except BaseException:
common.g_edge_labels[a, b] = "w. %d" % common.g[a][b]['weight']
if a == b:
common.g_edge_labels[a, b] = ""
common.g[a][b]['pseudoLabel'] = "auto link w. %d" \
% common.g[a][b]['weight']
# using networkX and matplotlib case
def closeNetworkXdisplay():
plt.close()
def openClearNetworkXdisplay():
if common.graphicStatus == "PythonViaTerminal":
plt.ion()
# plt.clf()
def clearNetworkXdisplay():
plt.clf()
def getGraph():
try:
return common.g
except BaseException:
return 0
def pruneEdges():
if not common.prune:
return
common.prune = False
print("New threshold to prune: < %d" % common.pruneThreshold)
#edges=common.g.edges() modified with NetworkX 2.0
edges=[]
for anE in common.g.edges():
edges.append(anE)
print("weights of the links")
for anEdge in edges:
u = anEdge[0].number
uu = anEdge[0]
v = anEdge[1].number
vv = anEdge[1]
w = common.g[anEdge[0]][anEdge[1]]["weight"]
print(u, v, w)
if w < common.pruneThreshold:
# managing labels, related to createEdge phase above
common.g_edge_labels.pop((uu, vv))
try:
common.g_edge_labels[vv,
uu] = "w. %d" % common.g[vv][uu]['weight']
except BaseException:
pass
if uu == vv:
common.g[uu][uu]['pseudoLabel'] = ""
common.g_labels[uu] = str(uu.number) + " (" +\
str(len(uu.recipeWaitingList)) + ")"
# removing
common.g.remove_edge(uu, vv)
def drawGraph():
# directed, due to the use of DiGraph
# draw_netwokx is well documented at
# https://networkx.github.io/documentation/latest/reference/
# generated/networkx.drawing.nx_pylab.draw_networkx.html
# nx.draw_networkx(agentGraph, font_size=10,node_size=500, \
clearNetworkXdisplay()
pruneEdges()
nx.draw_networkx(common.g, pos, font_size=10, node_size=500,
node_color=list(colors.values()),
labels=common.g_labels)
nx.draw_networkx_edge_labels(
common.g,
pos,
edge_labels=common.g_edge_labels,
font_size=9)
# plt.draw()
plt.show() # used by %Matplotlib inline [without ion()]; not conflicting
# with ion()
if common.graphicStatus == "PythonViaTerminal":
plt.pause(0.01)
# to show the sequence of the shown images in absence of pauses
# print agentGraph.nodes(data=True)
# print agentGraph.edges(data=True)
# print labels
# print edge_labels
# print a, agentGraph.node[a].keys(), agentGraph.node[a].values(),\
# agentGraph.node[a]['sector']
# adjacency
print()
for i in range(len(common.orderedListOfNodes)):
print("%d " % common.orderedListOfNodes[i].number, end=' ')
print()
# print "drawGraph verification of existing nodes",common.g.nodes()
if common.g.nodes() != []:
A = nx.adjacency_matrix(common.g, nodelist=common.orderedListOfNodes,
weight='weight')
# print A # as sparse matrix, defaul from nx 1.9.1
print(A.todense()) # as a regular matrix
else:
print("No nodes, impossible to create the adjacency_matrix")
print()
# neighbors
for aNode in common.g.nodes():
print(aNode.number, [node.number
for node in nx.neighbors(common.g, aNode)])
# betweenness_centrality
# Betweenness centrality of a node v is the sum of the fraction of all-pairs
# shortest paths that pass through v
# http://networkx.lanl.gov/reference/generated/
# networkx.algorithms.centrality.betweenness_centrality.html
print()
print("betweenness_centrality")
common.btwn = nx.betweenness_centrality(
common.g, normalized=False, weight='weight')
# print btw
for i in range(len(common.orderedListOfNodes)):
print(common.orderedListOfNodes[i].number,
common.btwn[common.orderedListOfNodes[i]])
# closeness_centrality
# Closeness centrality at a node is 1/average distance to all other nodes
# http://networkx.lanl.gov/reference/generated/
# networkx.algorithms.centrality.closeness_centrality.html
print()
print("closeness_centrality")
common.clsn = nx.closeness_centrality(common.g)
# print clsn
for i in range(len(common.orderedListOfNodes)):
print(common.orderedListOfNodes[i].number,
common.clsn[common.orderedListOfNodes[i]])