1 | #! /usr/bin/python |
---|
2 | |
---|
3 | from aubio.bench.node import * |
---|
4 | from os.path import dirname,basename |
---|
5 | |
---|
6 | def mmean(l): |
---|
7 | return sum(l)/max(float(len(l)),1) |
---|
8 | |
---|
9 | def stdev(l): |
---|
10 | smean = 0 |
---|
11 | if not len(l): return smean |
---|
12 | lmean = mmean(l) |
---|
13 | for i in l: |
---|
14 | smean += (i-lmean)**2 |
---|
15 | smean *= 1. / len(l) |
---|
16 | return smean**.5 |
---|
17 | |
---|
18 | class benchonset(bench): |
---|
19 | |
---|
20 | """ list of values to store per file """ |
---|
21 | valuenames = ['orig','missed','Tm','expc','bad','Td'] |
---|
22 | """ list of lists to store per file """ |
---|
23 | valuelists = ['l','labs'] |
---|
24 | """ list of values to print per dir """ |
---|
25 | printnames = [ 'mode', 'thres', 'dist', 'prec', 'recl', |
---|
26 | 'GD', 'FP', |
---|
27 | 'Torig', 'Ttrue', 'Tfp', 'Tfn', 'TTm', 'TTd', |
---|
28 | 'aTtrue', 'aTfp', 'aTfn', 'aTm', 'aTd', |
---|
29 | 'mean', 'smean', 'amean', 'samean'] |
---|
30 | |
---|
31 | """ per dir """ |
---|
32 | formats = {'mode': "%12s" , 'thres': "%5.4s", |
---|
33 | 'dist': "%5.4s", 'prec': "%5.4s", 'recl': "%5.4s", |
---|
34 | 'Torig': "%5.4s", 'Ttrue': "%5.4s", 'Tfp': "%5.4s", 'Tfn': "%5.4s", |
---|
35 | 'TTm': "%5.4s", 'TTd': "%5.4s", |
---|
36 | 'aTtrue':"%5.4s", 'aTfp': "%5.4s", 'aTfn': "%5.4s", |
---|
37 | 'aTm': "%5.4s", 'aTd': "%5.4s", |
---|
38 | 'mean': "%5.40s", 'smean': "%5.40s", |
---|
39 | 'amean': "%5.40s", 'samean': "%5.40s", |
---|
40 | "GD": "%5.4s", "FP": "%5.4s", |
---|
41 | "GDm": "%5.4s", "FPd": "%5.4s"} |
---|
42 | |
---|
43 | def dir_eval(self): |
---|
44 | """ evaluate statistical data over the directory """ |
---|
45 | v = self.v |
---|
46 | |
---|
47 | v['mode'] = self.params.onsetmode |
---|
48 | v['thres'] = self.params.threshold |
---|
49 | |
---|
50 | v['Torig'] = sum(v['orig']) |
---|
51 | v['TTm'] = sum(v['Tm']) |
---|
52 | v['TTd'] = sum(v['Td']) |
---|
53 | v['Texpc'] = sum(v['expc']) |
---|
54 | v['Tbad'] = sum(v['bad']) |
---|
55 | v['Tmissed'] = sum(v['missed']) |
---|
56 | v['aTm'] = mmean(v['Tm']) |
---|
57 | v['aTd'] = mmean(v['Td']) |
---|
58 | |
---|
59 | v['mean'] = mmean(v['l']) |
---|
60 | v['smean'] = stdev(v['l']) |
---|
61 | |
---|
62 | v['amean'] = mmean(v['labs']) |
---|
63 | v['samean'] = stdev(v['labs']) |
---|
64 | |
---|
65 | # old type calculations |
---|
66 | # good detection rate |
---|
67 | v['GD'] = 100.*(v['Torig']-v['Tmissed']-v['TTm'])/v['Torig'] |
---|
68 | # false positive rate |
---|
69 | v['FP'] = 100.*(v['Tbad']+v['TTd'])/v['Torig'] |
---|
70 | # good detection counting merged detections as good |
---|
71 | v['GDm'] = 100.*(v['Torig']-v['Tmissed'])/v['Torig'] |
---|
72 | # false positives counting doubled as good |
---|
73 | v['FPd'] = 100.*v['Tbad']/v['Torig'] |
---|
74 | |
---|
75 | # mirex type annotations |
---|
76 | totaltrue = v['Texpc']-v['Tbad']-v['TTd'] |
---|
77 | totalfp = v['Tbad']+v['TTd'] |
---|
78 | totalfn = v['Tmissed']+v['TTm'] |
---|
79 | self.v['Ttrue'] = totaltrue |
---|
80 | self.v['Tfp'] = totalfp |
---|
81 | self.v['Tfn'] = totalfn |
---|
82 | # average over the number of annotation files |
---|
83 | N = float(len(self.reslist)) |
---|
84 | self.v['aTtrue'] = totaltrue/N |
---|
85 | self.v['aTfp'] = totalfp/N |
---|
86 | self.v['aTfn'] = totalfn/N |
---|
87 | |
---|
88 | # F-measure |
---|
89 | self.P = 100.*float(totaltrue)/max(totaltrue + totalfp,1) |
---|
90 | self.R = 100.*float(totaltrue)/max(totaltrue + totalfn,1) |
---|
91 | #if self.R < 0: self.R = 0 |
---|
92 | self.F = 2.* self.P*self.R / max(float(self.P+self.R),1) |
---|
93 | self.v['dist'] = self.F |
---|
94 | self.v['prec'] = self.P |
---|
95 | self.v['recl'] = self.R |
---|
96 | |
---|
97 | def plotroc(self,d,plottitle=""): |
---|
98 | import Gnuplot, Gnuplot.funcutils |
---|
99 | gd = [] |
---|
100 | fp = [] |
---|
101 | for i in self.vlist: |
---|
102 | gd.append(i['GD']) |
---|
103 | fp.append(i['FP']) |
---|
104 | d.append(Gnuplot.Data(fp, gd, with='linespoints', |
---|
105 | title="%s %s" % (plottitle,i['mode']) )) |
---|
106 | |
---|
107 | def plotplotroc(self,d,outplot=0,extension='ps'): |
---|
108 | import Gnuplot, Gnuplot.funcutils |
---|
109 | from sys import exit |
---|
110 | g = Gnuplot.Gnuplot(debug=0, persist=1) |
---|
111 | if outplot: |
---|
112 | if extension == 'ps': ext, extension = '.ps' , 'postscript' |
---|
113 | elif extension == 'png': ext, extension = '.png', 'png' |
---|
114 | elif extension == 'svg': ext, extension = '.svg', 'svg' |
---|
115 | else: exit("ERR: unknown plot extension") |
---|
116 | g('set terminal %s' % extension) |
---|
117 | g('set output \'roc-%s%s\'' % (outplot,ext)) |
---|
118 | xmax = 30 #max(fp) |
---|
119 | ymin = 50 |
---|
120 | g('set xrange [0:%f]' % xmax) |
---|
121 | g('set yrange [%f:100]' % ymin) |
---|
122 | # grid set |
---|
123 | g('set grid') |
---|
124 | g('set xtics 0,5,%f' % xmax) |
---|
125 | g('set ytics %f,5,100' % ymin) |
---|
126 | g('set key 27,65') |
---|
127 | #g('set format \"%g\"') |
---|
128 | g.title(basename(self.datadir)) |
---|
129 | g.xlabel('false positives (%)') |
---|
130 | g.ylabel('correct detections (%)') |
---|
131 | g.plot(*d) |
---|
132 | |
---|
133 | def plotpr(self,d,plottitle=""): |
---|
134 | import Gnuplot, Gnuplot.funcutils |
---|
135 | x = [] |
---|
136 | y = [] |
---|
137 | for i in self.vlist: |
---|
138 | x.append(i['prec']) |
---|
139 | y.append(i['recl']) |
---|
140 | d.append(Gnuplot.Data(x, y, with='linespoints', |
---|
141 | title="%s %s" % (plottitle,i['mode']) )) |
---|
142 | |
---|
143 | def plotplotpr(self,d,outplot=0,extension='ps'): |
---|
144 | import Gnuplot, Gnuplot.funcutils |
---|
145 | from sys import exit |
---|
146 | g = Gnuplot.Gnuplot(debug=0, persist=1) |
---|
147 | if outplot: |
---|
148 | if extension == 'ps': ext, extension = '.ps' , 'postscript' |
---|
149 | elif extension == 'png': ext, extension = '.png', 'png' |
---|
150 | elif extension == 'svg': ext, extension = '.svg', 'svg' |
---|
151 | else: exit("ERR: unknown plot extension") |
---|
152 | g('set terminal %s' % extension) |
---|
153 | g('set output \'pr-%s%s\'' % (outplot,ext)) |
---|
154 | g.title(basename(self.datadir)) |
---|
155 | g.xlabel('Recall (%)') |
---|
156 | g.ylabel('Precision (%)') |
---|
157 | g.plot(*d) |
---|
158 | |
---|
159 | def plotfmeas(self,d,plottitle=""): |
---|
160 | import Gnuplot, Gnuplot.funcutils |
---|
161 | x,y = [],[] |
---|
162 | for i in self.vlist: |
---|
163 | x.append(i['thres']) |
---|
164 | y.append(i['dist']) |
---|
165 | d.append(Gnuplot.Data(x, y, with='linespoints', |
---|
166 | title="%s %s" % (plottitle,i['mode']) )) |
---|
167 | |
---|
168 | def plotplotfmeas(self,d,outplot="",extension='ps', title="F-measure"): |
---|
169 | import Gnuplot, Gnuplot.funcutils |
---|
170 | from sys import exit |
---|
171 | g = Gnuplot.Gnuplot(debug=0, persist=1) |
---|
172 | if outplot: |
---|
173 | if extension == 'ps': terminal = 'postscript' |
---|
174 | elif extension == 'png': terminal = 'png' |
---|
175 | elif extension == 'svg': terminal = 'svg' |
---|
176 | else: exit("ERR: unknown plot extension") |
---|
177 | g('set terminal %s' % terminal) |
---|
178 | g('set output \'fmeas-%s.%s\'' % (outplot,extension)) |
---|
179 | g.xlabel('threshold \\delta') |
---|
180 | g.ylabel('F-measure (%)') |
---|
181 | g('set xrange [0:1.2]') |
---|
182 | g('set yrange [0:100]') |
---|
183 | g.title(basename(self.datadir)) |
---|
184 | # grid set |
---|
185 | #g('set grid') |
---|
186 | #g('set xtics 0,5,%f' % xmax) |
---|
187 | #g('set ytics %f,5,100' % ymin) |
---|
188 | #g('set key 27,65') |
---|
189 | #g('set format \"%g\"') |
---|
190 | g.plot(*d) |
---|
191 | |
---|
192 | def plotdiffs(self,d,plottitle=""): |
---|
193 | import Gnuplot, Gnuplot.funcutils |
---|
194 | v = self.v |
---|
195 | l = v['l'] |
---|
196 | mean = v['mean'] |
---|
197 | smean = v['smean'] |
---|
198 | amean = v['amean'] |
---|
199 | samean = v['samean'] |
---|
200 | val = [] |
---|
201 | per = [0] * 100 |
---|
202 | for i in range(0,100): |
---|
203 | val.append(i*.001-.05) |
---|
204 | for j in l: |
---|
205 | if abs(j-val[i]) <= 0.001: |
---|
206 | per[i] += 1 |
---|
207 | total = v['Torig'] |
---|
208 | for i in range(len(per)): per[i] /= total/100. |
---|
209 | |
---|
210 | d.append(Gnuplot.Data(val, per, with='fsteps', |
---|
211 | title="%s %s" % (plottitle,v['mode']) )) |
---|
212 | #d.append('mean=%f,sigma=%f,eps(x) title \"\"'% (mean,smean)) |
---|
213 | #d.append('mean=%f,sigma=%f,eps(x) title \"\"'% (amean,samean)) |
---|
214 | |
---|
215 | |
---|
216 | def plotplotdiffs(self,d,outplot=0,extension='ps'): |
---|
217 | import Gnuplot, Gnuplot.funcutils |
---|
218 | from sys import exit |
---|
219 | g = Gnuplot.Gnuplot(debug=0, persist=1) |
---|
220 | if outplot: |
---|
221 | if extension == 'ps': ext, extension = '.ps' , 'postscript' |
---|
222 | elif extension == 'png': ext, extension = '.png', 'png' |
---|
223 | elif extension == 'svg': ext, extension = '.svg', 'svg' |
---|
224 | else: exit("ERR: unknown plot extension") |
---|
225 | g('set terminal %s' % extension) |
---|
226 | g('set output \'diffhist-%s%s\'' % (outplot,ext)) |
---|
227 | g('eps(x) = 1./(sigma*(2.*3.14159)**.5) * exp ( - ( x - mean ) ** 2. / ( 2. * sigma ** 2. ))') |
---|
228 | g.title(basename(self.datadir)) |
---|
229 | g.xlabel('delay to hand-labelled onset (s)') |
---|
230 | g.ylabel('% number of correct detections / ms ') |
---|
231 | g('set xrange [-0.05:0.05]') |
---|
232 | g('set yrange [0:50]') |
---|
233 | g.plot(*d) |
---|
234 | |
---|
235 | |
---|