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