annotate python/events.py @ 607:84690dfe5560

add some functions for behaviour analysis
author MohamedGomaa
date Tue, 25 Nov 2014 22:49:47 -0500
parents 07b1bd0785cd
children 0dc36203973d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
56
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
1 #! /usr/bin/env python
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
2 '''Libraries for events
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
3 Interactions, pedestrian crossing...'''
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
4
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
5 import numpy as np
306
93d851d0d21e bug correction, minor work on indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 299
diff changeset
6 from numpy import arccos
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
7
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
8 import multiprocessing
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
9 import itertools
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
10
607
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
11 import sys
341
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
12 import moving, prediction, indicators, utils
607
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
13 sys.path.append("D:/behaviourAnalysis/libs")
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
14 import trajLearning
56
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
15 __metaclass__ = type
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
16
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
17 class Interaction(moving.STObject):
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
18 '''Class for an interaction between two road users
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
19 or a road user and an obstacle
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
20
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
21 link to the moving objects
294
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
22 contains the indicators in a dictionary with the names as keys
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
23 '''
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
24
320
419f30491a4b renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 317
diff changeset
25 categories = {'Head On': 0,
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
26 'rearend': 1,
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
27 'side': 2,
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
28 'parallel': 3}
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
29
320
419f30491a4b renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 317
diff changeset
30 indicatorNames = ['Collision Course Dot Product',
419f30491a4b renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 317
diff changeset
31 'Collision Course Angle',
419f30491a4b renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 317
diff changeset
32 'Distance',
419f30491a4b renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 317
diff changeset
33 'Minimum Distance',
341
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
34 'Velocity Angle',
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
35 'Speed Differential',
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
36 'Collision Probability',
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
37 'Time to Collision',
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
38 'Probability of Successful Evasive Action',
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
39 'predicted Post Encroachment Time']
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
40
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
41 indicatorNameToIndices = utils.inverseEnumeration(indicatorNames)
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
42
388
6e0dedd34920 minor name change
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 387
diff changeset
43 indicatorShortNames = ['CCDP',
408
365d8dee44f3 last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 388
diff changeset
44 'CCA',
341
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
45 'Dist',
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
46 'MinDist',
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
47 'VA',
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
48 'SD',
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
49 'PoC',
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
50 'TTC',
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
51 'P(SEA)',
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
52 'pPET']
320
419f30491a4b renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 317
diff changeset
53
408
365d8dee44f3 last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 388
diff changeset
54 indicatorUnits = ['',
365d8dee44f3 last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 388
diff changeset
55 'rad',
365d8dee44f3 last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 388
diff changeset
56 'm',
365d8dee44f3 last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 388
diff changeset
57 'm',
365d8dee44f3 last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 388
diff changeset
58 'rad',
365d8dee44f3 last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 388
diff changeset
59 'm/s',
365d8dee44f3 last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 388
diff changeset
60 '',
365d8dee44f3 last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 388
diff changeset
61 's',
365d8dee44f3 last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 388
diff changeset
62 '',
365d8dee44f3 last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 388
diff changeset
63 '']
365d8dee44f3 last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 388
diff changeset
64
320
419f30491a4b renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 317
diff changeset
65 def __init__(self, num = None, timeInterval = None, roaduserNum1 = None, roaduserNum2 = None, roadUser1 = None, roadUser2 = None, categoryNum = None):
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
66 moving.STObject.__init__(self, num, timeInterval)
566
07b1bd0785cd simplifications to interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 559
diff changeset
67 if timeInterval == None and roadUser1 != None and roadUser2 != None:
07b1bd0785cd simplifications to interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 559
diff changeset
68 self.timeInterval = roadUser1.commonTimeInterval(roadUser2)
320
419f30491a4b renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 317
diff changeset
69 self.roadUser1 = roadUser1
419f30491a4b renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 317
diff changeset
70 self.roadUser2 = roadUser2
566
07b1bd0785cd simplifications to interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 559
diff changeset
71 if roaduserNum1 != None and roaduserNum2 != None:
07b1bd0785cd simplifications to interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 559
diff changeset
72 self.roadUserNumbers = set([roaduserNum1, roaduserNum2])
07b1bd0785cd simplifications to interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 559
diff changeset
73 elif roadUser1 != None and roadUser2 != None:
07b1bd0785cd simplifications to interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 559
diff changeset
74 self.roadUserNumbers = set(roadUser1.getNum(), roadUser2.getNum())
07b1bd0785cd simplifications to interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 559
diff changeset
75 else:
07b1bd0785cd simplifications to interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 559
diff changeset
76 self.roadUserNumbers = None
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
77 self.categoryNum = categoryNum
294
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
78 self.indicators = {}
451
cd342a774806 Point/CurvilinearTrajectory/Interaction utiles
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 436
diff changeset
79 self.interactionInterval = None
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
80
340
1046b7346886 work in progress on storing indicator values
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 339
diff changeset
81 def getRoadUserNumbers(self):
1046b7346886 work in progress on storing indicator values
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 339
diff changeset
82 return self.roadUserNumbers
1046b7346886 work in progress on storing indicator values
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 339
diff changeset
83
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
84 def getIndicator(self, indicatorName):
321
a5e40bd04cf4 rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 320
diff changeset
85 return self.indicators.get(indicatorName, None)
294
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
86
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
87 def addIndicator(self, indicator):
321
a5e40bd04cf4 rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 320
diff changeset
88 if indicator:
a5e40bd04cf4 rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 320
diff changeset
89 self.indicators[indicator.name] = indicator
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
90
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
91 def computeIndicators(self):
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
92 '''Computes the collision course cosine only if the cosine is positive'''
294
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
93 collisionCourseDotProducts = {}#[0]*int(self.timeInterval.length())
320
419f30491a4b renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 317
diff changeset
94 collisionCourseAngles = {}
325
6c9c7c956926 added velocity angle in indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
95 velocityAngles = {}
294
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
96 distances = {}#[0]*int(self.timeInterval.length())
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
97 speedDifferentials = {}
452
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
98 interactionInstants = []
294
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
99 for instant in self.timeInterval:
320
419f30491a4b renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 317
diff changeset
100 deltap = self.roadUser1.getPositionAtInstant(instant)-self.roadUser2.getPositionAtInstant(instant)
325
6c9c7c956926 added velocity angle in indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
101 v1 = self.roadUser1.getVelocityAtInstant(instant)
6c9c7c956926 added velocity angle in indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
102 v2 = self.roadUser2.getVelocityAtInstant(instant)
6c9c7c956926 added velocity angle in indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
103 deltav = v2-v1
6c9c7c956926 added velocity angle in indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
104 velocityAngles[instant] = arccos(moving.Point.dot(v1, v2)/(v1.norm2()*v2.norm2()))
299
7e5fb4abd070 renaming event to events and correcting errors in indicator computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 294
diff changeset
105 collisionCourseDotProducts[instant] = moving.Point.dot(deltap, deltav)
317
d280b881e860 added indicator min distance
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 310
diff changeset
106 distances[instant] = deltap.norm2()
294
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
107 speedDifferentials[instant] = deltav.norm2()
452
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
108 if collisionCourseDotProducts[instant] > 0:
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
109 interactionInstants.append(instant)
320
419f30491a4b renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 317
diff changeset
110 collisionCourseAngles[instant] = arccos(collisionCourseDotProducts[instant]/(distances[instant]*speedDifferentials[instant]))
306
93d851d0d21e bug correction, minor work on indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 299
diff changeset
111
452
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
112 if len(interactionInstants) >= 2:
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
113 self.interactionInterval = moving.TimeInterval(interactionInstants[0], interactionInstants[-1])
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
114 else:
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
115 self.interactionInterval = moving.TimeInterval()
387
91679eb2ff2c cleaning up safety analysis and the new traditional constant velocity method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 382
diff changeset
116 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[0], collisionCourseDotProducts))
91679eb2ff2c cleaning up safety analysis and the new traditional constant velocity method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 382
diff changeset
117 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[1], collisionCourseAngles))
91679eb2ff2c cleaning up safety analysis and the new traditional constant velocity method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 382
diff changeset
118 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[2], distances))
91679eb2ff2c cleaning up safety analysis and the new traditional constant velocity method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 382
diff changeset
119 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[4], velocityAngles))
91679eb2ff2c cleaning up safety analysis and the new traditional constant velocity method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 382
diff changeset
120 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[5], speedDifferentials))
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
121
317
d280b881e860 added indicator min distance
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 310
diff changeset
122 # if we have features, compute other indicators
559
806df5f61c03 adapted safety-analysis script to use multi-threading
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 558
diff changeset
123 if len(self.roadUser1.features) != 0 and len(self.roadUser2.features) != 0:
317
d280b881e860 added indicator min distance
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 310
diff changeset
124 minDistance={}
d280b881e860 added indicator min distance
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 310
diff changeset
125 for instant in self.timeInterval:
382
ba813f148ade development for clustering
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 378
diff changeset
126 minDistance[instant] = moving.MovingObject.minDistance(self.roadUser1, self.roadUser2, instant)
387
91679eb2ff2c cleaning up safety analysis and the new traditional constant velocity method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 382
diff changeset
127 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[3], minDistance))
317
d280b881e860 added indicator min distance
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 310
diff changeset
128
558
a80ef6931fd8 updated safety-analysis to test multiprocessing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 526
diff changeset
129 def computeCrossingsCollisions(self, predictionParameters, collisionDistanceThreshold, timeHorizon, computeCZ = False, debug = False, timeInterval = None, nProcesses = 1):
338
f3aceea2afbb first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 325
diff changeset
130 '''Computes all crossing and collision points at each common instant for two road users. '''
f3aceea2afbb first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 325
diff changeset
131 self.collisionPoints={}
339
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 338
diff changeset
132 self.crossingZones={}
338
f3aceea2afbb first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 325
diff changeset
133 TTCs = {}
f3aceea2afbb first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 325
diff changeset
134
f3aceea2afbb first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 325
diff changeset
135 if timeInterval:
f3aceea2afbb first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 325
diff changeset
136 commonTimeInterval = timeInterval
317
d280b881e860 added indicator min distance
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 310
diff changeset
137 else:
338
f3aceea2afbb first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 325
diff changeset
138 commonTimeInterval = self.timeInterval
559
806df5f61c03 adapted safety-analysis script to use multi-threading
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 558
diff changeset
139 self.collisionPoints, self.crossingZones = prediction.computeCrossingsCollisions(predictionParameters, self.roadUser1, self.roadUser2, collisionDistanceThreshold, timeHorizon, computeCZ, debug, commonTimeInterval, nProcesses)
558
a80ef6931fd8 updated safety-analysis to test multiprocessing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 526
diff changeset
140 for i, cp in self.collisionPoints.iteritems():
a80ef6931fd8 updated safety-analysis to test multiprocessing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 526
diff changeset
141 TTCs[i] = prediction.SafetyPoint.computeExpectedIndicator(cp)
341
2f39c4ed0b62 first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 340
diff changeset
142 # add probability of collision, and probability of successful evasive action
387
91679eb2ff2c cleaning up safety analysis and the new traditional constant velocity method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 382
diff changeset
143 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[7], TTCs))
338
f3aceea2afbb first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 325
diff changeset
144
f3aceea2afbb first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 325
diff changeset
145 if computeCZ:
f3aceea2afbb first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 325
diff changeset
146 pPETs = {}
f3aceea2afbb first safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 325
diff changeset
147 for i in list(commonTimeInterval)[:-1]:
357
e5fe0e6d48a1 corrected bug computing TTC (resp. pPET) if there is no collision point (resp. crossing zone)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 341
diff changeset
148 if len(self.crossingZones[i]) > 0:
358
c41ff9f3c263 moved current method for collision points and crossing zones computation into prediction parameters (put expectedindicator in SafetyPoint)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 357
diff changeset
149 pPETs[i] = prediction.SafetyPoint.computeExpectedIndicator(self.crossingZones[i])
387
91679eb2ff2c cleaning up safety analysis and the new traditional constant velocity method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 382
diff changeset
150 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[9], pPETs))
317
d280b881e860 added indicator min distance
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 310
diff changeset
151
607
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
152 def computeCrosssingCollisionsPrototypeAtInstant(self, instant,route1,route2,predictionParameters, collisionDistanceThreshold, timeHorizon, prototypes,secondStepPrototypes,nMatching,objects,noiseEntryNums,noiseExitNums,minSimilarity=0.1,mostMatched=None, computeCZ = False, debug = False,useDestination=True,useSpeedPrototype=True):
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
153 inter1=moving.Interval(self.roadUser1.timeInterval.first,instant)
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
154 inter2=moving.Interval(self.roadUser2.timeInterval.first,instant)
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
155 partialObjPositions1= self.roadUser1.getObjectInTimeInterval(inter1).positions
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
156 partialObjPositions2= self.roadUser2.getObjectInTimeInterval(inter2).positions
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
157 if useSpeedPrototype:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
158 prototypeTrajectories1=trajLearning.findPrototypesSpeed(prototypes,secondStepPrototypes,nMatching,objects,route1,partialObjPositions1,noiseEntryNums,noiseExitNums,minSimilarity,mostMatched,useDestination)
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
159 prototypeTrajectories2=trajLearning.findPrototypesSpeed(prototypes,secondStepPrototypes,nMatching,objects,route2,partialObjPositions2,noiseEntryNums,noiseExitNums,minSimilarity,mostMatched,useDestination)
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
160 else:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
161 prototypeTrajectories1=trajLearning.findPrototypes(prototypes,nMatching,objects,route1,partialObjPositions1,noiseEntryNums,noiseExitNums,minSimilarity,mostMatched)
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
162 prototypeTrajectories2=trajLearning.findPrototypes(prototypes,nMatching,objects,route2,partialObjPositions2,noiseEntryNums,noiseExitNums,minSimilarity,mostMatched)
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
163 if prototypeTrajectories1=={}:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
164 print self.roadUser1.num, 'is abnormal at instant', str(instant)
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
165 return [],[]
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
166 elif prototypeTrajectories2=={}:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
167 print self.roadUser2.num, 'is abnormal at instant', str(instant)
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
168 return [],[]
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
169 else:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
170 currentInstant,collisionPoints, crossingZones = predictionParameters.computeCrossingsCollisionsAtInstant(instant, self.roadUser1, self.roadUser2, collisionDistanceThreshold, timeHorizon, computeCZ, debug,prototypeTrajectories1,prototypeTrajectories2)
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
171 return collisionPoints,crossingZones
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
172
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
173 def computeCrossingsCollisionsPrototype2stages(self, predictionParameters, collisionDistanceThreshold, timeHorizon, prototypes,secondStepPrototypes,nMatching,objects,noiseEntryNums,noiseExitNums,step=10,minSimilarity=0.1,mostMatched=None, computeCZ = False, debug = False, timeInterval = None,acceptPartialLength=30,useDestination=True,useSpeedPrototype=True):
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
174 '''Computes all crossing and collision points at each common instant for two road users. '''
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
175 self.collisionPoints={}
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
176 self.crossingZones={}
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
177 TTCs = {}
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
178 route1=(self.roadUser1.startRouteID,self.roadUser1.endRouteID)
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
179 route2=(self.roadUser2.startRouteID,self.roadUser2.endRouteID)
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
180 if useDestination:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
181 if route1 not in prototypes.keys():
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
182 route1= trajLearning.findRoute(prototypes,objects,route1,self.roadUser1.num,noiseEntryNums,noiseExitNums)
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
183 if route2 not in prototypes.keys():
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
184 route2= trajLearning.findRoute(prototypes,objects,route2,self.roadUser2.num,noiseEntryNums,noiseExitNums)
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
185
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
186 if timeInterval:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
187 commonTimeInterval = timeInterval
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
188 else:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
189 commonTimeInterval = self.timeInterval
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
190 reCompute=False
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
191 for i in xrange(commonTimeInterval.first,commonTimeInterval.last,step): # incremental calculation of CP,CZ to save time
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
192 if i-self.roadUser1.timeInterval.first >= acceptPartialLength and i-self.roadUser2.timeInterval.first >= acceptPartialLength:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
193 self.collisionPoints[i], self.crossingZones[i] = self.computeCrosssingCollisionsPrototypeAtInstant(i,route1,route2,predictionParameters, collisionDistanceThreshold, timeHorizon, prototypes,secondStepPrototypes,nMatching,objects,noiseEntryNums,noiseExitNums,minSimilarity,mostMatched, computeCZ, debug,useDestination,useSpeedPrototype)
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
194 if len(self.collisionPoints[i]) >0 or len(self.crossingZones[i])>0:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
195 reCompute=True
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
196 break
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
197 if reCompute:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
198 for i in list(commonTimeInterval)[:-1]: # do not look at the 1 last position/velocities, often with errors
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
199 if i-self.roadUser1.timeInterval.first >= acceptPartialLength and i-self.roadUser2.timeInterval.first >= acceptPartialLength:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
200 self.collisionPoints[i], self.crossingZones[i] = self.computeCrosssingCollisionsPrototypeAtInstant(i,route1,route2,predictionParameters, collisionDistanceThreshold, timeHorizon, prototypes,secondStepPrototypes,nMatching,objects,noiseEntryNums,noiseExitNums,minSimilarity,mostMatched, computeCZ, debug,useDestination,useSpeedPrototype)
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
201 if len(self.collisionPoints[i]) > 0:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
202 TTCs[i] = prediction.SafetyPoint.computeExpectedIndicator(self.collisionPoints[i])
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
203 # add probability of collision, and probability of successful evasive action
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
204 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[7], TTCs))
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
205
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
206 if computeCZ:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
207 pPETs = {}
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
208 for i in list(commonTimeInterval)[:-1]:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
209 if i in self.crossingZones.keys() and len(self.crossingZones[i]) > 0:
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
210 pPETs[i] = prediction.SafetyPoint.computeExpectedIndicator(self.crossingZones[i])
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
211 self.addIndicator(indicators.SeverityIndicator(Interaction.indicatorNames[9], pPETs))
84690dfe5560 add some functions for behaviour analysis
MohamedGomaa
parents: 566
diff changeset
212
310
f7ca78a11ea6 add functions to add videofilename and interaction type in Interaction class
Mohamed Gomaa
parents: 306
diff changeset
213 def addVideoFilename(self,videoFilename):
f7ca78a11ea6 add functions to add videofilename and interaction type in Interaction class
Mohamed Gomaa
parents: 306
diff changeset
214 self.videoFilename= videoFilename
f7ca78a11ea6 add functions to add videofilename and interaction type in Interaction class
Mohamed Gomaa
parents: 306
diff changeset
215
f7ca78a11ea6 add functions to add videofilename and interaction type in Interaction class
Mohamed Gomaa
parents: 306
diff changeset
216 def addInteractionType(self,interactionType):
454
62d05436099d corrected indentation bug
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 452
diff changeset
217 ''' interaction types: conflict or collision if they are known'''
451
cd342a774806 Point/CurvilinearTrajectory/Interaction utiles
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 436
diff changeset
218 self.interactionType= interactionType
cd342a774806 Point/CurvilinearTrajectory/Interaction utiles
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 436
diff changeset
219
487
e04b22ce2fcd generalized createInteractions to 2 lists of objects (for cars and pedestrians for example)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 454
diff changeset
220 def createInteractions(objects, _others = None):
e04b22ce2fcd generalized createInteractions to 2 lists of objects (for cars and pedestrians for example)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 454
diff changeset
221 '''Create all interactions of two co-existing road users'''
e04b22ce2fcd generalized createInteractions to 2 lists of objects (for cars and pedestrians for example)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 454
diff changeset
222 if _others != None:
e04b22ce2fcd generalized createInteractions to 2 lists of objects (for cars and pedestrians for example)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 454
diff changeset
223 others = _others
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
224
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
225 interactions = []
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
226 num = 0
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
227 for i in xrange(len(objects)):
487
e04b22ce2fcd generalized createInteractions to 2 lists of objects (for cars and pedestrians for example)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 454
diff changeset
228 if _others == None:
e04b22ce2fcd generalized createInteractions to 2 lists of objects (for cars and pedestrians for example)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 454
diff changeset
229 others = objects[:i]
e04b22ce2fcd generalized createInteractions to 2 lists of objects (for cars and pedestrians for example)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 454
diff changeset
230 for j in xrange(len(others)):
489
000bddf84ad0 corrected bugs in safety analysis
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 487
diff changeset
231 commonTimeInterval = objects[i].commonTimeInterval(others[j])
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
232 if not commonTimeInterval.empty():
489
000bddf84ad0 corrected bugs in safety analysis
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 487
diff changeset
233 interactions.append(Interaction(num, commonTimeInterval, objects[i].num, others[j].num, objects[i], others[j]))
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
234 num += 1
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
235 return interactions
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
236
430
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
237 def prototypeCluster(interactions, similarityMatrix, alignmentMatrix, indicatorName, minSimilarity):
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
238 '''Finds exemplar indicator time series for all interactions
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
239 Returns the prototype indices (in the interaction list) and the label of each indicator (interaction)
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
240
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
241 if an indicator profile (time series) is different enough (<minSimilarity),
452
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
242 it will become a new prototype.
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
243 Non-prototype interactions will be assigned to an existing prototype'''
430
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
244
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
245 # sort indicators based on length
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
246 indices = range(similarityMatrix.shape[0])
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
247 def compare(i, j):
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
248 if len(interactions[i].getIndicator(indicatorName)) > len(interactions[j].getIndicator(indicatorName)):
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
249 return -1
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
250 elif len(interactions[i].getIndicator(indicatorName)) == len(interactions[j].getIndicator(indicatorName)):
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
251 return 0
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
252 else:
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
253 return 1
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
254 indices.sort(compare)
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
255 # go through all indicators
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
256 prototypeIndices = [indices[0]]
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
257 for i in indices[1:]:
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
258 if similarityMatrix[i][prototypeIndices].max() < minSimilarity:
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
259 prototypeIndices.append(i)
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
260
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
261 # assignment
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
262 labels = [-1]*similarityMatrix.shape[0]
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
263 indices = [i for i in range(similarityMatrix.shape[0]) if i not in prototypeIndices]
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
264 for i in prototypeIndices:
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
265 labels[i] = i
436
b64ff7fe7b45 corrected clustering bug (in indicator assignment)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 430
diff changeset
266 for i in indices:
430
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
267 prototypeIndex = similarityMatrix[i][prototypeIndices].argmax()
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
268 labels[i] = prototypeIndices[prototypeIndex]
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
269
fb3654a9127d integrating indicator clustering code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 408
diff changeset
270 return prototypeIndices, labels
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
271
452
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
272 def prototypeMultivariateCluster(interactions, similarityMatrics, indicatorNames, minSimilarities, minClusterSize):
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
273 '''Finds exmaple indicator time series (several indicators) for all interactions
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
274
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
275 if any interaction indicator time series is different enough (<minSimilarity),
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
276 it will become a new prototype.
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
277 Non-prototype interactions will be assigned to an existing prototype if all indicators are similar enough'''
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
278 pass
c59a47ce0209 reorganized interactioninterval (in compute indicators) and comments
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 451
diff changeset
279
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
280 # TODO:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
281 #http://stackoverflow.com/questions/3288595/multiprocessing-using-pool-map-on-a-function-defined-in-a-class
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
282 #http://www.rueckstiess.net/research/snippets/show/ca1d7d90
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
283 def calculateIndicatorPipe(pairs, predParam, timeHorizon=75,collisionDistanceThreshold=1.8):
320
419f30491a4b renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 317
diff changeset
284 collisionPoints, crossingZones = prediction.computeCrossingsCollisions(pairs.roadUser1, pairs.roadUser2, predParam, collisionDistanceThreshold, timeHorizon)
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
285 #print pairs.num
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
286 # Ignore empty collision points
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
287 empty = 1
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
288 for i in collisionPoints:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
289 if(collisionPoints[i] != []):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
290 empty = 0
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
291 if(empty == 1):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
292 pairs.hasCP = 0
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
293 else:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
294 pairs.hasCP = 1
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
295 pairs.CP = collisionPoints
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
296
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
297 # Ignore empty crossing zones
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
298 empty = 1
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
299 for i in crossingZones:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
300 if(crossingZones[i] != []):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
301 empty = 0
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
302 if(empty == 1):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
303 pairs.hasCZ = 0
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
304 else:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
305 pairs.hasCZ = 1
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
306 pairs.CZ = crossingZones
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
307 return pairs
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
308
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
309 def calculateIndicatorPipe_star(a_b):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
310 """Convert `f([1,2])` to `f(1,2)` call."""
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
311 return calculateIndicatorPipe(*a_b)
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
312
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
313 class VehPairs():
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
314 '''Create a veh-pairs object from objects list'''
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
315 def __init__(self,objects):
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
316 self.pairs = createInteractions(objects)
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
317 self.interactionCount = 0
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
318 self.CPcount = 0
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
319 self.CZcount = 0
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
320
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
321 # Process indicator calculation with support for multi-threading
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
322 def calculateIndicators(self,predParam,threads=1,timeHorizon=75,collisionDistanceThreshold=1.8):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
323 if(threads > 1):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
324 pool = multiprocessing.Pool(threads)
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
325 self.pairs = pool.map(calculateIndicatorPipe_star, itertools.izip(self.pairs, itertools.repeat(predParam)))
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
326 pool.close()
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
327 else:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
328 #prog = Tools.ProgressBar(0, len(self.pairs), 77) #Removed in traffic-intelligenc port
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
329 for j in xrange(len(self.pairs)):
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
330 #prog.updateAmount(j) #Removed in traffic-intelligenc port
320
419f30491a4b renamed fields movingObject to roadUser, etc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 317
diff changeset
331 collisionPoints, crossingZones = prediction.computeCrossingsCollisions(self.pairs[j].roadUser1, self.pairs[j].roadUser2, predParam, collisionDistanceThreshold, timeHorizon)
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
332
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
333 # Ignore empty collision points
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
334 empty = 1
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
335 for i in collisionPoints:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
336 if(collisionPoints[i] != []):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
337 empty = 0
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
338 if(empty == 1):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
339 self.pairs[j].hasCP = 0
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
340 else:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
341 self.pairs[j].hasCP = 1
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
342 self.pairs[j].CP = collisionPoints
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
343
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
344 # Ignore empty crossing zones
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
345 empty = 1
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
346 for i in crossingZones:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
347 if(crossingZones[i] != []):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
348 empty = 0
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
349 if(empty == 1):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
350 self.pairs[j].hasCZ = 0
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
351 else:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
352 self.pairs[j].hasCZ = 1
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
353 self.pairs[j].CZ = crossingZones
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
354
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
355 for j in self.pairs:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
356 self.interactionCount = self.interactionCount + len(j.CP)
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
357 self.CPcount = len(self.getCPlist())
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
358 self.Czcount = len(self.getCZlist())
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
359
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
360
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
361 def getPairsWCP(self):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
362 lists = []
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
363 for j in self.pairs:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
364 if(j.hasCP):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
365 lists.append(j.num)
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
366 return lists
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
367
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
368 def getPairsWCZ(self):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
369 lists = []
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
370 for j in self.pairs:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
371 if(j.hasCZ):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
372 lists.append(j.num)
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
373 return lists
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
374
526
21bdeb29f855 corrected bug in initialization of lists and loading trajectories from vissim files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 489
diff changeset
375 def getCPlist(self,indicatorThreshold=float('Inf')):
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
376 lists = []
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
377 for j in self.pairs:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
378 if(j.hasCP):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
379 for k in j.CP:
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
380 if(j.CP[k] != [] and j.CP[k][0].indicator < indicatorThreshold):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
381 lists.append([k,j.CP[k][0]])
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
382 return lists
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
383
526
21bdeb29f855 corrected bug in initialization of lists and loading trajectories from vissim files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 489
diff changeset
384 def getCZlist(self,indicatorThreshold=float('Inf')):
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
385 lists = []
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
386 for j in self.pairs:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
387 if(j.hasCZ):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
388 for k in j.CZ:
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
389 if(j.CZ[k] != [] and j.CZ[k][0].indicator < indicatorThreshold):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
390 lists.append([k,j.CZ[k][0]])
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
391 return lists
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
392
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
393 def genIndicatorHistogram(self, CPlist=False, bins=range(0,100,1)):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
394 if(not CPlist):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
395 CPlist = self.getCPlist()
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
396 if(not CPlist):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
397 return False
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
398 TTC_list = []
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
399 for i in CPlist:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
400 TTC_list.append(i[1].indicator)
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
401 histo = np.histogram(TTC_list,bins=bins)
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
402 histo += (histo[0].astype(float)/np.sum(histo[0]),)
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
403 return histo
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
404
59
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
405 class Crossing(moving.STObject):
56
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
406 '''Class for the event of a street crossing
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
407
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
408 TODO: detecter passage sur la chaussee
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
409 identifier origines et destination (ou uniquement chaussee dans FOV)
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
410 carac traversee
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
411 detecter proximite veh (retirer si trop similaire simultanement
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
412 carac interaction'''
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
413
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
414 def __init__(self, roaduserNum = None, num = None, timeInterval = None):
59
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
415 moving.STObject.__init__(self, num, timeInterval)
56
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
416 self.roaduserNum = roaduserNum
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
417
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
418
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
419
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
420 if __name__ == "__main__":
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
421 import doctest
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
422 import unittest
489
000bddf84ad0 corrected bugs in safety analysis
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 487
diff changeset
423 suite = doctest.DocFileSuite('tests/events.txt')
000bddf84ad0 corrected bugs in safety analysis
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 487
diff changeset
424 #suite = doctest.DocTestSuite()
56
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
425 unittest.TextTestRunner().run(suite)
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
426