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 |
---|
17 | #sys.exit(1) |
---|
18 | if output == '' or output == ['']: return |
---|
19 | return output |
---|
20 | |
---|
21 | def list_files(datapath,filter='f', maxdepth = -1): |
---|
22 | if not os.path.exists(datapath): |
---|
23 | print |
---|
24 | print "ERR: no directory %s were found" % datapath |
---|
25 | sys.exit(1) |
---|
26 | if maxdepth >= 0: maxstring = " -maxdepth %d " % maxdepth |
---|
27 | else: maxstring = "" |
---|
28 | cmd = '%s' * 5 % ('find ',datapath,maxstring,' -type ',filter) |
---|
29 | return runcommand(cmd) |
---|
30 | |
---|
31 | def list_wav_files(datapath,maxdepth = -1): |
---|
32 | return list_files(datapath, filter="f -name '*.wav'",maxdepth = maxdepth) |
---|
33 | |
---|
34 | sndfile_filter = "f -name '*.wav' -o -name '*.aif' -o -name '*.aiff'" |
---|
35 | |
---|
36 | def list_snd_files(datapath,maxdepth = -1): |
---|
37 | return list_files(datapath, filter=sndfile_filter, |
---|
38 | maxdepth = maxdepth) |
---|
39 | |
---|
40 | def list_res_files(datapath,maxdepth = -1): |
---|
41 | return list_files(datapath, filter="f -name '*.txt'", maxdepth = maxdepth) |
---|
42 | |
---|
43 | def list_dirs(datapath): |
---|
44 | return list_files(datapath, filter="d") |
---|
45 | |
---|
46 | def mkdir(path): |
---|
47 | cmd = '%s%s' % ('mkdir -p ',path) |
---|
48 | return runcommand(cmd) |
---|
49 | |
---|
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 = [] |
---|
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' |
---|
58 | for i in dirlist: |
---|
59 | j = re.split(datapath, i,maxsplit=1)[1] |
---|
60 | j = re.sub(sub,'',j) |
---|
61 | #j = "%s%s%s"%(respath,j,suffix) |
---|
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) |
---|
68 | action(i,j,**keywords) |
---|
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)) |
---|
79 | |
---|
80 | class bench: |
---|
81 | """ class to run benchmarks on directories """ |
---|
82 | def __init__(self,datadir,resdir=None,checkres=False,checkanno=False): |
---|
83 | self.datadir = datadir |
---|
84 | self.resdir = resdir |
---|
85 | self.results = [] |
---|
86 | print "Checking data directory", self.datadir |
---|
87 | self.checkdata() |
---|
88 | if checkanno: self.checkanno() |
---|
89 | if checkres: self.checkres() |
---|
90 | |
---|
91 | def checkdata(self): |
---|
92 | print "Listing directories in data directory", |
---|
93 | self.dirlist = list_dirs(self.datadir) |
---|
94 | if self.dirlist: |
---|
95 | print " (%d elements)" % len(self.dirlist) |
---|
96 | else: |
---|
97 | print " (0 elements)" |
---|
98 | print "ERR: no directory %s were found" % self.datadir |
---|
99 | sys.exit(1) |
---|
100 | print "Listing sound files in data directory", |
---|
101 | self.sndlist = list_snd_files(self.datadir) |
---|
102 | if self.sndlist: |
---|
103 | print " (%d elements)" % len(self.sndlist) |
---|
104 | else: |
---|
105 | print " (0 elements)" |
---|
106 | print "ERR: no sound files were found in", self.datadir |
---|
107 | sys.exit(1) |
---|
108 | #for each in self.sndlist: print each |
---|
109 | |
---|
110 | def checkanno(self): |
---|
111 | print "Listing annotations in data directory", |
---|
112 | self.reslist = list_res_files(self.datadir) |
---|
113 | print " (%d elements)" % len(self.reslist) |
---|
114 | #for each in self.reslist: print each |
---|
115 | if not self.reslist or len(self.reslist) < len (self.sndlist): |
---|
116 | print "ERR: not enough annotations" |
---|
117 | return -1 |
---|
118 | else: |
---|
119 | print "Found enough annotations" |
---|
120 | |
---|
121 | def checkres(self): |
---|
122 | print "Creating results directory" |
---|
123 | act_on_results(mkdir,self.datadir,self.resdir,filter='d') |
---|
124 | |
---|
125 | def pretty_print(self,values): |
---|
126 | for i in range(len(values)): |
---|
127 | print self.formats[i] % values[i], |
---|
128 | print |
---|
129 | |
---|
130 | def dir_exec(self): |
---|
131 | """ run file_exec on every input file """ |
---|
132 | self.orig, self.missed, self.merged, self.expc, \ |
---|
133 | self.bad, self.doubled = 0, 0, 0, 0, 0, 0 |
---|
134 | act_on_data(self.file_exec,self.datadir,self.resdir, \ |
---|
135 | suffix='',filter=sndfile_filter) |
---|
136 | |
---|
137 | def dir_eval(self): |
---|
138 | pass |
---|
139 | |
---|
140 | def file_exec(self): |
---|
141 | pass |
---|
142 | |
---|
143 | def file_eval(self): |
---|
144 | pass |
---|
145 | |
---|
146 | def file_plot(self): |
---|
147 | pass |
---|
148 | |
---|
149 | def dir_plot(self): |
---|
150 | pass |
---|
151 | |
---|
152 | def run_bench(self): |
---|
153 | for mode in self.modes: |
---|
154 | self.params.mode = mode |
---|
155 | self.dir_exec() |
---|
156 | self.dir_eval() |
---|
157 | self.dir_plot() |
---|