-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubGraph.py
More file actions
411 lines (303 loc) · 10 KB
/
githubGraph.py
File metadata and controls
411 lines (303 loc) · 10 KB
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
#!/usr/bin/env python
"""
Visualize the social network of a GitHub user
Copyright (C) 2007-2010 Martin Laprise (mlaprise@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 dated June, 1991.
This software is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
import numpy as np
import time
def importTxtGraph(filename):
'''
Import and Create a python-graph from a plain txt files
example: Stanford large network dataset collection
'''
graph_txt=np.loadtxt(filename,dtype=int)
G = nx.Graph()
# Add nodes
nodes = np.unique(graph_txt)
for node in nodes:
G.add_node(node)
# Add edges
for edge in graph_txt:
G.add_edge(edge[0], edge[1])
return G
# Import pygraph
from pygraph.classes.graph import graph
from pygraph.classes.digraph import digraph
from pygraph.algorithms.traversal import traversal
from pygraph.readwrite.dot import write
from pygraph.readwrite.dot import read
import networkx as nx
# Import pygraphviz
from pygraphviz import *
# Import github2 api client
from github2.client import Github
# Import the ubigraph server stuff
import xmlrpclib
import time
class githubGraph:
def __init__(self, userName, apiToken):
self.ghConnect = Github(username=userName, api_token=apiToken)
def getUserData(self, userID):
'''
Retrieve the social graph info from the user
'''
followingList = self.ghConnect.users.following(userID)
followersList = self.ghConnect.users.followers(userID)
return [followingList, followersList]
def addUserToDigraph(self, Graph, userID):
'''
Add the user to the digraph
'''
[followingList, followersList] = self.getUserData(userID)
for snUser in followingList:
if snUser not in Graph.nodes():
Graph.add_node(snUser)
if (userID, snUser) not in Graph.edges():
Graph.add_edge(userID, snUser)
for snUser in followersList:
if snUser not in Graph.nodes():
Graph.add_node(snUser)
if (snUser, userID) not in Graph.edges():
Graph.add_edge(snUser, userID)
print str(len(Graph)) + ' nodes'
def addUserToGraph(self, Graph, userID):
'''
Add the user to the graph
'''
[followingList, followersList] = self.getUserData(userID)
for snUser in followingList:
if (snUser not in Graph.nodes()) and (snUser in followersList):
Graph.add_node(snUser)
if (userID, snUser) not in Graph.edges() and (snUser in followersList):
Graph.add_edge(userID, snUser)
print str(len(Graph)) + ' nodes'
def addCollToGraph(self, Graph, userID):
'''
Add a collaborators to the graph
'''
repos = self.ghConnect.repos.list(userID)
for repo in repos:
try:
repoColls = self.ghConnect.repos.list_collaborators(userID+'/'+repo.name)
except (RuntimeError, gaierror):
time.sleep(60)
repoColls = self.ghConnect.repos.list_collaborators(userID+'/'+repo.name)
for coll in repoColls:
if (coll not in Graph.nodes()):
Graph.add_node(coll)
if (userID, coll) not in Graph.edges():
Graph.add_edge(userID, coll)
print str(len(Graph)) + ' nodes'
def newCollToGraph(self, Graph, userID):
'''
Add a collaborators to the graph
'''
repos = self.ghConnect.repos.list(userID)
newSubGraph = nx.Graph()
for repo in repos:
try:
repoColls = self.ghConnect.repos.list_collaborators(userID+'/'+repo.name)
except (RuntimeError, gaierror):
time.sleep(60)
repoColls = self.ghConnect.repos.list_collaborators(userID+'/'+repo.name)
for coll in repoColls:
if (coll not in Graph.nodes()):
Graph.add_node(coll)
if (coll not in newSubGraph.nodes()):
newSubGraph.add_node(coll)
if (userID, coll) not in newSubGraph.edges():
newSubGraph.add_edge(userID, coll)
Graph.add_graph(newSubGraph)
return newSubGraph
def ffDigraph(self, myID, depth = 1, pngOutput = 1, dotOutput = 1, pngDPI = 10):
'''
Generate the following/followers digraph
'''
myName = myID
[followingList, followersList] = self.getUserData(myID)
# Graph creation
githubGraph = DiGraph()
githubGraph.add_node(myID)
self.addUserToDigraph(githubGraph, myID)
# Graph traversal
for d in range(depth):
retrievalItr = traversal(githubGraph, myID, 'post')
try:
while 1:
userID=retrievalItr.next()
'''
Add a user to the graph
Waiting 60 sec if we go beyond the API limitation (60 requests/min)
'''
try:
self.addUserToDigraph(githubGraph, userID)
except RuntimeError:
time.sleep(60)
self.addUserToDigraph(githubGraph, userID)
except StopIteration:
print 'Depth ' + str(d+1) + ' Done !'
return githubGraph
def ffGraph(self, myID, depth = 1):
'''
Generate the following/followers graph, only add an (coll not in Graph.nodes())edge if the two
users follow each other.
'''
myName = myID
[followingList, followersList] = self.getUserData(myID)
# Graph creation
githubGraph = nx.Graph()
githubGraph.add_node(myID)
self.addUserToGraph(githubGraph, myID)
# Graph traversal
for d in range(depth):
dfsList = nx.dfs_postorder(githubGraph, myID)
for userID in dfsList:
'''
Add a user to the graph
Waiting 60 sec if we go beyond the API limitation (60 requests/min)
'''
try:
self.addUserToGraph(githubGraph, userID)
except RuntimeError, gaierror:
time.sleep(60)
self.addUserToGraph(githubGraph, userID)
print 'Depth ' + str(d+1) + ' Done !'
return githubGraph
def collGraph(self, myID, depth = 1):
'''
Generate the collaborators graph, only add an edge if the two nodes
collaborate on the same project.
'''
myName = myID
# Graph creation
githubGraph = nx.Graph()
githubGraph.add_node(myID)
self.addCollToGraph(githubGraph, myID)
# Graph traversal
for d in range(depth):
dfsList = nx.dfs_postorder(githubGraph, myID)
for userID in dfsList:
'''
Add a user to the graph
Waiting 60 sec if we go beyond the API limitation (60 requests/min)
'''
try:
self.addCollToGraph(githubGraph, userID)
except (RuntimeError, gaierror):
time.sleep(60)
self.addCollToGraph(githubGraph, userID)
print 'Depth ' + str(d+1) + ' Done !'
return githubGraph
def collGraphViz(self, myID, depth = 1):
'''
Generate the collaborators graph, only add an edge if the two nodes
collaborate on the same project.
'''
# Ubiserver stuff
server_url = 'http://127.0.0.1:20738/RPC2'
server = xmlrpclib.Server(server_url)
G = server.ubigraph;
G.clear()
myName = myID
# Graph creation
githubGraph = graph()
githubGraph.add_node(myID)
self.addCollToGraph(githubGraph, myID)
# Graph traversal
for d in range(depth):
retrievalItr = traversal(githubGraph, myID, 'post')
try:
while 1:
userID=retrievalItr.next()
'''
Add a user to the graph
Waiting 60 sec if we go beyond the API limitation (60 requests/min)
'''
try:
subGraph = self.newCollToGraph(githubGraph, userID)
except RuntimeError:
time.sleep(60)
subGraph = self.newCollToGraph(githubGraph, userID)
except StopIteration:
self.updateUbiServer(G, subGraph)
return githubGraph
def pngViz(self, graph, filename, pngDPI = 10, penwidth = 5):
# Construct the image of the graph
dot = write(graph)
githubGraphViz = AGraph(string=dot)
githubGraphViz.graph_attr['label']='Social Graph of ' + str(graph)
githubGraphViz.graph_attr['dpi'] = str(pngDPI)
githubGraphViz.graph_attr['overlap'] = 'scale'
githubGraphViz.node_attr['label']= ''
githubGraphViz.node_attr['color']= 'blue'
githubGraphViz.node_attr['style']= 'filled'
githubGraphViz.node_attr['shape']='circle'
githubGraphViz.edge_attr['color']='black'
githubGraphViz.edge_attr['penwidth']='10'
githubGraphViz.layout()
# Draw as PNG
githubGraphViz.draw(filename + '.png')
def ubiServer(self, graph, label=[]):
'''
Dynamicaly visualizing the graph using the ubigraph server
(force-directed layout algorithm)
'''
server_url = 'http://127.0.0.1:20738/RPC2'
server = xmlrpclib.Server(server_url)
G = server.ubigraph;
G.clear()
#G.set_edge_style_attribute(0, "spline", "true")
# List of nodes
nodes = graph.nodes()
# List of edges
edges = graph.edges()
# Dict mapping the name of the nodes with the id returned by the server
nodes_id = {}
# Add all the nodes to the server
for node in nodes:
node_id = G.new_vertex()
nodes_id[node] = node_id
if node in label:
G.set_vertex_attribute(node_id, "label", node)
G.set_vertex_attribute(node_id, 'color', '#ffff40')
G.set_vertex_attribute(node_id, 'size', '5.0')
# Add all the edges to the server
for edge in edges:
G.new_edge(nodes_id[edge[0]], nodes_id[edge[1]])
def updateUbiServer(self, G, graph, label = []):
'''
Add the subgraph to the ubiserver graph G
'''
# List of nodes
nodes = graph.nodes()
# List of edges
edges = graph.edges()
# Dict mapping the name of the nodes with the id returned by the server
nodes_id = {}
# Add all the nodes to the server
for node in nodes:
node_id = G.new_vertex()
nodes_id[node] = node_id
if node in label:
G.set_vertex_attribute(node_id, "label", node)
G.set_vertex_attribute(node_id, 'color', '#ffff40')
G.set_vertex_attribute(node_id, 'size', '5.0')
# Add all the edges to the server
for edge in edges:
G.new_edge(nodes_id[edge[0]], nodes_id[edge[1]])
def dotViz(self, graph, filename):
# Construct the image of the graph
dot = write(graph)
# Write a dot file
myfile = file(filename + '.dot', 'w')
myfile.write(dot)