-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
290 lines (233 loc) · 8.72 KB
/
app.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from flask import Flask, redirect, render_template, jsonify
import requests
from networkx.algorithms import community
from flask import request
import json
import os
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import uuid
import itertools
import time
app = Flask(__name__, template_folder='templates')
@app.route('/', methods=["GET"])
def index():
data = {
"list" : os.listdir('./static/ressources/'),
}
return render_template('index.html', data=data)
@app.route('/graph/<graph_id>', methods=["GET"])
def load_graph(graph_id):
return render_template('graph_display.html', data=graph_id)
@app.route('/upload_file', methods=["POST"])
def file_upload():
if request.method == 'POST':
f = request.files['file_field']
# read gml file
file_text = f.read().decode("utf-8")
dir_name = str(uuid.uuid4().hex)
path = os.getcwd()+"/static/ressources/"+dir_name
try:
os.makedirs(path)
except OSError:
print ("Creation of the directory %s failed" % path)
else:
print ("Successfully created the directory %s" % path)
# store gml file
with open(os.getcwd()+"/static/ressources/"+dir_name+"/gml_file.gml", 'w') as outfile:
outfile.write(file_text)
graph = nx.read_gml(os.getcwd()+"\\static\\ressources\\"+dir_name+"\\gml_file.gml", label=None)
if(len(list(graph.nodes)) >= 2500):
graph = graph.subgraph(list(graph.nodes)[0:2500])
edgesFile = []
### Informations and computing relative to edges ###
edgesBetweenness = getEdgesBetweenness(graph)
for edge in graph.edges:
edgesFile.append({
'source': edge[0],
'target' : edge[1],
'edges_betweenness' : edgesBetweenness[edge],
'type': 'type'
})
### Informations and computing relative to nodes ###
# degree_centrality
degree_centrality = getDegreeCentrality(graph)
# betweenness_centrality
betweenness_centrality = getBetweenneessCentrality(graph)
# closeness_centrality
closeness_centrality = getClosenessCentrality(graph)
# in_degree
in_degree = getInDegree(graph)
# out_degree
out_degree = getOutDegree(graph)
### General informations about graph ###
graphFile = {
"nb_nodes" : getNbNodes(graph),
"nb_edges" : getNbEdges(graph),
"density" : getNetworkDensity(graph),
"avg_path_lenght" : getNetworkAvgPathLength(graph)
}
with open(os.getcwd()+"/static/ressources/"+dir_name+"/graph_info.json", 'w') as outfile:
json.dump(graphFile, outfile)
with open(os.getcwd()+"\\static\\ressources\\"+dir_name+"\\graph_edges.json", 'w') as outfile:
json.dump(edgesFile, outfile)
### Generating clusters ###
# Cluster set generation with Girman Newman algorithm:
clusteringSet = community.girvan_newman(graph)
# For each set construct the json file.
performance = 0
k = 0
for set in clusteringSet:
performanceSet = community.quality.performance(graph,list(set))
if performanceSet > performance:
performance = performanceSet
print("increasing k = "+str(k)+" - "+str(performance))
else:
print("Final iteration because equals or decreasing : "+str(performance))
selectedSet = set
break
k = k + 1
nb_clust = len(selectedSet)
nb_inters = 0
nb_intras = 0
clustsFile = {
"performance": performance,
"iteration_number": k,
"nb_clust" : nb_clust,
"clusters" : [],
}
nodesFile = [] # json vide
clustId = 1 # set Id
#forEach cluster look graph node and add it if it's in the cluster.
for cluster in set:
subgraph = graph.subgraph(list(cluster))
nb_nodes_cls = len(list(subgraph.nodes))
nb_intras_cls = len(list(subgraph.edges))
nb_inter_cls = getNbInterEdges(subgraph,graph)
nb_intras += nb_intras_cls
nb_inters += nb_inter_cls
clustsFile["mean_nb_intra"] = nb_intras/nb_clust
clustsFile["mean_nb_inters"] = nb_inters/nb_clust
clustsFile["clusters"].append({
"id" : clustId,
"nb_nodes" : nb_nodes_cls,
"nb_intra_edges" : nb_intras_cls,
"nb_inter_edges" : nb_inter_cls,
"intra-density" : getNetworkDensity(subgraph),
"inter-density" : getInterDensity(subgraph, graph),
"most-important-node" : getMostImportantNode(subgraph)
})
for i in range(0, len(graph.nodes)):
id = list(graph.nodes)[i]
try:
label = graph.nodes[i]['label']
except:
label = id
if(id in list(subgraph.nodes)):
nodesFile.append({
'id': id,
'nom' : label,
'cluster' : clustId,
'degree_centrality': degree_centrality[id],
'betweenness_centrality': betweenness_centrality[id],
'closeness_centrality': closeness_centrality[id],
'in_degree': in_degree[id],
'out_degree': out_degree[id],
})
clustId += 1
with open(os.getcwd()+"\\static\\ressources\\"+dir_name+"\\graph_nodes.json", 'w') as outfile:
json.dump(nodesFile, outfile)
with open(os.getcwd()+"\\static\\ressources\\"+dir_name+"\\clusters_info.json", 'w') as outfile:
json.dump(clustsFile, outfile)
return redirect("/graph/"+dir_name)
def getNbNodes(g):
return len(list(g.nodes))
def getNbEdges(g):
return len(list(g.edges))
def getEdgesBetweenness(g):
return nx.edge_betweenness_centrality(g,k=None, normalized=True, weight=None, seed=None)
def getDegreeCentrality(g):
return nx.degree_centrality(g)
def getBetweenneessCentrality(g):
return nx.betweenness_centrality(g, k=None, normalized=True, weight=None, endpoints=False, seed=None)
def getClosenessCentrality(g):
return nx.closeness_centrality(g, u=None, distance=None, wf_improved=True)
def maxDegree(g):
max = 0;
for degree in g.degree:
if(degree[1] > max):
max = degree[1]
return max
def getNetworkDegreeCentrality(g):
maxDeg = maxDegree(g)
N=len(g.nodes)
res=0
for degree in g.degree:
res += maxDeg - degree[1]
return res/((N-1)*(N-2))
def getNetworkAvgPathLength(g):
if nx.is_connected(g) :
return nx.average_shortest_path_length(g, weight=None)
else:
tab = []
for c in nx.algorithms.connected_components(g):
C = g.subgraph(c).copy()
tab.append(nx.average_shortest_path_length(C))
return np.mean(tab)
def getOutDegree(g):
res=[]
for node in g.nodes:
if isinstance(g,nx.DiGraph):
return g.out_degree
else:
return g.degree
def getInDegree(g):
res=[]
for node in g.nodes:
if isinstance(g,nx.DiGraph):
return g.in_degree
else:
return g.degree
def getNetworkDensity(g):
return nx.density(g)
def girvanNewman(graph, k):
t1 = time.time()
comp = community.girvan_newman(graph)
res =[]
for communities in itertools.islice(comp, k): ## prend seulement les k itérations
res.append(communities)
t2 = time.time()
girvanPerf = t2 - t1
return res, girvanPerf
def getInterDensity(subgraph, graph):
nb_inter=0
nbc=len(subgraph.nodes)
nb = len(graph.nodes)
for edge in graph.edges:
if((edge[0] in list(subgraph.nodes) and edge[1] not in list(subgraph.nodes))
or (edge[1] in list(subgraph.nodes) and edge[0] not in list(subgraph.nodes))):
nb_inter+=1
return nb_inter/(nbc*(nb-nbc))
def getMostImportantNode(subgraph):
max = 0
max_id = 0
for couple in subgraph.degree:
if(couple[1] >= max):
max = couple[1]
max_id = couple[0]
return max_id
def getNbInterEdges(subgraph,graph):
res = 0
nodes_cls = list(subgraph.nodes)
edges_cls = list(subgraph.edges)
for edge in list(graph.edges):
if(
((edge[0] in nodes_cls) and (edge[1] not in nodes_cls))
or
((edge[0] not in nodes_cls) and (edge[1] in nodes_cls))
):
res+=1
return res
if __name__ == "__main__":
app.run()