5 def runcommand(cmd,debug=0):
6 if VERBOSE >= VERBOSE_CMD or debug: print cmd
8 status, output = commands.getstatusoutput(cmd)
9 if status == 0 or VERBOSE >= VERBOSE_OUT:
10 output = output.split('\n')
11 if VERBOSE >= VERBOSE_OUT:
15 print 'error:',status,output
16 print 'command returning error was',cmd
18 if output == '' or output == ['']: return
21 def list_files(datapath,filter='f', maxdepth = -1):
22 if not os.path.exists(datapath):
24 print "ERR: no directory %s were found" % datapath
26 if maxdepth >= 0: maxstring = " -maxdepth %d " % maxdepth
28 cmd = '%s' * 5 % ('find ',datapath,maxstring,' -type ',filter)
29 return runcommand(cmd)
31 def list_wav_files(datapath,maxdepth = -1):
32 return list_files(datapath, filter="f -name '*.wav'",maxdepth = maxdepth)
34 sndfile_filter = "f -name '*.wav' -o -name '*.aif' -o -name '*.aiff'"
36 def list_snd_files(datapath,maxdepth = -1):
37 return list_files(datapath, filter=sndfile_filter,
40 def list_res_files(datapath,maxdepth = -1):
41 return list_files(datapath, filter="f -name '*.txt'", maxdepth = maxdepth)
43 def list_dirs(datapath):
44 return list_files(datapath, filter="d")
47 cmd = '%s%s' % ('mkdir -p ',path)
48 return runcommand(cmd)
50 def act_on_data (action,datapath,respath=None,suffix='.txt',filter='f',sub='\.wav$',**keywords):
51 """ execute action(datafile,resfile) on all files in datapath """
52 dirlist = list_files(datapath,filter=filter)
53 if dirlist == ['']: dirlist = []
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'
59 j = re.split(datapath, i,maxsplit=1)[1]
61 #j = "%s%s%s"%(respath,j,suffix)
63 j = "%s%s"%(respath,j)
65 j = re.sub(sub,suffix,j)
67 j = "%s%s" % (j,suffix)
68 action(i,j,**keywords)
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'
77 s = re.split(datapath, i ,maxsplit=1)[1]
78 action("%s%s%s"%(respath,'/',s))
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):
83 for i in range(len(listfiles)):
84 action(listfiles[i],listres[i],**keywords)
87 action(i,None,**keywords)
90 """ class to run benchmarks on directories """
91 def __init__(self,datadir,resdir=None,checkres=False,checkanno=False):
92 self.datadir = datadir
93 # path to write results path to
95 # list of annotation files
97 # list used to gather results
99 print "Checking data directory", self.datadir
101 if checkanno: self.checkanno()
102 if checkres: self.checkres()
105 if os.path.isfile(self.datadir):
106 self.dirlist = os.path.dirname(self.datadir)
107 print "DBG: found a file"
108 elif os.path.isdir(self.datadir):
109 self.dirlist = list_dirs(self.datadir)
110 print "DBG: found a dir"
111 # allow dir* matching through find commands?
113 print "ERR: path not understood"
115 print "Listing directories in data directory",
117 print " (%d elements)" % len(self.dirlist)
119 print " (0 elements)"
120 print "ERR: no directory %s were found" % self.datadir
122 print "Listing sound files in data directory",
123 self.sndlist = list_snd_files(self.datadir)
125 print " (%d elements)" % len(self.sndlist)
127 print " (0 elements)"
128 print "ERR: no sound files were found in", self.datadir
132 print "Listing annotations in data directory",
133 self.reslist = list_res_files(self.datadir)
134 print " (%d elements)" % len(self.reslist)
135 #for each in self.reslist: print each
136 if not self.reslist or len(self.reslist) < len (self.sndlist):
137 print "ERR: not enough annotations"
140 print "Found enough annotations"
143 print "Creating results directory"
144 act_on_results(mkdir,self.datadir,self.resdir,filter='d')
146 def pretty_print(self,sep='|'):
147 for i in self.printnames:
148 print self.formats[i] % self.v[i], sep,
151 def pretty_titles(self,sep='|'):
152 for i in self.printnames:
153 print self.formats[i] % i, sep,
157 """ run file_exec on every input file """
158 self.l , self.labs = [], []
160 for i in self.valuenames:
162 for i in self.valuelists:
164 act_on_files(self.file_exec,self.sndlist,self.reslist, \
165 suffix='',filter=sndfile_filter)
170 def file_gettruth(self,input):
171 """ get ground truth filenames """
172 from os.path import isfile
174 # search for match as filetask.input,".txt"
175 ftru = '.'.join(input.split('.')[:-1])
176 ftru = '.'.join((ftru,'txt'))
178 ftrulist.append(ftru)
180 # search for matches for filetask.input in the list of results
181 for i in range(len(self.reslist)):
182 check = '.'.join(self.reslist[i].split('.')[:-1])
183 check = '_'.join(check.split('_')[:-1])
184 if check == '.'.join(input.split('.')[:-1]):
185 ftrulist.append(self.reslist[i])
188 def file_exec(self,input,output):
189 """ create filetask, extract data, evaluate """
190 filetask = self.task(input,params=self.params)
191 computed_data = filetask.compute_all()
192 ftrulist = self.file_gettruth(filetask.input)
194 filetask.eval(computed_data,i,mode='rocloc',vmode='')
195 """ append filetask.v to self.v """
196 for i in self.valuenames:
197 self.v[i].append(filetask.v[i])
198 for j in self.valuelists:
200 for i in range(len(filetask.v[j])):
201 self.v[j].append(filetask.v[j][i])
213 for mode in self.modes:
214 self.params.mode = mode