Mercurial Hosting > traffic-intelligence
comparison python/events.py @ 619:dc2d0a0d7fe1
merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 10 Dec 2014 15:27:08 -0500 |
parents | 306db0f3c7a2 |
children | 82e9f78a4714 |
comparison
equal
deleted
inserted
replaced
596:04a8304e13f0 | 619:dc2d0a0d7fe1 |
---|---|
7 | 7 |
8 import multiprocessing | 8 import multiprocessing |
9 import itertools | 9 import itertools |
10 | 10 |
11 import moving, prediction, indicators, utils | 11 import moving, prediction, indicators, utils |
12 | |
13 __metaclass__ = type | 12 __metaclass__ = type |
13 | |
14 def findRoute(prototypes,objects,i,j,noiseEntryNums,noiseExitNums,minSimilarity= 0.3, spatialThreshold=1.0, delta=180): | |
15 if i[0] not in noiseEntryNums: | |
16 prototypesRoutes= [ x for x in sorted(prototypes.keys()) if i[0]==x[0]] | |
17 elif i[1] not in noiseExitNums: | |
18 prototypesRoutes=[ x for x in sorted(prototypes.keys()) if i[1]==x[1]] | |
19 else: | |
20 prototypesRoutes=[x for x in sorted(prototypes.keys())] | |
21 routeSim={} | |
22 lcss = utils.LCSS(similarityFunc=lambda x,y: (distanceForLCSS(x,y) <= spatialThreshold),delta=delta) | |
23 for y in prototypesRoutes: | |
24 if y in prototypes.keys(): | |
25 prototypesIDs=prototypes[y] | |
26 similarity=[] | |
27 for x in prototypesIDs: | |
28 s=lcss.computeNormalized(objects[j].positions, objects[x].positions) | |
29 similarity.append(s) | |
30 routeSim[y]=max(similarity) | |
31 route=max(routeSim, key=routeSim.get) | |
32 if routeSim[route]>=minSimilarity: | |
33 return route | |
34 else: | |
35 return i | |
36 | |
37 def getRoute(obj,prototypes,objects,noiseEntryNums,noiseExitNums,useDestination=True): | |
38 route=(obj.startRouteID,obj.endRouteID) | |
39 if useDestination: | |
40 if route not in prototypes.keys(): | |
41 route= findRoute(prototypes,objects,route,obj.getNum(),noiseEntryNums,noiseExitNums) | |
42 return route | |
14 | 43 |
15 class Interaction(moving.STObject): | 44 class Interaction(moving.STObject): |
16 '''Class for an interaction between two road users | 45 '''Class for an interaction between two road users |
17 or a road user and an obstacle | 46 or a road user and an obstacle |
18 | 47 |
122 minDistance={} | 151 minDistance={} |
123 for instant in self.timeInterval: | 152 for instant in self.timeInterval: |
124 minDistance[instant] = moving.MovingObject.minDistance(self.roadUser1, self.roadUser2, instant) | 153 minDistance[instant] = moving.MovingObject.minDistance(self.roadUser1, self.roadUser2, instant) |
125 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[3], minDistance)) | 154 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[3], minDistance)) |
126 | 155 |
127 def computeCrossingsCollisions(self, predictionParameters, collisionDistanceThreshold, timeHorizon, computeCZ = False, debug = False, timeInterval = None, nProcesses = 1): | 156 def computeCrossingsCollisions(self, predictionParameters, collisionDistanceThreshold, timeHorizon, computeCZ = False, debug = False, timeInterval = None, nProcesses = 1,usePrototypes=True,route1= (-1,-1),route2=(-1,-1),prototypes={},secondStepPrototypes={},nMatching={},objects=[],noiseEntryNums=[],noiseExitNums=[],minSimilarity=0.1,mostMatched=None,useDestination=True,useSpeedPrototype=True,acceptPartialLength=30, step=1): |
128 '''Computes all crossing and collision points at each common instant for two road users. ''' | 157 '''Computes all crossing and collision points at each common instant for two road users. ''' |
129 self.collisionPoints={} | 158 self.collisionPoints={} |
130 self.crossingZones={} | 159 self.crossingZones={} |
131 TTCs = {} | 160 TTCs = {} |
161 if usePrototypes: | |
162 route1= getRoute(self.roadUser1,prototypes,objects,noiseEntryNums,noiseExitNums,useDestination) | |
163 route2= getRoute(self.roadUser2,prototypes,objects,noiseEntryNums,noiseExitNums,useDestination) | |
132 | 164 |
133 if timeInterval: | 165 if timeInterval: |
134 commonTimeInterval = timeInterval | 166 commonTimeInterval = timeInterval |
135 else: | 167 else: |
136 commonTimeInterval = self.timeInterval | 168 commonTimeInterval = self.timeInterval |
137 self.collisionPoints, self.crossingZones = prediction.computeCrossingsCollisions(predictionParameters, self.roadUser1, self.roadUser2, collisionDistanceThreshold, timeHorizon, computeCZ, debug, commonTimeInterval, nProcesses) | 169 self.collisionPoints, self.crossingZones = prediction.computeCrossingsCollisions(predictionParameters, self.roadUser1, self.roadUser2, collisionDistanceThreshold, timeHorizon, computeCZ, debug, commonTimeInterval, nProcesses,usePrototypes,route1,route2,prototypes,secondStepPrototypes,nMatching,objects,noiseEntryNums,noiseExitNums,minSimilarity,mostMatched,useDestination,useSpeedPrototype,acceptPartialLength, step) |
138 for i, cp in self.collisionPoints.iteritems(): | 170 for i, cp in self.collisionPoints.iteritems(): |
139 TTCs[i] = prediction.SafetyPoint.computeExpectedIndicator(cp) | 171 TTCs[i] = prediction.SafetyPoint.computeExpectedIndicator(cp) |
140 # add probability of collision, and probability of successful evasive action | 172 # add probability of collision, and probability of successful evasive action |
141 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[7], TTCs)) | 173 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[7], TTCs)) |
142 | 174 |
143 if computeCZ: | 175 if computeCZ: |
144 pPETs = {} | 176 pPETs = {} |
145 for i in list(commonTimeInterval)[:-1]: | 177 for i, cz in self.crossingZones.iteritems(): |
146 if len(self.crossingZones[i]) > 0: | 178 pPETs[i] = prediction.SafetyPoint.computeExpectedIndicator(cz) |
147 pPETs[i] = prediction.SafetyPoint.computeExpectedIndicator(self.crossingZones[i]) | |
148 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[9], pPETs)) | 179 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[9], pPETs)) |
149 | 180 |
150 def addVideoFilename(self,videoFilename): | 181 def addVideoFilename(self,videoFilename): |
151 self.videoFilename= videoFilename | 182 self.videoFilename= videoFilename |
152 | 183 |
153 def addInteractionType(self,interactionType): | 184 def addInteractionType(self,interactionType): |
154 ''' interaction types: conflict or collision if they are known''' | 185 ''' interaction types: conflict or collision if they are known''' |
155 self.interactionType= interactionType | 186 self.interactionType= interactionType |
156 | 187 |