-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEvalStatistics.py
112 lines (105 loc) · 3.95 KB
/
EvalStatistics.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
Programmer: Chris Tralie
Purpose: Classification Evaluation Statistics / Results Web Page Generation
"""
import numpy as np
import scipy.io as sio
def getCovers80EvalStatistics(ScoresParam, topsidx, fout, name = "default"):
"""
Compute MR, MRR, MAP, and Median Rank for the covers80 dataset
:param ScoresParam: A 160x160 array holding all pairwise
scores, where the first 80 songs are in the the first 80 rows
and the second 80 songs are in the last 80 rows
:param topsidx: An array of cutoffs for reporting songs in the
top K indices
:param fout: A file handle to an HTML file holding the results
:param name: A name to use for this test in the HTML file
"""
NSongs = 80
N = NSongs*2
Scores = np.array(ScoresParam)
#Fill diagonal with -infinity to exclude song from comparison with self
np.fill_diagonal(Scores, -np.inf)
idx = np.argsort(-Scores, 1) #Sort row by row in descending order of score
ranks = np.zeros(N)
for i in range(N):
cover = (i+NSongs)%N #The index of the correct song
for k in range(N):
if idx[i, k] == cover:
ranks[i] = k+1
break
print(ranks)
MR = np.mean(ranks)
MRR = 1.0/N*(np.sum(1.0/ranks))
MDR = np.median(ranks)
print("MR = %g\nMRR = %g\nMDR = %g\n"%(MR, MRR, MDR))
fout.write("<tr><td>%s</td><td>%g</td><td>%g</td><td>%g</td>"%(name, MR, MRR, MDR))
tops = np.zeros(len(topsidx))
for i in range(len(tops)):
tops[i] = np.sum(ranks <= topsidx[i])
print("Top-%i: %i"%(topsidx[i], tops[i]))
fout.write("<td>%i</td>"%tops[i])
#Covers80 score
Scores = Scores[0:NSongs, NSongs::]
idx = np.argmax(Scores, 1)
score = np.sum(idx == np.arange(len(idx)))
print("Covers80 Score: %i / %i"%(score, NSongs))
fout.write("<td>%i/%i</td></tr>\n\n"%(score, NSongs))
return (MR, MRR, MDR, tops)
def getEvalStatistics(ScoresParam, Ks, topsidx, fout, name):
Scores = np.array(ScoresParam)
N = Scores.shape[0]
#Compute MR, MRR, MAP, and Median Rank
#Fill diagonal with -infinity to exclude song from comparison with self
np.fill_diagonal(Scores, -np.inf)
idx = np.argsort(-Scores, 1) #Sort row by row in descending order of score
ranks = np.zeros(N)
startidx = 0
kidx = 0
for i in range(N):
if i >= startidx + Ks[kidx]:
startidx += Ks[kidx]
kidx += 1
print(startidx)
for k in range(N):
diff = idx[i, k] - startidx
if diff >= 0 and diff < Ks[kidx]:
ranks[i] = k+1
break
print(ranks)
MR = np.mean(ranks)
MRR = 1.0/N*(np.sum(1.0/ranks))
MDR = np.median(ranks)
print("MR = %g\nMRR = %g\nMDR = %g\n"%(MR, MRR, MDR))
fout.write("<tr><td>%s</td><td>%g</td><td>%g</td><td>%g</td>"%(name, MR, MRR, MDR))
tops = np.zeros(len(topsidx))
for i in range(len(tops)):
tops[i] = np.sum(ranks <= topsidx[i])
print("Top-%i: %i"%(topsidx[i], tops[i]))
fout.write("<td>%i</td>"%tops[i])
fout.write("</tr>\n\n")
return (MR, MRR, MDR, tops)
def getCovers1000Ks():
import glob
Ks = []
for i in range(1, 396):
songs = glob.glob("Covers1000/%i/*.txt"%i)
Ks.append(len(songs))
return Ks
if __name__ == '__main__':
Scores = sio.loadmat("Covers1000D.mat")
Ks = getCovers1000Ks()
fout = open("Covers1000Results.html", "a")
for FeatureName in ['MFCCs', 'SSMs', 'Chromas', 'SNF', 'Late']:
S = Scores[FeatureName]
S = np.maximum(S, S.T)
getEvalStatistics(S, Ks, [1, 25, 50, 100], fout, FeatureName)
fout.close()
if __name__ == '__main__2':
Scores = sio.loadmat("SHSResults.mat")
SHSIDs = sio.loadmat("SHSIDs.mat")
Ks = SHSIDs['Ks'].flatten()
fout = open("SHSDataset/results.html", "a")
for FeatureName in ['Chromas', 'MFCCs', 'SSMs']:
getEvalStatistics(Scores[FeatureName], Ks, [1, 25, 50, 100], fout, FeatureName)
fout.close()