-
Notifications
You must be signed in to change notification settings - Fork 14
/
compute_Linv.py
68 lines (53 loc) · 1.8 KB
/
compute_Linv.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
import numpy as np, sys, os, getopt
#========================================================================================#
def printHelp():
print '''Argument flags:
-e <path_to_edge_list> (required)\n'''
def row_sum_normalize(A):
s = np.sum(A,axis=1)
X,Y = np.meshgrid(s,s)
A = A / Y
return A
def load_edge_list(path):
edge_text = open(path).read()
edge_text = edge_text.replace('\r','\n').replace('\n\n','\n').strip('\n')
out = []
for l in edge_text.split('\n'):
l = l.split(',')
i = int(l[0])
j = int(l[1])
out += [(i,j)]
return out
def make_adjacency_matrix(edges):
N = np.max([i for i,j in edges]+[j for i,j in edges])+1
A = np.zeros((N,N))
for i,j in edges:
A[i,j] = 1.
A[j,i] = 1.
return A
#========================================================================================#
def main(argv):
try:
opts,args = getopt.getopt(argv, 'e:')
except:
print '\nInputs formatted incorrectly'
printHelp(); sys.exit(2)
#get the arguments and turn them into variables
path_to_edge_list = None
for o,a in opts:
if o == '-e': path_to_edge_list = a
#====================================================================================#
if path_to_edge_list == None: print 'Error: You must input an edge list using the -e flag'; sys.exit(2)
if not os.path.exists(path_to_edge_list): print 'Error: The file '+path_to_edge_list+' does not exist'; sys.exit(2)
# Make Laplacian matrix
print 'Making Laplancian matrix'
edges = load_edge_list(path_to_edge_list)
A = make_adjacency_matrix(edges)
L = np.identity(A.shape[0]) - row_sum_normalize(A)
# Invert graph Laplacian
print 'Inverting the Laplacian'
Linv = np.linalg.pinv(L)
outpath = '/'.join(path_to_edge_list.split('/')[:-1] + ['Linv.npy'])
np.save(outpath, Linv)
if __name__ == '__main__':
main(sys.argv[1:])