[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): |
---|
| 22 | if maxdepth >= 0: maxstring = " -maxdepth %d " % maxdepth |
---|
| 23 | else: maxstring = "" |
---|
| 24 | cmd = '%s' * 5 % ('find ',datapath,maxstring,' -type ',filter) |
---|
[4cc9fe5] | 25 | return runcommand(cmd) |
---|
| 26 | |
---|
[257debc] | 27 | def list_wav_files(datapath,maxdepth = -1): |
---|
| 28 | return list_files(datapath, filter="f -name '*.wav'",maxdepth = maxdepth) |
---|
[d45520a] | 29 | |
---|
[257debc] | 30 | def list_snd_files(datapath,maxdepth = -1): |
---|
| 31 | return list_files(datapath, filter="f -name '*.wav' -o -name '*.aif'", |
---|
| 32 | maxdepth = maxdepth) |
---|
| 33 | |
---|
| 34 | def list_res_files(datapath,maxdepth = -1): |
---|
| 35 | return list_files(datapath, filter="f -name '*.txt'", maxdepth = maxdepth) |
---|
[2bd1a2a] | 36 | |
---|
[d45520a] | 37 | def list_dirs(datapath): |
---|
| 38 | return list_files(datapath, filter="d") |
---|
| 39 | |
---|
[4cc9fe5] | 40 | def mkdir(path): |
---|
| 41 | cmd = '%s%s' % ('mkdir -p ',path) |
---|
| 42 | return runcommand(cmd) |
---|
| 43 | |
---|
[50791b3] | 44 | def act_on_data (action,datapath,respath=None,suffix='.txt',filter='f',sub='\.wav$',**keywords): |
---|
[4cc9fe5] | 45 | """ execute action(datafile,resfile) on all files in datapath """ |
---|
| 46 | dirlist = list_files(datapath,filter=filter) |
---|
[d45520a] | 47 | if dirlist == ['']: dirlist = [] |
---|
[50791b3] | 48 | if respath: |
---|
| 49 | respath_in_datapath = re.split(datapath, respath,maxsplit=1)[1:] |
---|
| 50 | if(respath_in_datapath and suffix == ''): |
---|
| 51 | print 'error: respath in datapath and no suffix used' |
---|
[4cc9fe5] | 52 | for i in dirlist: |
---|
[d45520a] | 53 | j = re.split(datapath, i,maxsplit=1)[1] |
---|
| 54 | j = re.sub(sub,'',j) |
---|
| 55 | #j = "%s%s%s"%(respath,j,suffix) |
---|
[50791b3] | 56 | if respath: |
---|
| 57 | j = "%s%s"%(respath,j) |
---|
| 58 | if sub != '': |
---|
| 59 | j = re.sub(sub,suffix,j) |
---|
| 60 | else: |
---|
| 61 | j = "%s%s" % (j,suffix) |
---|
[d45520a] | 62 | action(i,j,**keywords) |
---|
[4cc9fe5] | 63 | |
---|
| 64 | def act_on_results (action,datapath,respath,filter='d'): |
---|
| 65 | """ execute action(respath) an all subdirectories in respath """ |
---|
| 66 | dirlist = list_files(datapath,filter='d') |
---|
| 67 | respath_in_datapath = re.split(datapath, respath,maxsplit=1)[1:] |
---|
| 68 | if(respath_in_datapath and not filter == 'd' and suffix == ''): |
---|
| 69 | print 'warning: respath is in datapath' |
---|
| 70 | for i in dirlist: |
---|
| 71 | s = re.split(datapath, i ,maxsplit=1)[1] |
---|
| 72 | action("%s%s%s"%(respath,'/',s)) |
---|
[257debc] | 73 | |
---|
[336cf77] | 74 | class bench: |
---|
[257debc] | 75 | |
---|
[50791b3] | 76 | def __init__(self,datadir,resdir=None,checkres=False,checkanno=False): |
---|
[257debc] | 77 | self.datadir = datadir |
---|
| 78 | self.resdir = resdir |
---|
[50791b3] | 79 | self.results = [] |
---|
[257debc] | 80 | print "Checking data directory", self.datadir |
---|
| 81 | self.checkdata() |
---|
[336cf77] | 82 | if checkanno: self.checkanno() |
---|
| 83 | if checkres: self.checkres() |
---|
[257debc] | 84 | |
---|
| 85 | def checkdata(self): |
---|
| 86 | print "Listing directories in data directory", |
---|
| 87 | self.dirlist = list_dirs(self.datadir) |
---|
| 88 | print " (%d elements)" % len(self.dirlist) |
---|
| 89 | #for each in self.dirlist: print each |
---|
| 90 | print "Listing sound files in data directory", |
---|
| 91 | self.sndlist = list_snd_files(self.datadir) |
---|
| 92 | print " (%d elements)" % len(self.sndlist) |
---|
| 93 | #for each in self.sndlist: print each |
---|
[336cf77] | 94 | |
---|
| 95 | def checkanno(self): |
---|
[257debc] | 96 | print "Listing annotations in data directory", |
---|
| 97 | self.reslist = list_res_files(self.datadir) |
---|
| 98 | print " (%d elements)" % len(self.reslist) |
---|
| 99 | #for each in self.reslist: print each |
---|
| 100 | if not self.reslist or len(self.reslist) < len (self.sndlist): |
---|
| 101 | print "ERR: not enough annotations" |
---|
| 102 | return -1 |
---|
| 103 | else: |
---|
| 104 | print "Found enough annotations" |
---|
| 105 | |
---|
| 106 | def checkres(self): |
---|
| 107 | print "Creating results directory" |
---|
| 108 | act_on_results(mkdir,self.datadir,self.resdir,filter='d') |
---|
[336cf77] | 109 | |
---|
| 110 | def pretty_print(self,values): |
---|
| 111 | for i in range(len(values)): |
---|
| 112 | print self.formats[i] % values[i], |
---|
| 113 | print |
---|
| 114 | |
---|