source: python/test/bench/onset/benchonset.py @ 6ae3a77

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 6ae3a77 was 6ae3a77, checked in by Paul Brossier <piem@altern.org>, 18 years ago

update onset bench
update onset bench

  • Property mode set to 100644
File size: 9.3 KB
Line 
1#! /usr/bin/python
2
3from aubio.bench.node import *
4from os.path import dirname,basename
5
6def mmean(l):
7        return sum(l)/max(float(len(l)),1)
8
9def stdev(l):
10        smean = 0
11        if not len(l): return smean
12        lmean = mmean(l)
13        for i in l:
14                smean += (i-lmean)**2
15        smean *= 1. / len(l)
16        return smean**.5
17
18class benchonset(bench):
19
20        """ list of values to store per file """
21        valuenames = ['orig','missed','Tm','expc','bad','Td']
22        """ list of lists to store per file """
23        valuelists = ['l','labs']
24        """ list of values to print per dir """
25        printnames = [ 'mode', 'thres', 'dist', 'prec', 'recl',
26                'GD', 'FP', 
27                'Torig', 'Ttrue', 'Tfp',  'Tfn',  'TTm',   'TTd',
28                'aTtrue', 'aTfp', 'aTfn', 'aTm',  'aTd', 
29                'mean', 'smean',  'amean', 'samean']
30
31        """ per dir """
32        formats = {'mode': "%12s" , 'thres': "%5.4s", 
33                'dist':  "%5.4s", 'prec': "%5.4s", 'recl':  "%5.4s",
34                'Torig': "%5.4s", 'Ttrue': "%5.4s", 'Tfp':   "%5.4s", 'Tfn':   "%5.4s", 
35                'TTm':    "%5.4s", 'TTd':    "%5.4s",
36                'aTtrue':"%5.4s", 'aTfp':  "%5.4s", 'aTfn':  "%5.4s", 
37                'aTm':   "%5.4s", 'aTd':   "%5.4s",
38                'mean':  "%5.40s", 'smean': "%5.40s", 
39                'amean':  "%5.40s", 'samean': "%5.40s", 
40                "GD":     "%5.4s", "FP":     "%5.4s",
41                "GDm":     "%5.4s", "FPd":     "%5.4s"}
42
43        def dir_eval(self):
44                """ evaluate statistical data over the directory """
45                v = self.v
46
47                v['mode']      = self.params.onsetmode
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
53
54                v['Torig']     = sum(v['orig'])
55                v['TTm']       = sum(v['Tm'])
56                v['TTd']       = sum(v['Td'])
57                v['Texpc']     = sum(v['expc'])
58                v['Tbad']      = sum(v['bad'])
59                v['Tmissed']   = sum(v['missed'])
60                v['aTm']       = mmean(v['Tm'])
61                v['aTd']       = mmean(v['Td'])
62
63                v['mean']      = mmean(v['l'])
64                v['smean']     = stdev(v['l'])
65
66                v['amean']     = mmean(v['labs'])
67                v['samean']    = stdev(v['labs'])
68               
69                # old type calculations
70                # good detection rate
71                v['GD']  = 100.*(v['Torig']-v['Tmissed']-v['TTm'])/v['Torig']
72                # false positive rate
73                v['FP']  = 100.*(v['Tbad']+v['TTd'])/v['Torig']
74                # good detection counting merged detections as good
75                v['GDm'] = 100.*(v['Torig']-v['Tmissed'])/v['Torig'] 
76                # false positives counting doubled as good
77                v['FPd'] = 100.*v['Tbad']/v['Torig']               
78               
79                # mirex type annotations
80                totaltrue = v['Texpc']-v['Tbad']-v['TTd']
81                totalfp = v['Tbad']+v['TTd']
82                totalfn = v['Tmissed']+v['TTm']
83                self.v['Ttrue']     = totaltrue
84                self.v['Tfp']       = totalfp
85                self.v['Tfn']       = totalfn
86                # average over the number of annotation files
87                N = float(len(self.reslist))
88                self.v['aTtrue']    = totaltrue/N
89                self.v['aTfp']      = totalfp/N
90                self.v['aTfn']      = totalfn/N
91
92                # F-measure
93                self.P = 100.*float(totaltrue)/max(totaltrue + totalfp,1)
94                self.R = 100.*float(totaltrue)/max(totaltrue + totalfn,1)
95                #if self.R < 0: self.R = 0
96                self.F = 2.* self.P*self.R / max(float(self.P+self.R),1)
97                self.v['dist']      = self.F
98                self.v['prec']      = self.P
99                self.v['recl']      = self.R
100
101
102        """
103        Plot functions
104        """
105
106        def plotroc(self,d,plottitle=""):
107                import Gnuplot, Gnuplot.funcutils
108                gd = []
109                fp = []
110                for i in self.vlist:
111                        gd.append(i['GD']) 
112                        fp.append(i['FP']) 
113                d.append(Gnuplot.Data(fp, gd, with='linespoints', 
114                        title="%s %s" % (plottitle,i['mode']) ))
115
116        def plotplotroc(self,d,outplot=0,extension='ps'):
117                import Gnuplot, Gnuplot.funcutils
118                from sys import exit
119                g = Gnuplot.Gnuplot(debug=0, persist=1)
120                if outplot:
121                        if   extension == 'ps':  ext, extension = '.ps' , 'postscript'
122                        elif extension == 'png': ext, extension = '.png', 'png'
123                        elif extension == 'svg': ext, extension = '.svg', 'svg'
124                        else: exit("ERR: unknown plot extension")
125                        g('set terminal %s' % extension)
126                        g('set output \'roc-%s%s\'' % (outplot,ext))
127                xmax = 30 #max(fp)
128                ymin = 50 
129                g('set xrange [0:%f]' % xmax)
130                g('set yrange [%f:100]' % ymin)
131                # grid set
132                g('set grid')
133                g('set xtics 0,5,%f' % xmax)
134                g('set ytics %f,5,100' % ymin)
135                g('set key 27,65')
136                #g('set format \"%g\"')
137                g.title(basename(self.datadir))
138                g.xlabel('false positives (%)')
139                g.ylabel('correct detections (%)')
140                g.plot(*d)
141
142        def plotpr(self,d,plottitle=""):
143                import Gnuplot, Gnuplot.funcutils
144                x = []
145                y = []
146                for i in self.vlist:
147                        x.append(i['prec']) 
148                        y.append(i['recl']) 
149                d.append(Gnuplot.Data(x, y, with='linespoints', 
150                        title="%s %s" % (plottitle,i['mode']) ))
151
152        def plotplotpr(self,d,outplot=0,extension='ps'):
153                import Gnuplot, Gnuplot.funcutils
154                from sys import exit
155                g = Gnuplot.Gnuplot(debug=0, persist=1)
156                if outplot:
157                        if   extension == 'ps':  ext, extension = '.ps' , 'postscript'
158                        elif extension == 'png': ext, extension = '.png', 'png'
159                        elif extension == 'svg': ext, extension = '.svg', 'svg'
160                        else: exit("ERR: unknown plot extension")
161                        g('set terminal %s' % extension)
162                        g('set output \'pr-%s%s\'' % (outplot,ext))
163                g.title(basename(self.datadir))
164                g.xlabel('Recall (%)')
165                g.ylabel('Precision (%)')
166                g.plot(*d)
167
168        def plotfmeas(self,d,plottitle=""):
169                import Gnuplot, Gnuplot.funcutils
170                x,y = [],[]
171                for i in self.vlist:
172                        x.append(i['thres']) 
173                        y.append(i['dist']) 
174                d.append(Gnuplot.Data(x, y, with='linespoints', 
175                        title="%s %s" % (plottitle,i['mode']) ))
176
177        def plotplotfmeas(self,d,outplot="",extension='ps', title="F-measure"):
178                import Gnuplot, Gnuplot.funcutils
179                from sys import exit
180                g = Gnuplot.Gnuplot(debug=0, persist=1)
181                if outplot:
182                        if   extension == 'ps':  terminal = 'postscript'
183                        elif extension == 'png': terminal = 'png'
184                        elif extension == 'svg': terminal = 'svg'
185                        else: exit("ERR: unknown plot extension")
186                        g('set terminal %s' % terminal)
187                        g('set output \'fmeas-%s.%s\'' % (outplot,extension))
188                g.xlabel('threshold \\delta')
189                g.ylabel('F-measure (%)')
190                g('set xrange [0:1.2]')
191                g('set yrange [0:100]')
192                g.title(basename(self.datadir))
193                # grid set
194                #g('set grid')
195                #g('set xtics 0,5,%f' % xmax)
196                #g('set ytics %f,5,100' % ymin)
197                #g('set key 27,65')
198                #g('set format \"%g\"')
199                g.plot(*d)
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
228        def plotdiffs(self,d,plottitle=""):
229                import Gnuplot, Gnuplot.funcutils
230                v = self.v
231                l = v['l']
232                mean   = v['mean']
233                smean  = v['smean']
234                amean  = v['amean']
235                samean = v['samean']
236                val = []
237                per = [0] * 100
238                for i in range(0,100):
239                        val.append(i*.001-.05)
240                        for j in l: 
241                                if abs(j-val[i]) <= 0.001:
242                                        per[i] += 1
243                total = v['Torig']
244                for i in range(len(per)): per[i] /= total/100.
245
246                d.append(Gnuplot.Data(val, per, with='fsteps', 
247                        title="%s %s" % (plottitle,v['mode']) ))
248                #d.append('mean=%f,sigma=%f,eps(x) title \"\"'% (mean,smean))
249                #d.append('mean=%f,sigma=%f,eps(x) title \"\"'% (amean,samean))
250
251
252        def plotplotdiffs(self,d,outplot=0,extension='ps'):
253                import Gnuplot, Gnuplot.funcutils
254                from sys import exit
255                g = Gnuplot.Gnuplot(debug=0, persist=1)
256                if outplot:
257                        if   extension == 'ps':  ext, extension = '.ps' , 'postscript'
258                        elif extension == 'png': ext, extension = '.png', 'png'
259                        elif extension == 'svg': ext, extension = '.svg', 'svg'
260                        else: exit("ERR: unknown plot extension")
261                        g('set terminal %s' % extension)
262                        g('set output \'diffhist-%s%s\'' % (outplot,ext))
263                g('eps(x) = 1./(sigma*(2.*3.14159)**.5) * exp ( - ( x - mean ) ** 2. / ( 2. * sigma ** 2. ))')
264                g.title(basename(self.datadir))
265                g.xlabel('delay to hand-labelled onset (s)')
266                g.ylabel('% number of correct detections / ms ')
267                g('set xrange [-0.05:0.05]')
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]')
300                g.plot(*d)
301
302
Note: See TracBrowser for help on using the repository browser.