-
Notifications
You must be signed in to change notification settings - Fork 0
/
richclubcoefficient.py
149 lines (113 loc) · 5.28 KB
/
richclubcoefficient.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
import pandas as pd
import networkx as nx
import snap
import json
import argparse
import sys
from tqdm import tqdm
sourcepath = 'graphs/'
outpath = 'coefficients/'
def get_lists(rc):
x = []
y = []
for k in rc:
x.append(k)
y.append(rc[k])
return (x, y)
def get_supergraph(path):
# open edgelist using networkx
Data = open(path + '{}_usergraph_edges.csv'.format(p), "r")
next(Data, None)
Graphtype = nx.Graph()
G = nx.parse_edgelist(Data, delimiter='\t', create_using=Graphtype,
nodetype=int, data=(('weight', float),))
return G
def get_activity_graph(path, atype):
context = snap.TTableContext()
e_schema = snap.Schema()
e_schema.Add(snap.TStrTAttrPr("source", snap.atStr))
e_schema.Add(snap.TStrTAttrPr("target", snap.atStr))
e_schema.Add(snap.TStrTAttrPr("count", snap.atStr))
n_schema = snap.Schema()
n_schema.Add(snap.TStrTAttrPr("id_user", snap.atStr))
n_schema.Add(snap.TStrTAttrPr("id", snap.atStr))
edgetable = snap.TTable.LoadSS(e_schema, path + '{}_{}_edges_reduced.csv'.format(p, atype), context, ",", snap.TBool(True))
nodetable = snap.TTable.LoadSS(n_schema, path + '{}_{}_nodes_reduced.csv'.format(p, atype), context, ",", snap.TBool(True))
edgeattrv = snap.TStrV()
nodeattrv = snap.TStrV()
net = snap.ToNetwork(snap.PNEANet, edgetable, "source", "target", edgeattrv, nodetable, "id", nodeattrv, snap.aaFirst)
snap.DelSelfEdges(net)
snap.SaveEdgeList(net, 'temp/{}_temp_edgelist.csv'.format(atype))
Data = open('temp/{}_temp_edgelist.csv'.format(atype), 'r')
Graphtype = nx.Graph()
G = nx.parse_edgelist(Data, delimiter='\t', create_using=Graphtype, nodetype=int, data=(('weight', float),), comments='#')
return G
def get_commits_graph(path):
context = snap.TTableContext()
e_schema = snap.Schema()
e_schema.Add(snap.TStrTAttrPr("source", snap.atStr))
e_schema.Add(snap.TStrTAttrPr("target", snap.atStr))
e_schema.Add(snap.TStrTAttrPr("weight", snap.atStr))
n_schema = snap.Schema()
n_schema.Add(snap.TStrTAttrPr("id", snap.atStr))
n_schema.Add(snap.TStrTAttrPr("username", snap.atStr))
n_schema.Add(snap.TStrTAttrPr("size", snap.atStr))
edgetable = snap.TTable.LoadSS(e_schema, path + '{}_edges.csv'.format(pname), context, ",", snap.TBool(True))
nodetable = snap.TTable.LoadSS(n_schema, path + '{}_nodes.csv'.format(pname), context, ",", snap.TBool(True))
edgeattrv = snap.TStrV()
nodeattrv = snap.TStrV()
net = snap.ToNetwork(snap.PNEANet, edgetable, "source", "target", edgeattrv, nodetable, "id", nodeattrv, snap.aaFirst)
snap.DelSelfEdges(net)
snap.SaveEdgeList(net, 'temp/commits_temp_edgelist.csv')
Data = open('temp/commits_temp_edgelist.csv', 'r')
Graphtype = nx.Graph()
G = nx.parse_edgelist(Data, delimiter='\t', create_using=Graphtype, nodetype=int, data=(('weight', float),), comments='#')
return G
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Rich-club coefficient calculation.')
parser.add_argument('--graph', type=str, default='G', help='Source graph for coefficient computation')
parser.add_argument('--N', type=int, default=20, help='Number of projects for which compute the coefficient')
args = parser.parse_args()
projects = pd.read_csv('project.csv')[:args.N]
# rich-club coefficient dictionary
# each project is referred using the id_project as keys
rcc = {}
# select path for source graphs
if args.graph == 'G':
path = sourcepath + 'supergraphs/'
outname = 'supergraph'
elif args.graph == 'i':
path = sourcepath + 'issues/'
outname = 'issues'
elif args.graph == 'p':
path = sourcepath + 'pullrequests/'
outname = 'pullrequests'
elif args.graph == 'c':
path = sourcepath + 'commits/'
outname = 'commits'
else:
print('Error in input parameter: only G, i, p, c are allowed')
sys.exit(1)
num = projects.shape[0]
for index, row in tqdm(projects.iterrows()):
p = str(row['id'])
pname = row['name']
try:
if args.graph == 'G':
G = get_supergraph(path)
elif args.graph == 'i':
G = get_activity_graph(path, 'is')
elif args.graph == 'p':
G = get_activity_graph(path, 'pr')
elif args.graph == 'c':
G = get_commits_graph(path)
rc = nx.algorithms.richclub.rich_club_coefficient(G, normalized=True)
k, rho = get_lists(rc)
rcc[p] = {}
rcc[p]['k'] = k
rcc[p]['rho'] = rho
except Exception as e:
print('Cannot calculate coefficient for this project: {}'.format(p))
# for each project a dictionary {k:[], rho:[]} is defined with the list of degrees and correspondent coefficient values
with open(outpath + 'rcc_{}.json'.format(outname), 'w') as fp:
json.dump(rcc, fp)