-
Notifications
You must be signed in to change notification settings - Fork 0
/
sna-github.py
765 lines (687 loc) · 28.6 KB
/
sna-github.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
import urllib.request, json
import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
from itertools import combinations
from timeit import Timer
import datetime
import warnings
warnings.filterwarnings('ignore')
G = nx.Graph()
def usuarios():
#url1='https://api.github.com/search/users?q=+type:user+location:colombia'
url1='https://api.github.com/search/users?q=+type:user+location:colombia+repos:>10+language:javascript&per_page=50'
with urllib.request.urlopen(url1) as url:
users= json.loads(url.read().decode())
for item in users['items']:
del item['id']
del item['node_id']
del item['avatar_url']
del item['gravatar_id']
del item['url']
del item['html_url']
#del item['followers_url']
del item['following_url']
del item['gists_url']
del item['starred_url']
del item['subscriptions_url']
del item['organizations_url']
del item['events_url']
del item['received_events_url']
del item['type']
del item['site_admin']
del item['score']
del item['repos_url']
#return str(users['items'])
userDoc= open("users.txt","w+")
userDoc.write(str(users['items']))
userDoc.close()
return users['items']
def seguidores(G, l_usu):
# print("############## l_usu ###############")
# print(l_usu)
# print("#############################")
#contadorFollowers = 0
contadorl_usu = 0
try:
for u in l_usu:
contadorl_usu = contadorl_usu + 1
#print("#####contador de ciclos a l_usu()######")
#Timer.timeit(10.0, print(contadorl_usu))
print(contadorl_usu)
print("##############################")
url_seg2=u['followers_url']
try:
with urllib.request.urlopen(url_seg2) as url:
seguid= json.loads(url.read().decode())
except Exception as inst:
print("#####función seguidores()######")
print("error de conexión")
print(inst)
print("##############################")
return "false"
FollowersDoc= open("seguidores.txt","w+")
FollowersDoc.write(str(seguid))
FollowersDoc.close()
# for item in seguid:
# #del item['login']
# del item['id']
# del item['node_id']
# del item['avatar_url']
# del item['gravatar_id']
# del item['url']
# del item['html_url']
# del item['followers_url']
# del item['following_url']
# del item['gists_url']
# del item['starred_url']
# del item['subscriptions_url']
# del item['organizations_url']
# del item['repos_url']
# del item['events_url']
# del item['received_events_url']
# del item['type']
# del item['site_admin']
for s in seguid:
# contadorFollowers = contadorFollowers + 1
# print("#####contador de llamdos a follower()######")
# print(contadorFollowers)
# print("##############################")
G.add_edge(u['login'],s['login'])
# print("##############################")
# print("#####u['login']############")
# print(u['login'])
# print("#####s['login']############")
# print(s['login'])
# print("##############################")
# for f in s['items']:
# url_seg2=f['followers_url']
# try:
# with urllib.request.urlopen(url_seg2) as url:
# githubUsers= json.loads(url.read().decode())
# except Exception as inst:
# print("##############################")
# print("error al consultar followerFromInitUser")
# print(inst)
# print("##############################")
# for followerFromInitUser in githubUsers:
# G.add_edge(s['login'],followerFromInitUser['login'])
# print("########followerFromInitUser#######")
# print("#####u['login']############")
# print(s['login'])
# print("#####s['login']############")
# print(followerFromInitUser['login'])
# print("##############################")
return "true"
except ValueError:
print ("error en la conexión")
return "false"
# métricas de Nodos
# density of a graph
def nodeMeasure_density(G):
G = nx.convert_node_labels_to_integers(G,first_label=1)
density = nx.density(G)
print("############## density #############")
print(density)
print("####################################")
print(density)
return density
# métricas de Nodos
# degree: número de nodos adyacentes
def nodeMeasure_degree(G):
#print("G = ")
#print(nx.degree(G).values())
#Gtemp = nx.convert_node_labels_to_integers(G,first_label=1)
#degree_sequence=sorted(nx.degree(Gtemp).values(),reverse=True)
degrees = [val for (G, val) in G.degree()]
dmax=max(degrees)
dmin =min(degrees)
print("############## degrees #############")
print(degrees)
print("####################################")
print("################ dmax ##############")
print(dmax)
print("##############################")
print("############# dmin #################")
print(dmin)
print("####################################")
def centrality(G):
G = nx.convert_node_labels_to_integers(G,first_label=1)
degCent = nx.degree_centrality(G)
#sorted_by_value = sorted(degCent)
sorted_by_value = sorted(degCent.items(), key=lambda kv: kv[1])
#sorted_by_value
# print("############centrality - degCent#################")
# print(degCent)
# print("##############################")
graphDoc= open("degree_centrality.txt","w+")
graphDoc.write(str(degCent))
graphDoc.close()
# print("###############centrality - sorted_by_value###############")
# print(sorted_by_value)
# print("##############################")
graphDoc= open("degree_centrality_sorted_by_value.txt","w+")
graphDoc.write(str(sorted_by_value))
graphDoc.close()
def centralityTopFive(G):
#G = nx.convert_node_labels_to_integers(G,first_label=1)
degCent = nx.degree_centrality(G)
#sorted_by_value = sorted(degCent)
sorted_by_value = sorted(degCent.items(), key=lambda kv: kv[1], reverse=True)
top_five=sorted_by_value[0:5]
graphDoc= open("centralityTopFive.txt","w+")
graphDoc.write(str(top_five))
graphDoc.close()
return top_five
def closenessCentrality(G):
closeCent = nx.closeness_centrality(G)
graphDoc= open("closeness_centrality.txt","w+")
graphDoc.write(str(closeCent))
graphDoc.close()
return closeCent
def closenessCentralityTopFive(G):
closeCent = nx.closeness_centrality(G)
sorted_by_value = sorted(closeCent.items(), key=lambda kv: kv[1])
closeness_top_five=sorted_by_value[0:5]
graphDoc= open("closeness_centralityTopFive.txt","w+")
graphDoc.write(str(closeness_top_five))
graphDoc.close()
return closeness_top_five
def betweennessCentrality(G):
#G = nx.barbell_graph(m1=5, m2=1)
betweCent=nx.betweenness_centrality(G)
graphDoc= open("betweenness_centrality.txt","w+")
graphDoc.write(str(betweCent))
graphDoc.close()
#print(betweCent)
def betweennessCentralityTopFive(G):
betweCent=nx.betweenness_centrality(G)
sorted_by_value = sorted(betweCent.items(), key=lambda kv: kv[1], reverse=True)
betweenness_top_five=sorted_by_value[0:5]
graphDoc= open("betweenness_top_five_TopFive.txt","w+")
graphDoc.write(str(betweenness_top_five))
graphDoc.close()
def eigenvectorCentrality(G):
eigenCent = nx.eigenvector_centrality(G)
#print(eigenCent)
graphDoc= open("eigenvector_centrality.txt","w+")
graphDoc.write(str(eigenCent))
graphDoc.close()
def eigenvectorCentralityTopFive(G):
eigenCent = nx.eigenvector_centrality(G)
#print(eigenCent)
sorted_by_value = sorted(eigenCent.items(), key=lambda kv: kv[1], reverse=True)
eigenvector_top_five=sorted_by_value[0:5]
graphDoc= open("eigenvector_centrality_topFive.txt","w+")
graphDoc.write(str(eigenvector_top_five))
graphDoc.close()
#print(eigenvector_top_five)
def intersection(lst1, lst2):
lst3 = [value for value in lst1 if value in lst2]
graphDoc= open("intersection_of_nodes_in_topFive.txt","w+")
graphDoc.write(str(lst3))
graphDoc.close()
#maximal-cliques in a graph
def max_cliques(G):
#print(list(nx.find_cliques(G)))
# print("##############################")
# print("####### max_cliques() ########")
# print(list(nx.find_cliques(G)))
graphDoc= open("find_cliques.txt","w+")
graphDoc.write(str(list(nx.find_cliques(G))))
graphDoc.close()
return nx.find_cliques(G)
# Define get_nodes_and_nbrs()
def get_nodes_and_nbrs(G, nodes_of_interest):
"""
Returns a subgraph of the graph `G` with only the `nodes_of_interest` and their neighbors.
"""
nodes_to_draw = []
# Iterate over the nodes of interest
for n in nodes_of_interest:
# Append the nodes of interest to nodes_to_draw
nodes_to_draw.append(n)
# Iterate over all the neighbors of node n
for nbr in G.neighbors(n):
# Append the neighbors of n to nodes_to_draw
nodes_to_draw.append(nbr)
graphDoc= open("get_nodes_and_nbrs.txt","w+")
graphDoc.write(str(G.subgraph(nodes_to_draw)))
graphDoc.close()
return G.subgraph(nodes_to_draw)
# Define is_in_triangle()
def is_in_triangle(G, n):
"""
Checks whether a node `n` in graph `G` is in a triangle relationship or not.
Returns a boolean.
"""
in_triangle = False
# Iterate over all possible triangle relationship combinations
for n1, n2 in combinations(G.neighbors(n), 2): # G.neighbors(n) return a list of the nodes connected to the node n.
# Check if an edge exists between n1 and n2
if G.has_edge(n1, n2):
in_triangle = True
break
print("############## in_triangle: ################")
print(in_triangle)
print("############## ################")
return in_triangle
#function that return all of the graph nodes that are in a triangle_clique
def nodes_in_triangle(G):
list=[]
for n in G.nodes():
if is_in_triangle(G, n):
list.append(n)
# print("############## nodes_in_triangle: ################")
# print(list)
# print("############## ################")
graphDoc= open("nodes_in_triangle.txt","w+")
graphDoc.write(str(list))
graphDoc.close()
return list
# return a subgraph with the degree-1 egocentric network of the node n
def degree_1_Network(G, n):
vecinos = []
# Iterate over all possible triangle relationship combinations
for m in G.neighbors(n):
vecinos.append([n,m])
graphDoc= open("degree_1_Network.txt","w+")
graphDoc.write(str(vecinos))
graphDoc.close()
return vecinos
#return a subgraph with the degree-1-5 egocentric network of the node
def degree_1_5_Network(G, n):
vecinos = []
# Iterate over all possible triangle relationship combinations
for m in G.neighbors(n):
for o in G.neighbors(m):
if G.has_edge(m,n) :
vecinos.append([m,o])
graphDoc= open("degree_1_5_Network.txt","w+")
graphDoc.write(str(vecinos))
graphDoc.close()
return vecinos
#return a subgraph with the degree-2 egocentric network of the node
def degree_2_Network(G, n):
vecinos = []
# Iterate over all possible triangle relationship combinations
for m in G.neighbors(n):
vecinos.append([n,m])
for o in G.neighbors(m):
vecinos.append([m,o])
for p in G.neighbors(o):
vecinos.append([o,p])
graphDoc= open("degree_2_Network.txt","w+")
graphDoc.write(str(vecinos))
graphDoc.close()
return vecinos
def distanceMeasures(G):
#print("average",nx.average_shortest_path_length(G)) #Average distance between every pair of nodes.
print("diameter",nx.diameter(G)) #maximum distance between any pair of nodes.
print("eccen",nx.eccentricity(G)) #of a node n is the largest distance between n and all other nodes.
print("radius",nx.radius(G)) #Minimum eccentricity.
print("periphery",nx.periphery(G)) #Set of nodes that have eccentricity equal to the diameter.
print("center",nx.center(G)) #set of nodes that have eccentricity equal to the radius.
def draw(graph_to_draw, nameOfDraw):
try:
plt.figure()
nx.draw_networkx(graph_to_draw)
plt.show()
plt.savefig(nameOfDraw + datetime.datetime.now() +"x.png")
except Exception as inst:
print("##############################")
print("error al crear gráfica:")
print(nameOfDraw)
print(inst)
print("##############################")
def graph_is_connected(G):
print("############## is_connected: ################")
print(nx.is_connected(G))
print("############## ################")
def graph_number_connected_components(G):
#G.remove_node(0)
print("############## number_connected_components ################")
print(nx.number_connected_components(G))
print("#############################")
#print("############## sorted connected_components ################")
#print(sorted(nx.connected_components(G)))
#print("#############################")
graphDoc= open("sorted_connected_components.txt","w+")
graphDoc.write(str(sorted(nx.connected_components(G))))
graphDoc.close()
print(sorted(nx.connected_components(G)))
def simplePaths(G):
G.nodes()
paths=nx.all_simple_paths(G, source=9, target=26)
type(paths)
print("############## all_simple_paths ################")
print(paths)
print("#############################")
def draw2(G, nameOfDraw):
try:
pos = nx.get_node_attributes(G, 'login')
plt.figure()
nx.draw_networkx(G, pos)
plt.show()
plt.savefig(nameOfDraw + datetime.datetime.now() +"x.png")
except Exception as inst:
print("##############################")
print("error al crear gráfica:")
print(nameOfDraw)
print(inst)
print("##############################")
def draw3(G, nameOfDraw):
try:
plt.figure(figsize=(10,7))
nx.draw_networkx(G, alpha=0.7, with_labels=False, edge_color='.4')
plt.axis('off')
plt.tight_layout()
plt.show()
plt.savefig(nameOfDraw + datetime.datetime.now() +"x.png")
except Exception as inst:
print("##############################")
print("error al crear gráfica:")
print(nameOfDraw)
print(inst)
print("##############################")
def draw4(G, nameOfDraw):
try:
plt.figure()
nx.draw_networkx(G)
plt.tight_layout()
plt.show()
plt.savefig(nameOfDraw + datetime.datetime.now() +"x.png")
except Exception as inst:
print("##############################")
print("error al crear gráfica:")
print(nameOfDraw)
print(inst)
print("##############################")
def main():
#se consultan los usuarios de github para el analisis
try:
l_usuarios=usuarios()
except Exception as inst:
print("##############################")
print("error al obtener usuarios de la api de github")
print(inst)
print("##############################")
return
#se consultan los seguidores de los usuarios seleccionados
if seguidores(G, l_usuarios) != "false":
##############################
# se ejecuta después de validar que si existe el grafo de usuarios y seguidores
# la función seguidores(G,l_usuarios) carga el grafo con el dataset seleccionado.
# La función seguidores() se ejecuta en la validación que indica si el grafo se creó
# exitosamente (en este caso la función seguidores() retorna "true", caso contrario
# retorna "false", es decir, si el grafo se crea correctamente la función retorna "true"
# y sino retorna "false). Lo anterior antes de permitir hacer cálculos con el grafo
##############################
print("##############################")
print("si existen usuarios y seguidores. Se ha creado correctamente el dataset y el grafo")
print("##############################")
try:
graph_is_connected(G)
except Exception as inst:
print("##############################")
print("error al ejecutar graph_is_connected()")
print(inst)
print("##############################")
##############################
try:
graph_number_connected_components(G)
except Exception as inst:
print("##############################")
print("error al ejecutar graph_is_connected()")
print(inst)
print("##############################")
try:
simplePaths(G)
except Exception as inst:
print("##############################")
print("error al ejecutar simplePaths()")
print(inst)
print("##############################")
##############################
#pintar el grafo con el dataset consultado a github
# plt.figure()
# #nx.draw_networkx(G, [1,2,3], with_labels=False, alpha=0.4,font_size=0.0,node_size=10)
# nx.draw_networkx(G)
# plt.savefig("G.png")
# plt.show()
draw(G, "grafo")
draw3(G, "grafo-tight_layout")
print("##############################")
print("############ G ###############")
print(G)
graphDoc= open("network-graph.txt","w+")
graphDoc.write(str(G))
graphDoc.close()
print("##############################")
##############################
##############################
#pintar los nodos del grafo
# plt.figure()
# nx.draw_networkx(G.edges())
# plt.show()
# plt.savefig("G.edges.png")
print("##############################")
print("######## G.edges() ###########")
print(G.edges())
graphDoc= open("network-graph_edges.txt","w+")
graphDoc.write(str(G.edges()))
graphDoc.close()
draw(G.edges(), "G.edges()")
print("##############################")
##############################
##############################
try:
nodeMeasure_density(G)
except Exception as inst:
print("##############################")
print("error al ejecutar nodeMeasure_density()")
print(inst)
print("##############################")
##############################
##############################
try:
nodeMeasure_degree(G)
except Exception as inst:
print("##############################")
print("error al ejecutar nodeMeasure_degree()")
print(inst)
print("##############################")
##############################
try:
centrality(G)
except Exception as inst:
print("##############################")
print("error al ejecutar centrality():")
print(inst)
print("##############################")
##############################
try:
centralityTopFive(G)
except Exception as inst:
print("##############################")
print("error al ejecutar centralityTopFive():")
print(inst)
print("##############################")
##############################
try:
betweennessCentrality(G)
except Exception as inst:
print("##############################")
print("error al ejecutar betweennessCentrality():")
print(inst)
print("##############################")
##############################
##############################
try:
betweennessCentralityTopFive(G)
except Exception as inst:
print("##############################")
print("error al ejecutar betweennessCentralityTopFive():")
print(inst)
print("##############################")
##############################
##############################
try:
eigenvectorCentrality(G)
except Exception as inst:
print("##############################")
print("error al ejecutar eigenvectorCentrality():")
print(inst)
print("##############################")
##############################
try:
eigenvectorCentralityTopFive(G)
except Exception as inst:
print("##############################")
print("error al ejecutar eigenvectorCentralityTopFive():")
print(inst)
print("##############################")
##############################
try:
closenessCentrality(G)
except Exception as inst:
print("##############################")
print("error al ejecutar closenessCentrality():")
print(inst)
print("##############################")
##############################
try:
closenessCentralityTopFive(G)
except Exception as inst:
print("##############################")
print("error al ejecutar closenessCentralityTopFive():")
print(inst)
print("##############################")
##############################
try:
intersection(closenessCentralityTopFive(G), centralityTopFive(G))
except Exception as inst:
print("##############################")
print("error al ejecutar intersection():")
print(inst)
print("##############################")
##############################
try:
# plt.figure()
# nx.draw_networkx(max_cliques(G))
# plt.show()
# plt.savefig("max_cliques.png")
draw(max_cliques(G), "max_cliques")
except Exception as inst:
print("##############################")
print("Exception nx.draw_networkx(max_cliques(G)).")
print(inst)
print("##############################")
##############################
##############################
# Extract the subgraph with the nodes of interest: T_draw
try:
T_draw = get_nodes_and_nbrs(G, ['angelbotto'])
plt.figure()
nx.draw_networkx(T_draw)
plt.show()
plt.savefig("get_nodes_and_nbrs.png")
except Exception as inst:
print("##############################")
print("exception in get_nodes_and_nbrs")
print(inst)
print("##############################")
##############################
##############################
try:
print("###############is_in_triangle###############")
print(is_in_triangle(G, "angelbotto"))
# print("##############################")
# print(is_in_triangle(G,6))
print("##############################")
except Exception as inst:
print("##############################")
print("exception in is_in_triangle.")
print(inst)
print("##############################")
##############################
#the graph nodes that are in a triangle_clique
try:
nodes_in_triangle(G)
except Exception as inst:
print("##############################")
print("exception in nodes_in_triangle")
print(inst)
print("##############################")
##############################
try:
print("######## degree_1_Network #########")
print(degree_1_Network(G, "angelbotto"))
print("##############################")
except Exception as inst:
print("##############################")
print("exception degree_1_Network")
print(inst)
print("##############################")
##############################
##############################
try:
print("######## degree_1_5_Network #########")
print(degree_1_5_Network(G, "angelbotto"))
print("##############################")
except Exception as inst:
print("##############################")
print("degree_1_5_Network. There are not subgraph with the degree-1 egocentric network of the node")
print(inst)
print("##############################")
##############################
##############################
try:
print("######## degree_2_Network #########")
print(degree_2_Network(G, 'angelbotto'))
print("##############################")
except Exception as inst:
print("##############################")
print("degree_2_Network. There are not subgraph with the degree-1 egocentric network of the node")
print(inst)
print("##############################")
##############################
##############################
try:
print("######## DistanceMeasures #########")
distanceMeasures(G)
print("##############################")
except Exception as inst:
print("##############################")
print("DistanceMeasures Exception")
print(inst)
print("##############################")
##############################
##############################
try:
draw2(max_cliques(G), "max_cliques")
except Exception as inst:
print("##############################")
print("draw2 max_cliques Exception")
print(inst)
print("##############################")
##############################
##############################
try:
draw3(G, "draw3")
except Exception as inst:
print("##############################")
print("draw3 Exception")
print(inst)
print("##############################")
##############################
##############################
try:
draw4(G, "draw4")
except Exception as inst:
print("##############################")
print("draw4 Exception")
print(inst)
print("##############################")
##############################
main()