comparison Functions/FilesFunctions.py @ 1:0f7f611deca4

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