[4cc9fe5] | 1 | from config import * |
---|
| 2 | import commands,sys |
---|
| 3 | import re |
---|
| 4 | |
---|
| 5 | def runcommand(cmd,debug=0): |
---|
| 6 | if VERBOSE >= VERBOSE_CMD or debug: print cmd |
---|
| 7 | if debug: return |
---|
| 8 | status, output = commands.getstatusoutput(cmd) |
---|
| 9 | if status == 0 or VERBOSE >= VERBOSE_OUT: |
---|
| 10 | output = output.split('\n') |
---|
| 11 | if VERBOSE >= VERBOSE_OUT: |
---|
| 12 | for i in output: |
---|
| 13 | if i: print i |
---|
| 14 | if not status == 0: |
---|
| 15 | print 'error:',status,output |
---|
| 16 | print 'command returning error was',cmd |
---|
[d45520a] | 17 | #sys.exit(1) |
---|
[257debc] | 18 | if output == '' or output == ['']: return |
---|
[4cc9fe5] | 19 | return output |
---|
| 20 | |
---|
[257debc] | 21 | def list_files(datapath,filter='f', maxdepth = -1): |
---|
[50e99cc] | 22 | if not os.path.exists(datapath): |
---|
| 23 | print |
---|
| 24 | print "ERR: no directory %s were found" % datapath |
---|
| 25 | sys.exit(1) |
---|
[257debc] | 26 | if maxdepth >= 0: maxstring = " -maxdepth %d " % maxdepth |
---|
| 27 | else: maxstring = "" |
---|
[5d1c070] | 28 | cmd = '%s' * 6 % ('find ',datapath,maxstring,' -type ',filter, "| sort -n") |
---|
[4cc9fe5] | 29 | return runcommand(cmd) |
---|
| 30 | |
---|
[257debc] | 31 | def list_wav_files(datapath,maxdepth = -1): |
---|
| 32 | return list_files(datapath, filter="f -name '*.wav'",maxdepth = maxdepth) |
---|
[d45520a] | 33 | |
---|
[50e99cc] | 34 | sndfile_filter = "f -name '*.wav' -o -name '*.aif' -o -name '*.aiff'" |
---|
| 35 | |
---|
[257debc] | 36 | def list_snd_files(datapath,maxdepth = -1): |
---|
[50e99cc] | 37 | return list_files(datapath, filter=sndfile_filter, |
---|
[257debc] | 38 | maxdepth = maxdepth) |
---|
| 39 | |
---|
| 40 | def list_res_files(datapath,maxdepth = -1): |
---|
| 41 | return list_files(datapath, filter="f -name '*.txt'", maxdepth = maxdepth) |
---|
[2bd1a2a] | 42 | |
---|
[d45520a] | 43 | def list_dirs(datapath): |
---|
| 44 | return list_files(datapath, filter="d") |
---|
| 45 | |
---|
[4cc9fe5] | 46 | def mkdir(path): |
---|
| 47 | cmd = '%s%s' % ('mkdir -p ',path) |
---|
| 48 | return runcommand(cmd) |
---|
| 49 | |
---|
[50791b3] | 50 | def act_on_data (action,datapath,respath=None,suffix='.txt',filter='f',sub='\.wav$',**keywords): |
---|
[4cc9fe5] | 51 | """ execute action(datafile,resfile) on all files in datapath """ |
---|
| 52 | dirlist = list_files(datapath,filter=filter) |
---|
[d45520a] | 53 | if dirlist == ['']: dirlist = [] |
---|
[50791b3] | 54 | if respath: |
---|
| 55 | respath_in_datapath = re.split(datapath, respath,maxsplit=1)[1:] |
---|
| 56 | if(respath_in_datapath and suffix == ''): |
---|
| 57 | print 'error: respath in datapath and no suffix used' |
---|
[4cc9fe5] | 58 | for i in dirlist: |
---|
[d45520a] | 59 | j = re.split(datapath, i,maxsplit=1)[1] |
---|
| 60 | j = re.sub(sub,'',j) |
---|
| 61 | #j = "%s%s%s"%(respath,j,suffix) |
---|
[50791b3] | 62 | if respath: |
---|
| 63 | j = "%s%s"%(respath,j) |
---|
| 64 | if sub != '': |
---|
| 65 | j = re.sub(sub,suffix,j) |
---|
| 66 | else: |
---|
| 67 | j = "%s%s" % (j,suffix) |
---|
[d45520a] | 68 | action(i,j,**keywords) |
---|
[4cc9fe5] | 69 | |
---|
| 70 | def act_on_results (action,datapath,respath,filter='d'): |
---|
| 71 | """ execute action(respath) an all subdirectories in respath """ |
---|
| 72 | dirlist = list_files(datapath,filter='d') |
---|
| 73 | respath_in_datapath = re.split(datapath, respath,maxsplit=1)[1:] |
---|
| 74 | if(respath_in_datapath and not filter == 'd' and suffix == ''): |
---|
| 75 | print 'warning: respath is in datapath' |
---|
| 76 | for i in dirlist: |
---|
| 77 | s = re.split(datapath, i ,maxsplit=1)[1] |
---|
| 78 | action("%s%s%s"%(respath,'/',s)) |
---|
[257debc] | 79 | |
---|
[1dae4eb] | 80 | def act_on_files (action,listfiles,listres=None,suffix='.txt',filter='f',sub='\.wav$',**keywords): |
---|
| 81 | """ execute action(respath) an all subdirectories in respath """ |
---|
| 82 | if listres and len(listfiles) <= len(listres): |
---|
[a3254df] | 83 | for i in range(len(listfiles)): |
---|
| 84 | action(listfiles[i],listres[i],**keywords) |
---|
[1dae4eb] | 85 | else: |
---|
| 86 | for i in listfiles: |
---|
| 87 | action(i,None,**keywords) |
---|
| 88 | |
---|
[336cf77] | 89 | class bench: |
---|
[50e99cc] | 90 | """ class to run benchmarks on directories """ |
---|
[a7880d9] | 91 | def __init__(self,datadir,resdir=None,checkres=False,checkanno=False,params=[]): |
---|
[43938de] | 92 | from aubio.task.params import taskparams |
---|
[257debc] | 93 | self.datadir = datadir |
---|
[1dae4eb] | 94 | # path to write results path to |
---|
[257debc] | 95 | self.resdir = resdir |
---|
[1dae4eb] | 96 | # list of annotation files |
---|
| 97 | self.reslist = [] |
---|
| 98 | # list used to gather results |
---|
[50791b3] | 99 | self.results = [] |
---|
[a7880d9] | 100 | if not params: self.params = taskparams() |
---|
| 101 | else: self.params = params |
---|
[257debc] | 102 | print "Checking data directory", self.datadir |
---|
| 103 | self.checkdata() |
---|
[336cf77] | 104 | if checkanno: self.checkanno() |
---|
| 105 | if checkres: self.checkres() |
---|
[257debc] | 106 | |
---|
| 107 | def checkdata(self): |
---|
[1dae4eb] | 108 | if os.path.isfile(self.datadir): |
---|
| 109 | self.dirlist = os.path.dirname(self.datadir) |
---|
| 110 | elif os.path.isdir(self.datadir): |
---|
| 111 | self.dirlist = list_dirs(self.datadir) |
---|
| 112 | # allow dir* matching through find commands? |
---|
| 113 | else: |
---|
| 114 | print "ERR: path not understood" |
---|
| 115 | sys.exit(1) |
---|
[257debc] | 116 | print "Listing directories in data directory", |
---|
[50e99cc] | 117 | if self.dirlist: |
---|
| 118 | print " (%d elements)" % len(self.dirlist) |
---|
| 119 | else: |
---|
| 120 | print " (0 elements)" |
---|
| 121 | print "ERR: no directory %s were found" % self.datadir |
---|
| 122 | sys.exit(1) |
---|
[257debc] | 123 | print "Listing sound files in data directory", |
---|
| 124 | self.sndlist = list_snd_files(self.datadir) |
---|
[50e99cc] | 125 | if self.sndlist: |
---|
| 126 | print " (%d elements)" % len(self.sndlist) |
---|
| 127 | else: |
---|
| 128 | print " (0 elements)" |
---|
| 129 | print "ERR: no sound files were found in", self.datadir |
---|
| 130 | sys.exit(1) |
---|
[336cf77] | 131 | |
---|
| 132 | def checkanno(self): |
---|
[257debc] | 133 | print "Listing annotations in data directory", |
---|
| 134 | self.reslist = list_res_files(self.datadir) |
---|
| 135 | print " (%d elements)" % len(self.reslist) |
---|
| 136 | #for each in self.reslist: print each |
---|
| 137 | if not self.reslist or len(self.reslist) < len (self.sndlist): |
---|
| 138 | print "ERR: not enough annotations" |
---|
| 139 | return -1 |
---|
| 140 | else: |
---|
| 141 | print "Found enough annotations" |
---|
| 142 | |
---|
| 143 | def checkres(self): |
---|
| 144 | print "Creating results directory" |
---|
| 145 | act_on_results(mkdir,self.datadir,self.resdir,filter='d') |
---|
[336cf77] | 146 | |
---|
[c912c67] | 147 | def pretty_print(self,sep='|'): |
---|
| 148 | for i in self.printnames: |
---|
| 149 | print self.formats[i] % self.v[i], sep, |
---|
| 150 | print |
---|
| 151 | |
---|
| 152 | def pretty_titles(self,sep='|'): |
---|
| 153 | for i in self.printnames: |
---|
| 154 | print self.formats[i] % i, sep, |
---|
[336cf77] | 155 | print |
---|
| 156 | |
---|
[50e99cc] | 157 | def dir_exec(self): |
---|
| 158 | """ run file_exec on every input file """ |
---|
[c912c67] | 159 | self.l , self.labs = [], [] |
---|
| 160 | self.v = {} |
---|
| 161 | for i in self.valuenames: |
---|
| 162 | self.v[i] = [] |
---|
| 163 | for i in self.valuelists: |
---|
| 164 | self.v[i] = [] |
---|
| 165 | act_on_files(self.file_exec,self.sndlist,self.reslist, \ |
---|
| 166 | suffix='',filter=sndfile_filter) |
---|
[dbc0351] | 167 | |
---|
[50e99cc] | 168 | def dir_eval(self): |
---|
| 169 | pass |
---|
| 170 | |
---|
[c912c67] | 171 | def file_gettruth(self,input): |
---|
| 172 | """ get ground truth filenames """ |
---|
| 173 | from os.path import isfile |
---|
| 174 | ftrulist = [] |
---|
| 175 | # search for match as filetask.input,".txt" |
---|
| 176 | ftru = '.'.join(input.split('.')[:-1]) |
---|
| 177 | ftru = '.'.join((ftru,'txt')) |
---|
| 178 | if isfile(ftru): |
---|
| 179 | ftrulist.append(ftru) |
---|
| 180 | else: |
---|
| 181 | # search for matches for filetask.input in the list of results |
---|
| 182 | for i in range(len(self.reslist)): |
---|
| 183 | check = '.'.join(self.reslist[i].split('.')[:-1]) |
---|
| 184 | check = '_'.join(check.split('_')[:-1]) |
---|
| 185 | if check == '.'.join(input.split('.')[:-1]): |
---|
| 186 | ftrulist.append(self.reslist[i]) |
---|
| 187 | return ftrulist |
---|
| 188 | |
---|
| 189 | def file_exec(self,input,output): |
---|
| 190 | """ create filetask, extract data, evaluate """ |
---|
| 191 | filetask = self.task(input,params=self.params) |
---|
| 192 | computed_data = filetask.compute_all() |
---|
| 193 | ftrulist = self.file_gettruth(filetask.input) |
---|
| 194 | for i in ftrulist: |
---|
| 195 | filetask.eval(computed_data,i,mode='rocloc',vmode='') |
---|
| 196 | """ append filetask.v to self.v """ |
---|
| 197 | for i in self.valuenames: |
---|
| 198 | self.v[i].append(filetask.v[i]) |
---|
| 199 | for j in self.valuelists: |
---|
| 200 | if filetask.v[j]: |
---|
| 201 | for i in range(len(filetask.v[j])): |
---|
| 202 | self.v[j].append(filetask.v[j][i]) |
---|
[50e99cc] | 203 | |
---|
| 204 | def file_eval(self): |
---|
| 205 | pass |
---|
| 206 | |
---|
| 207 | def file_plot(self): |
---|
| 208 | pass |
---|
| 209 | |
---|
| 210 | def dir_plot(self): |
---|
| 211 | pass |
---|
| 212 | |
---|
| 213 | def run_bench(self): |
---|
| 214 | for mode in self.modes: |
---|
| 215 | self.params.mode = mode |
---|
| 216 | self.dir_exec() |
---|
| 217 | self.dir_eval() |
---|
| 218 | self.dir_plot() |
---|
[a7880d9] | 219 | |
---|
| 220 | def dir_eval_print(self): |
---|
| 221 | self.dir_exec() |
---|
| 222 | self.dir_eval() |
---|
| 223 | self.pretty_print() |
---|
| 224 | |
---|