From 1206e61a25397d730206d56acbab6a54174e5d23 Mon Sep 17 00:00:00 2001 From: Stepan Bahnik Date: Wed, 4 Mar 2015 22:48:36 +0100 Subject: [PATCH] started rewrite of saving images --- .gitignore | 7 + Stuff/Modules/cm.py | 14 +- Stuff/Modules/explorer.py | 81 ++--------- Stuff/Modules/filestorage.py | 2 + Stuff/Modules/image.py | 337 ++++++++++++++++++++++++++++++++----------- Stuff/Modules/starter.py | 22 ++- Stuff/Modules/tools.py | 13 +- 7 files changed, 303 insertions(+), 173 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a7ea3b2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.dat +*.svg +*.files +*bugs.txt +Options.txt +*.pyc +Data diff --git a/Stuff/Modules/cm.py b/Stuff/Modules/cm.py index b21c2cd..67e2f0e 100644 --- a/Stuff/Modules/cm.py +++ b/Stuff/Modules/cm.py @@ -1045,7 +1045,11 @@ def recognizeStrategy(self, i0, i1, minSpeed, percentSize): x0, y0 = self.data[i0][self.indices] x1, y1 = self.data[i1][self.indices] t0, t1 = self.data[i0][1], self.data[i1][1] - speed = (sqrt(((x1 - x0)**2 + (y1 - y0)**2)) / self.trackerResolution) / ((t1 - t0) / 1000) + try: + speed = (sqrt(((x1 - x0)**2 + (y1 - y0)**2)) / self.trackerResolution) / ((t1 - t0) / 1000) + except Exception: + print(self.data[i0]) + print(self.data[i1]) if speed > minSpeed: border = self.radius * (1 - (percentSize / 100)) cx, cy = self.centerX, self.centerY @@ -1087,8 +1091,10 @@ def getStrategies(self, time = 20, startTime = 0, rows = 25, minSpeed = 10, minA shocks = deque(self.getShocks(time = time, startTime = startTime, indices = True)) nextShock = shocks.popleft() if shocks else i1 + 2*rows + 2 - if nextShock < i0 + rows: + if i0 < nextShock < i0 + rows: lastStrategy = self.recognizeStrategy(i0, nextShock, minSpeed, borderPercentSize) + elif i0 == nextShock: + lastStrategy = self.recognizeAfterShockStrategy(i0, i0 + rows, minAngle) else: lastStrategy = self.recognizeStrategy(i0, i0 + rows, minSpeed, borderPercentSize) beginning = i0 if indices else t0 @@ -1192,7 +1198,7 @@ def main(): start = 9 end = 12 diff = end - start - + class DummyNumber: def __init__(self, N): self.number = str(N) @@ -1211,7 +1217,7 @@ def __init__(self): import image import graphs - cm = CM("12rNO453_Arena.dat") + cm = CM("13a_12_Arena.dat") svg = image.SVG(600, 120, scale = 2) graph = graphs.AngleGraph(parent = Parent(), cm = cm, purpose = "") _, __, furtherText = graph.saveGraph(cm) diff --git a/Stuff/Modules/explorer.py b/Stuff/Modules/explorer.py index c8e49a4..f86bf85 100644 --- a/Stuff/Modules/explorer.py +++ b/Stuff/Modules/explorer.py @@ -30,7 +30,7 @@ from showtracks import ShowTracks from filestorage import FileStorageFrame from commonframes import TimeFrame, returnName -from image import SVG +from image import svgSave from processor import ProgressWindow from optionget import optionGet from graphs import getGraphTypes, Graphs, SvgGraph, SpeedGraph, DistanceFromCenterGraph @@ -302,19 +302,19 @@ def saveImages(self): self.status.set("No file selected.") self.bell() return - + problems = 0 for filename in files: - try: - if filename in self.fileStorage.pairedfiles: - cm = m.CL(filename, self.fileStorage.pairedfiles[filename]) - else: - cm = m.CL(filename, "auto") - if self.removeReflectionsVar.get(): - cm.removeReflections(points = self.fileStorage.reflections.get(filename, None)) - self.saveOneImage(cm, filename) - except Exception: - problems += 0 + #try: + if filename in self.fileStorage.pairedfiles: + cm = m.CL(filename, self.fileStorage.pairedfiles[filename]) + else: + cm = m.CL(filename, "auto") + if self.removeReflectionsVar.get(): + cm.removeReflections(points = self.fileStorage.reflections.get(filename, None)) + svgSave(cm, filename, self.saveWhatVar.get(), self) + #except Exception: + # problems += 1 if len(files) > 1: progress.addOne() @@ -338,63 +338,6 @@ def saveImages(self): self.root.config(cursor = "") - def saveOneImage(self, cm, filename): - "saves image for one file" - directory = optionGet("ImageDirectory", os.getcwd(), "str", True) - # pridat moznost scale u vsech SVG - - what = self.saveWhatVar.get() - - if what == "both frames": - self.svg = SVG(600, 300) - self.saveArenaFrame(cm) - self.saveRoomFrame(cm, origin = (300, 0)) - elif what == "arena frame": - self.svg = SVG(300, 300) - self.saveArenaFrame(cm) - elif what == "room frame": - self.svg = SVG(300, 300) - self.saveRoomFrame(cm) - elif what == "graph": - self.svg = SVG(600, 120) - self.saveGraph(cm) - elif what == "all": - self.svg = SVG(600, 420) - self.saveArenaFrame(cm) - self.saveRoomFrame(cm, origin = (300, 0)) - self.saveGraph(cm, origin = (0, 300)) - - self.svg.save(os.path.join(directory, os.path.splitext(os.path.basename(filename))[0] + - "_" + what.replace(" ", "_") + ".svg")) # upravit - - - def saveArenaFrame(self, cm, origin = (0, 0)): - "saves info about arena frame in self.svg" - self.svg.drawAAPA(cm, frame = "arena", startTime = int(self.timeFrame.startTimeVar.get()), - time = int(self.timeFrame.timeVar.get()), boundary = True, - sector = False, shocks = False, origin = origin) # dat do options - - - def saveRoomFrame(self, cm, origin = (0, 0)): - "saves info about room frame in self.svg" - self.svg.drawAAPA(cm, frame = "room", startTime = int(self.timeFrame.startTimeVar.get()), - time = int(self.timeFrame.timeVar.get()), boundary = True, - sector = True, shocks = True, origin = origin) - - - def saveGraph(self, cm, origin = (0, 0)): - "saves info about graph in self.svg" - size = (600, 120) - graph = eval(self.graphTypeVar.get()[:-1] + ', cm, purpose = "svg")') - yCoordinates, maxY, furtherText = graph.saveGraph(cm) - points = [] - if yCoordinates: - length = len(yCoordinates) - 1 - for count, y in enumerate(yCoordinates): - points.append(((count * size[0]) / length, size[1] - ((y * size[1]) / maxY))) - self.svg.drawGraph(points, furtherText = furtherText, origin = origin, boundary = True) - - def _returnSelectedFiles(self): "returns files that are chosen for creating of images" which = self.saveWhichFilesVar.get() diff --git a/Stuff/Modules/filestorage.py b/Stuff/Modules/filestorage.py index 1493c85..bb4aee9 100644 --- a/Stuff/Modules/filestorage.py +++ b/Stuff/Modules/filestorage.py @@ -752,6 +752,8 @@ def getFiles(self): return [], [] if "}" in filenames and "{" in filenames: filenames = filenames[1:-1].split("} {") + elif filenames.endswith(")") and filenames.startswith("("): + filenames = list(eval(filenames)) else: filenames = [x + ".dat" for x in filenames.split(".dat ")] if filenames[-1].endswith(".dat.dat"): diff --git a/Stuff/Modules/image.py b/Stuff/Modules/image.py index e383951..f8914ac 100644 --- a/Stuff/Modules/image.py +++ b/Stuff/Modules/image.py @@ -20,100 +20,261 @@ from math import radians, cos, sin +import os + + +from optionget import optionGet +from graphs import getGraphTypes, Graphs, SvgGraph, SpeedGraph, DistanceFromCenterGraph +from graphs import AngleGraph, DistanceFromPlatformGraph, DistanceFromRobotGraph +import mode as m + + +def svgSave(cm, filename, what, root): + "saves image for one file" + directory = optionGet("ImageDirectory", os.getcwd(), "str", True) + +## what = root.saveWhatVar.get() +## start = int(root.timeFrame.startTimeVar.get()) +## end = int(root.timeFrame.timeVar.get()) +## graph = eval(root.graphTypeVar.get()[:-1] + ', cm, purpose = "svg")') + + if what == "both frames": + components = ["arena", "room"] + elif what == "arena frame": + components = ["arena"] + elif what == "room frame": + components = ["room"] + elif what == "graph": + components = ["graph"] + elif what == "all": + components = ["arena", "room", "graph"] + + svg = SVG(cm, components) + svg.save(os.path.join(directory, os.path.splitext(os.path.basename(filename))[0] + + "_" + what.replace(" ", "_") + ".svg")) # upravit + + + + + + class SVG(): "represents .svg file - text of the file is stored in self.content" - def __init__(self, width, height, scale = 1): - # dodelat scale tu a v save, odstranit jinde - self.content = '\n'.format( - width * scale, height * scale) - self.content += '\n'.format(scale) + def __init__(self, cm, components): + self.cm = cm + self.components = components + self.scale = 1 + self.main = "Main" + self.xgap = 10 + self.ygap = 10 + self.xlab = "xlab" + self.ylab = "ylab" + self.xticks = True + self.yticks = True + + self.addComponents() + self.computeSize() + self.addIntro() + + for component in self.components: + if component in ["xgap", "ygap"]: + continue + elif component == "main": + self.addMain() + elif component == "arena": + self.addArena() + elif component == "room": + self.addRoom() + elif component == "graph": + self.addGraph() + elif component == "xticks" and "graph" in self.components: + self.addXticks() + elif component == "yticks" and "graph" in self.components: + self.addYticks() + elif component == "xlab" and "graph" in self.components: + self.addXlab() + elif component == "ylab" and "graph" in self.components: + self.addYlab() + + + def addComponents(self): + if self.main: + self.components.append("main") + if self.xgap and "room" in self.components and "arena" in self.components: + self.components.append("xgap") + if self.ygap and "graph" in self.components and "arena" in self.components: + self.components.append("ygap") + if self.xticks and "graph" in self.components: + self.components.append("xticks") + if self.yticks and "graph" in self.components: + self.components.append("yticks") + if self.xlab and "graph" in self.components: + self.components.append("xlab") + if self.ylab and "graph" in self.components: + self.components.append("ylab") + + + def computeSize(self): + x = [0] * 6 + y = [0] * 7 + sizes = {"main": [(0, 0), (20, 1)], + "arena": [(300, 3), (300, 2)], + "room": [(300, 5), (300, 2)], + "graph": [(600, 5), (120, 4)], + "xgap": [(self.xgap, 4), (0, 0)], + "ygap": [(0, 0), (self.ygap, 3)], + "xticks": [(0, 0), (10, 5)], + "yticks": [(10, 2), (0, 0)], + "xlab": [(0, 0), (15, 6)], + "ylab": [(15, 1), (0, 0)] + } + for component in self.components: + xs, ys = sizes[component] + for i in range(1, len(x)): + if i >= xs[1] and (x[i-1] + xs[0]) < x[i]: + x[i] = x[i-1] + xs[0] + for i in range(1, len(y)): + if i >= ys[1] and (y[i-1] + ys[0]) < y[i]: + y[i] = y[i-1] + ys[0] + self.x = x + self.y = y + + + def addIntro(self): + t1 = '\n' + self.content = t1.format(self.x[-1] * self.scale, self.y[-1] * self.scale) + self.content += '\n'.format(self.scale) self.content += '\n'.format(width, height) - - - def drawAAPA(self, cm, frame, startTime = 0, time = 20, origin = (0, 0), boundary = False, - sector = False, shocks = False, size = 300, skip = 3): - "adds text into sefl.content corresponding to track from one frame of AAPA" - - self.content += '\n'.format(origin) - - if boundary: - self.content += '\n'.format(size) - - start = cm.findStart(startTime) - time = time * 60000 - r = cm.radius - - self.content += '\n'.format((size / 2) - r) - - if sector: - self.angle = cm.centerAngle - self.width = cm.width - a1 = radians(self.angle - (self.width / 2)) - a2 = radians(self.angle + (self.width / 2)) - Sx1, Sy1 = r + (cos(a1) * r), r - (sin(a1) * r) - Sx2, Sy2 = r + (cos(a2) * r), r - (sin(a2) * r) - self.content += '\n'.format(Sx1, Sy1, r, r, Sx2, Sy2) - - self.content += ' time: - break - positions = line[2:4] - if positions != prev: - self.content += ",".join(map(str, positions)) + " " - if line[6] > 0: - if not shock: - shock = True - shockPositions.append(positions) - else: - shock = False - prev = positions - elif frame == "arena": - prev = (0, 0) - for count, line in enumerate(cm.data[start:], skip): - if line[1] > time: - break - positions = line[7:9] - if count % skip == 0 and positions != prev: - self.content += ",".join(map(str, positions)) + " " - prev = positions - self.content += '" style = "fill:none;stroke:black"/>\n' - - if shocks and frame == "room": # u double avoidance odstranit frame podminku - for position in shockPositions: - self.content += '\n'.format(*position) - - self.content += '\n'.format(r) + 'width="{}" height="{}" />\n'.format(self.x[-1], self.y[-1]) - self.content += '\n' - - self.content += '\n' + def add(self, new, x, y): + self.content += '\n'.format(self.x[x], self.y[y]) + self.content += new + "\n" + self.content += '\n' - def drawGraph(self, points, furtherText = "", origin = (0, 0), boundary = False): - "adds text containing information about graph in self.content" - size = (600, 120) - self.content += '\n'.format(origin) - if boundary: - self.content += '\n'.format(*size) - self.content += furtherText - if points: - self.content += '\n'.format(size) +## +## start = cm.findStart(startTime) +## time = time * 60000 +## r = cm.radius +## +## self.content += '\n'.format((size / 2) - r) +## +## if sector: +## self.angle = cm.centerAngle +## self.width = cm.width +## a1 = radians(self.angle - (self.width / 2)) +## a2 = radians(self.angle + (self.width / 2)) +## Sx1, Sy1 = r + (cos(a1) * r), r - (sin(a1) * r) +## Sx2, Sy2 = r + (cos(a2) * r), r - (sin(a2) * r) +## self.content += '\n'.format(Sx1, Sy1, r, r, Sx2, Sy2) +## +## self.content += ' time: +## break +## positions = line[2:4] +## if positions != prev: +## self.content += ",".join(map(str, positions)) + " " +## if line[6] > 0: +## if not shock: +## shock = True +## shockPositions.append(positions) +## else: +## shock = False +## prev = positions +## elif frame == "arena": +## prev = (0, 0) +## for count, line in enumerate(cm.data[start:], skip): +## if line[1] > time: +## break +## positions = line[7:9] +## if count % skip == 0 and positions != prev: +## self.content += ",".join(map(str, positions)) + " " +## prev = positions +## self.content += '" style = "fill:none;stroke:black"/>\n' +## +## if shocks and frame == "room": # u double avoidance odstranit frame podminku +## for position in shockPositions: +## self.content += '\n'.format(*position) +## +## self.content += '\n'.format(r) +## +## self.content += '\n' +## +## self.content += '\n' +## +## +## def drawGraph(self, points, furtherText = "", origin = (0, 0), boundary = False): +## "adds text containing information about graph in self.content" +## size = (600, 120) +## self.content += '\n'.format(origin) +## if boundary: +## self.content += '\n'.format(*size) +## self.content += furtherText +## if points: +## self.content += '