forked from bmcfee/hypergraph_playlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
computeLikelihood.py
executable file
·88 lines (65 loc) · 2.3 KB
/
computeLikelihood.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
'''
CREATED:2012-03-30 13:20:49 by Brian McFee <bmcfee@cs.ucsd.edu>
Compute test-set likelihoods for each playlist category from a given model
Usage:
./computeLikelihood.py likelihoods_OUT.pickle perfs_IN.pickle
'''
import sys
import cPickle as pickle
import hypergraph
import argparse
import glob, os
def processArguments():
parser = argparse.ArgumentParser(description='Playlist likelihood computation')
parser.add_argument( 'likelihoods_out',
nargs = 1,
help = 'Path to likelihood output')
parser.add_argument( 'perf_in',
nargs = 1,
help = 'Path to training results file')
return vars(parser.parse_args(sys.argv[1:]))
def loadPerfs(params):
def loadModel(p):
with open(p['model_in'][0], 'r') as f:
G = pickle.load(f)['G']
pass
return G
with open(params['perf_in'][0], 'r') as f:
P = pickle.load(f)
pass
G = loadModel(P['params'])
return (G, P)
def getFiles(params, suffix='test'):
"""
Generator to walk a basedir and grab all files of the specified extension
"""
for root, dirs, files in os.walk(params['playlist_dir'][0]):
for f in glob.glob(os.path.join(root, '*_'+suffix+'.pickle')):
with open(os.path.abspath(f), 'r') as infile:
P = pickle.load(infile)
pass
yield (P, os.path.basename(f).replace('_'+suffix+'.pickle',''))
pass
def computeLikelihood(params):
# Load the perfs and model data
(G, Perfs) = loadPerfs(params)
L = {}
LL_FUNCTION = G.loglikelihood_stateless
if Perfs['params']['markov']:
LL_FUNCTION = G.loglikelihood
pass
for (P, name) in getFiles(Perfs['params']):
# Set the weights for this set
print 'Testing %s' % name
G.setWeights(Perfs['weights'][name])
L[name] = map(LL_FUNCTION, P)
pass
return {'params': params, 'L': L}
if __name__ == '__main__':
params = processArguments()
results = computeLikelihood(params)
with open(params['likelihoods_out'][0], 'w') as f:
pickle.dump(results, f)
pass
pass