forked from sharnett/wiki_pagerank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompactify.py
executable file
·52 lines (46 loc) · 1.66 KB
/
compactify.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
from __future__ import division
from cPickle import load, dump
from scipy.io import savemat
from scipy.sparse import coo_matrix
from numpy import savez
def create_compact_dicts():
print 'compactifying...'
sparse_to_dense, dense_to_sparse = {}, []
graphLineCount = int(open('graph.txt.wc').read().strip().split(' ')[0])
for i, line in enumerate(open('graph.txt')):
ID = int(line.split()[0])
sparse_to_dense[ID] = i
dense_to_sparse.append(ID)
if (i % 10000000 == 0):
print i
print(i/graphLineCount)
dump(sparse_to_dense, open('sparse_to_dense.pickle', 'w'), 2)
dump(dense_to_sparse, open('dense_to_sparse.pickle', 'w'), 2)
def create_matrix():
graphLineCount = int(open('graph.txt.wc').read().strip().split(' ')[0])
sparse_to_dense = load(open('sparse_to_dense.pickle'))
print 'reading graph file and matrixifying...'
I, J = [], []
lineN = 0
for line in open('graph.txt'):
if (lineN % 10000000 == 0):
print lineN
print(lineN/graphLineCount)
lineN = lineN + 1
converted = [sparse_to_dense.get(int(ID), -1) for ID in line.split()]
converted = [x for x in converted if x>=0]
i = converted[0]
I.extend([i]*(len(converted)-1))
J.extend(converted[1:])
n = max([max(I), max(J)]) + 1
data = [1]*len(I)
return coo_matrix((data, (I,J)), shape=(n,n), dtype='i1')
def main():
create_compact_dicts()
A = create_matrix()
print 'saving compactified matrix'
f = open('A.npy','w')
savez(f,row=A.row,col=A.col,data=A.data,shape=A.shape)
print 'saved'
if __name__ == '__main__':
main()