annotate Functions/FilesFunctions.py @ 2:46fb79167a61 tip

Main Code
author Victor Padilla <victor.padilla.mc@gmail.com>
date Mon, 04 May 2015 22:56:18 +0200
parents 0f7f611deca4
children
rev   line source
victor@1 1 '''
victor@1 2 @organization: Lancaster University & University of Leeds
victor@1 3 @version: 1.0
victor@1 4 Created on 11/12/2014
victor@1 5
victor@1 6 @author: Victor Padilla
victor@1 7 @contact: v.padilla@lancaster.ac.uk
victor@1 8
victor@1 9 Auxiliary functions to manipulate files and convert to music21
victor@1 10 for processing
victor@1 11
victor@1 12 '''
victor@1 13
victor@1 14 from music21 import converter
victor@1 15 import os
victor@1 16
victor@1 17 class FilesFunctions:
victor@1 18
victor@1 19 def getFiles(self,path):
victor@1 20 '''
victor@1 21 Takes the XML files in a folder excluding ground and results
victor@1 22 returns alphabetically
victor@1 23
victor@1 24 usage:
victor@1 25 ff=FilesFunctions()
victor@1 26 files=ff.getFiles("C:\\Users\\victor\\Desktop\\data\k458\Process\m2\parts\0\XML")
victor@1 27 '''
victor@1 28
victor@1 29 omr_files=[]
victor@1 30 dir_content = os.listdir(path)
victor@1 31 dir_content.sort()
victor@1 32 for myfile in dir_content:
victor@1 33 directory = os.path.join(path,myfile)
victor@1 34 extension = os.path.splitext(myfile)[1]
victor@1 35 if myfile.find("result.")==-1 and myfile!="ground.xml" and extension.upper()==".XML":
victor@1 36 omr_files.append(os.path.abspath(directory))
victor@1 37 return omr_files
victor@1 38
victor@1 39 def getAllFiles(self,path):
victor@1 40 '''
victor@1 41 Takes all the files in a folder
victor@1 42 returns alphabetically
victor@1 43
victor@1 44 usage:
victor@1 45 ff=FilesFunctions()
victor@1 46 files=ff.getAllFiles("C:\\Users\\victor\\Desktop\\data\k458\Process\m2\parts\0\XML")
victor@1 47 '''
victor@1 48 omr_files=[]
victor@1 49 dir_content = os.listdir(path)
victor@1 50 dir_content.sort()
victor@1 51 for myfile in dir_content:
victor@1 52 directory = os.path.join(path,myfile)
victor@1 53 omr_files.append(os.path.abspath(directory))
victor@1 54 return omr_files
victor@1 55
victor@1 56
victor@1 57 def SubDirPath (self,d):
victor@1 58 '''
victor@1 59 Returns the directories from a path
victor@1 60
victor@1 61 usage:
victor@1 62 ff=FilesFunctions()
victor@1 63 subdirArray=ff.SubDirPath("C:\\Users\\victor\\Desktop\\data\k458\Process\m2\parts")
victor@1 64
victor@1 65 #returns ["C:\\Users\\victor\\Desktop\\data\k458\Process\m2\parts\1",
victor@1 66 "C:\\Users\\victor\\Desktop\\data\k458\Process\m2\parts\2"....]
victor@1 67 '''
victor@1 68 return filter(os.path.isdir, [os.path.join(d,f) for f in os.listdir(d)])
victor@1 69
victor@1 70
victor@1 71 def getGround(self,path):
victor@1 72 '''
victor@1 73 Returns the ground file from a path
victor@1 74
victor@1 75 usage:
victor@1 76 ff=FilesFunctions()
victor@1 77 ground=ff.getGround("C:\\Users\\victor\\Desktop\\data\\k458\\Process")
victor@1 78 '''
victor@1 79 dir_content = os.listdir(path)
victor@1 80 dir_content.sort()
victor@1 81 for myfile in dir_content:
victor@1 82 if myfile=="ground.xml":
victor@1 83 directoryFile = os.path.join(path,myfile)
victor@1 84 return directoryFile
victor@1 85
victor@1 86
victor@1 87 def getFinalScore(self,path):
victor@1 88 '''
victor@1 89 Returns the finalScore.xml file from a path
victor@1 90
victor@1 91 usage:
victor@1 92 ff=FilesFunctions()
victor@1 93 finalScore=ff.getFinalScore("C:\\Users\\victor\\Desktop\\data\\k458\\Process")
victor@1 94 '''
victor@1 95 dir_content = os.listdir(path)
victor@1 96 dir_content.sort()
victor@1 97 for myfile in dir_content:
victor@1 98 if myfile=="finalScore.xml":
victor@1 99 directoryFile = os.path.join(path,myfile)
victor@1 100 return directoryFile
victor@1 101
victor@1 102 def writeText(self,path,betterOmrIds):
victor@1 103 '''
victor@1 104 Writes betterOMR.txt file with the number of the OMR files chosen in the phylogenetic tree
victor@1 105
victor@1 106 usage:
victor@1 107 ff=FilesFunctions()
victor@1 108 betterOmrIds=[2,3,5]
victor@1 109 ff.writeText("C:\\Users\\victor\\Desktop\\data\\k458\\Process",betterOmrIds)
victor@1 110 '''
victor@1 111 f=open(path+"\\betterOMR.txt","w")
victor@1 112 for idOMR in betterOmrIds:
victor@1 113 f.write(str(idOMR)+"\n")
victor@1 114 f.close()
victor@1 115
victor@1 116 def getOMRs(self,path):
victor@1 117 '''
victor@1 118 Takes the different .xml in a folder and convert them to music21
victor@1 119 Returns an array with the files processed
victor@1 120
victor@1 121 ff=FilesFunctions()
victor@1 122 OMRs=ff.getOMRs("C:\\Users\\victor\\Desktop\\data\\k458\\Process\\m2\parts\0\XML",)
victor@1 123 '''
victor@1 124 fsFiles=self.getFiles(path)
victor@1 125 OMRs=[]
victor@1 126 for f in fsFiles:
victor@1 127 try:
victor@1 128 print f
victor@1 129 OMRs.append(converter.parse(f, forceSource=True))
victor@1 130 except:
victor@1 131 OMRs.append([])
victor@1 132 print "OMR error"
victor@1 133 return OMRs
victor@1 134
victor@1 135 def getOMR(self,f):
victor@1 136 '''
victor@1 137 Takes the .xml file in a folder and convert them to music21
victor@1 138
victor@1 139 '''
victor@1 140 OMR=[]
victor@1 141 try:
victor@1 142 print f
victor@1 143 OMR=converter.parse(f, forceSource=True)
victor@1 144 except:
victor@1 145 OMR=[]
victor@1 146 print "OMR error"
victor@1 147 return OMR
victor@1 148 def getAllImgFiles(self,d):
victor@1 149 '''
victor@1 150 Returns all the .tif files from a directory ordered alphabetically
victor@1 151
victor@1 152 usage:
victor@1 153 ff=FilesFunctions()
victor@1 154 imageFiles=ff.getAllImgFiles("C:\\Users\\victor\\Desktop\\data\\k458\\OMRS\\m2\parts\Peters\0\XML",)
victor@1 155 '''
victor@1 156 files=[]
victor@1 157 dir_content = os.listdir(d)
victor@1 158 dir_content.sort()
victor@1 159 for myfile in dir_content:
victor@1 160 if myfile.endswith('.tif'):
victor@1 161 files.append(myfile)
victor@1 162 return files
victor@1 163
victor@1 164 def getKernFile(self,d):
victor@1 165 '''
victor@1 166 Returns the first .krn file from a directory ordered alphabetically
victor@1 167
victor@1 168 usage:
victor@1 169 ff=FilesFunctions()
victor@1 170 kernFile=ff.getKernFile("C:\\Users\\victor\\Desktop\\data\\k458\\Process\\m2\parts\Peters\0\XML",)
victor@1 171 '''
victor@1 172 dir_content = os.listdir(d)
victor@1 173 dir_content.sort()
victor@1 174 for myfile in dir_content:
victor@1 175 if myfile.endswith('.krn'):
victor@1 176 return myfile
victor@1 177 def getXMLFile(self,d):
victor@1 178 '''
victor@1 179 Returns the first .xml file from a directory ordered alphabetically
victor@1 180
victor@1 181 usage:
victor@1 182 ff=FilesFunctions()
victor@1 183 xmlFile=ff.getXMLFile("C:\\Users\\victor\\Desktop\\data\\k458\\Process\\m2\parts\Peters\0\XML",)
victor@1 184 '''
victor@1 185 dir_content = os.listdir(d)
victor@1 186 dir_content.sort()
victor@1 187 for myfile in dir_content:
victor@1 188 if myfile.endswith('.xml'):
victor@1 189 return myfile
victor@1 190
victor@1 191 def getAllXMLFiles(self,d):
victor@1 192 '''
victor@1 193 Returns the .xml files from a directory ordered alphabetically
victor@1 194
victor@1 195 usage:
victor@1 196 ff=FilesFunctions()
victor@1 197 xmlFiles=ff.getAllXMLFiles("C:\\Users\\victor\\Desktop\\data\\k458\\OMRS\\m2\parts\Peters\0\XML",)
victor@1 198 '''
victor@1 199 files=[]
victor@1 200 dir_content = os.listdir(d)
victor@1 201 dir_content.sort()
victor@1 202 for myfile in dir_content:
victor@1 203 if myfile.endswith('.xml'):
victor@1 204 files.append(myfile)
victor@1 205 return files
victor@1 206