source: python/aubio/bench/onset.py @ bab3161

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

remove /usr/bin/python header
remove /usr/bin/python header

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