# -*- coding: cp1252 -*- import os import os.path from xml.etree import ElementTree import win32com.client from win32com.client import constants as c import cPickle import math excel = win32com.client.Dispatch("Excel.Application") book = excel.Workbooks.Add() # For charts path = os.getcwd() device = os.path.split(path)[-1] outputPath= os.path.join(path, "output") DEBUG_PRINT = False pickle_file = "data_pickle.txt" data={} if os.path.exists(outputPath) and os.path.exists(os.path.join(outputPath, pickle_file)): FILE = open(os.path.join(outputPath, pickle_file), "r") data = cPickle.load(FILE) FILE.close() else: print "You need to create the log pickle first with the analyzer file" exit filename = device + ".xlsx" LONG_LENGTH = 500 INTERVAL_LENGTH = 250 names = [] if path.find("asus") != -1: width = 800 height = 1280 else: width = 480 height = 800 xdiv = 0 ydiv = 0 def debug(obj): if DEBUG_PRINT: print obj def distanceToCenter(x,y,goal,cells): if cells=="12": xdiv = 3 ydiv= 4 elif cells == "6": xdiv = 2 ydiv = 3 else: xdiv= 0 ydiv= 0 row = (goal - 1) / xdiv column = (goal-1) % xdiv xmin = column * (width / xdiv) xmax = (column + 1 ) * (width / xdiv) ymin = row * (height / ydiv) ymax = (row + 1 ) * (height / ydiv) xcenter = (xmax + xmin ) / 2 ycenter = (ymax + ymin ) / 2 xd = xcenter - x yd = ycenter - y distance = math.sqrt(xd * xd + yd*yd) return distance def parseTap(name, combo, current, sheet, row): print combo column = 1 for key in sorted(current.keys()): d = current[key] maxTapDuration = int(d[4]) betweenTaps = int(d[5]) x = int(d[6]) y = int(d[7]) sheet.Cells(row,column).Value = name column += 1 debug(name) sheet.Cells(row,column).Value = combo[0] column += 1 debug(combo[0]) sheet.Cells(row,column).Value = combo[1] column += 1 debug(combo[1]) sheet.Cells(row,column).Value = combo[2] column += 1 debug(combo[2]) sheet.Cells(row,column).Value = key column += 1 debug(key) sheet.Cells(row,column).Value = d[0] column += 1 debug(d[0]) sheet.Cells(row,column).Value = str(d[1]) column += 1 debug(str(d[1])) sheet.Cells(row,column).Value = str(d[2]) column += 1 debug(str(d[2])) sheet.Cells(row,column).Value = str(d[3]) column += 1 debug(str(d[3])) sheet.Cells(row, column).Value = sheet.Cells(row, column-1).Value - sheet.Cells(row, column-2).Value column += 1 if betweenTaps == -1: betweenTaps = 0 sheet.Cells(row, column).Value = str(betweenTaps) column += 1 sheet.Cells(row, column).Value = "=LEN(F" + str(row) + ")" column += 1 if combo[2] == "Double": sheet.Cells(row, column).Value = 2 else: sheet.Cells(row, column).Value = 1 column += 1 #Incorrect land and lift sheet.Cells(row, column).Value = "=IF(OR(EXACT($E" + str(row) + ",LEFT($F" + str(row) + ",1)),EXACT($E" + str(row) + ",RIGHT($F" + str(row) + ",1))), 0, 1)" column += 1 #Just incorrect land sheet.Cells(row, column).Value = "=IF(EXACT($E" + str(row) + ", LEFT($F" + str(row) + ",1)), 0, 1) - N" + str(row) column += 1 #Just incorrect lift sheet.Cells(row, column).Value = "=IF(EXACT($E" + str(row) + ", RIGHT($F" + str(row) + ",1)), 0, 1) - N" + str(row) column += 1 #Overall Incorrect land sheet.Cells(row, column).Value = "=SUM(N" + str(row) + ":O" + str(row) + ")" column += 1 #Position errors sheet.Cells(row, column).Value = "=SUM(N" + str(row) + ":P" + str(row) + ")" column += 1 # Frequency (number of detected items) sheet.Cells(row, column).Value = "=IF(EXACT(L" + str(row) + ", M" + str(row) + "),0,1)" column += 1 # Tap length if combo[2] == "Long": if maxTapDuration >= LONG_LENGTH: l = "0" else: l = "1" else: if maxTapDuration < LONG_LENGTH: l = "0" else: l = "1" sheet.Cells(row, column).Value = l column += 1 #Interval Length if combo[2] == "Double": if betweenTaps < INTERVAL_LENGTH: b = "0" else: b = "1" else: b = "0" sheet.Cells(row, column).Value = b column += 1 #Automatic Detection if d[1].find(combo[2]) != -1: a = 0 else: a = 1 sheet.Cells(row, column).Value = str(a) column += 1 #Primitive Errors sheet.Cells(row, column).Value = "=IF(SUM(S" + str(row) + ":U" + str(row) + ") > 1,1,0)" column += 1 # Extra metrics ############################################################### # Distance to target center if key == "*": key = 10 elif key == "0": key = 11 elif key == "#": key = 12 else: key = int (key) sheet.Cells(row, column).Value = distanceToCenter(x, y, key, combo[1]) column += 1 row = row + 1 column = 1 return row def writeUserOverall(sheet, row, name, method, grid, primitive, start, end): column = 1 sheet.Cells(row,column).Value = name column += 1 sheet.Cells(row,column).Value = method column += 1 sheet.Cells(row,column).Value = grid column += 1 sheet.Cells(row,column).Value = primitive column += 1 sheet.Cells(row,column).Value = "" column += 1 sheet.Cells(row,column).Value = "" column += 1 sheet.Cells(row,column).Value = "" column += 1 for col in ["H", "I", "J", "K", "L", "M", "N" , "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X"]: sheet.Cells(row,column).Value = "=AVERAGE("+ col + str(start) + ":" + col + str(end) + ")" column += 1 def writeOverall(sheet): sheet.Cells(1,2).Value = "Method" sheet.Cells(1,3).Value = "Grid" sheet.Cells(1,4).Value = "Primitive" sheet.Cells(1,5).Value = "Target" sheet.Cells(1,6).Value = "Acquired" sheet.Cells(1,7).Value = "Attempts" sheet.Cells(1,8).Value = "start" sheet.Cells(1,9).Value = "end" sheet.Cells(1,10).Value = "duration" sheet.Cells(1,11).Value = "interval" sheet.Cells(1,12).Value = "Length" sheet.Cells(1,13).Value = "Taps" sheet.Cells(1,14).Value = "Incorrect Land and Lift" sheet.Cells(1,15).Value = "Just Incorrect Land" sheet.Cells(1,16).Value = "Just Incorrect Lift" sheet.Cells(1,17).Value = "Incorrect Land" sheet.Cells(1,18).Value = "Position Errors" sheet.Cells(1,19).Value = "Frequency" sheet.Cells(1,20).Value = "Tap length" sheet.Cells(1,21).Value = "Interval length" sheet.Cells(1,22).Value = "Automatic detection" sheet.Cells(1,23).Value = "Primitive Errors" sheet.Cells(1,24).Value = "Distance to target center" anotherSheet = book.Sheets(2) for column in [2, 3, 4, 5, 6]: for row in range(2, 84): sheet.Cells(row, column).Value = anotherSheet.Cells(row, column).Value col = chr(8 + 96) col = col.upper() formula = "=AVERAGE(" for name in names: formula += "'" + name + "'!" + col + str(row) + "," formula = formula[:-1] formula += ")" print "COPIAR PARA OVERALL e ARRASTAR: ", formula formula = "=STDEV(" for name in names: formula += "'" + name + "'!" + col + str(row) + "," formula = formula[:-1] formula += ")" print "COPIAR PARA OVERALL e ARRASTAR: ", formula def writeUserLog(): global data for key in sorted(data.keys()): name = key.split('\\')[-1] row = 2 column = 1 print "Parsing ", name sheet = book.Sheets.Add() sheet.Name = name names.append(name) sheet.Cells(1,1).Value = "Name" sheet.Cells(1,2).Value = "Method" sheet.Cells(1,3).Value = "Grid" sheet.Cells(1,4).Value = "Primitive" sheet.Cells(1,5).Value = "Target" sheet.Cells(1,6).Value = "Acquired" sheet.Cells(1,7).Value = "Attempts" sheet.Cells(1,8).Value = "start" sheet.Cells(1,9).Value = "end" sheet.Cells(1,10).Value = "duration" sheet.Cells(1,11).Value = "interval" sheet.Cells(1,12).Value = "Length" sheet.Cells(1,13).Value = "Taps" sheet.Cells(1,14).Value = "Incorrect Land and Lift" sheet.Cells(1,15).Value = "Just Incorrect Land" sheet.Cells(1,16).Value = "Just Incorrect Lift" sheet.Cells(1,17).Value = "Incorrect Land" sheet.Cells(1,18).Value = "Position Errors" sheet.Cells(1,19).Value = "Frequency" sheet.Cells(1,20).Value = "Tap length" sheet.Cells(1,21).Value = "Interval length" sheet.Cells(1,22).Value = "Automatic detection" sheet.Cells(1,23).Value = "Primitive Errors" sheet.Cells(1,24).Value = "Distance to target center" # Grid 6 Tap combo = ("Grid", "6", "Tap") if data[key].has_key(combo): current = data[key][combo] row = parseTap(name, combo, current, sheet, row) row = 8 # Grid 6 Long combo = ("Grid", "6", "Long") if data[key].has_key(combo): current = data[key][combo] row = parseTap(name, combo, current, sheet, row) row = 14 # Grid 6 Double combo = ("Grid", "6", "Double") if data[key].has_key(combo): current = data[key][combo] row = parseTap(name, combo, current, sheet, row) row = 20 # Grid 12 Tap combo = ("Grid", "12", "Tap") if data[key].has_key(combo): current = data[key][combo] row = parseTap(name, combo, current, sheet, row) row = 32 # Grid 12 Long combo = ("Grid", "12", "Long") if data[key].has_key(combo): current = data[key][combo] row = parseTap(name, combo, current, sheet, row) row = 44 # Grid 12 Double combo = ("Grid", "12", "Double") if data[key].has_key(combo): current = data[key][combo] row = parseTap(name, combo, current, sheet, row) row = 78 writeUserOverall(sheet, row, name, "Grid", "6", "Tap", 2, 7) row += 1 writeUserOverall(sheet, row, name, "Grid", "6", "Long", 8, 13) row += 1 writeUserOverall(sheet, row, name, "Grid", "6", "Double", 14, 19) row += 1 writeUserOverall(sheet, row, name, "Grid", "12", "Tap", 20, 31) row += 1 writeUserOverall(sheet, row, name, "Grid", "12", "Long", 32, 43) row += 1 writeUserOverall(sheet, row, name, "Grid", "12", "Double", 44, 55) row += 1 s = book.Sheets.Add() s.Name = "Overall" writeOverall(s) writeUserLog() if os.path.exists(os.path.join(path, "output", filename)): os.remove(os.path.join(path, "output", filename)) book.SaveAs(os.path.join(path, "output", filename)) sheet = None book = None excel.Quit() excel = None