Mercurial > hg > nsaunier > traffic-intelligence
comparison python/events.py @ 452:c59a47ce0209
reorganized interactioninterval (in compute indicators) and comments
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Thu, 13 Feb 2014 14:10:52 -0500 |
| parents | cd342a774806 |
| children | 62d05436099d |
comparison
equal
deleted
inserted
replaced
| 451:cd342a774806 | 452:c59a47ce0209 |
|---|---|
| 84 collisionCourseDotProducts = {}#[0]*int(self.timeInterval.length()) | 84 collisionCourseDotProducts = {}#[0]*int(self.timeInterval.length()) |
| 85 collisionCourseAngles = {} | 85 collisionCourseAngles = {} |
| 86 velocityAngles = {} | 86 velocityAngles = {} |
| 87 distances = {}#[0]*int(self.timeInterval.length()) | 87 distances = {}#[0]*int(self.timeInterval.length()) |
| 88 speedDifferentials = {} | 88 speedDifferentials = {} |
| 89 interactionInstants = [] | |
| 89 for instant in self.timeInterval: | 90 for instant in self.timeInterval: |
| 90 deltap = self.roadUser1.getPositionAtInstant(instant)-self.roadUser2.getPositionAtInstant(instant) | 91 deltap = self.roadUser1.getPositionAtInstant(instant)-self.roadUser2.getPositionAtInstant(instant) |
| 91 v1 = self.roadUser1.getVelocityAtInstant(instant) | 92 v1 = self.roadUser1.getVelocityAtInstant(instant) |
| 92 v2 = self.roadUser2.getVelocityAtInstant(instant) | 93 v2 = self.roadUser2.getVelocityAtInstant(instant) |
| 93 deltav = v2-v1 | 94 deltav = v2-v1 |
| 94 velocityAngles[instant] = arccos(moving.Point.dot(v1, v2)/(v1.norm2()*v2.norm2())) | 95 velocityAngles[instant] = arccos(moving.Point.dot(v1, v2)/(v1.norm2()*v2.norm2())) |
| 95 collisionCourseDotProducts[instant] = moving.Point.dot(deltap, deltav) | 96 collisionCourseDotProducts[instant] = moving.Point.dot(deltap, deltav) |
| 96 distances[instant] = deltap.norm2() | 97 distances[instant] = deltap.norm2() |
| 97 speedDifferentials[instant] = deltav.norm2() | 98 speedDifferentials[instant] = deltav.norm2() |
| 98 #if collisionCourseDotProducts[instant] > 0: | 99 if collisionCourseDotProducts[instant] > 0: |
| 100 interactionInstants.append(instant) | |
| 99 collisionCourseAngles[instant] = arccos(collisionCourseDotProducts[instant]/(distances[instant]*speedDifferentials[instant])) | 101 collisionCourseAngles[instant] = arccos(collisionCourseDotProducts[instant]/(distances[instant]*speedDifferentials[instant])) |
| 100 | 102 |
| 101 # todo shorten the time intervals based on the interaction definition | 103 if len(interactionInstants) >= 2: |
| 104 self.interactionInterval = moving.TimeInterval(interactionInstants[0], interactionInstants[-1]) | |
| 105 else: | |
| 106 self.interactionInterval = moving.TimeInterval() | |
| 102 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[0], collisionCourseDotProducts)) | 107 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[0], collisionCourseDotProducts)) |
| 103 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[1], collisionCourseAngles)) | 108 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[1], collisionCourseAngles)) |
| 104 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[2], distances)) | 109 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[2], distances)) |
| 105 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[4], velocityAngles)) | 110 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[4], velocityAngles)) |
| 106 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[5], speedDifferentials)) | 111 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[5], speedDifferentials)) |
| 107 | |
| 108 # todo test for interaction instants and interval, compute indicators | |
| 109 | 112 |
| 110 # if we have features, compute other indicators | 113 # if we have features, compute other indicators |
| 111 if self.roadUser1.features != None and self.roadUser2.features != None: | 114 if self.roadUser1.features != None and self.roadUser2.features != None: |
| 112 minDistance={} | 115 minDistance={} |
| 113 for instant in self.timeInterval: | 116 for instant in self.timeInterval: |
| 143 | 146 |
| 144 def addInteractionType(self,interactionType): | 147 def addInteractionType(self,interactionType): |
| 145 ''' interaction types: conflict or collision if they are known''' | 148 ''' interaction types: conflict or collision if they are known''' |
| 146 self.interactionType= interactionType | 149 self.interactionType= interactionType |
| 147 | 150 |
| 148 def computeInteractionInterval(self): | |
| 149 ''' Computes the times during which the road users are getting closer, | |
| 150 ie the collision course angle is positive | |
| 151 (Other thresholds/indicators could be tried)''' | |
| 152 collisionCourseDotProducts = self.getIndicator(Interaction.indicatorNames[0]) | |
| 153 inter = collisionCourseDotProducts.getTimeInterval() | |
| 154 interactionInstants = [t for t in inter if collisionCourseDotProducts[t] >= 0] | |
| 155 if len(interactionInstants) >= 2: | |
| 156 self.interactionInterval = moving.TimeInterval(interactionInstants[0], interactionInstants[-1]) | |
| 157 else: | |
| 158 self.interactionInterval = moving.TimeInterval() | |
| 159 | |
| 160 def createInteractions(objects): | 151 def createInteractions(objects): |
| 161 '''Create all interactions of two co-existing road users | 152 '''Create all interactions of two co-existing road users |
| 162 | 153 |
| 163 todo add test to compute categories?''' | 154 todo add test to compute categories?''' |
| 164 interactions = [] | 155 interactions = [] |
| 174 def prototypeCluster(interactions, similarityMatrix, alignmentMatrix, indicatorName, minSimilarity): | 165 def prototypeCluster(interactions, similarityMatrix, alignmentMatrix, indicatorName, minSimilarity): |
| 175 '''Finds exemplar indicator time series for all interactions | 166 '''Finds exemplar indicator time series for all interactions |
| 176 Returns the prototype indices (in the interaction list) and the label of each indicator (interaction) | 167 Returns the prototype indices (in the interaction list) and the label of each indicator (interaction) |
| 177 | 168 |
| 178 if an indicator profile (time series) is different enough (<minSimilarity), | 169 if an indicator profile (time series) is different enough (<minSimilarity), |
| 179 it will become a new prototype. Otherwise, it will be assigned to an existing prototypes''' | 170 it will become a new prototype. |
| 171 Non-prototype interactions will be assigned to an existing prototype''' | |
| 180 | 172 |
| 181 # sort indicators based on length | 173 # sort indicators based on length |
| 182 indices = range(similarityMatrix.shape[0]) | 174 indices = range(similarityMatrix.shape[0]) |
| 183 def compare(i, j): | 175 def compare(i, j): |
| 184 if len(interactions[i].getIndicator(indicatorName)) > len(interactions[j].getIndicator(indicatorName)): | 176 if len(interactions[i].getIndicator(indicatorName)) > len(interactions[j].getIndicator(indicatorName)): |
| 202 for i in indices: | 194 for i in indices: |
| 203 prototypeIndex = similarityMatrix[i][prototypeIndices].argmax() | 195 prototypeIndex = similarityMatrix[i][prototypeIndices].argmax() |
| 204 labels[i] = prototypeIndices[prototypeIndex] | 196 labels[i] = prototypeIndices[prototypeIndex] |
| 205 | 197 |
| 206 return prototypeIndices, labels | 198 return prototypeIndices, labels |
| 199 | |
| 200 def prototypeMultivariateCluster(interactions, similarityMatrics, indicatorNames, minSimilarities, minClusterSize): | |
| 201 '''Finds exmaple indicator time series (several indicators) for all interactions | |
| 202 | |
| 203 if any interaction indicator time series is different enough (<minSimilarity), | |
| 204 it will become a new prototype. | |
| 205 Non-prototype interactions will be assigned to an existing prototype if all indicators are similar enough''' | |
| 206 pass | |
| 207 | 207 |
| 208 # TODO: | 208 # TODO: |
| 209 #http://stackoverflow.com/questions/3288595/multiprocessing-using-pool-map-on-a-function-defined-in-a-class | 209 #http://stackoverflow.com/questions/3288595/multiprocessing-using-pool-map-on-a-function-defined-in-a-class |
| 210 #http://www.rueckstiess.net/research/snippets/show/ca1d7d90 | 210 #http://www.rueckstiess.net/research/snippets/show/ca1d7d90 |
| 211 def calculateIndicatorPipe(pairs, predParam, timeHorizon=75,collisionDistanceThreshold=1.8): | 211 def calculateIndicatorPipe(pairs, predParam, timeHorizon=75,collisionDistanceThreshold=1.8): |
