MCPcopy Index your code
hub / github.com/Persper/code-analytics / devrank

Function devrank

persper/analytics/devrank.py:6–63  ·  view source on GitHub ↗

Memory efficient DevRank using scipy.sparse Args: G - A nx.Digraph object. weight_label - A string, each node in graph should have this attribute. - It will be used as the weight of each node. alpha - A float between 0 and 1, DevRan

(G, weight_label, alpha=0.85, epsilon=1e-5, max_iters=300)

Source from the content-addressed store, hash-verified

4
5
6def devrank(G, weight_label, alpha=0.85, epsilon=1e-5, max_iters=300):
7 """Memory efficient DevRank using scipy.sparse
8
9 Args:
10 G - A nx.Digraph object.
11 weight_label - A string, each node in graph should have this attribute.
12 - It will be used as the weight of each node.
13 alpha - A float between 0 and 1, DevRank's damping factor.
14 epsilon - A float.
15 max_iters - An integer, specify max number of iterations to run.
16
17 Returns:
18 A dict with node names being keys and DevRanks being values.
19 """
20 ni = {}
21 for i, u in enumerate(G):
22 ni[u] = i
23
24 def sizeof(u):
25 return G.node[u][weight_label]
26
27 num_nodes = len(G.nodes())
28 row, col, data = [], [], []
29 for u in G:
30 size_sum = 0
31 for v in G[u]:
32 size_sum += sizeof(v)
33 for v in G[u]:
34 row.append(ni[v])
35 col.append(ni[u])
36 data.append(sizeof(v) / size_sum)
37
38 P = coo_matrix((data, (row, col)), shape=(num_nodes, num_nodes)).tocsr()
39
40 universe_size = 0
41 for u in G:
42 universe_size += sizeof(u)
43
44 p = np.empty(num_nodes)
45 for u in G:
46 p[ni[u]] = sizeof(u) / universe_size
47
48 v = np.ones(num_nodes) / num_nodes
49
50 for i in range(max_iters):
51 new_v = alpha * P.dot(v)
52 gamma = LA.norm(v, 1) - LA.norm(new_v, 1)
53 new_v += gamma * p
54 delta = LA.norm(new_v - v, 1)
55 if delta < epsilon:
56 break
57 v = new_v
58
59 dr = {}
60 for u in G:
61 dr[u] = v[ni[u]]
62
63 return dr

Callers 1

function_devranksMethod · 0.90

Calls 3

nodesMethod · 0.80
sizeofFunction · 0.70
emptyMethod · 0.45

Tested by

no test coverage detected