Mercurial > hg > multiomr
comparison Result/ExcellData.py @ 2:46fb79167a61 tip
Main Code
author | Victor Padilla <victor.padilla.mc@gmail.com> |
---|---|
date | Mon, 04 May 2015 22:56:18 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:0f7f611deca4 | 2:46fb79167a61 |
---|---|
1 ''' | |
2 Created on 10/11/2014 | |
3 | |
4 @organization: Lancaster University & University of Leeds | |
5 @version: 1.0 | |
6 Created on 11/12/2014 | |
7 | |
8 @author: Victor Padilla | |
9 @contact: v.padilla@lancaster.ac.uk | |
10 | |
11 Class for writing data in .xlsx format | |
12 It allows writing the results comparing with the ground in .xls | |
13 | |
14 ''' | |
15 import xlsxwriter | |
16 from openpyxl import load_workbook | |
17 | |
18 class ExcellData: | |
19 def saveData(self,data,files,percentages): | |
20 ''' | |
21 save the data from one part (one movement) | |
22 in the "result.xlsx" file | |
23 | |
24 usage: | |
25 | |
26 ed=ExcellData() | |
27 ff=FilesFunctions() | |
28 OMRs=ff.getOMRs(path) | |
29 for omr in OMRs: | |
30 OMRs_compare=[] | |
31 OMRs_compare.append(groundparsed) | |
32 OMRs_compare.append(omr) | |
33 | |
34 percentage,errors,scoreWithErrors= pg.getSimilarity(OMRs_compare,0) | |
35 ErrorsMatrix.append(errors) | |
36 percentages.append(percentage) | |
37 | |
38 ed.saveData(ErrorsMatrix,files,percentages) | |
39 ''' | |
40 | |
41 path=self.__getPathStepsBack(files[0],2) | |
42 wb=xlsxwriter.Workbook(path+'\\Result\\result.xlsx') | |
43 ws=wb.add_worksheet() | |
44 c=0 | |
45 ws.write(0, 0,str(files[0])) | |
46 | |
47 for col in data: | |
48 r=2 | |
49 ws.write(1, c,str(percentages[c])) | |
50 fArray=files[c].split('\\') | |
51 ws.write(r, c,str(fArray[-1])) | |
52 for row in col: | |
53 r+=1 | |
54 ws.write(r, c,str(row)) | |
55 c+=1 | |
56 wb.close() | |
57 | |
58 def saveGlobalData(self,data,path,betterOMRIds,files): | |
59 ''' | |
60 Takes the individual parts and store the result | |
61 in "resultGeneral.xlsx" | |
62 | |
63 usage: | |
64 ed=ExcellData() | |
65 ed.saveGlobalData(percentagesArray,dirGeneral,betterOMRIds,files) | |
66 | |
67 files=['file1.xml','file2.xml','file3.xml','file4.xml'] | |
68 | |
69 data=[[0.7 ,0.8, 0.5, 0.6] | |
70 [0.7 ,0.8, 0.5, 0.6] | |
71 [0.7 ,0.8, 0.5, 0.6]] | |
72 | |
73 betterOMRIds=[[0,1,2] | |
74 [2,3,1] | |
75 [1,0,2]] | |
76 | |
77 ed=ExcellData() | |
78 ed.saveGlobalData(percentagesArray,dirGeneral,betterOMRIds,files) | |
79 ''' | |
80 | |
81 | |
82 wb=xlsxwriter.Workbook(path+'\\parts\\resultGeneral.xlsx') | |
83 ws=wb.add_worksheet() | |
84 r=0 | |
85 ws.write(0, 1,str(files[0])) | |
86 print betterOMRIds | |
87 for row in data: | |
88 c=0 | |
89 myBetterOMR=betterOMRIds[r] | |
90 for col in row: | |
91 format1 = wb.add_format() | |
92 format1.set_font_color('black') | |
93 if str(c-1)+"\n" in myBetterOMR: | |
94 format1.set_font_color('red') | |
95 fArray=files[c].split('\\') | |
96 ws.write(0, c,str(fArray[-1])) | |
97 ws.write(r+1, c,float(col),format1) | |
98 c+=1 | |
99 r+=1 | |
100 wb.close() | |
101 | |
102 def processResultGeneral(self,f): | |
103 ''' | |
104 This function takes one xls file and get the percentage of accuracy of each omr | |
105 Returns in this order S2Out,CPOut,PSOut,SEOut,SSOut | |
106 ''' | |
107 wb = load_workbook(f) | |
108 ws = wb['Sheet1'] | |
109 rows=ws.iter_rows() | |
110 S2=0 | |
111 iS2=0 | |
112 CP=0 | |
113 iCP=0 | |
114 PS=0 | |
115 iPS=0 | |
116 SE=0 | |
117 iSE=0 | |
118 SS=0 | |
119 iSS=0 | |
120 indexRow=0 | |
121 headRow=[] | |
122 for row in rows: | |
123 if (indexRow==0): | |
124 headRow=row | |
125 else: | |
126 for cell in row: | |
127 col=row.index(cell) | |
128 strHead=headRow[col].value.upper() | |
129 if '.S2.XML' in strHead: | |
130 try: | |
131 S2+=cell.value | |
132 iS2+=1 | |
133 except: | |
134 print "error" | |
135 | |
136 if '.CP.XML' in strHead: | |
137 try: | |
138 CP+=cell.value | |
139 iCP+=1 | |
140 except: | |
141 print "error" | |
142 if '.PS.XML' in strHead: | |
143 try: | |
144 PS+=cell.value | |
145 iPS+=1 | |
146 except: | |
147 print "error" | |
148 if '.SE.XML' in strHead: | |
149 try: | |
150 SE+=cell.value | |
151 iSE+=1 | |
152 except: | |
153 print "error" | |
154 if '.SS.XML' in strHead: | |
155 try: | |
156 SS+=cell.value | |
157 iSS+=1 | |
158 except: | |
159 print "error" | |
160 indexRow+=1 | |
161 S2Out=0 | |
162 CPOut=0 | |
163 PSOut=0 | |
164 SEOut=0 | |
165 SSOut=0 | |
166 if iS2>0: | |
167 S2Out= S2/iS2 | |
168 if iCP>0: | |
169 CPOut= CP/iCP | |
170 if iPS>0: | |
171 PSOut=PS/iPS | |
172 if SE>0: | |
173 SEOut=SE/iSE | |
174 if SS>0: | |
175 SSOut=SS/iSS | |
176 return S2Out,CPOut,PSOut,SEOut,SSOut | |
177 | |
178 def writeFinalXLS(self, rootDir,S2,CP,PS,SE,SS): | |
179 ''' | |
180 Writes the final.xlsx upon the inputs S2,CP,PS,SE,SS | |
181 ''' | |
182 wb=xlsxwriter.Workbook(rootDir+'\\final.xlsx') | |
183 ws=wb.add_worksheet() | |
184 ws.write(0, 0,"S2") | |
185 ws.write(0, 1,"CP") | |
186 ws.write(0, 2,"PS") | |
187 ws.write(0, 3,"SE") | |
188 ws.write(0, 4,"SS") | |
189 ws.write(1, 0,S2) | |
190 ws.write(1, 1,CP) | |
191 ws.write(1, 2,PS) | |
192 ws.write(1, 3,SE) | |
193 ws.write(1, 4,SS) | |
194 | |
195 wb.close() | |
196 | |
197 def __getPathStepsBack(self,f,stepsBack): | |
198 ''' | |
199 Private function: takes n steps back in the path from a file | |
200 ''' | |
201 fArray=f.split('\\') | |
202 path='' | |
203 for i in range(len(fArray)-stepsBack): | |
204 path+=fArray[i]+"/" | |
205 return path | |
206 |