[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 | |
---|
[4f4a8a4] | 50 | def run_bench(self,modes=['schmitt']): |
---|
[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') |
---|
| 93 | self.plotpitchtessiture(plot, |
---|
| 94 | truth, |
---|
| 95 | allMchr, |
---|
| 96 | plottitle="%s %s" % (self.v['mode'],"%12"), |
---|
| 97 | plotmode='lines') |
---|
| 98 | """ |
---|
| 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) |
---|
| 106 | d.append(plot) |
---|
[ee7b9da] | 107 | outplot = "_-_".join(('pitchtessiture',title)) |
---|
[5d1c070] | 108 | self.xmin = 20. #min(self.v['truth']) |
---|
| 109 | self.xmax = 110. #max(self.v['truth']) |
---|
[ee7b9da] | 110 | for ext in ('ps','png','svg',''): |
---|
| 111 | self.plotplotpitchtessiture(d, |
---|
[5d1c070] | 112 | plottitle="".join(['Performance against MIDI Note number (', |
---|
| 113 | title, |
---|
| 114 | ", %s" % len(self.sndlist), " samples)"]), |
---|
[ee7b9da] | 115 | outplot=outplot, |
---|
| 116 | extension=ext) |
---|
[5d1c070] | 117 | #d.append('beta = .25,orig(x) title \"-2 octave\"') |
---|
| 118 | #d.append('beta = .50,orig(x) title \"-1 octave\"') |
---|
| 119 | #d.append('beta = 1.0,orig(x) title \"original\"') |
---|
| 120 | #d.append('beta = 2.0,orig(x) title \"+1 octave\"') |
---|
[ee7b9da] | 121 | |
---|
| 122 | """ |
---|
| 123 | Plot functions |
---|
| 124 | """ |
---|
| 125 | |
---|
| 126 | def plotpitchtessiture(self,d,lx,ly,plottitle="",plotmode='linespoints'): |
---|
| 127 | import Gnuplot, Gnuplot.funcutils |
---|
| 128 | d.append(Gnuplot.Data(lx, ly, with=plotmode, title="%s" % (plottitle) )) |
---|
| 129 | |
---|
[5d1c070] | 130 | def plotplotpitchtessiture(self,d,plottitle='',outplot=0,extension='',multiplot=1): |
---|
[ee7b9da] | 131 | from aubio.gnuplot import gnuplot_create |
---|
| 132 | g = gnuplot_create(outplot=outplot,extension=extension) |
---|
[5d1c070] | 133 | #g.title(plottitle) |
---|
| 134 | #g('orig(x) = beta*x') |
---|
| 135 | #g.xlabel('original pitch (Hz)') |
---|
| 136 | #g.ylabel('detected pitch (Hz)') |
---|
| 137 | #g('set key left top') |
---|
| 138 | #g('set log xy') |
---|
| 139 | #g('set xrange [50:2000]') |
---|
| 140 | g('set yrange [0:100]') |
---|
| 141 | #g.plot(*d) |
---|
| 142 | if multiplot: |
---|
| 143 | g('set multiplot') |
---|
| 144 | for i in range(len(d)): |
---|
| 145 | # plot onset detection functions |
---|
| 146 | g('set size 1,%f' % ( 1.0/float(len(d)) ) ) |
---|
| 147 | g('set origin 0,%f' % ( 1.0*float(len(d)-i-1)/float(len(d)) ) ) |
---|
| 148 | # erase axis |
---|
| 149 | g('set border 3') |
---|
| 150 | g('set xtics nomirror') |
---|
| 151 | g('set ytics nomirror') |
---|
| 152 | g('set key left top') |
---|
| 153 | g('set xrange [%f:%f]' % (self.xmin,self.xmax)) #(self.xmax - (self.xmax-self.xmin)*5./4.,self.xmax)) |
---|
| 154 | #g.ylabel('%Correct detections') |
---|
| 155 | if i == len(d)-1: |
---|
| 156 | g.xlabel(plottitle) |
---|
| 157 | g.plot(*d[i]) |
---|
| 158 | g('unset title') |
---|
| 159 | g('unset multiplot') |
---|
| 160 | else: |
---|
| 161 | g.plot(*d) |
---|
[ee7b9da] | 162 | |
---|
[0029638] | 163 | |
---|
| 164 | if __name__ == "__main__": |
---|
| 165 | import sys |
---|
| 166 | if len(sys.argv) > 1: datapath = sys.argv[1] |
---|
| 167 | else: print "error: a path is required"; sys.exit(1) |
---|
[4f4a8a4] | 168 | if len(sys.argv) > 2: |
---|
| 169 | for each in sys.argv[3:-1]: print each |
---|
[807f029] | 170 | modes = ['schmitt', 'yin', 'yinfft', 'mcomb', 'fcomb'] |
---|
[0029638] | 171 | |
---|
[ee7b9da] | 172 | params = taskparams() |
---|
[807f029] | 173 | params.bufsize = 2048 #4096 |
---|
| 174 | params.hopsize = 256 |
---|
| 175 | params.silence = -70. |
---|
[5d1c070] | 176 | params.pitchsmooth = 0 |
---|
| 177 | params.pitchmax = 20000 |
---|
| 178 | params.pitchmin = 20 |
---|
[807f029] | 179 | params.pitchyinfft = 0.95 |
---|
[ee7b9da] | 180 | benchpitch = benchpitch(datapath,params=params) |
---|
[0029638] | 181 | benchpitch.task = taskpitch |
---|
| 182 | |
---|
[ee7b9da] | 183 | #benchpitch.titles = [ 'mode', 'thres', 'avg', 'avgdist' ] |
---|
| 184 | #benchpitch.formats = ["%12s" , "| %6s", "| %6s", "| %6s", "| %6s", "| %6s" ] |
---|
[0029638] | 185 | try: |
---|
| 186 | benchpitch.run_bench(modes=modes) |
---|
| 187 | except KeyboardInterrupt: |
---|
| 188 | print "Interrupted by user" |
---|
| 189 | sys.exit(1) |
---|
| 190 | |
---|
| 191 | sys.exit(0) |
---|