Mercurial Hosting > traffic-intelligence
comparison trafficintelligence/ubc_utils.py @ 1028:cc5cb04b04b0
major update using the trafficintelligence package name and install through pip
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 15 Jun 2018 11:19:10 -0400 |
parents | python/ubc_utils.py@933670761a57 |
children | aafbc0bab925 |
comparison
equal
deleted
inserted
replaced
1027:6129296848d3 | 1028:cc5cb04b04b0 |
---|---|
1 #! /usr/bin/env python | |
2 '''Various utilities to load data saved by the UBC tool(s)''' | |
3 | |
4 from trafficintelligence import utils, events, storage | |
5 from trafficintelligence.moving import MovingObject, TimeInterval, Trajectory | |
6 | |
7 | |
8 fileTypeNames = ['feature', | |
9 'object', | |
10 'prototype', | |
11 'contoursequence'] | |
12 | |
13 severityIndicatorNames = ['Distance', | |
14 'Collision Course Cosine', | |
15 'Velocity Cosine', | |
16 'Speed Differential', | |
17 'Collision Probability', | |
18 'Severity Index', | |
19 'Time to Collision'] | |
20 | |
21 userTypeNames = ['car', | |
22 'pedestrian', | |
23 'twowheels', | |
24 'bus', | |
25 'truck'] | |
26 | |
27 # severityIndicator = {'Distance': 0, | |
28 # 'Cosine': 1, | |
29 # 'Velocity Cosine': 2, | |
30 # 'Speed Differential': 3, | |
31 # 'Collision Probability': 4, | |
32 # 'Severity Index': 5, | |
33 # 'TTC': 6} | |
34 | |
35 mostSevereIsMax = [False, | |
36 False, | |
37 True, | |
38 True, | |
39 True, | |
40 True, | |
41 False] | |
42 | |
43 ignoredValue = [None, None, None, None, None, None, -1] | |
44 | |
45 def getFileType(s): | |
46 'Finds the type in fileTypeNames' | |
47 for fileType in fileTypeNames: | |
48 if s.find(fileType)>0: | |
49 return fileType | |
50 return '' | |
51 | |
52 def isFileType(s, fileType): | |
53 return (s.find(fileType)>0) | |
54 | |
55 def saveTrajectoryUserTypes(inFilename, outFilename, objects): | |
56 '''The program saves the objects, | |
57 by just copying the corresponding trajectory and velocity data | |
58 from the inFilename, and saving the characteristics in objects (first line) | |
59 into outFilename''' | |
60 infile = storage.openCheck(inFilename) | |
61 outfile = storage.openCheck(outFilename,'w') | |
62 | |
63 if (inFilename.find('features') >= 0) or (not infile) or (not outfile): | |
64 return | |
65 | |
66 lines = storage.getLines(infile) | |
67 objNum = 0 # in inFilename | |
68 while lines != []: | |
69 # find object in objects (index i) | |
70 i = 0 | |
71 while (i<len(objects)) and (objects[i].num != objNum): | |
72 i+=1 | |
73 | |
74 if i<len(objects): | |
75 l = lines[0].split(' ') | |
76 l[3] = str(objects[i].userType) | |
77 outfile.write(' '.join(l)+'\n') | |
78 for l in lines[1:]: | |
79 outfile.write(l+'\n') | |
80 outfile.write(utils.delimiterChar+'\n') | |
81 # next object | |
82 objNum += 1 | |
83 lines = storage.getLines(infile) | |
84 | |
85 print('read {0} objects'.format(objNum)) | |
86 | |
87 def modifyTrajectoryFile(modifyLines, filenameIn, filenameOut): | |
88 '''Reads filenameIn, replaces the lines with the result of modifyLines and writes the result in filenameOut''' | |
89 fileIn = storage.openCheck(filenameIn, 'r', True) | |
90 fileOut = storage.openCheck(filenameOut, "w", True) | |
91 | |
92 lines = storage.getLines(fileIn) | |
93 trajNum = 0 | |
94 while (lines != []): | |
95 modifiedLines = modifyLines(trajNum, lines) | |
96 if modifiedLines: | |
97 for l in modifiedLines: | |
98 fileOut.write(l+"\n") | |
99 fileOut.write(utils.delimiterChar+"\n") | |
100 lines = storage.getLines(fileIn) | |
101 trajNum += 1 | |
102 | |
103 fileIn.close() | |
104 fileOut.close() | |
105 | |
106 def copyTrajectoryFile(keepTrajectory, filenameIn, filenameOut): | |
107 '''Reads filenameIn, keeps the trajectories for which the function keepTrajectory(trajNum, lines) is True | |
108 and writes the result in filenameOut''' | |
109 fileIn = storage.openCheck(filenameIn, 'r', True) | |
110 fileOut = storage.openCheck(filenameOut, "w", True) | |
111 | |
112 lines = storage.getLines(fileIn) | |
113 trajNum = 0 | |
114 while (lines != []): | |
115 if keepTrajectory(trajNum, lines): | |
116 for l in lines: | |
117 fileOut.write(l+"\n") | |
118 fileOut.write(utils.delimiterChar+"\n") | |
119 lines = storage.getLines(fileIn) | |
120 trajNum += 1 | |
121 | |
122 fileIn.close() | |
123 fileOut.close() | |
124 | |
125 def loadTrajectories(filename, nObjects = -1): | |
126 '''Loads trajectories''' | |
127 | |
128 file = storage.openCheck(filename) | |
129 if (not file): | |
130 return [] | |
131 | |
132 objects = [] | |
133 objNum = 0 | |
134 objectType = getFileType(filename) | |
135 lines = storage.getLines(file) | |
136 while (lines != []) and ((nObjects<0) or (objNum<nObjects)): | |
137 l = lines[0].split(' ') | |
138 parsedLine = [int(n) for n in l[:4]] | |
139 obj = MovingObject(num = objNum, timeInterval = TimeInterval(parsedLine[1],parsedLine[2])) | |
140 #add = True | |
141 if len(lines) >= 3: | |
142 obj.positions = Trajectory.load(lines[1], lines[2]) | |
143 if len(lines) >= 5: | |
144 obj.velocities = Trajectory.load(lines[3], lines[4]) | |
145 if objectType == 'object': | |
146 obj.userType = parsedLine[3] | |
147 obj.nObjects = float(l[4]) | |
148 obj.featureNumbers = [int(n) for n in l[5:]] | |
149 | |
150 # load contour data if available | |
151 if len(lines) >= 6: | |
152 obj.contourType = utils.line2Floats(lines[6]) | |
153 obj.contourOrigins = Trajectory.load(lines[7], lines[8]) | |
154 obj.contourSizes = Trajectory.load(lines[9], lines[10]) | |
155 elif objectType == 'prototype': | |
156 obj.userType = parsedLine[3] | |
157 obj.nMatchings = int(l[4]) | |
158 | |
159 if len(lines) != 2: | |
160 objects.append(obj) | |
161 objNum+=1 | |
162 else: | |
163 print("Error two lines of data for feature {}".format(f.num)) | |
164 | |
165 lines = storage.getLines(file) | |
166 | |
167 file.close() | |
168 return objects | |
169 | |
170 def getFeatureNumbers(objects): | |
171 featureNumbers=[] | |
172 for o in objects: | |
173 featureNumbers += o.featureNumbers | |
174 return featureNumbers | |
175 | |
176 def loadInteractions(filename, nInteractions = -1): | |
177 'Loads interactions from the old UBC traffic event format' | |
178 from events import Interaction | |
179 from indicators import SeverityIndicator | |
180 file = storage.openCheck(filename) | |
181 if (not file): | |
182 return [] | |
183 | |
184 interactions = [] | |
185 interactionNum = 0 | |
186 lines = storage.getLines(file) | |
187 while (lines != []) and ((nInteractions<0) or (interactionNum<nInteractions)): | |
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]) | |
190 | |
191 indicatorFrameNums = [int(n) for n in lines[1].split(' ')] | |
192 for indicatorNum,line in enumerate(lines[2:]): | |
193 values = {} | |
194 for i,v in enumerate([float(n) for n in line.split(' ')]): | |
195 if not ignoredValue[indicatorNum] or v != ignoredValue[indicatorNum]: | |
196 values[indicatorFrameNums[i]] = v | |
197 inter.addIndicator(SeverityIndicator(severityIndicatorNames[indicatorNum], values, None, mostSevereIsMax[indicatorNum])) | |
198 | |
199 interactions.append(inter) | |
200 interactionNum+=1 | |
201 lines = storage.getLines(file) | |
202 | |
203 file.close() | |
204 return interactions | |
205 | |
206 def loadCollisionPoints(filename, nPoints = -1): | |
207 '''Loads collision points and returns a dict | |
208 with keys as a pair of the numbers of the two interacting objects''' | |
209 file = storage.openCheck(filename) | |
210 if (not file): | |
211 return [] | |
212 | |
213 points = {} | |
214 num = 0 | |
215 lines = storage.getLines(file) | |
216 while (lines != []) and ((nPoints<0) or (num<nPoints)): | |
217 parsedLine = [int(n) for n in lines[0].split(' ')] | |
218 protagonistNums = (parsedLine[0], parsedLine[1]) | |
219 points[protagonistNums] = [[float(n) for n in lines[1].split(' ')], | |
220 [float(n) for n in lines[2].split(' ')]] | |
221 | |
222 num+=1 | |
223 lines = storage.getLines(file) | |
224 | |
225 file.close() | |
226 return points |