[83c6734] | 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", |
---|
[ac4f38e] | 38 | 'mean': "%5.6s", 'smean': "%5.6s", |
---|
| 39 | 'amean': "%5.6s", 'samean': "%5.6s", |
---|
[83c6734] | 40 | "GD": "%5.4s", "FP": "%5.4s", |
---|
[ac4f38e] | 41 | "GDm": "%5.4s", "FPd": "%5.4s", |
---|
| 42 | "bufsize": "%5.4s", "hopsize": "%5.4s", |
---|
| 43 | "time": "%5.4s"} |
---|
[83c6734] | 44 | |
---|
| 45 | def dir_eval(self): |
---|
| 46 | """ evaluate statistical data over the directory """ |
---|
| 47 | v = self.v |
---|
| 48 | |
---|
| 49 | v['mode'] = self.params.onsetmode |
---|
| 50 | v['thres'] = self.params.threshold |
---|
[6ae3a77] | 51 | v['bufsize'] = self.params.bufsize |
---|
| 52 | v['hopsize'] = self.params.hopsize |
---|
| 53 | v['silence'] = self.params.silence |
---|
| 54 | v['mintol'] = self.params.mintol |
---|
[83c6734] | 55 | |
---|
| 56 | v['Torig'] = sum(v['orig']) |
---|
| 57 | v['TTm'] = sum(v['Tm']) |
---|
| 58 | v['TTd'] = sum(v['Td']) |
---|
| 59 | v['Texpc'] = sum(v['expc']) |
---|
| 60 | v['Tbad'] = sum(v['bad']) |
---|
| 61 | v['Tmissed'] = sum(v['missed']) |
---|
| 62 | v['aTm'] = mmean(v['Tm']) |
---|
| 63 | v['aTd'] = mmean(v['Td']) |
---|
| 64 | |
---|
| 65 | v['mean'] = mmean(v['l']) |
---|
| 66 | v['smean'] = stdev(v['l']) |
---|
| 67 | |
---|
| 68 | v['amean'] = mmean(v['labs']) |
---|
| 69 | v['samean'] = stdev(v['labs']) |
---|
| 70 | |
---|
| 71 | # old type calculations |
---|
| 72 | # good detection rate |
---|
| 73 | v['GD'] = 100.*(v['Torig']-v['Tmissed']-v['TTm'])/v['Torig'] |
---|
| 74 | # false positive rate |
---|
| 75 | v['FP'] = 100.*(v['Tbad']+v['TTd'])/v['Torig'] |
---|
| 76 | # good detection counting merged detections as good |
---|
| 77 | v['GDm'] = 100.*(v['Torig']-v['Tmissed'])/v['Torig'] |
---|
| 78 | # false positives counting doubled as good |
---|
| 79 | v['FPd'] = 100.*v['Tbad']/v['Torig'] |
---|
| 80 | |
---|
| 81 | # mirex type annotations |
---|
| 82 | totaltrue = v['Texpc']-v['Tbad']-v['TTd'] |
---|
| 83 | totalfp = v['Tbad']+v['TTd'] |
---|
| 84 | totalfn = v['Tmissed']+v['TTm'] |
---|
| 85 | self.v['Ttrue'] = totaltrue |
---|
| 86 | self.v['Tfp'] = totalfp |
---|
| 87 | self.v['Tfn'] = totalfn |
---|
| 88 | # average over the number of annotation files |
---|
| 89 | N = float(len(self.reslist)) |
---|
| 90 | self.v['aTtrue'] = totaltrue/N |
---|
| 91 | self.v['aTfp'] = totalfp/N |
---|
| 92 | self.v['aTfn'] = totalfn/N |
---|
| 93 | |
---|
| 94 | # F-measure |
---|
| 95 | self.P = 100.*float(totaltrue)/max(totaltrue + totalfp,1) |
---|
| 96 | self.R = 100.*float(totaltrue)/max(totaltrue + totalfn,1) |
---|
| 97 | #if self.R < 0: self.R = 0 |
---|
| 98 | self.F = 2.* self.P*self.R / max(float(self.P+self.R),1) |
---|
| 99 | self.v['dist'] = self.F |
---|
| 100 | self.v['prec'] = self.P |
---|
| 101 | self.v['recl'] = self.R |
---|
| 102 | |
---|
[6ae3a77] | 103 | |
---|
| 104 | """ |
---|
| 105 | Plot functions |
---|
| 106 | """ |
---|
| 107 | |
---|
[83c6734] | 108 | def plotroc(self,d,plottitle=""): |
---|
| 109 | import Gnuplot, Gnuplot.funcutils |
---|
| 110 | gd = [] |
---|
| 111 | fp = [] |
---|
| 112 | for i in self.vlist: |
---|
| 113 | gd.append(i['GD']) |
---|
| 114 | fp.append(i['FP']) |
---|
| 115 | d.append(Gnuplot.Data(fp, gd, with='linespoints', |
---|
| 116 | title="%s %s" % (plottitle,i['mode']) )) |
---|
| 117 | |
---|
| 118 | def plotplotroc(self,d,outplot=0,extension='ps'): |
---|
| 119 | import Gnuplot, Gnuplot.funcutils |
---|
| 120 | from sys import exit |
---|
| 121 | g = Gnuplot.Gnuplot(debug=0, persist=1) |
---|
| 122 | if outplot: |
---|
| 123 | if extension == 'ps': ext, extension = '.ps' , 'postscript' |
---|
| 124 | elif extension == 'png': ext, extension = '.png', 'png' |
---|
| 125 | elif extension == 'svg': ext, extension = '.svg', 'svg' |
---|
| 126 | else: exit("ERR: unknown plot extension") |
---|
| 127 | g('set terminal %s' % extension) |
---|
| 128 | g('set output \'roc-%s%s\'' % (outplot,ext)) |
---|
| 129 | xmax = 30 #max(fp) |
---|
| 130 | ymin = 50 |
---|
| 131 | g('set xrange [0:%f]' % xmax) |
---|
| 132 | g('set yrange [%f:100]' % ymin) |
---|
| 133 | # grid set |
---|
| 134 | g('set grid') |
---|
| 135 | g('set xtics 0,5,%f' % xmax) |
---|
| 136 | g('set ytics %f,5,100' % ymin) |
---|
| 137 | g('set key 27,65') |
---|
| 138 | #g('set format \"%g\"') |
---|
| 139 | g.title(basename(self.datadir)) |
---|
| 140 | g.xlabel('false positives (%)') |
---|
| 141 | g.ylabel('correct detections (%)') |
---|
| 142 | g.plot(*d) |
---|
| 143 | |
---|
| 144 | def plotpr(self,d,plottitle=""): |
---|
| 145 | import Gnuplot, Gnuplot.funcutils |
---|
| 146 | x = [] |
---|
| 147 | y = [] |
---|
| 148 | for i in self.vlist: |
---|
| 149 | x.append(i['prec']) |
---|
| 150 | y.append(i['recl']) |
---|
| 151 | d.append(Gnuplot.Data(x, y, with='linespoints', |
---|
| 152 | title="%s %s" % (plottitle,i['mode']) )) |
---|
| 153 | |
---|
| 154 | def plotplotpr(self,d,outplot=0,extension='ps'): |
---|
| 155 | import Gnuplot, Gnuplot.funcutils |
---|
| 156 | from sys import exit |
---|
| 157 | g = Gnuplot.Gnuplot(debug=0, persist=1) |
---|
| 158 | if outplot: |
---|
| 159 | if extension == 'ps': ext, extension = '.ps' , 'postscript' |
---|
| 160 | elif extension == 'png': ext, extension = '.png', 'png' |
---|
| 161 | elif extension == 'svg': ext, extension = '.svg', 'svg' |
---|
| 162 | else: exit("ERR: unknown plot extension") |
---|
| 163 | g('set terminal %s' % extension) |
---|
| 164 | g('set output \'pr-%s%s\'' % (outplot,ext)) |
---|
| 165 | g.title(basename(self.datadir)) |
---|
| 166 | g.xlabel('Recall (%)') |
---|
| 167 | g.ylabel('Precision (%)') |
---|
| 168 | g.plot(*d) |
---|
| 169 | |
---|
| 170 | def plotfmeas(self,d,plottitle=""): |
---|
| 171 | import Gnuplot, Gnuplot.funcutils |
---|
| 172 | x,y = [],[] |
---|
| 173 | for i in self.vlist: |
---|
| 174 | x.append(i['thres']) |
---|
| 175 | y.append(i['dist']) |
---|
| 176 | d.append(Gnuplot.Data(x, y, with='linespoints', |
---|
| 177 | title="%s %s" % (plottitle,i['mode']) )) |
---|
| 178 | |
---|
| 179 | def plotplotfmeas(self,d,outplot="",extension='ps', title="F-measure"): |
---|
| 180 | import Gnuplot, Gnuplot.funcutils |
---|
| 181 | from sys import exit |
---|
| 182 | g = Gnuplot.Gnuplot(debug=0, persist=1) |
---|
| 183 | if outplot: |
---|
| 184 | if extension == 'ps': terminal = 'postscript' |
---|
| 185 | elif extension == 'png': terminal = 'png' |
---|
| 186 | elif extension == 'svg': terminal = 'svg' |
---|
| 187 | else: exit("ERR: unknown plot extension") |
---|
| 188 | g('set terminal %s' % terminal) |
---|
| 189 | g('set output \'fmeas-%s.%s\'' % (outplot,extension)) |
---|
| 190 | g.xlabel('threshold \\delta') |
---|
| 191 | g.ylabel('F-measure (%)') |
---|
| 192 | g('set xrange [0:1.2]') |
---|
| 193 | g('set yrange [0:100]') |
---|
| 194 | g.title(basename(self.datadir)) |
---|
| 195 | # grid set |
---|
| 196 | #g('set grid') |
---|
| 197 | #g('set xtics 0,5,%f' % xmax) |
---|
| 198 | #g('set ytics %f,5,100' % ymin) |
---|
| 199 | #g('set key 27,65') |
---|
| 200 | #g('set format \"%g\"') |
---|
| 201 | g.plot(*d) |
---|
| 202 | |
---|
[6ae3a77] | 203 | def plotfmeasvar(self,d,var,plottitle=""): |
---|
| 204 | import Gnuplot, Gnuplot.funcutils |
---|
| 205 | x,y = [],[] |
---|
| 206 | for i in self.vlist: |
---|
| 207 | x.append(i[var]) |
---|
| 208 | y.append(i['dist']) |
---|
| 209 | d.append(Gnuplot.Data(x, y, with='linespoints', |
---|
| 210 | title="%s %s" % (plottitle,i['mode']) )) |
---|
| 211 | |
---|
| 212 | def plotplotfmeasvar(self,d,var,outplot="",extension='ps', title="F-measure"): |
---|
| 213 | import Gnuplot, Gnuplot.funcutils |
---|
| 214 | from sys import exit |
---|
| 215 | g = Gnuplot.Gnuplot(debug=0, persist=1) |
---|
| 216 | if outplot: |
---|
| 217 | if extension == 'ps': terminal = 'postscript' |
---|
| 218 | elif extension == 'png': terminal = 'png' |
---|
| 219 | elif extension == 'svg': terminal = 'svg' |
---|
| 220 | else: exit("ERR: unknown plot extension") |
---|
| 221 | g('set terminal %s' % terminal) |
---|
| 222 | g('set output \'fmeas-%s.%s\'' % (outplot,extension)) |
---|
| 223 | g.xlabel(var) |
---|
| 224 | g.ylabel('F-measure (%)') |
---|
| 225 | #g('set xrange [0:1.2]') |
---|
| 226 | g('set yrange [0:100]') |
---|
| 227 | g.title(basename(self.datadir)) |
---|
| 228 | g.plot(*d) |
---|
| 229 | |
---|
[83c6734] | 230 | def plotdiffs(self,d,plottitle=""): |
---|
| 231 | import Gnuplot, Gnuplot.funcutils |
---|
| 232 | v = self.v |
---|
| 233 | l = v['l'] |
---|
| 234 | mean = v['mean'] |
---|
| 235 | smean = v['smean'] |
---|
| 236 | amean = v['amean'] |
---|
| 237 | samean = v['samean'] |
---|
| 238 | val = [] |
---|
| 239 | per = [0] * 100 |
---|
| 240 | for i in range(0,100): |
---|
| 241 | val.append(i*.001-.05) |
---|
| 242 | for j in l: |
---|
| 243 | if abs(j-val[i]) <= 0.001: |
---|
| 244 | per[i] += 1 |
---|
| 245 | total = v['Torig'] |
---|
| 246 | for i in range(len(per)): per[i] /= total/100. |
---|
| 247 | |
---|
| 248 | d.append(Gnuplot.Data(val, per, with='fsteps', |
---|
| 249 | title="%s %s" % (plottitle,v['mode']) )) |
---|
| 250 | #d.append('mean=%f,sigma=%f,eps(x) title \"\"'% (mean,smean)) |
---|
| 251 | #d.append('mean=%f,sigma=%f,eps(x) title \"\"'% (amean,samean)) |
---|
| 252 | |
---|
| 253 | |
---|
| 254 | def plotplotdiffs(self,d,outplot=0,extension='ps'): |
---|
| 255 | import Gnuplot, Gnuplot.funcutils |
---|
| 256 | from sys import exit |
---|
| 257 | g = Gnuplot.Gnuplot(debug=0, persist=1) |
---|
| 258 | if outplot: |
---|
| 259 | if extension == 'ps': ext, extension = '.ps' , 'postscript' |
---|
| 260 | elif extension == 'png': ext, extension = '.png', 'png' |
---|
| 261 | elif extension == 'svg': ext, extension = '.svg', 'svg' |
---|
| 262 | else: exit("ERR: unknown plot extension") |
---|
| 263 | g('set terminal %s' % extension) |
---|
| 264 | g('set output \'diffhist-%s%s\'' % (outplot,ext)) |
---|
| 265 | g('eps(x) = 1./(sigma*(2.*3.14159)**.5) * exp ( - ( x - mean ) ** 2. / ( 2. * sigma ** 2. ))') |
---|
| 266 | g.title(basename(self.datadir)) |
---|
| 267 | g.xlabel('delay to hand-labelled onset (s)') |
---|
| 268 | g.ylabel('% number of correct detections / ms ') |
---|
| 269 | g('set xrange [-0.05:0.05]') |
---|
[6ae3a77] | 270 | g('set yrange [0:20]') |
---|
| 271 | g.plot(*d) |
---|
| 272 | |
---|
| 273 | |
---|
| 274 | def plothistcat(self,d,plottitle=""): |
---|
| 275 | import Gnuplot, Gnuplot.funcutils |
---|
| 276 | total = v['Torig'] |
---|
| 277 | for i in range(len(per)): per[i] /= total/100. |
---|
| 278 | |
---|
| 279 | d.append(Gnuplot.Data(val, per, with='fsteps', |
---|
| 280 | title="%s %s" % (plottitle,v['mode']) )) |
---|
| 281 | #d.append('mean=%f,sigma=%f,eps(x) title \"\"'% (mean,smean)) |
---|
| 282 | #d.append('mean=%f,sigma=%f,eps(x) title \"\"'% (amean,samean)) |
---|
| 283 | |
---|
| 284 | |
---|
| 285 | def plotplothistcat(self,d,outplot=0,extension='ps'): |
---|
| 286 | import Gnuplot, Gnuplot.funcutils |
---|
| 287 | from sys import exit |
---|
| 288 | g = Gnuplot.Gnuplot(debug=0, persist=1) |
---|
| 289 | if outplot: |
---|
| 290 | if extension == 'ps': ext, extension = '.ps' , 'postscript' |
---|
| 291 | elif extension == 'png': ext, extension = '.png', 'png' |
---|
| 292 | elif extension == 'svg': ext, extension = '.svg', 'svg' |
---|
| 293 | else: exit("ERR: unknown plot extension") |
---|
| 294 | g('set terminal %s' % extension) |
---|
| 295 | g('set output \'diffhist-%s%s\'' % (outplot,ext)) |
---|
| 296 | g('eps(x) = 1./(sigma*(2.*3.14159)**.5) * exp ( - ( x - mean ) ** 2. / ( 2. * sigma ** 2. ))') |
---|
| 297 | g.title(basename(self.datadir)) |
---|
| 298 | g.xlabel('delay to hand-labelled onset (s)') |
---|
| 299 | g.ylabel('% number of correct detections / ms ') |
---|
| 300 | g('set xrange [-0.05:0.05]') |
---|
| 301 | g('set yrange [0:20]') |
---|
[83c6734] | 302 | g.plot(*d) |
---|
| 303 | |
---|
| 304 | |
---|