comparison python/ubc_utils.py @ 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 15e244d2a1b5
children 933670761a57
comparison
equal deleted inserted replaced
721:52272f6bf62a 722:49e99ca34a7d
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 import utils, events 4 import utils, events, storage
5 from moving import MovingObject, TimeInterval, Trajectory 5 from moving import MovingObject, TimeInterval, Trajectory
6 6
7 7
8 fileTypeNames = ['feature', 8 fileTypeNames = ['feature',
9 'object', 9 'object',
55 def saveTrajectoryUserTypes(inFilename, outFilename, objects): 55 def saveTrajectoryUserTypes(inFilename, outFilename, objects):
56 '''The program saves the objects, 56 '''The program saves the objects,
57 by just copying the corresponding trajectory and velocity data 57 by just copying the corresponding trajectory and velocity data
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 = storage.openCheck(inFilename)
61 outfile = utils.openCheck(outFilename,'w') 61 outfile = storage.openCheck(outFilename,'w')
62 62
63 if (inFilename.find('features') >= 0) or (not infile) or (not outfile): 63 if (inFilename.find('features') >= 0) or (not infile) or (not outfile):
64 return 64 return
65 65
66 lines = utils.getLines(infile) 66 lines = storage.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 = utils.getLines(infile) 83 lines = storage.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 = storage.openCheck(filenameIn, 'r', True)
90 fileOut = utils.openCheck(filenameOut, "w", True) 90 fileOut = storage.openCheck(filenameOut, "w", True)
91 91
92 lines = utils.getLines(fileIn) 92 lines = storage.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 = utils.getLines(fileIn) 100 lines = storage.getLines(fileIn)
101 trajNum += 1 101 trajNum += 1
102 102
103 fileIn.close() 103 fileIn.close()
104 fileOut.close() 104 fileOut.close()
105 105
106 def copyTrajectoryFile(keepTrajectory, filenameIn, filenameOut): 106 def copyTrajectoryFile(keepTrajectory, filenameIn, filenameOut):
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 = storage.openCheck(filenameIn, 'r', True)
110 fileOut = utils.openCheck(filenameOut, "w", True) 110 fileOut = storage.openCheck(filenameOut, "w", True)
111 111
112 lines = utils.getLines(fileIn) 112 lines = storage.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 = utils.getLines(fileIn) 119 lines = storage.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 file = storage.openCheck(filename)
129 if (not file): 129 if (not file):
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 = utils.getLines(file) 135 lines = storage.getLines(file)
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 %d"%(f.num)) 163 print("Error two lines of data for feature %d"%(f.num))
164 164
165 lines = utils.getLines(file) 165 lines = storage.getLines(file)
166 166
167 file.close() 167 file.close()
168 return objects 168 return objects
169 169
170 def getFeatureNumbers(objects): 170 def getFeatureNumbers(objects):
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 from events import Interaction
179 from indicators import SeverityIndicator 179 from indicators import SeverityIndicator
180 file = utils.openCheck(filename) 180 file = storage.openCheck(filename)
181 if (not file): 181 if (not file):
182 return [] 182 return []
183 183
184 interactions = [] 184 interactions = []
185 interactionNum = 0 185 interactionNum = 0
186 lines = utils.getLines(file) 186 lines = storage.getLines(file)
187 while (lines != []) and ((nInteractions<0) or (interactionNum<nInteractions)): 187 while (lines != []) and ((nInteractions<0) or (interactionNum<nInteractions)):
188 parsedLine = [int(n) for n in lines[0].split(' ')] 188 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]) 189 inter = Interaction(interactionNum, TimeInterval(parsedLine[1],parsedLine[2]), parsedLine[3], parsedLine[4], categoryNum = parsedLine[5])
190 190
191 indicatorFrameNums = [int(n) for n in lines[1].split(' ')] 191 indicatorFrameNums = [int(n) for n in lines[1].split(' ')]
196 values[indicatorFrameNums[i]] = v 196 values[indicatorFrameNums[i]] = v
197 inter.addIndicator(SeverityIndicator(severityIndicatorNames[indicatorNum], values, None, mostSevereIsMax[indicatorNum])) 197 inter.addIndicator(SeverityIndicator(severityIndicatorNames[indicatorNum], values, None, mostSevereIsMax[indicatorNum]))
198 198
199 interactions.append(inter) 199 interactions.append(inter)
200 interactionNum+=1 200 interactionNum+=1
201 lines = utils.getLines(file) 201 lines = storage.getLines(file)
202 202
203 file.close() 203 file.close()
204 return interactions 204 return interactions
205 205
206 def loadCollisionPoints(filename, nPoints = -1): 206 def loadCollisionPoints(filename, nPoints = -1):
207 '''Loads collision points and returns a dict 207 '''Loads collision points and returns a dict
208 with keys as a pair of the numbers of the two interacting objects''' 208 with keys as a pair of the numbers of the two interacting objects'''
209 file = utils.openCheck(filename) 209 file = storage.openCheck(filename)
210 if (not file): 210 if (not file):
211 return [] 211 return []
212 212
213 points = {} 213 points = {}
214 num = 0 214 num = 0
215 lines = utils.getLines(file) 215 lines = storage.getLines(file)
216 while (lines != []) and ((nPoints<0) or (num<nPoints)): 216 while (lines != []) and ((nPoints<0) or (num<nPoints)):
217 parsedLine = [int(n) for n in lines[0].split(' ')] 217 parsedLine = [int(n) for n in lines[0].split(' ')]
218 protagonistNums = (parsedLine[0], parsedLine[1]) 218 protagonistNums = (parsedLine[0], parsedLine[1])
219 points[protagonistNums] = [[float(n) for n in lines[1].split(' ')], 219 points[protagonistNums] = [[float(n) for n in lines[1].split(' ')],
220 [float(n) for n in lines[2].split(' ')]] 220 [float(n) for n in lines[2].split(' ')]]
221 221
222 num+=1 222 num+=1
223 lines = utils.getLines(file) 223 lines = storage.getLines(file)
224 224
225 file.close() 225 file.close()
226 return points 226 return points