Showing the changes to python (4 of 4 files). Show all
Unified · Split
| @@ -1,6 +1,5 @@ | |||
| 1 | #! /usr/bin/python | 1 | #! /usr/bin/python |
| 2 | 2 | ||
| 3 | from aubio.bench.node import * | ||
| 4 | from aubio.tasks import * | 3 | from aubio.tasks import * |
| 5 | 4 | ||
| 6 | from benchonset import mmean, stdev, benchonset | 5 | from benchonset import mmean, stdev, benchonset |
| @@ -9,53 +8,44 @@ class mybenchonset(benchonset): · 2 unmodified lines | |||
| 9 | 8 | ||
| 10 | def run_bench(self,modes=['dual'],thresholds=[0.5]): | 9 | def run_bench(self,modes=['dual'],thresholds=[0.5]): |
| 11 | from os.path import dirname,basename | 10 | from os.path import dirname,basename |
| 12 | self.modes = modes | ||
| 13 | self.thresholds = thresholds | 11 | self.thresholds = thresholds |
| 14 | self.pretty_titles() | 12 | self.pretty_titles() |
| 15 | for mode in self.modes: | ||
| 16 | d = [] | ||
| 17 | outplot = "_-_".join(("delay",mode, | ||
| 18 | basename(self.datadir) )) | ||
| 19 | 13 | ||
| 14 | for mode in modes: | ||
| 15 | d = [] | ||
| 20 | self.params.onsetmode = mode | 16 | self.params.onsetmode = mode |
| 17 | self.params.localmin = True | ||
| 18 | self.params.delay = 1. | ||
| 21 | self.params.threshold = thresholds[0] | 19 | self.params.threshold = thresholds[0] |
| 22 | 20 | # | |
| 23 | self.params.localmin = False | 21 | self.params.localmin = False |
| 24 | self.params.delay = 0. | 22 | self.params.delay = 0. |
| 25 | 23 | self.dir_eval_print() | |
| 26 | self.dir_exec() | ||
| 27 | self.dir_eval() | ||
| 28 | self.pretty_print() | ||
| 29 | self.plotdiffs(d,plottitle="Causal") | 24 | self.plotdiffs(d,plottitle="Causal") |
| 30 | 25 | # | |
| 31 | self.params.localmin = True | 26 | self.params.localmin = True |
| 32 | self.params.delay = 0. | 27 | self.params.delay = 0. |
| 33 | self.dir_exec() | 28 | self.dir_eval_print() |
| 34 | self.dir_eval() | ||
| 35 | self.pretty_print() | ||
| 36 | self.plotdiffs(d,plottitle="Local min") | 29 | self.plotdiffs(d,plottitle="Local min") |
| 37 | 30 | ||
| 38 | self.params.localmin = False | 31 | self.params.localmin = False |
| 39 | self.params.delay = 6. | 32 | self.params.delay = 6. |
| 40 | self.dir_exec() | 33 | self.dir_eval_print() |
| 41 | self.dir_eval() | ||
| 42 | self.pretty_print() | ||
| 43 | self.plotdiffs(d,plottitle="Fixed delay") | 34 | self.plotdiffs(d,plottitle="Fixed delay") |
| 44 | 35 | ||
| 45 | self.plotplotdiffs(d) | 36 | #self.plotplotdiffs(d) |
| 46 | self.plotplotdiffs(d,outplot=outplot,extension="png") | 37 | outplot = "_-_".join(("delay",mode,basename(self.datadir) )) |
| 47 | self.plotplotdiffs(d,outplot=outplot,extension="ps") | 38 | for ext in ("png","svg","ps"): |
| 48 | self.plotplotdiffs(d,outplot=outplot,extension="svg") | 39 | self.plotplotdiffs(d,outplot=outplot,extension=ext) |
| 49 | |||
| 50 | 40 | ||
| 51 | if __name__ == "__main__": | 41 | if __name__ == "__main__": |
| 52 | import sys | 42 | import sys |
| 53 | if len(sys.argv) > 1: datapath = sys.argv[1] | 43 | if len(sys.argv) > 1: datapath = sys.argv[1] |
| 54 | else: print "ERR: a path is required"; sys.exit(1) | 44 | else: print "ERR: a path is required"; sys.exit(1) |
| 55 | modes = ['complex', 'energy', 'phase', 'specdiff', 'kl', 'mkl', 'dual'] | 45 | modes = ['complex', 'energy', 'phase', 'hfc', 'specdiff', 'kl', 'mkl', 'dual'] |
| 56 | #modes = [ 'complex' ] | 46 | #thresholds = [ 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2] |
| 57 | thresholds = [ 0.5] | 47 | #modes = [ 'hfc' ] |
| 58 | #thresholds = [1.5] | 48 | thresholds = [0.5] |
| 59 | 49 | ||
| 60 | #datapath = "%s%s" % (DATADIR,'/onset/DB/*/') | 50 | #datapath = "%s%s" % (DATADIR,'/onset/DB/*/') |
| 61 | respath = '/var/tmp/DB-testings' | 51 | respath = '/var/tmp/DB-testings' |
| @@ -10,21 +10,28 @@ class mybenchonset(benchonset): · 9 unmodified lines | |||
| 10 | from os.path import dirname,basename | 10 | from os.path import dirname,basename |
| 11 | self.thresholds = thresholds | 11 | self.thresholds = thresholds |
| 12 | self.pretty_titles() | 12 | self.pretty_titles() |
| 13 | d,e,f = [],[],[] | 13 | d,e,f,g = [],[],[],[] |
| 14 | for mode in modes: | 14 | for mode in modes: |
| 15 | self.vlist = [] | 15 | self.vlist = [] |
| 16 | self.params.onsetmode = mode | 16 | self.params.onsetmode = mode |
| 17 | #self.params.localmin = True | ||
| 18 | self.params.delay = 2. | ||
| 19 | self.params.bufsize = 1024 | ||
| 20 | self.params.hopsize = 256 | ||
| 21 | self.params.step = float(self.params.hopsize)/float(self.params.samplerate) | ||
| 22 | #self.params.mintol = 8. | ||
| 23 | |||
| 17 | for threshold in self.thresholds: | 24 | for threshold in self.thresholds: |
| 18 | self.params.threshold = threshold | 25 | self.params.threshold = threshold |
| 19 | self.dir_exec() | 26 | self.dir_eval_print() |
| 20 | self.dir_eval() | ||
| 21 | self.pretty_print() | ||
| 22 | #print self.v | ||
| 23 | self.vlist.append(self.v) | 27 | self.vlist.append(self.v) |
| 24 | self.plotroc(d) | 28 | self.plotroc(d) |
| 25 | self.plotfmeas(e) | 29 | self.plotfmeas(e) |
| 26 | self.plotpr(f) | 30 | self.plotpr(f) |
| 27 | #print vlist | 31 | #self.plothistcat(g) |
| 32 | |||
| 33 | |||
| 34 | |||
| 28 | #self.plotplotroc(d) | 35 | #self.plotplotroc(d) |
| 29 | #self.plotplotfmeas(e) | 36 | #self.plotplotfmeas(e) |
| 30 | #self.plotplotpr(f) | 37 | #self.plotplotpr(f) |
| @@ -33,93 +40,7 @@ class mybenchonset(benchonset): · 2 unmodified lines | |||
| 33 | self.plotplotroc(d,outplot=outplot,extension=ext) | 40 | self.plotplotroc(d,outplot=outplot,extension=ext) |
| 34 | self.plotplotfmeas(e,outplot=outplot,extension=ext) | 41 | self.plotplotfmeas(e,outplot=outplot,extension=ext) |
| 35 | self.plotplotpr(f,outplot=outplot,extension=ext) | 42 | self.plotplotpr(f,outplot=outplot,extension=ext) |
| 36 | 43 | #self.plotplothistcat(g,outplot=outplot,extension=ext) | |
| 37 | |||
| 38 | def auto_learn(self,modes=['dual'],thresholds=[0.1,1.5]): | ||
| 39 | """ simple dichotomia like algorithm to optimise threshold """ | ||
| 40 | self.modes = modes | ||
| 41 | self.pretty_titles() | ||
| 42 | for mode in self.modes: | ||
| 43 | steps = 11 | ||
| 44 | lesst = thresholds[0] | ||
| 45 | topt = thresholds[1] | ||
| 46 | self.params.onsetmode = mode | ||
| 47 | |||
| 48 | self.params.threshold = topt | ||
| 49 | self.dir_exec() | ||
| 50 | self.dir_eval() | ||
| 51 | self.pretty_print() | ||
| 52 | topF = self.F | ||
| 53 | |||
| 54 | self.params.threshold = lesst | ||
| 55 | self.dir_exec() | ||
| 56 | self.dir_eval() | ||
| 57 | self.pretty_print() | ||
| 58 | lessF = self.F | ||
| 59 | |||
| 60 | for i in range(steps): | ||
| 61 | self.params.localmin = True | ||
| 62 | self.params.delay = 1. | ||
| 63 | self.dir_exec() | ||
| 64 | self.dir_eval() | ||
| 65 | self.params.threshold = ( lesst + topt ) * .5 | ||
| 66 | self.dir_exec() | ||
| 67 | self.dir_eval() | ||
| 68 | self.pretty_print() | ||
| 69 | if self.F == 100.0 or self.F == topF: | ||
| 70 | print "assuming we converged, stopping" | ||
| 71 | break | ||
| 72 | #elif abs(self.F - topF) < 0.01 : | ||
| 73 | # print "done converging" | ||
| 74 | # break | ||
| 75 | if topF < self.F: | ||
| 76 | #lessF = topF | ||
| 77 | #lesst = topt | ||
| 78 | topF = self.F | ||
| 79 | topt = self.params.threshold | ||
| 80 | elif lessF < self.F: | ||
| 81 | lessF = self.F | ||
| 82 | lesst = self.params.threshold | ||
| 83 | if topt == lesst: | ||
| 84 | lesst /= 2. | ||
| 85 | |||
| 86 | def auto_learn2(self,modes=['dual'],thresholds=[0.00001,1.0]): | ||
| 87 | """ simple dichotomia like algorithm to optimise threshold """ | ||
| 88 | self.modes = modes | ||
| 89 | self.pretty_titles([]) | ||
| 90 | for mode in self.modes: | ||
| 91 | steps = 10 | ||
| 92 | step = 0.4 | ||
| 93 | self.params.onsetmode = mode | ||
| 94 | self.params.threshold = thresholds[0] | ||
| 95 | cur = 0 | ||
| 96 | |||
| 97 | for i in range(steps): | ||
| 98 | self.dir_exec() | ||
| 99 | self.dir_eval() | ||
| 100 | self.pretty_print() | ||
| 101 | new = self.P | ||
| 102 | if self.R == 0.0: | ||
| 103 | #print "Found maximum, highering" | ||
| 104 | step /= 2. | ||
| 105 | self.params.threshold -= step | ||
| 106 | elif new == 100.0: | ||
| 107 | #print "Found maximum, highering" | ||
| 108 | step *= .99 | ||
| 109 | self.params.threshold += step | ||
| 110 | elif cur > new: | ||
| 111 | #print "lower" | ||
| 112 | step /= 2. | ||
| 113 | self.params.threshold -= step | ||
| 114 | elif cur < new: | ||
| 115 | #print "higher" | ||
| 116 | step *= .99 | ||
| 117 | self.params.threshold += step | ||
| 118 | else: | ||
| 119 | print "Assuming we converged" | ||
| 120 | break | ||
| 121 | cur = new | ||
| 122 | |||
| 123 | 44 | ||
| 124 | if __name__ == "__main__": | 45 | if __name__ == "__main__": |
| 125 | import sys | 46 | import sys |
| @@ -128,7 +49,7 @@ if __name__ == "__main__": · 2 unmodified lines | |||
| 128 | modes = ['complex', 'energy', 'phase', 'hfc', 'specdiff', 'kl', 'mkl', 'dual'] | 49 | modes = ['complex', 'energy', 'phase', 'hfc', 'specdiff', 'kl', 'mkl', 'dual'] |
| 129 | thresholds = [ 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2] | 50 | thresholds = [ 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2] |
| 130 | #modes = [ 'hfc' ] | 51 | #modes = [ 'hfc' ] |
| 131 | #thresholds = [0.1, 1.5] | 52 | #thresholds = [0.5] |
| 132 | 53 | ||
| 133 | #datapath = "%s%s" % (DATADIR,'/onset/DB/*/') | 54 | #datapath = "%s%s" % (DATADIR,'/onset/DB/*/') |
| 134 | respath = '/var/tmp/DB-testings' | 55 | respath = '/var/tmp/DB-testings' |
| @@ -2,7 +2,7 @@ · 1 unmodified line | |||
| 2 | 2 | ||
| 3 | from aubio.tasks import * | 3 | from aubio.tasks import * |
| 4 | 4 | ||
| 5 | from benchonset import mmean, stdev, plotdiffs, plotplotdiffs, benchonset | 5 | from benchonset import mmean, stdev, benchonset |
| 6 | 6 | ||
| 7 | class mybenchonset(benchonset): | 7 | class mybenchonset(benchonset): |
| 8 | 8 | ||
| @@ -10,43 +10,39 @@ class mybenchonset(benchonset): · 1 unmodified line | |||
| 10 | from os.path import dirname,basename | 10 | from os.path import dirname,basename |
| 11 | self.thresholds = thresholds | 11 | self.thresholds = thresholds |
| 12 | self.pretty_titles() | 12 | self.pretty_titles() |
| 13 | |||
| 13 | for mode in modes: | 14 | for mode in modes: |
| 14 | 15 | ||
| 15 | self.params.onsetmode = mode | 16 | self.params.onsetmode = mode |
| 17 | self.params.localmin = True | ||
| 18 | self.params.delay = 1. | ||
| 16 | self.params.threshold = thresholds[0] | 19 | self.params.threshold = thresholds[0] |
| 17 | self.params.localmin = False | 20 | self.params.localmin = False |
| 18 | 21 | # | |
| 19 | for delay in (0., 4.): | 22 | for delay in (0., 4.): |
| 20 | d = [] | 23 | d = [] |
| 21 | outplot = "_-_".join(("window",mode,"delay-%s" % delay, | ||
| 22 | basename(self.datadir) )) | ||
| 23 | self.params.delay = delay | 24 | self.params.delay = delay |
| 24 | |||
| 25 | for buf in (2048, 1024, 512): | 25 | for buf in (2048, 1024, 512): |
| 26 | for hop in (buf/2, buf/4): | 26 | for hop in (buf/2, buf/4): |
| 27 | self.params.bufsize = buf | 27 | self.params.bufsize = buf |
| 28 | self.params.hopsize = hop | 28 | self.params.hopsize = hop |
| 29 | self.params.step = float(self.params.hopsize)/float(self.params.samplerate) | 29 | self.params.step = float(self.params.hopsize)/float(self.params.samplerate) |
| 30 | 30 | self.dir_eval_print() | |
| 31 | self.dir_exec() | 31 | self.plotdiffs(d,plottitle="%s %s" % (buf,hop)) |
| 32 | self.dir_eval() | 32 | #plotplotdiffs(d) |
| 33 | self.pretty_print() | 33 | outplot = "_-_".join(("window",mode,"delay-%s" % int(delay), |
| 34 | plotdiffs(self.v,d,plottitle="%s %s" % (buf,hop)) | 34 | basename(self.datadir) )) |
| 35 | 35 | for ext in ("png","svg","ps"): | |
| 36 | plotplotdiffs(d) | 36 | self.plotplotdiffs(d,outplot=outplot,extension=ext) |
| 37 | plotplotdiffs(d,outplot=outplot,extension="png") | ||
| 38 | plotplotdiffs(d,outplot=outplot,extension="ps") | ||
| 39 | plotplotdiffs(d,outplot=outplot,extension="svg") | ||
| 40 | |||
| 41 | 37 | ||
| 42 | if __name__ == "__main__": | 38 | if __name__ == "__main__": |
| 43 | import sys | 39 | import sys |
| 44 | if len(sys.argv) > 1: datapath = sys.argv[1] | 40 | if len(sys.argv) > 1: datapath = sys.argv[1] |
| 45 | else: print "ERR: a path is required"; sys.exit(1) | 41 | else: print "ERR: a path is required"; sys.exit(1) |
| 46 | modes = ['complex', 'energy', 'phase', 'specdiff', 'kl', 'mkl', 'dual'] | 42 | modes = ['complex', 'energy', 'phase', 'hfc', 'specdiff', 'kl', 'mkl', 'dual'] |
| 47 | #modes = [ 'complex' ] | 43 | #thresholds = [ 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2] |
| 48 | thresholds = [ 0.5] | 44 | #modes = [ 'hfc' ] |
| 49 | #thresholds = [1.5] | 45 | thresholds = [0.5] |
| 50 | 46 | ||
| 51 | #datapath = "%s%s" % (DATADIR,'/onset/DB/*/') | 47 | #datapath = "%s%s" % (DATADIR,'/onset/DB/*/') |
| 52 | respath = '/var/tmp/DB-testings' | 48 | respath = '/var/tmp/DB-testings' |
| @@ -46,6 +46,10 @@ class benchonset(bench): · 45 unmodified lines | |||
| 46 | 46 | ||
| 47 | v['mode'] = self.params.onsetmode | 47 | v['mode'] = self.params.onsetmode |
| 48 | v['thres'] = self.params.threshold | 48 | v['thres'] = self.params.threshold |
| 49 | v['bufsize'] = self.params.bufsize | ||
| 50 | v['hopsize'] = self.params.hopsize | ||
| 51 | v['silence'] = self.params.silence | ||
| 52 | v['mintol'] = self.params.mintol | ||
| 49 | 53 | ||
| 50 | v['Torig'] = sum(v['orig']) | 54 | v['Torig'] = sum(v['orig']) |
| 51 | v['TTm'] = sum(v['Tm']) | 55 | v['TTm'] = sum(v['Tm']) |
| @@ -94,6 +98,11 @@ class benchonset(bench): · 42 unmodified lines | |||
| 94 | self.v['prec'] = self.P | 98 | self.v['prec'] = self.P |
| 95 | self.v['recl'] = self.R | 99 | self.v['recl'] = self.R |
| 96 | 100 | ||
| 101 | |||
| 102 | """ | ||
| 103 | Plot functions | ||
| 104 | """ | ||
| 105 | |||
| 97 | def plotroc(self,d,plottitle=""): | 106 | def plotroc(self,d,plottitle=""): |
| 98 | import Gnuplot, Gnuplot.funcutils | 107 | import Gnuplot, Gnuplot.funcutils |
| 99 | gd = [] | 108 | gd = [] |
| @@ -189,6 +198,33 @@ class benchonset(bench): · 89 unmodified lines | |||
| 189 | #g('set format \"%g\"') | 198 | #g('set format \"%g\"') |
| 190 | g.plot(*d) | 199 | g.plot(*d) |
| 191 | 200 | ||
| 201 | def plotfmeasvar(self,d,var,plottitle=""): | ||
| 202 | import Gnuplot, Gnuplot.funcutils | ||
| 203 | x,y = [],[] | ||
| 204 | for i in self.vlist: | ||
| 205 | x.append(i[var]) | ||
| 206 | y.append(i['dist']) | ||
| 207 | d.append(Gnuplot.Data(x, y, with='linespoints', | ||
| 208 | title="%s %s" % (plottitle,i['mode']) )) | ||
| 209 | |||
| 210 | def plotplotfmeasvar(self,d,var,outplot="",extension='ps', title="F-measure"): | ||
| 211 | import Gnuplot, Gnuplot.funcutils | ||
| 212 | from sys import exit | ||
| 213 | g = Gnuplot.Gnuplot(debug=0, persist=1) | ||
| 214 | if outplot: | ||
| 215 | if extension == 'ps': terminal = 'postscript' | ||
| 216 | elif extension == 'png': terminal = 'png' | ||
| 217 | elif extension == 'svg': terminal = 'svg' | ||
| 218 | else: exit("ERR: unknown plot extension") | ||
| 219 | g('set terminal %s' % terminal) | ||
| 220 | g('set output \'fmeas-%s.%s\'' % (outplot,extension)) | ||
| 221 | g.xlabel(var) | ||
| 222 | g.ylabel('F-measure (%)') | ||
| 223 | #g('set xrange [0:1.2]') | ||
| 224 | g('set yrange [0:100]') | ||
| 225 | g.title(basename(self.datadir)) | ||
| 226 | g.plot(*d) | ||
| 227 | |||
| 192 | def plotdiffs(self,d,plottitle=""): | 228 | def plotdiffs(self,d,plottitle=""): |
| 193 | import Gnuplot, Gnuplot.funcutils | 229 | import Gnuplot, Gnuplot.funcutils |
| 194 | v = self.v | 230 | v = self.v |
| @@ -229,7 +265,38 @@ class benchonset(bench): · 34 unmodified lines | |||
| 229 | g.xlabel('delay to hand-labelled onset (s)') | 265 | g.xlabel('delay to hand-labelled onset (s)') |
| 230 | g.ylabel('% number of correct detections / ms ') | 266 | g.ylabel('% number of correct detections / ms ') |
| 231 | g('set xrange [-0.05:0.05]') | 267 | g('set xrange [-0.05:0.05]') |
| 232 | g('set yrange [0:50]') | 268 | g('set yrange [0:20]') |
| 269 | g.plot(*d) | ||
| 270 | |||
| 271 | |||
| 272 | def plothistcat(self,d,plottitle=""): | ||
| 273 | import Gnuplot, Gnuplot.funcutils | ||
| 274 | total = v['Torig'] | ||
| 275 | for i in range(len(per)): per[i] /= total/100. | ||
| 276 | |||
| 277 | d.append(Gnuplot.Data(val, per, with='fsteps', | ||
| 278 | title="%s %s" % (plottitle,v['mode']) )) | ||
| 279 | #d.append('mean=%f,sigma=%f,eps(x) title \"\"'% (mean,smean)) | ||
| 280 | #d.append('mean=%f,sigma=%f,eps(x) title \"\"'% (amean,samean)) | ||
| 281 | |||
| 282 | |||
| 283 | def plotplothistcat(self,d,outplot=0,extension='ps'): | ||
| 284 | import Gnuplot, Gnuplot.funcutils | ||
| 285 | from sys import exit | ||
| 286 | g = Gnuplot.Gnuplot(debug=0, persist=1) | ||
| 287 | if outplot: | ||
| 288 | if extension == 'ps': ext, extension = '.ps' , 'postscript' | ||
| 289 | elif extension == 'png': ext, extension = '.png', 'png' | ||
| 290 | elif extension == 'svg': ext, extension = '.svg', 'svg' | ||
| 291 | else: exit("ERR: unknown plot extension") | ||
| 292 | g('set terminal %s' % extension) | ||
| 293 | g('set output \'diffhist-%s%s\'' % (outplot,ext)) | ||
| 294 | g('eps(x) = 1./(sigma*(2.*3.14159)**.5) * exp ( - ( x - mean ) ** 2. / ( 2. * sigma ** 2. ))') | ||
| 295 | g.title(basename(self.datadir)) | ||
| 296 | g.xlabel('delay to hand-labelled onset (s)') | ||
| 297 | g.ylabel('% number of correct detections / ms ') | ||
| 298 | g('set xrange [-0.05:0.05]') | ||
| 299 | g('set yrange [0:20]') | ||
| 233 | g.plot(*d) | 300 | g.plot(*d) |
| 234 | 301 | ||
| 235 | 302 | ||