forked from bmcfee/hypergraph_playlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topEdgeWeights.py
executable file
·40 lines (28 loc) · 972 Bytes
/
topEdgeWeights.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
#!/usr/bin/env python
'''
CREATED:2012-03-28 12:54:50 by Brian McFee <bmcfee@cs.ucsd.edu>
Extract the top k edges (by weight) from each playlist model
Usage:
./topEdgeWeights.py results_IN.pickle PLAYLIST_CATEGORIES.txt k topweights_OUT.csv
'''
import sys
import cPickle as pickle
def topEdgeWeights(results_IN, cats_IN, k, weights_OUT):
with open(results_IN, 'r') as f:
weights = pickle.load(f)['weights']
pass
with open(cats_IN, 'r') as f:
cats = [s.strip() for s in f.readlines()]
pass
with open(weights_OUT, 'w') as f:
for c in cats:
W = [(weight, edge) for (edge, weight) in weights[c].iteritems()]
W.sort(reverse=True)
edges = [edge for (weight,edge) in W]
f.write('%s,%s\n' % (c, ','.join(edges[:k])))
pass
pass
pass
if __name__ == '__main__':
topEdgeWeights(sys.argv[1], sys.argv[2], int(sys.argv[3]), sys.argv[4])
pass