forked from bmcfee/hypergraph_playlist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopicToEdge.py
More file actions
executable file
·48 lines (37 loc) · 1016 Bytes
/
topicToEdge.py
File metadata and controls
executable file
·48 lines (37 loc) · 1016 Bytes
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
#!/usr/bin/env python
'''
CREATED:2012-03-22 13:41:57 by Brian McFee <bmcfee@cs.ucsd.edu>
Convert latent topic assignment vectors to edge sets
Usage:
./topicToEdge.py edge_OUT.pickle data_IN.pickle NUM_ASSIGNMENTS DESCRIPTION
'''
import sys
import numpy
import cPickle as pickle
import heapq
def loadData(infile):
with open(infile, 'r') as f:
X = pickle.load(f)['X']
pass
return X
def makeEdges(X, k, DESC):
d = len(X[X.keys()[0]])
E = {}
idx = range(d)
edge_labels = ['%s-%02d.%02d' % (DESC, k, z) for z in idx]
for (v, topics) in X.iteritems():
E[v] = []
for (score, i) in heapq.nlargest(k, zip(topics, idx)):
E[v].append(edge_labels[i])
pass
pass
return E
if __name__ == '__main__':
X = loadData(sys.argv[2])
k = int(sys.argv[3])
DESC = sys.argv[4]
E = makeEdges(X, k, DESC)
with open(sys.argv[1], 'w') as f:
pickle.dump({'X': E}, f)
pass
pass