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