[4cc9fe5] | 1 | #! /usr/bin/python |
---|
| 2 | |
---|
[4f4a8a4] | 3 | from aubio.tasks import * |
---|
[75139a9] | 4 | |
---|
[83c6734] | 5 | from benchonset import mmean, stdev, benchonset |
---|
[e968939] | 6 | |
---|
[83c6734] | 7 | class mybenchonset(benchonset): |
---|
[75139a9] | 8 | |
---|
| 9 | def run_bench(self,modes=['dual'],thresholds=[0.5]): |
---|
[83c6734] | 10 | from os.path import dirname,basename |
---|
[75139a9] | 11 | self.thresholds = thresholds |
---|
[e968939] | 12 | self.pretty_titles() |
---|
[83c6734] | 13 | d,e,f = [],[],[] |
---|
| 14 | for mode in modes: |
---|
| 15 | self.vlist = [] |
---|
[4f4a8a4] | 16 | self.params.onsetmode = mode |
---|
[75139a9] | 17 | for threshold in self.thresholds: |
---|
| 18 | self.params.threshold = threshold |
---|
[4f4a8a4] | 19 | self.dir_exec() |
---|
| 20 | self.dir_eval() |
---|
[e968939] | 21 | self.pretty_print() |
---|
| 22 | #print self.v |
---|
[83c6734] | 23 | self.vlist.append(self.v) |
---|
| 24 | self.plotroc(d) |
---|
| 25 | self.plotfmeas(e) |
---|
| 26 | self.plotpr(f) |
---|
| 27 | #print vlist |
---|
| 28 | #self.plotplotroc(d) |
---|
| 29 | #self.plotplotfmeas(e) |
---|
| 30 | #self.plotplotpr(f) |
---|
| 31 | outplot = basename(self.datadir) |
---|
| 32 | for ext in ("png","svg","ps"): |
---|
| 33 | self.plotplotroc(d,outplot=outplot,extension=ext) |
---|
| 34 | self.plotplotfmeas(e,outplot=outplot,extension=ext) |
---|
| 35 | self.plotplotpr(f,outplot=outplot,extension=ext) |
---|
| 36 | |
---|
[e968939] | 37 | |
---|
[75139a9] | 38 | def auto_learn(self,modes=['dual'],thresholds=[0.1,1.5]): |
---|
| 39 | """ simple dichotomia like algorithm to optimise threshold """ |
---|
| 40 | self.modes = modes |
---|
[e968939] | 41 | self.pretty_titles() |
---|
[75139a9] | 42 | for mode in self.modes: |
---|
[c912c67] | 43 | steps = 11 |
---|
[75139a9] | 44 | lesst = thresholds[0] |
---|
| 45 | topt = thresholds[1] |
---|
[4f4a8a4] | 46 | self.params.onsetmode = mode |
---|
[75139a9] | 47 | |
---|
| 48 | self.params.threshold = topt |
---|
[4f4a8a4] | 49 | self.dir_exec() |
---|
| 50 | self.dir_eval() |
---|
[e968939] | 51 | self.pretty_print() |
---|
[75139a9] | 52 | topF = self.F |
---|
| 53 | |
---|
| 54 | self.params.threshold = lesst |
---|
[4f4a8a4] | 55 | self.dir_exec() |
---|
| 56 | self.dir_eval() |
---|
[e968939] | 57 | self.pretty_print() |
---|
[75139a9] | 58 | lessF = self.F |
---|
| 59 | |
---|
| 60 | for i in range(steps): |
---|
[83c6734] | 61 | self.params.localmin = True |
---|
| 62 | self.params.delay = 1. |
---|
| 63 | self.dir_exec() |
---|
| 64 | self.dir_eval() |
---|
[75139a9] | 65 | self.params.threshold = ( lesst + topt ) * .5 |
---|
[4f4a8a4] | 66 | self.dir_exec() |
---|
| 67 | self.dir_eval() |
---|
[e968939] | 68 | self.pretty_print() |
---|
[75139a9] | 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 | |
---|
[e968939] | 86 | def auto_learn2(self,modes=['dual'],thresholds=[0.00001,1.0]): |
---|
[af445db] | 87 | """ simple dichotomia like algorithm to optimise threshold """ |
---|
| 88 | self.modes = modes |
---|
[e968939] | 89 | self.pretty_titles([]) |
---|
[af445db] | 90 | for mode in self.modes: |
---|
| 91 | steps = 10 |
---|
[e968939] | 92 | step = 0.4 |
---|
[af445db] | 93 | self.params.onsetmode = mode |
---|
[e968939] | 94 | self.params.threshold = thresholds[0] |
---|
| 95 | cur = 0 |
---|
[af445db] | 96 | |
---|
| 97 | for i in range(steps): |
---|
| 98 | self.dir_exec() |
---|
| 99 | self.dir_eval() |
---|
[e968939] | 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" |
---|
[af445db] | 120 | break |
---|
[e968939] | 121 | cur = new |
---|
| 122 | |
---|
[75139a9] | 123 | |
---|
[4f4a8a4] | 124 | if __name__ == "__main__": |
---|
| 125 | import sys |
---|
| 126 | if len(sys.argv) > 1: datapath = sys.argv[1] |
---|
| 127 | else: print "ERR: a path is required"; sys.exit(1) |
---|
[83c6734] | 128 | modes = ['complex', 'energy', 'phase', 'hfc', 'specdiff', 'kl', 'mkl', 'dual'] |
---|
[e968939] | 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] |
---|
[83c6734] | 130 | #modes = [ 'hfc' ] |
---|
| 131 | #thresholds = [0.1, 1.5] |
---|
[4f4a8a4] | 132 | |
---|
| 133 | #datapath = "%s%s" % (DATADIR,'/onset/DB/*/') |
---|
| 134 | respath = '/var/tmp/DB-testings' |
---|
| 135 | |
---|
[83c6734] | 136 | benchonset = mybenchonset(datapath,respath,checkres=True,checkanno=True) |
---|
[4f4a8a4] | 137 | benchonset.params = taskparams() |
---|
| 138 | benchonset.task = taskonset |
---|
[e968939] | 139 | benchonset.valuesdict = {} |
---|
[4f4a8a4] | 140 | |
---|
| 141 | try: |
---|
[e968939] | 142 | #benchonset.auto_learn2(modes=modes) |
---|
| 143 | benchonset.run_bench(modes=modes,thresholds=thresholds) |
---|
[4f4a8a4] | 144 | except KeyboardInterrupt: |
---|
| 145 | sys.exit(1) |
---|