victor@2: ''' victor@2: Created on 10/11/2014 victor@2: victor@2: @organization: Lancaster University & University of Leeds victor@2: @version: 1.0 victor@2: Created on 11/12/2014 victor@2: victor@2: @author: Victor Padilla victor@2: @contact: v.padilla@lancaster.ac.uk victor@2: victor@2: Class for writing data in .xlsx format victor@2: It allows writing the results comparing with the ground in .xls victor@2: victor@2: ''' victor@2: import xlsxwriter victor@2: from openpyxl import load_workbook victor@2: victor@2: class ExcellData: victor@2: def saveData(self,data,files,percentages): victor@2: ''' victor@2: save the data from one part (one movement) victor@2: in the "result.xlsx" file victor@2: victor@2: usage: victor@2: victor@2: ed=ExcellData() victor@2: ff=FilesFunctions() victor@2: OMRs=ff.getOMRs(path) victor@2: for omr in OMRs: victor@2: OMRs_compare=[] victor@2: OMRs_compare.append(groundparsed) victor@2: OMRs_compare.append(omr) victor@2: victor@2: percentage,errors,scoreWithErrors= pg.getSimilarity(OMRs_compare,0) victor@2: ErrorsMatrix.append(errors) victor@2: percentages.append(percentage) victor@2: victor@2: ed.saveData(ErrorsMatrix,files,percentages) victor@2: ''' victor@2: victor@2: path=self.__getPathStepsBack(files[0],2) victor@2: wb=xlsxwriter.Workbook(path+'\\Result\\result.xlsx') victor@2: ws=wb.add_worksheet() victor@2: c=0 victor@2: ws.write(0, 0,str(files[0])) victor@2: victor@2: for col in data: victor@2: r=2 victor@2: ws.write(1, c,str(percentages[c])) victor@2: fArray=files[c].split('\\') victor@2: ws.write(r, c,str(fArray[-1])) victor@2: for row in col: victor@2: r+=1 victor@2: ws.write(r, c,str(row)) victor@2: c+=1 victor@2: wb.close() victor@2: victor@2: def saveGlobalData(self,data,path,betterOMRIds,files): victor@2: ''' victor@2: Takes the individual parts and store the result victor@2: in "resultGeneral.xlsx" victor@2: victor@2: usage: victor@2: ed=ExcellData() victor@2: ed.saveGlobalData(percentagesArray,dirGeneral,betterOMRIds,files) victor@2: victor@2: files=['file1.xml','file2.xml','file3.xml','file4.xml'] victor@2: victor@2: data=[[0.7 ,0.8, 0.5, 0.6] victor@2: [0.7 ,0.8, 0.5, 0.6] victor@2: [0.7 ,0.8, 0.5, 0.6]] victor@2: victor@2: betterOMRIds=[[0,1,2] victor@2: [2,3,1] victor@2: [1,0,2]] victor@2: victor@2: ed=ExcellData() victor@2: ed.saveGlobalData(percentagesArray,dirGeneral,betterOMRIds,files) victor@2: ''' victor@2: victor@2: victor@2: wb=xlsxwriter.Workbook(path+'\\parts\\resultGeneral.xlsx') victor@2: ws=wb.add_worksheet() victor@2: r=0 victor@2: ws.write(0, 1,str(files[0])) victor@2: print betterOMRIds victor@2: for row in data: victor@2: c=0 victor@2: myBetterOMR=betterOMRIds[r] victor@2: for col in row: victor@2: format1 = wb.add_format() victor@2: format1.set_font_color('black') victor@2: if str(c-1)+"\n" in myBetterOMR: victor@2: format1.set_font_color('red') victor@2: fArray=files[c].split('\\') victor@2: ws.write(0, c,str(fArray[-1])) victor@2: ws.write(r+1, c,float(col),format1) victor@2: c+=1 victor@2: r+=1 victor@2: wb.close() victor@2: victor@2: def processResultGeneral(self,f): victor@2: ''' victor@2: This function takes one xls file and get the percentage of accuracy of each omr victor@2: Returns in this order S2Out,CPOut,PSOut,SEOut,SSOut victor@2: ''' victor@2: wb = load_workbook(f) victor@2: ws = wb['Sheet1'] victor@2: rows=ws.iter_rows() victor@2: S2=0 victor@2: iS2=0 victor@2: CP=0 victor@2: iCP=0 victor@2: PS=0 victor@2: iPS=0 victor@2: SE=0 victor@2: iSE=0 victor@2: SS=0 victor@2: iSS=0 victor@2: indexRow=0 victor@2: headRow=[] victor@2: for row in rows: victor@2: if (indexRow==0): victor@2: headRow=row victor@2: else: victor@2: for cell in row: victor@2: col=row.index(cell) victor@2: strHead=headRow[col].value.upper() victor@2: if '.S2.XML' in strHead: victor@2: try: victor@2: S2+=cell.value victor@2: iS2+=1 victor@2: except: victor@2: print "error" victor@2: victor@2: if '.CP.XML' in strHead: victor@2: try: victor@2: CP+=cell.value victor@2: iCP+=1 victor@2: except: victor@2: print "error" victor@2: if '.PS.XML' in strHead: victor@2: try: victor@2: PS+=cell.value victor@2: iPS+=1 victor@2: except: victor@2: print "error" victor@2: if '.SE.XML' in strHead: victor@2: try: victor@2: SE+=cell.value victor@2: iSE+=1 victor@2: except: victor@2: print "error" victor@2: if '.SS.XML' in strHead: victor@2: try: victor@2: SS+=cell.value victor@2: iSS+=1 victor@2: except: victor@2: print "error" victor@2: indexRow+=1 victor@2: S2Out=0 victor@2: CPOut=0 victor@2: PSOut=0 victor@2: SEOut=0 victor@2: SSOut=0 victor@2: if iS2>0: victor@2: S2Out= S2/iS2 victor@2: if iCP>0: victor@2: CPOut= CP/iCP victor@2: if iPS>0: victor@2: PSOut=PS/iPS victor@2: if SE>0: victor@2: SEOut=SE/iSE victor@2: if SS>0: victor@2: SSOut=SS/iSS victor@2: return S2Out,CPOut,PSOut,SEOut,SSOut victor@2: victor@2: def writeFinalXLS(self, rootDir,S2,CP,PS,SE,SS): victor@2: ''' victor@2: Writes the final.xlsx upon the inputs S2,CP,PS,SE,SS victor@2: ''' victor@2: wb=xlsxwriter.Workbook(rootDir+'\\final.xlsx') victor@2: ws=wb.add_worksheet() victor@2: ws.write(0, 0,"S2") victor@2: ws.write(0, 1,"CP") victor@2: ws.write(0, 2,"PS") victor@2: ws.write(0, 3,"SE") victor@2: ws.write(0, 4,"SS") victor@2: ws.write(1, 0,S2) victor@2: ws.write(1, 1,CP) victor@2: ws.write(1, 2,PS) victor@2: ws.write(1, 3,SE) victor@2: ws.write(1, 4,SS) victor@2: victor@2: wb.close() victor@2: victor@2: def __getPathStepsBack(self,f,stepsBack): victor@2: ''' victor@2: Private function: takes n steps back in the path from a file victor@2: ''' victor@2: fArray=f.split('\\') victor@2: path='' victor@2: for i in range(len(fArray)-stepsBack): victor@2: path+=fArray[i]+"/" victor@2: return path victor@2: