# -*- coding: cp1252 -*- import os import os.path from xml.etree import ElementTree import matplotlib import numpy as np import matplotlib.pyplot as plt import matplotlib.mlab as mlab import matplotlib.cbook as cbook from matplotlib.ticker import MultipleLocator, FormatStrFormatter path = os.getcwd() data = {} grid = "" goal = 0 primitive = "" method = "" trialStart = 0 start = 0 first = True firsPrimitive = True scrollsX = [] scrollsY = [] pixelsFor250 = 10 xdiv = 0 ydiv = 0 numDict = {"Um": 1, "Dois": 2, u"Três": 3, "Quatro": 4, "Cinco": 5, "Seis": 6, "Sete": 7, "Oito": 8, "Nove": 9, "Asterisco": 10, "Zero": 11, "Cardinal": 12, "Para cima": 1, "Para baixo" : 2, "Para a esquerda": 3, "Para a direita": 4, "No lado direito para cima ": 5, "No lado esquerdo para cima ": 6, "No lado direito para baixo": 7, "No lado esquerdo para baixo": 8, "No topo para a esquerda": 9, "No topo para a direita": 10, "Em baixo para a direita": 11, "Em baixo para a esquerda": 12, "Canto superior esquerdo": 13, "Canto inferior esquerdo": 14, "Canto superior direito": 15, "Canto inferior direito": 16, "Esquerda": 17, "Direita": 18, "Cima": 19, "Baixo": 20, "Centro": 21, "10":22, "11":23, "12":24 } if path.find("asus") != -1: width = 800 height = 1280 else: width = 480 height = 800 outputPath = os.path.join(path, "output", "point_plots") if not os.path.isdir(outputPath): os.mkdir(outputPath) def getMilli(time): parts=time.strip().split(":") miliseconds = int(parts[0]) * 3600000 + int(parts[1]) * 60000 + int(parts[2].split(".")[0]) * 1000 + int(parts[2].split(".")[1]) return miliseconds def isInside(x,y,goal,cells): 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) if x >=xmin and x <= xmax and y <=ymax and y >= ymin: return True else: return False def parseFileForOverall(dir, area = None): global xdiv,ydiv, method, primitive, grid data = {} with open(dir, 'rt') as f: tree = ElementTree.parse(f) xs = [] ys = [] c = [] for node in tree.getiterator(): if node.tag == "session": method = node.attrib["method"] grid = node.attrib["cells"] primitive = node.attrib["primitive"] elif node.tag == "trial": trialStart = getMilli(node.attrib["time"]) start = 0 first = True firstPrimitive = True if node.getchildren(): for child in node: if child.tag == "goal": goal = numDict.get(unicode(child.text), "Free") else: if child.tag == "regTouch": if first: first = False start = getMilli(child.attrib["time"]) - trialStart x = child.attrib["x"] y = 1280 - child.attrib["y"] if area == None or area == goal: xs.append(int(x)) ys.append(int(y)) c.append("gray") return method + primitive + grid, xs, ys, c def parseFile(dir): global xdiv,ydiv, method, primitive, grid data = {} with open(dir, 'rt') as f: tree = ElementTree.parse(f) xs = [] ys = [] c = [] s = [] for node in tree.getiterator(): if node.tag == "session": method = node.attrib["method"] grid = node.attrib["cells"] primitive = node.attrib["primitive"] if grid=="12": xdiv = 3 ydiv= 4 elif grid == "6": xdiv = 2 ydiv = 3 else: xdiv= 0 ydiv= 0 elif node.tag == "trial": trialStart = getMilli(node.attrib["time"]) start = 0 first = True firstPrimitive = True if node.getchildren(): for child in node: if child.tag == "goal": goal = numDict.get(unicode(child.text), "Free") elif child.tag == "sessions": pass else: if child.tag == "regTap" or child.tag == "regLong" or child.tag == "regDoubleTap": if not firstPrimitive: x = child.attrib["x"] y = 1280 - child.attrib["y"] xs.append(int(x)) ys.append(int(y)) c.append("y") s.append(15) firstPrimitive = False elif child.tag == "regDown": if first: first = False start = getMilli(child.attrib["time"]) - trialStart x = child.attrib["x"] y = 1280 - child.attrib["y"] xs.append(int(x)) ys.append(int(y)) if grid == "": c.append("g") elif isInside(int(x),int(y),goal,grid): c.append("g") else: c.append("r") s.append(pixelsFor250 * start / 250) elif child.tag == "regTouch": x = child.attrib["x"] y = 1280 - child.attrib["y"] elif child.tag == "regScroll": xs.append(int(x)) ys.append(int(y)) c.append("gray") s.append(15) return xs, ys, c, s def getOverallCharts(dirs, area = None): global xdiv, ydiv chartData = {} for dir in dirs: cur = os.path.join(path, dir) if os.path.isdir(cur) and dir != "output": files = os.listdir(cur) for f in files: filepath=os.path.join(cur, f) chart, xs, ys, c = parseFileForOverall(filepath, area) current = chartData.get(chart, ([],[],[])) current[0].extend(xs) current[1].extend(ys) current[2].extend(c) chartData[chart] = current for key in chartData.keys(): data = chartData[key] fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(data[0], data[1], 10, data[2], alpha=1,faceted=False) if xdiv == 0: ax.xaxis.grid(False,'minor') ax.yaxis.grid(False,'minor') ax.xaxis.grid(False,'major') ax.yaxis.grid(False,'major') else: ax.xaxis.set_major_locator(MultipleLocator(width/xdiv)) ax.yaxis.set_major_locator(MultipleLocator(height/ydiv)) ax.xaxis.grid(False,'minor') ax.yaxis.grid(False,'minor') ax.xaxis.grid(True,'major') ax.yaxis.grid(True,'major') ax.set_xlabel(r'X', fontsize=20) ax.set_ylabel(r'Y', fontsize=20) ax.set_xlim(0, width) ax.set_ylim(0, height) ax.set_aspect(1) ax.set_title('Taps') extra = "" if area != None: extra = str(area) if len(data[0]) > 0: fig.savefig(os.path.join(path, "output", "point_plots", key + extra + ".png"), dpi=fig.dpi) def saveAll(dirs): global xdiv, ydiv for dir in dirs: cur = os.path.join(path, dir) if os.path.isdir(cur) and dir != "output": files = os.listdir(cur) for f in files: filepath=os.path.join(cur, f) if grid=="12": xdiv = 3 ydiv= 4 elif grid == "6": xdiv = 2 ydiv = 3 else: xdiv= 0 ydiv= 0 xs, ys, c ,s = parseFile(filepath) fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(xs, ys, s, c, alpha=0.8,faceted=False) if xdiv == 0: ax.xaxis.grid(False,'minor') ax.yaxis.grid(False,'minor') ax.xaxis.grid(False,'major') ax.yaxis.grid(False,'major') else: ax.xaxis.set_major_locator(MultipleLocator(width/xdiv)) ax.yaxis.set_major_locator(MultipleLocator(height/ydiv)) ax.xaxis.grid(False,'minor') ax.yaxis.grid(False,'minor') ax.xaxis.grid(True,'major') ax.yaxis.grid(True,'major') ax.set_xlabel(r'X', fontsize=20) ax.set_ylabel(r'Y', fontsize=20) ax.set_xlim(0, width) ax.set_ylim(0, height) ax.set_aspect(1) ax.set_title('Taps') fig.savefig(os.path.join(path, "output", "point_plots", dir + "_" + method + "_" + grid + "_" + primitive + ".png"), dpi=fig.dpi) while True: dirs = os.listdir(path) i = 1 print "Choose the participant ('s' para sair, 'p' para guardar imagens de todos em disco)" for d in dirs: print i, " - " , d i = i + 1 n = raw_input() if n == "s": break elif n == "p": saveAll(dirs) elif n == "a": getOverallCharts(dirs) for i in range(1,25): getOverallCharts(dirs, i) else: nome = dirs[int(n) - 1] cur = os.path.join(path, nome) if os.path.isdir(cur): files = os.listdir(cur) a = 1 for f in files: print a, " - " , f[f.find("_")+1:-4] a = a + 1 x = raw_input() filename = files[int(x) - 1] filepath=os.path.join(cur, filename) xs, ys, c ,s = parseFile(filepath) fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(xs, ys, s, c, alpha=0.8,faceted=False) if xdiv == 0: ax.xaxis.grid(False,'minor') ax.yaxis.grid(False,'minor') ax.xaxis.grid(False,'major') ax.yaxis.grid(False,'major') else: ax.xaxis.set_major_locator(MultipleLocator(width/xdiv)) ax.yaxis.set_major_locator(MultipleLocator(height/ydiv)) ax.xaxis.grid(False,'minor') ax.yaxis.grid(False,'minor') ax.xaxis.grid(True,'major') ax.yaxis.grid(True,'major') ax.set_xlabel(r'X', fontsize=20) ax.set_ylabel(r'Y', fontsize=20) ax.set_xlim(0,width) ax.set_ylim(0, height) ax.set_aspect(1) ax.set_title('Taps') plt.show()