[4cc9fe5] | 1 | #! /usr/bin/python |
---|
| 2 | |
---|
| 3 | from aubio.bench.node import * |
---|
[43938de] | 4 | from aubio.task import * |
---|
[0029638] | 5 | |
---|
| 6 | class benchpitch(bench): |
---|
| 7 | |
---|
[ee7b9da] | 8 | """ list of values to store per file """ |
---|
| 9 | valuenames = ['mode'] |
---|
| 10 | """ list of lists to store per file """ |
---|
[5d1c070] | 11 | valuelists = ['truth', 'osil', 'esil', 'opit', 'epit', 'echr', |
---|
[807f029] | 12 | 'Msil', 'Mpit', 'Mchr', |
---|
| 13 | 'TotalPit', 'TotalPit', 'TotalChr' ] |
---|
[ee7b9da] | 14 | """ list of values to print per dir """ |
---|
[807f029] | 15 | printnames = [ 'mode', 'MinPit', 'MaxPit', 'TotalSil', 'TotalPit', 'TotalChr'] |
---|
[ee7b9da] | 16 | |
---|
| 17 | """ per dir """ |
---|
[5d1c070] | 18 | formats = {'mode': "%12s" , |
---|
| 19 | 'truth': "%s", |
---|
| 20 | 'osil': "%s", 'esil': "%s", |
---|
| 21 | 'opit': "%s", 'epit': "%s", 'echr': "%s", |
---|
[807f029] | 22 | 'TotalPit': "%s", 'TotalSil': "%s", 'TotalChr': "%s", |
---|
| 23 | 'MinPit': "%s", 'MaxPit': "%s", |
---|
[5d1c070] | 24 | 'Msil': "%s", 'Mpit': "%s", 'Mchr': "%s"} |
---|
[ee7b9da] | 25 | |
---|
| 26 | def dir_eval(self): |
---|
| 27 | """ evaluate statistical data over the directory """ |
---|
| 28 | v = self.v |
---|
| 29 | v['mode'] = self.params.pitchmode |
---|
| 30 | |
---|
[4f4a8a4] | 31 | def file_exec(self,input,output): |
---|
[0029638] | 32 | filetask = self.task(input,params=self.params) |
---|
| 33 | computed_data = filetask.compute_all() |
---|
[807f029] | 34 | osil, esil, opit, epit, echr = filetask.eval(computed_data,tol=0.5) |
---|
[5d1c070] | 35 | self.v['truth'].append(int(filetask.truth)) |
---|
| 36 | assert opit > 0 |
---|
[ee7b9da] | 37 | |
---|
[5d1c070] | 38 | self.v['osil'].append(osil) |
---|
| 39 | self.v['esil'].append(esil) |
---|
| 40 | self.v['opit'].append(opit) |
---|
| 41 | self.v['epit'].append(epit) |
---|
| 42 | self.v['echr'].append(echr) |
---|
| 43 | |
---|
| 44 | self.v['Msil'].append(esil/float(osil)*100.) |
---|
| 45 | self.v['Mpit'].append(epit/float(opit)*100.) |
---|
| 46 | self.v['Mchr'].append(echr/float(opit)*100.) |
---|
[ee7b9da] | 47 | #print results#, computed_data |
---|
[0029638] | 48 | #print input, results, results - float(input.split('.')[-2]) |
---|
| 49 | |
---|
[b965fb1] | 50 | def run_bench(self,modes=['schmitt'],multiplot=0): |
---|
[ee7b9da] | 51 | from os.path import basename |
---|
[0029638] | 52 | self.modes = modes |
---|
[ee7b9da] | 53 | self.pretty_titles() |
---|
[5d1c070] | 54 | d = [] |
---|
[0029638] | 55 | for mode in self.modes: |
---|
[4f4a8a4] | 56 | self.params.pitchmode = mode |
---|
[807f029] | 57 | self.dir_exec() |
---|
| 58 | self.dir_eval() |
---|
[5d1c070] | 59 | truth = [i for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
| 60 | allOsil = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
| 61 | allEsil = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
| 62 | allOpit = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
| 63 | allEpit = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
| 64 | allEchr = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
| 65 | allMsil = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
| 66 | allMpit = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
| 67 | allMchr = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
| 68 | for i in range(len(self.v['truth'])): |
---|
| 69 | allOsil[self.v['truth'][i]-min(self.v['truth'])] += self.v['osil'][i] |
---|
| 70 | allEsil[self.v['truth'][i]-min(self.v['truth'])] += self.v['esil'][i] |
---|
| 71 | allOpit[self.v['truth'][i]-min(self.v['truth'])] += self.v['opit'][i] |
---|
| 72 | allEpit[self.v['truth'][i]-min(self.v['truth'])] += self.v['epit'][i] |
---|
| 73 | allEchr[self.v['truth'][i]-min(self.v['truth'])] += self.v['echr'][i] |
---|
| 74 | for i in range(len(truth)): |
---|
[807f029] | 75 | allOsil[i] = max(1,allOsil[i]) |
---|
| 76 | allOpit[i] = max(1,allOpit[i]) |
---|
[5d1c070] | 77 | allMsil[i] = allEsil[i]/float(allOsil[i])*100. |
---|
| 78 | allMpit[i] = allEpit[i]/float(allOpit[i])*100. |
---|
| 79 | allMchr[i] = allEchr[i]/float(allOpit[i])*100. |
---|
[807f029] | 80 | self.v['TotalSil'] = sum(allMsil)/len(truth) |
---|
| 81 | self.v['TotalPit'] = sum(allMpit)/len(truth) |
---|
| 82 | self.v['TotalChr'] = sum(allMchr)/len(truth) |
---|
| 83 | self.v['MinPit'] = min(truth) |
---|
| 84 | self.v['MaxPit'] = max(truth) |
---|
| 85 | self.pretty_print() |
---|
[5d1c070] | 86 | |
---|
| 87 | plot = [] |
---|
| 88 | self.plotpitchtessiture(plot, |
---|
| 89 | truth, |
---|
| 90 | allMpit, |
---|
| 91 | plottitle="%s %s" % (self.v['mode'],self.params.bufsize), |
---|
| 92 | plotmode='lines') |
---|
[b965fb1] | 93 | """ |
---|
[5d1c070] | 94 | self.plotpitchtessiture(plot, |
---|
| 95 | truth, |
---|
| 96 | allMchr, |
---|
| 97 | plottitle="%s %s" % (self.v['mode'],"%12"), |
---|
| 98 | plotmode='lines') |
---|
| 99 | self.plotpitchtessiture(plot, |
---|
| 100 | truth, |
---|
| 101 | allMsil, |
---|
| 102 | plottitle="%s %s" % (self.v['mode'],"sil"), |
---|
| 103 | plotmode='lines') |
---|
| 104 | """ |
---|
| 105 | title = basename(self.datadir) |
---|
[b965fb1] | 106 | if multiplot: |
---|
| 107 | d.append(plot) |
---|
| 108 | else: |
---|
| 109 | d += plot |
---|
[ee7b9da] | 110 | outplot = "_-_".join(('pitchtessiture',title)) |
---|
[b965fb1] | 111 | self.xmin = min(self.v['truth']) #20. |
---|
| 112 | self.xmax = max(self.v['truth']) |
---|
| 113 | for ext in ('ps','png','svg'): #,''): |
---|
[ee7b9da] | 114 | self.plotplotpitchtessiture(d, |
---|
[5d1c070] | 115 | plottitle="".join(['Performance against MIDI Note number (', |
---|
| 116 | title, |
---|
| 117 | ", %s" % len(self.sndlist), " samples)"]), |
---|
[ee7b9da] | 118 | outplot=outplot, |
---|
[b965fb1] | 119 | extension=ext,multiplot=multiplot) |
---|
[5d1c070] | 120 | #d.append('beta = .25,orig(x) title \"-2 octave\"') |
---|
| 121 | #d.append('beta = .50,orig(x) title \"-1 octave\"') |
---|
| 122 | #d.append('beta = 1.0,orig(x) title \"original\"') |
---|
| 123 | #d.append('beta = 2.0,orig(x) title \"+1 octave\"') |
---|
[ee7b9da] | 124 | |
---|
| 125 | """ |
---|
| 126 | Plot functions |
---|
| 127 | """ |
---|
| 128 | |
---|
| 129 | def plotpitchtessiture(self,d,lx,ly,plottitle="",plotmode='linespoints'): |
---|
| 130 | import Gnuplot, Gnuplot.funcutils |
---|
| 131 | d.append(Gnuplot.Data(lx, ly, with=plotmode, title="%s" % (plottitle) )) |
---|
| 132 | |
---|
[5d1c070] | 133 | def plotplotpitchtessiture(self,d,plottitle='',outplot=0,extension='',multiplot=1): |
---|
[ee7b9da] | 134 | from aubio.gnuplot import gnuplot_create |
---|
| 135 | g = gnuplot_create(outplot=outplot,extension=extension) |
---|
[5d1c070] | 136 | #g.title(plottitle) |
---|
| 137 | #g('orig(x) = beta*x') |
---|
[b965fb1] | 138 | g.title(plottitle) |
---|
| 139 | g('set yrange [50:100]') |
---|
| 140 | # erase axis |
---|
| 141 | g('set xrange [%f:%f]' % (self.xmin,self.xmax)) #(self.xmax - (self.xmax-self.xmin)*5./4.,self.xmax)) |
---|
[5d1c070] | 142 | #g.plot(*d) |
---|
[b965fb1] | 143 | g('set border 3') |
---|
| 144 | g('set xtics nomirror') |
---|
| 145 | g('set ytics nomirror') |
---|
| 146 | g('set key bottom') |
---|
[5d1c070] | 147 | if multiplot: |
---|
| 148 | g('set multiplot') |
---|
| 149 | for i in range(len(d)): |
---|
| 150 | # plot onset detection functions |
---|
| 151 | g('set size 1,%f' % ( 1.0/float(len(d)) ) ) |
---|
| 152 | g('set origin 0,%f' % ( 1.0*float(len(d)-i-1)/float(len(d)) ) ) |
---|
| 153 | #g.ylabel('%Correct detections') |
---|
[b965fb1] | 154 | g('set xrange [%f:%f]' % (self.xmin,self.xmax)) #(self.xmax - (self.xmax-self.xmin)*5./4.,self.xmax)) |
---|
[5d1c070] | 155 | g.plot(*d[i]) |
---|
| 156 | g('unset title') |
---|
| 157 | g('unset multiplot') |
---|
| 158 | else: |
---|
| 159 | g.plot(*d) |
---|
[ee7b9da] | 160 | |
---|
[0029638] | 161 | |
---|
| 162 | if __name__ == "__main__": |
---|
| 163 | import sys |
---|
| 164 | if len(sys.argv) > 1: datapath = sys.argv[1] |
---|
| 165 | else: print "error: a path is required"; sys.exit(1) |
---|
[4f4a8a4] | 166 | if len(sys.argv) > 2: |
---|
| 167 | for each in sys.argv[3:-1]: print each |
---|
[807f029] | 168 | modes = ['schmitt', 'yin', 'yinfft', 'mcomb', 'fcomb'] |
---|
[b965fb1] | 169 | #modes = ['mcomb'] |
---|
[0029638] | 170 | |
---|
[ee7b9da] | 171 | params = taskparams() |
---|
[b965fb1] | 172 | params.bufsize = 2048 # 4096 |
---|
[807f029] | 173 | params.hopsize = 256 |
---|
[b965fb1] | 174 | params.silence = -60. |
---|
[5d1c070] | 175 | params.pitchsmooth = 0 |
---|
| 176 | params.pitchmax = 20000 |
---|
| 177 | params.pitchmin = 20 |
---|
[807f029] | 178 | params.pitchyinfft = 0.95 |
---|
[ee7b9da] | 179 | benchpitch = benchpitch(datapath,params=params) |
---|
[0029638] | 180 | benchpitch.task = taskpitch |
---|
| 181 | |
---|
[ee7b9da] | 182 | #benchpitch.titles = [ 'mode', 'thres', 'avg', 'avgdist' ] |
---|
| 183 | #benchpitch.formats = ["%12s" , "| %6s", "| %6s", "| %6s", "| %6s", "| %6s" ] |
---|
[0029638] | 184 | try: |
---|
| 185 | benchpitch.run_bench(modes=modes) |
---|
| 186 | except KeyboardInterrupt: |
---|
| 187 | print "Interrupted by user" |
---|
| 188 | sys.exit(1) |
---|
| 189 | |
---|
| 190 | sys.exit(0) |
---|