Mercurial > hg > nsaunier > traffic-intelligence
comparison trafficintelligence/ubc_utils.py @ 1101:e23828659a66
bug fixes
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 22 Feb 2019 17:22:04 -0500 |
| parents | aafbc0bab925 |
| children |
comparison
equal
deleted
inserted
replaced
| 1100:1e833fd8490d | 1101:e23828659a66 |
|---|---|
| 1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
| 2 '''Various utilities to load data saved by the UBC tool(s)''' | 2 '''Various utilities to load data saved by the UBC tool(s)''' |
| 3 | 3 |
| 4 from trafficintelligence import utils, events, storage | 4 from trafficintelligence import utils, events, storage, indicators |
| 5 from trafficintelligence.moving import MovingObject, TimeInterval, Trajectory | 5 from trafficintelligence.moving import MovingObject, TimeInterval, Trajectory |
| 6 | 6 |
| 7 | 7 |
| 8 fileTypeNames = ['feature', | 8 fileTypeNames = ['feature', |
| 9 'object', | 9 'object', |
| 58 from the inFilename, and saving the characteristics in objects (first line) | 58 from the inFilename, and saving the characteristics in objects (first line) |
| 59 into outFilename''' | 59 into outFilename''' |
| 60 infile = utils.openCheck(inFilename) | 60 infile = utils.openCheck(inFilename) |
| 61 outfile = utils.openCheck(outFilename,'w') | 61 outfile = utils.openCheck(outFilename,'w') |
| 62 | 62 |
| 63 if (inFilename.find('features') >= 0) or (not infile) or (not outfile): | 63 if (inFilename.find('features') >= 0) or infile is None or outfile is None: |
| 64 return | 64 return |
| 65 | 65 |
| 66 lines = storage.getLines(infile) | 66 lines = utils.getLines(infile) |
| 67 objNum = 0 # in inFilename | 67 objNum = 0 # in inFilename |
| 68 while lines != []: | 68 while lines != []: |
| 69 # find object in objects (index i) | 69 # find object in objects (index i) |
| 70 i = 0 | 70 i = 0 |
| 71 while (i<len(objects)) and (objects[i].num != objNum): | 71 while (i<len(objects)) and (objects[i].num != objNum): |
| 78 for l in lines[1:]: | 78 for l in lines[1:]: |
| 79 outfile.write(l+'\n') | 79 outfile.write(l+'\n') |
| 80 outfile.write(utils.delimiterChar+'\n') | 80 outfile.write(utils.delimiterChar+'\n') |
| 81 # next object | 81 # next object |
| 82 objNum += 1 | 82 objNum += 1 |
| 83 lines = storage.getLines(infile) | 83 lines = utils.getLines(infile) |
| 84 | 84 |
| 85 print('read {0} objects'.format(objNum)) | 85 print('read {0} objects'.format(objNum)) |
| 86 | 86 |
| 87 def modifyTrajectoryFile(modifyLines, filenameIn, filenameOut): | 87 def modifyTrajectoryFile(modifyLines, filenameIn, filenameOut): |
| 88 '''Reads filenameIn, replaces the lines with the result of modifyLines and writes the result in filenameOut''' | 88 '''Reads filenameIn, replaces the lines with the result of modifyLines and writes the result in filenameOut''' |
| 89 fileIn = utils.openCheck(filenameIn, 'r', True) | 89 fileIn = utils.openCheck(filenameIn, 'r', True) |
| 90 fileOut = utils.openCheck(filenameOut, "w", True) | 90 fileOut = utils.openCheck(filenameOut, "w", True) |
| 91 | 91 |
| 92 lines = storage.getLines(fileIn) | 92 lines = utils.getLines(fileIn) |
| 93 trajNum = 0 | 93 trajNum = 0 |
| 94 while (lines != []): | 94 while (lines != []): |
| 95 modifiedLines = modifyLines(trajNum, lines) | 95 modifiedLines = modifyLines(trajNum, lines) |
| 96 if modifiedLines: | 96 if modifiedLines: |
| 97 for l in modifiedLines: | 97 for l in modifiedLines: |
| 98 fileOut.write(l+"\n") | 98 fileOut.write(l+"\n") |
| 99 fileOut.write(utils.delimiterChar+"\n") | 99 fileOut.write(utils.delimiterChar+"\n") |
| 100 lines = storage.getLines(fileIn) | 100 lines = utils.getLines(fileIn) |
| 101 trajNum += 1 | 101 trajNum += 1 |
| 102 | 102 |
| 103 fileIn.close() | 103 fileIn.close() |
| 104 fileOut.close() | 104 fileOut.close() |
| 105 | 105 |
| 107 '''Reads filenameIn, keeps the trajectories for which the function keepTrajectory(trajNum, lines) is True | 107 '''Reads filenameIn, keeps the trajectories for which the function keepTrajectory(trajNum, lines) is True |
| 108 and writes the result in filenameOut''' | 108 and writes the result in filenameOut''' |
| 109 fileIn = utils.openCheck(filenameIn, 'r', True) | 109 fileIn = utils.openCheck(filenameIn, 'r', True) |
| 110 fileOut = utils.openCheck(filenameOut, "w", True) | 110 fileOut = utils.openCheck(filenameOut, "w", True) |
| 111 | 111 |
| 112 lines = storage.getLines(fileIn) | 112 lines = utils.getLines(fileIn) |
| 113 trajNum = 0 | 113 trajNum = 0 |
| 114 while (lines != []): | 114 while (lines != []): |
| 115 if keepTrajectory(trajNum, lines): | 115 if keepTrajectory(trajNum, lines): |
| 116 for l in lines: | 116 for l in lines: |
| 117 fileOut.write(l+"\n") | 117 fileOut.write(l+"\n") |
| 118 fileOut.write(utils.delimiterChar+"\n") | 118 fileOut.write(utils.delimiterChar+"\n") |
| 119 lines = storage.getLines(fileIn) | 119 lines = utils.getLines(fileIn) |
| 120 trajNum += 1 | 120 trajNum += 1 |
| 121 | 121 |
| 122 fileIn.close() | 122 fileIn.close() |
| 123 fileOut.close() | 123 fileOut.close() |
| 124 | 124 |
| 125 def loadTrajectories(filename, nObjects = -1): | 125 def loadTrajectories(filename, nObjects = -1): |
| 126 '''Loads trajectories''' | 126 '''Loads trajectories''' |
| 127 | 127 |
| 128 file = utils.openCheck(filename) | 128 f = utils.openCheck(filename) |
| 129 if (not file): | 129 if f is None: |
| 130 return [] | 130 return [] |
| 131 | 131 |
| 132 objects = [] | 132 objects = [] |
| 133 objNum = 0 | 133 objNum = 0 |
| 134 objectType = getFileType(filename) | 134 objectType = getFileType(filename) |
| 135 lines = storage.getLines(file) | 135 lines = utils.getLines(f) |
| 136 while (lines != []) and ((nObjects<0) or (objNum<nObjects)): | 136 while (lines != []) and ((nObjects<0) or (objNum<nObjects)): |
| 137 l = lines[0].split(' ') | 137 l = lines[0].split(' ') |
| 138 parsedLine = [int(n) for n in l[:4]] | 138 parsedLine = [int(n) for n in l[:4]] |
| 139 obj = MovingObject(num = objNum, timeInterval = TimeInterval(parsedLine[1],parsedLine[2])) | 139 obj = MovingObject(num = objNum, timeInterval = TimeInterval(parsedLine[1],parsedLine[2])) |
| 140 #add = True | 140 #add = True |
| 160 objects.append(obj) | 160 objects.append(obj) |
| 161 objNum+=1 | 161 objNum+=1 |
| 162 else: | 162 else: |
| 163 print("Error two lines of data for feature {}".format(f.num)) | 163 print("Error two lines of data for feature {}".format(f.num)) |
| 164 | 164 |
| 165 lines = storage.getLines(file) | 165 lines = utils.getLines(f) |
| 166 | 166 |
| 167 file.close() | 167 f.close() |
| 168 return objects | 168 return objects |
| 169 | 169 |
| 170 def getFeatureNumbers(objects): | 170 def getFeatureNumbers(objects): |
| 171 featureNumbers=[] | 171 featureNumbers=[] |
| 172 for o in objects: | 172 for o in objects: |
| 173 featureNumbers += o.featureNumbers | 173 featureNumbers += o.featureNumbers |
| 174 return featureNumbers | 174 return featureNumbers |
| 175 | 175 |
| 176 def loadInteractions(filename, nInteractions = -1): | 176 def loadInteractions(filename, nInteractions = -1): |
| 177 'Loads interactions from the old UBC traffic event format' | 177 'Loads interactions from the old UBC traffic event format' |
| 178 from events import Interaction | 178 f = utils.openCheck(filename) |
| 179 from indicators import SeverityIndicator | 179 if f is None: |
| 180 file = utils.openCheck(filename) | |
| 181 if (not file): | |
| 182 return [] | 180 return [] |
| 183 | 181 |
| 184 interactions = [] | 182 interactions = [] |
| 185 interactionNum = 0 | 183 interactionNum = 0 |
| 186 lines = storage.getLines(file) | 184 lines = utils.getLines(f) |
| 187 while (lines != []) and ((nInteractions<0) or (interactionNum<nInteractions)): | 185 while (lines != []) and ((nInteractions<0) or (interactionNum<nInteractions)): |
| 188 parsedLine = [int(n) for n in lines[0].split(' ')] | 186 parsedLine = [int(n) for n in lines[0].split(' ')] |
| 189 inter = Interaction(interactionNum, TimeInterval(parsedLine[1],parsedLine[2]), parsedLine[3], parsedLine[4], categoryNum = parsedLine[5]) | 187 inter = events.Interaction(interactionNum, TimeInterval(parsedLine[1],parsedLine[2]), parsedLine[3], parsedLine[4], categoryNum = parsedLine[5]) |
| 190 | 188 |
| 191 indicatorFrameNums = [int(n) for n in lines[1].split(' ')] | 189 indicatorFrameNums = [int(n) for n in lines[1].split(' ')] |
| 192 for indicatorNum,line in enumerate(lines[2:]): | 190 for indicatorNum,line in enumerate(lines[2:]): |
| 193 values = {} | 191 values = {} |
| 194 for i,v in enumerate([float(n) for n in line.split(' ')]): | 192 for i,v in enumerate([float(n) for n in line.split(' ')]): |
| 195 if not ignoredValue[indicatorNum] or v != ignoredValue[indicatorNum]: | 193 if not ignoredValue[indicatorNum] or v != ignoredValue[indicatorNum]: |
| 196 values[indicatorFrameNums[i]] = v | 194 values[indicatorFrameNums[i]] = v |
| 197 inter.addIndicator(SeverityIndicator(severityIndicatorNames[indicatorNum], values, None, mostSevereIsMax[indicatorNum])) | 195 inter.addIndicator(indicators.SeverityIndicator(severityIndicatorNames[indicatorNum], values, None, mostSevereIsMax[indicatorNum])) |
| 198 | 196 |
| 199 interactions.append(inter) | 197 interactions.append(inter) |
| 200 interactionNum+=1 | 198 interactionNum+=1 |
| 201 lines = storage.getLines(file) | 199 lines = utils.getLines(f) |
| 202 | 200 |
| 203 file.close() | 201 f.close() |
| 204 return interactions | 202 return interactions |
| 205 | 203 |
| 206 def loadCollisionPoints(filename, nPoints = -1): | 204 def loadCollisionPoints(filename, nPoints = -1): |
| 207 '''Loads collision points and returns a dict | 205 '''Loads collision points and returns a dict |
| 208 with keys as a pair of the numbers of the two interacting objects''' | 206 with keys as a pair of the numbers of the two interacting objects''' |
| 209 file = utils.openCheck(filename) | 207 f = utils.openCheck(filename) |
| 210 if (not file): | 208 if f is None: |
| 211 return [] | 209 return [] |
| 212 | 210 |
| 213 points = {} | 211 points = {} |
| 214 num = 0 | 212 num = 0 |
| 215 lines = storage.getLines(file) | 213 lines = utils.getLines(f) |
| 216 while (lines != []) and ((nPoints<0) or (num<nPoints)): | 214 while (lines != []) and ((nPoints<0) or (num<nPoints)): |
| 217 parsedLine = [int(n) for n in lines[0].split(' ')] | 215 parsedLine = [int(n) for n in lines[0].split(' ')] |
| 218 protagonistNums = (parsedLine[0], parsedLine[1]) | 216 protagonistNums = (parsedLine[0], parsedLine[1]) |
| 219 points[protagonistNums] = [[float(n) for n in lines[1].split(' ')], | 217 points[protagonistNums] = [[float(n) for n in lines[1].split(' ')], |
| 220 [float(n) for n in lines[2].split(' ')]] | 218 [float(n) for n in lines[2].split(' ')]] |
| 221 | 219 |
| 222 num+=1 | 220 num+=1 |
| 223 lines = storage.getLines(file) | 221 lines = utils.getLines(f) |
| 224 | 222 |
| 225 file.close() | 223 f.close() |
| 226 return points | 224 return points |
