Mercurial Hosting > traffic-intelligence
changeset 722:49e99ca34a7d
corrected bugs in old ubc code
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 05 Aug 2015 00:12:52 -0400 |
parents | 52272f6bf62a |
children | e14e2101a5a9 |
files | python/storage.py python/ubc_utils.py |
diffstat | 2 files changed, 24 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/python/storage.py Mon Aug 03 14:48:41 2015 -0400 +++ b/python/storage.py Wed Aug 05 00:12:52 2015 -0400 @@ -654,11 +654,11 @@ s = f.readline() return s.strip() -def getLines(f, commentCharacters = commentChar): +def getLines(f, delimiterChar = delimiterChar, commentCharacters = commentChar): '''Gets a complete entry (all the lines) in between delimiterChar.''' dataStrings = [] s = readline(f, commentCharacters) - while len(s) > 0: + while len(s) > 0 and s[0] != delimiterChar: dataStrings += [s.strip()] s = readline(f, commentCharacters) return dataStrings
--- a/python/ubc_utils.py Mon Aug 03 14:48:41 2015 -0400 +++ b/python/ubc_utils.py Wed Aug 05 00:12:52 2015 -0400 @@ -1,7 +1,7 @@ #! /usr/bin/env python '''Various utilities to load data saved by the UBC tool(s)''' -import utils, events +import utils, events, storage from moving import MovingObject, TimeInterval, Trajectory @@ -57,13 +57,13 @@ by just copying the corresponding trajectory and velocity data from the inFilename, and saving the characteristics in objects (first line) into outFilename''' - infile = utils.openCheck(inFilename) - outfile = utils.openCheck(outFilename,'w') + infile = storage.openCheck(inFilename) + outfile = storage.openCheck(outFilename,'w') if (inFilename.find('features') >= 0) or (not infile) or (not outfile): return - lines = utils.getLines(infile) + lines = storage.getLines(infile) objNum = 0 # in inFilename while lines != []: # find object in objects (index i) @@ -80,16 +80,16 @@ outfile.write(utils.delimiterChar+'\n') # next object objNum += 1 - lines = utils.getLines(infile) + lines = storage.getLines(infile) print('read {0} objects'.format(objNum)) def modifyTrajectoryFile(modifyLines, filenameIn, filenameOut): '''Reads filenameIn, replaces the lines with the result of modifyLines and writes the result in filenameOut''' - fileIn = utils.openCheck(filenameIn, 'r', True) - fileOut = utils.openCheck(filenameOut, "w", True) + fileIn = storage.openCheck(filenameIn, 'r', True) + fileOut = storage.openCheck(filenameOut, "w", True) - lines = utils.getLines(fileIn) + lines = storage.getLines(fileIn) trajNum = 0 while (lines != []): modifiedLines = modifyLines(trajNum, lines) @@ -97,7 +97,7 @@ for l in modifiedLines: fileOut.write(l+"\n") fileOut.write(utils.delimiterChar+"\n") - lines = utils.getLines(fileIn) + lines = storage.getLines(fileIn) trajNum += 1 fileIn.close() @@ -106,17 +106,17 @@ def copyTrajectoryFile(keepTrajectory, filenameIn, filenameOut): '''Reads filenameIn, keeps the trajectories for which the function keepTrajectory(trajNum, lines) is True and writes the result in filenameOut''' - fileIn = utils.openCheck(filenameIn, 'r', True) - fileOut = utils.openCheck(filenameOut, "w", True) + fileIn = storage.openCheck(filenameIn, 'r', True) + fileOut = storage.openCheck(filenameOut, "w", True) - lines = utils.getLines(fileIn) + lines = storage.getLines(fileIn) trajNum = 0 while (lines != []): if keepTrajectory(trajNum, lines): for l in lines: fileOut.write(l+"\n") fileOut.write(utils.delimiterChar+"\n") - lines = utils.getLines(fileIn) + lines = storage.getLines(fileIn) trajNum += 1 fileIn.close() @@ -125,14 +125,14 @@ def loadTrajectories(filename, nObjects = -1): '''Loads trajectories''' - file = utils.openCheck(filename) + file = storage.openCheck(filename) if (not file): return [] objects = [] objNum = 0 objectType = getFileType(filename) - lines = utils.getLines(file) + lines = storage.getLines(file) while (lines != []) and ((nObjects<0) or (objNum<nObjects)): l = lines[0].split(' ') parsedLine = [int(n) for n in l[:4]] @@ -162,7 +162,7 @@ else: print("Error two lines of data for feature %d"%(f.num)) - lines = utils.getLines(file) + lines = storage.getLines(file) file.close() return objects @@ -177,13 +177,13 @@ 'Loads interactions from the old UBC traffic event format' from events import Interaction from indicators import SeverityIndicator - file = utils.openCheck(filename) + file = storage.openCheck(filename) if (not file): return [] interactions = [] interactionNum = 0 - lines = utils.getLines(file) + lines = storage.getLines(file) while (lines != []) and ((nInteractions<0) or (interactionNum<nInteractions)): parsedLine = [int(n) for n in lines[0].split(' ')] inter = Interaction(interactionNum, TimeInterval(parsedLine[1],parsedLine[2]), parsedLine[3], parsedLine[4], categoryNum = parsedLine[5]) @@ -198,7 +198,7 @@ interactions.append(inter) interactionNum+=1 - lines = utils.getLines(file) + lines = storage.getLines(file) file.close() return interactions @@ -206,13 +206,13 @@ def loadCollisionPoints(filename, nPoints = -1): '''Loads collision points and returns a dict with keys as a pair of the numbers of the two interacting objects''' - file = utils.openCheck(filename) + file = storage.openCheck(filename) if (not file): return [] points = {} num = 0 - lines = utils.getLines(file) + lines = storage.getLines(file) while (lines != []) and ((nPoints<0) or (num<nPoints)): parsedLine = [int(n) for n in lines[0].split(' ')] protagonistNums = (parsedLine[0], parsedLine[1]) @@ -220,7 +220,7 @@ [float(n) for n in lines[2].split(' ')]] num+=1 - lines = utils.getLines(file) + lines = storage.getLines(file) file.close() return points