victor@1: ''' victor@1: @organization: Lancaster University & University of Leeds victor@1: @version: 1.0 victor@1: Created on 11/12/2014 victor@1: victor@1: @author: Victor Padilla victor@1: @contact: v.padilla@lancaster.ac.uk victor@1: victor@1: Auxiliary functions to manipulate files and convert to music21 victor@1: for processing victor@1: victor@1: ''' victor@1: victor@1: from music21 import converter victor@1: import os victor@1: victor@1: class FilesFunctions: victor@1: victor@1: def getFiles(self,path): victor@1: ''' victor@1: Takes the XML files in a folder excluding ground and results victor@1: returns alphabetically victor@1: victor@1: usage: victor@1: ff=FilesFunctions() victor@1: files=ff.getFiles("C:\\Users\\victor\\Desktop\\data\k458\Process\m2\parts\0\XML") victor@1: ''' victor@1: victor@1: omr_files=[] victor@1: dir_content = os.listdir(path) victor@1: dir_content.sort() victor@1: for myfile in dir_content: victor@1: directory = os.path.join(path,myfile) victor@1: extension = os.path.splitext(myfile)[1] victor@1: if myfile.find("result.")==-1 and myfile!="ground.xml" and extension.upper()==".XML": victor@1: omr_files.append(os.path.abspath(directory)) victor@1: return omr_files victor@1: victor@1: def getAllFiles(self,path): victor@1: ''' victor@1: Takes all the files in a folder victor@1: returns alphabetically victor@1: victor@1: usage: victor@1: ff=FilesFunctions() victor@1: files=ff.getAllFiles("C:\\Users\\victor\\Desktop\\data\k458\Process\m2\parts\0\XML") victor@1: ''' victor@1: omr_files=[] victor@1: dir_content = os.listdir(path) victor@1: dir_content.sort() victor@1: for myfile in dir_content: victor@1: directory = os.path.join(path,myfile) victor@1: omr_files.append(os.path.abspath(directory)) victor@1: return omr_files victor@1: victor@1: victor@1: def SubDirPath (self,d): victor@1: ''' victor@1: Returns the directories from a path victor@1: victor@1: usage: victor@1: ff=FilesFunctions() victor@1: subdirArray=ff.SubDirPath("C:\\Users\\victor\\Desktop\\data\k458\Process\m2\parts") victor@1: victor@1: #returns ["C:\\Users\\victor\\Desktop\\data\k458\Process\m2\parts\1", victor@1: "C:\\Users\\victor\\Desktop\\data\k458\Process\m2\parts\2"....] victor@1: ''' victor@1: return filter(os.path.isdir, [os.path.join(d,f) for f in os.listdir(d)]) victor@1: victor@1: victor@1: def getGround(self,path): victor@1: ''' victor@1: Returns the ground file from a path victor@1: victor@1: usage: victor@1: ff=FilesFunctions() victor@1: ground=ff.getGround("C:\\Users\\victor\\Desktop\\data\\k458\\Process") victor@1: ''' victor@1: dir_content = os.listdir(path) victor@1: dir_content.sort() victor@1: for myfile in dir_content: victor@1: if myfile=="ground.xml": victor@1: directoryFile = os.path.join(path,myfile) victor@1: return directoryFile victor@1: victor@1: victor@1: def getFinalScore(self,path): victor@1: ''' victor@1: Returns the finalScore.xml file from a path victor@1: victor@1: usage: victor@1: ff=FilesFunctions() victor@1: finalScore=ff.getFinalScore("C:\\Users\\victor\\Desktop\\data\\k458\\Process") victor@1: ''' victor@1: dir_content = os.listdir(path) victor@1: dir_content.sort() victor@1: for myfile in dir_content: victor@1: if myfile=="finalScore.xml": victor@1: directoryFile = os.path.join(path,myfile) victor@1: return directoryFile victor@1: victor@1: def writeText(self,path,betterOmrIds): victor@1: ''' victor@1: Writes betterOMR.txt file with the number of the OMR files chosen in the phylogenetic tree victor@1: victor@1: usage: victor@1: ff=FilesFunctions() victor@1: betterOmrIds=[2,3,5] victor@1: ff.writeText("C:\\Users\\victor\\Desktop\\data\\k458\\Process",betterOmrIds) victor@1: ''' victor@1: f=open(path+"\\betterOMR.txt","w") victor@1: for idOMR in betterOmrIds: victor@1: f.write(str(idOMR)+"\n") victor@1: f.close() victor@1: victor@1: def getOMRs(self,path): victor@1: ''' victor@1: Takes the different .xml in a folder and convert them to music21 victor@1: Returns an array with the files processed victor@1: victor@1: ff=FilesFunctions() victor@1: OMRs=ff.getOMRs("C:\\Users\\victor\\Desktop\\data\\k458\\Process\\m2\parts\0\XML",) victor@1: ''' victor@1: fsFiles=self.getFiles(path) victor@1: OMRs=[] victor@1: for f in fsFiles: victor@1: try: victor@1: print f victor@1: OMRs.append(converter.parse(f, forceSource=True)) victor@1: except: victor@1: OMRs.append([]) victor@1: print "OMR error" victor@1: return OMRs victor@1: victor@1: def getOMR(self,f): victor@1: ''' victor@1: Takes the .xml file in a folder and convert them to music21 victor@1: victor@1: ''' victor@1: OMR=[] victor@1: try: victor@1: print f victor@1: OMR=converter.parse(f, forceSource=True) victor@1: except: victor@1: OMR=[] victor@1: print "OMR error" victor@1: return OMR victor@1: def getAllImgFiles(self,d): victor@1: ''' victor@1: Returns all the .tif files from a directory ordered alphabetically victor@1: victor@1: usage: victor@1: ff=FilesFunctions() victor@1: imageFiles=ff.getAllImgFiles("C:\\Users\\victor\\Desktop\\data\\k458\\OMRS\\m2\parts\Peters\0\XML",) victor@1: ''' victor@1: files=[] victor@1: dir_content = os.listdir(d) victor@1: dir_content.sort() victor@1: for myfile in dir_content: victor@1: if myfile.endswith('.tif'): victor@1: files.append(myfile) victor@1: return files victor@1: victor@1: def getKernFile(self,d): victor@1: ''' victor@1: Returns the first .krn file from a directory ordered alphabetically victor@1: victor@1: usage: victor@1: ff=FilesFunctions() victor@1: kernFile=ff.getKernFile("C:\\Users\\victor\\Desktop\\data\\k458\\Process\\m2\parts\Peters\0\XML",) victor@1: ''' victor@1: dir_content = os.listdir(d) victor@1: dir_content.sort() victor@1: for myfile in dir_content: victor@1: if myfile.endswith('.krn'): victor@1: return myfile victor@1: def getXMLFile(self,d): victor@1: ''' victor@1: Returns the first .xml file from a directory ordered alphabetically victor@1: victor@1: usage: victor@1: ff=FilesFunctions() victor@1: xmlFile=ff.getXMLFile("C:\\Users\\victor\\Desktop\\data\\k458\\Process\\m2\parts\Peters\0\XML",) victor@1: ''' victor@1: dir_content = os.listdir(d) victor@1: dir_content.sort() victor@1: for myfile in dir_content: victor@1: if myfile.endswith('.xml'): victor@1: return myfile victor@1: victor@1: def getAllXMLFiles(self,d): victor@1: ''' victor@1: Returns the .xml files from a directory ordered alphabetically victor@1: victor@1: usage: victor@1: ff=FilesFunctions() victor@1: xmlFiles=ff.getAllXMLFiles("C:\\Users\\victor\\Desktop\\data\\k458\\OMRS\\m2\parts\Peters\0\XML",) victor@1: ''' victor@1: files=[] victor@1: dir_content = os.listdir(d) victor@1: dir_content.sort() victor@1: for myfile in dir_content: victor@1: if myfile.endswith('.xml'): victor@1: files.append(myfile) victor@1: return files victor@1: