source: python/test/bench/onset/bench-onset @ c912c67

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

merge some benchonset code into node
merge some benchonset code into node

  • Property mode set to 100755
File size: 5.2 KB
Line 
1#! /usr/bin/python
2
3from aubio.bench.node import *
4from aubio.tasks import *
5
6
7
8
9def mmean(l):
10        return sum(l)/float(len(l))
11
12def stdev(l):
13        smean = 0
14        lmean = mmean(l)
15        for i in l:
16                smean += (i-lmean)**2
17        smean *= 1. / len(l)
18        return smean**.5
19
20class benchonset(bench):
21
22        """ list of values to store per file """
23        valuenames = ['orig','missed','Tm','expc','bad','Td']
24        """ list of lists to store per file """
25        valuelists = ['l','labs']
26        """ list of values to print per dir """
27        printnames = [ 'mode', 'thres', 'dist', 'prec', 'recl',
28                'Ttrue', 'Tfp',  'Tfn',  'Tm',   'Td',
29                'aTtrue', 'aTfp', 'aTfn', 'aTm',  'aTd', 
30                'mean', 'smean',  'amean', 'samean']
31
32        """ per dir """
33        formats = {'mode': "%12s" , 'thres': "%5.4s",
34                'dist':  "%5.4s", 'prec': "%5.4s", 'recl':  "%5.4s",
35                'Ttrue': "%5.4s", 'Tfp':   "%5.4s", 'Tfn':   "%5.4s",
36                'Tm':    "%5.4s", 'Td':    "%5.4s",
37                'aTtrue':"%5.4s", 'aTfp':  "%5.4s", 'aTfn':  "%5.4s",
38                'aTm':   "%5.4s", 'aTd':   "%5.4s",
39                'mean':  "%5.40s", 'smean': "%5.40s",
40                'amean':  "%5.40s", 'samean': "%5.40s"}
41
42        def dir_eval(self):
43                """ evaluate statistical data over the directory """
44                totaltrue = sum(self.v['expc'])-sum(self.v['bad'])-sum(self.v['Td'])
45                totalfp = sum(self.v['bad'])+sum(self.v['Td'])
46                totalfn = sum(self.v['missed'])+sum(self.v['Tm'])
47                self.P = 100*float(totaltrue)/max(totaltrue + totalfp,1)
48                self.R = 100*float(totaltrue)/max(totaltrue + totalfn,1)
49                if self.R < 0: self.R = 0
50                self.F = 2.* self.P*self.R / max(float(self.P+self.R),1)
51                N = float(len(self.reslist))
52                self.v['mode']      = self.params.onsetmode
53                self.v['thres']     = self.params.threshold
54                self.v['thres']     = "%2.3f" % self.params.threshold
55                self.v['dist']      = "%2.3f" % self.F
56                self.v['prec']      = "%2.3f" % self.P
57                self.v['recl']      = "%2.3f" % self.R
58                self.v['Ttrue']     = totaltrue
59                self.v['Tfp']       = totalfp
60                self.v['Tfn']       = totalfn
61                self.v['aTtrue']    = totaltrue/N
62                self.v['aTfp']      = totalfp/N
63                self.v['aTfn']      = totalfn/N
64                self.v['aTm']       = sum(self.v['Tm'])/N
65                self.v['aTd']       = sum(self.v['Td'])/N
66                self.v['mean']      = mmean(self.v['l'])
67                self.v['smean']     = stdev(self.v['l'])
68                self.v['amean']     = mmean(self.v['labs'])
69                self.v['samean']    = stdev(self.v['labs'])
70
71        def run_bench(self,modes=['dual'],thresholds=[0.5]):
72                self.modes = modes
73                self.thresholds = thresholds
74                self.pretty_titles()
75                for mode in self.modes:
76                        self.params.onsetmode = mode
77                        for threshold in self.thresholds:
78                                self.params.threshold = threshold
79                                self.dir_exec()
80                                self.dir_eval()
81                                self.pretty_print()
82                                #print self.v
83
84        def auto_learn(self,modes=['dual'],thresholds=[0.1,1.5]):
85                """ simple dichotomia like algorithm to optimise threshold """
86                self.modes = modes
87                self.pretty_titles()
88                for mode in self.modes:
89                        steps = 11
90                        lesst = thresholds[0]
91                        topt = thresholds[1]
92                        self.params.onsetmode = mode
93
94                        self.params.threshold = topt
95                        self.dir_exec()
96                        self.dir_eval()
97                        self.pretty_print()
98                        topF = self.F
99
100                        self.params.threshold = lesst
101                        self.dir_exec()
102                        self.dir_eval()
103                        self.pretty_print()
104                        lessF = self.F
105
106                        for i in range(steps):
107                                self.params.threshold = ( lesst + topt ) * .5
108                                self.dir_exec()
109                                self.dir_eval()
110                                self.pretty_print()
111                                if self.F == 100.0 or self.F == topF:
112                                        print "assuming we converged, stopping"
113                                        break
114                                #elif abs(self.F - topF) < 0.01 :
115                                #       print "done converging"
116                                #       break
117                                if topF < self.F:
118                                        #lessF = topF
119                                        #lesst = topt
120                                        topF = self.F
121                                        topt = self.params.threshold
122                                elif lessF < self.F:
123                                        lessF = self.F
124                                        lesst = self.params.threshold
125                                if topt == lesst:
126                                        lesst /= 2.
127
128        def auto_learn2(self,modes=['dual'],thresholds=[0.00001,1.0]):
129                """ simple dichotomia like algorithm to optimise threshold """
130                self.modes = modes
131                self.pretty_titles([])
132                for mode in self.modes:
133                        steps = 10
134                        step = 0.4
135                        self.params.onsetmode = mode
136                        self.params.threshold = thresholds[0]
137                        cur = 0
138
139                        for i in range(steps):
140                                self.dir_exec()
141                                self.dir_eval()
142                                self.pretty_print()
143                                new = self.P
144                                if self.R == 0.0:
145                                        #print "Found maximum, highering"
146                                        step /= 2.
147                                        self.params.threshold -= step
148                                elif new == 100.0:
149                                        #print "Found maximum, highering"
150                                        step *= .99
151                                        self.params.threshold += step
152                                elif cur > new:
153                                        #print "lower"
154                                        step /= 2.
155                                        self.params.threshold -= step
156                                elif cur < new:
157                                        #print "higher"
158                                        step *= .99
159                                        self.params.threshold += step
160                                else:
161                                        print "Assuming we converged"
162                                        break
163                                cur = new
164
165
166if __name__ == "__main__":
167        import sys
168        if len(sys.argv) > 1: datapath = sys.argv[1]
169        else: print "ERR: a path is required"; sys.exit(1)
170        modes = ['complex', 'energy', 'phase', 'specdiff', 'kl', 'mkl', 'dual']
171        #modes = [ 'mkl' ]
172        thresholds = [ 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2]
173        #thresholds = [1.5]
174
175        #datapath = "%s%s" % (DATADIR,'/onset/DB/*/')
176        respath = '/var/tmp/DB-testings'
177
178        benchonset = benchonset(datapath,respath,checkres=True,checkanno=True)
179        benchonset.params = taskparams()
180        benchonset.task = taskonset
181        benchonset.valuesdict = {}
182
183        try:
184                #benchonset.auto_learn2(modes=modes)
185                benchonset.run_bench(modes=modes,thresholds=thresholds)
186        except KeyboardInterrupt:
187                sys.exit(1)
Note: See TracBrowser for help on using the repository browser.