-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
47 lines (38 loc) · 1.33 KB
/
utils.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
import scipy.sparse as sparse
def save_edgelist(G, fname, weighted=False, sep=' '):
with open(fname, 'w') as f:
for src in G:
for dst, weight in zip(G[src], G[src].weights):
f.write(unicode(str(src)))
f.write(sep)
f.write(unicode(str(dst)))
if weighted:
f.write(sep)
f.write(unicode(str(weight)))
f.write('\n')
def to_adjacency_matrix(G, nodelist=None, dtype=None, real=False):
"""Inspired by the NetworkX equivalent."""
if nodelist is None:
nodelist = sorted(G.nodes())
nodeset = set(nodelist)
if len(nodelist) != len(nodeset):
raise ValueError("Ambiguous ordering: `nodelist` contained duplicates.")
nlen=len(nodelist)
index=dict(zip(nodelist,range(nlen)))
I = []
J = []
A_ij = []
# TODO support weights
for src in G:
for dst, weight in zip(G[src], G[src].weights):
try:
I.append(index[src])
J.append(index[dst])
if real:
A_ij.append(1. * weight)
else:
A_ij.append(weight)
except KeyError:
pass
A = sparse.coo_matrix((A_ij, (I, J)), shape=(nlen, nlen), dtype=dtype).tocsr()
return A