annotate python/events.py @ 310:f7ca78a11ea6

add functions to add videofilename and interaction type in Interaction class
author Mohamed Gomaa
date Wed, 03 Apr 2013 22:13:01 -0400
parents 93d851d0d21e
children d280b881e860
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
59
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
11 import moving
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
12 import prediction
299
7e5fb4abd070 renaming event to events and correcting errors in indicator computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 294
diff changeset
13 import indicators
56
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
14
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
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
25 categories = {'headon': 0,
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
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
30 def __init__(self, num = None, timeInterval = None, roaduserNum1 = None, roaduserNum2 = None, movingObject1 = None, movingObject2 = None, categoryNum = None):
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
31 moving.STObject.__init__(self, num, timeInterval)
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
32 self.roaduserNumbers = set([roaduserNum1, roaduserNum2])
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
33 self.movingObject1 = movingObject1
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
34 self.movingObject2 = movingObject2
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
35 self.categoryNum = categoryNum
294
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
36 self.indicators = {}
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
37
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
38 def getIndicator(self, indicatorName):
294
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
39 return self.indicators[indicatorName]
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
40
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
41 def addIndicator(self, indicator):
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
42 self.indicators[indicator.name] = indicator
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
43
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
44 def computeIndicators(self):
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
45 '''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
46 collisionCourseDotProducts = {}#[0]*int(self.timeInterval.length())
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
47 collisionCourseCosines = {}
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
48 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
49 speedDifferentials = {}
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
50 for instant in self.timeInterval:
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
51 deltap = self.movingObject1.getPositionAtInstant(instant)-self.movingObject2.getPositionAtInstant(instant)
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
52 deltav = self.movingObject2.getVelocityAtInstant(instant)-self.movingObject1.getVelocityAtInstant(instant)
299
7e5fb4abd070 renaming event to events and correcting errors in indicator computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 294
diff changeset
53 collisionCourseDotProducts[instant] = moving.Point.dot(deltap, deltav)
306
93d851d0d21e bug correction, minor work on indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 299
diff changeset
54 distances[instant] = deltap.norm2() # todo compute closest feature distance, if features
294
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
55 speedDifferentials[instant] = deltav.norm2()
299
7e5fb4abd070 renaming event to events and correcting errors in indicator computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 294
diff changeset
56 if collisionCourseDotProducts[instant] > 0:
306
93d851d0d21e bug correction, minor work on indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 299
diff changeset
57 collisionCourseCosines[instant] = arccos(collisionCourseDotProducts[instant]/(distances[instant]*speedDifferentials[instant]))
93d851d0d21e bug correction, minor work on indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 299
diff changeset
58
294
1f253f218b9f evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 293
diff changeset
59 # todo shorten the time intervals based on the interaction definition
299
7e5fb4abd070 renaming event to events and correcting errors in indicator computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 294
diff changeset
60 self.addIndicator(indicators.SeverityIndicator('Collision Course Dot Product', collisionCourseDotProducts))
7e5fb4abd070 renaming event to events and correcting errors in indicator computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 294
diff changeset
61 self.addIndicator(indicators.SeverityIndicator('Distance', distances))
7e5fb4abd070 renaming event to events and correcting errors in indicator computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 294
diff changeset
62 self.addIndicator(indicators.SeverityIndicator('Speed Differential', speedDifferentials))
7e5fb4abd070 renaming event to events and correcting errors in indicator computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 294
diff changeset
63 self.addIndicator(indicators.SeverityIndicator('Collision Course Cosine', collisionCourseCosines))
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
64
306
93d851d0d21e bug correction, minor work on indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 299
diff changeset
65 # todo test for interaction instants and interval, compute indicators
93d851d0d21e bug correction, minor work on indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 299
diff changeset
66
310
f7ca78a11ea6 add functions to add videofilename and interaction type in Interaction class
Mohamed Gomaa
parents: 306
diff changeset
67 def addVideoFilename(self,videoFilename):
f7ca78a11ea6 add functions to add videofilename and interaction type in Interaction class
Mohamed Gomaa
parents: 306
diff changeset
68 self.videoFilename= videoFilename
f7ca78a11ea6 add functions to add videofilename and interaction type in Interaction class
Mohamed Gomaa
parents: 306
diff changeset
69
f7ca78a11ea6 add functions to add videofilename and interaction type in Interaction class
Mohamed Gomaa
parents: 306
diff changeset
70 def addInteractionType(self,interactionType):
f7ca78a11ea6 add functions to add videofilename and interaction type in Interaction class
Mohamed Gomaa
parents: 306
diff changeset
71 ''' interaction types: conflict or collision if they are known'''
f7ca78a11ea6 add functions to add videofilename and interaction type in Interaction class
Mohamed Gomaa
parents: 306
diff changeset
72 self.interactionType= interactionType
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
73
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
74 def createInteractions(objects):
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
75 '''Create all interactions of two co-existing road users
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
76
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
77 todo add test to compute categories?'''
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
78 interactions = []
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
79 num = 0
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
80 for i in xrange(len(objects)):
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
81 for j in xrange(i):
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
82 commonTimeInterval = objects[i].commonTimeInterval(objects[j])
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
83 if not commonTimeInterval.empty():
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
84 interactions.append(Interaction(num, commonTimeInterval, objects[i].num, objects[j].num, objects[i], objects[j]))
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
85 num += 1
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
86 return interactions
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
87
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
88
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
89 # TODO:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
90 #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
91 #http://www.rueckstiess.net/research/snippets/show/ca1d7d90
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
92 def calculateIndicatorPipe(pairs, predParam, timeHorizon=75,collisionDistanceThreshold=1.8):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
93 collisionPoints, crossingZones = prediction.computeCrossingsCollisions(pairs.movingObject1, pairs.movingObject2, predParam, collisionDistanceThreshold, timeHorizon)
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
94 #print pairs.num
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
95 # Ignore empty collision points
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
96 empty = 1
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
97 for i in collisionPoints:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
98 if(collisionPoints[i] != []):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
99 empty = 0
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
100 if(empty == 1):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
101 pairs.hasCP = 0
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
102 else:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
103 pairs.hasCP = 1
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
104 pairs.CP = collisionPoints
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
105
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
106 # Ignore empty crossing zones
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
107 empty = 1
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
108 for i in crossingZones:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
109 if(crossingZones[i] != []):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
110 empty = 0
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
111 if(empty == 1):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
112 pairs.hasCZ = 0
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
113 else:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
114 pairs.hasCZ = 1
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
115 pairs.CZ = crossingZones
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
116 return pairs
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
117
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
118 def calculateIndicatorPipe_star(a_b):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
119 """Convert `f([1,2])` to `f(1,2)` call."""
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
120 return calculateIndicatorPipe(*a_b)
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
121
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
122 class VehPairs():
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
123 '''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
124 def __init__(self,objects):
293
ee3302528cdc rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 292
diff changeset
125 self.pairs = createInteractions(objects)
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
126 self.interactionCount = 0
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
127 self.CPcount = 0
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
128 self.CZcount = 0
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
129
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
130 # Process indicator calculation with support for multi-threading
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
131 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
132 if(threads > 1):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
133 pool = multiprocessing.Pool(threads)
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
134 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
135 pool.close()
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
136 else:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
137 #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
138 for j in xrange(len(self.pairs)):
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
139 #prog.updateAmount(j) #Removed in traffic-intelligenc port
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
140 collisionPoints, crossingZones = prediction.computeCrossingsCollisions(self.pairs[j].movingObject1, self.pairs[j].movingObject2, predParam, collisionDistanceThreshold, timeHorizon)
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
141
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
142 # Ignore empty collision points
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
143 empty = 1
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
144 for i in collisionPoints:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
145 if(collisionPoints[i] != []):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
146 empty = 0
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
147 if(empty == 1):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
148 self.pairs[j].hasCP = 0
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
149 else:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
150 self.pairs[j].hasCP = 1
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
151 self.pairs[j].CP = collisionPoints
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
152
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
153 # Ignore empty crossing zones
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
154 empty = 1
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
155 for i in crossingZones:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
156 if(crossingZones[i] != []):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
157 empty = 0
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
158 if(empty == 1):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
159 self.pairs[j].hasCZ = 0
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
160 else:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
161 self.pairs[j].hasCZ = 1
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
162 self.pairs[j].CZ = crossingZones
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
163
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
164 for j in self.pairs:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
165 self.interactionCount = self.interactionCount + len(j.CP)
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
166 self.CPcount = len(self.getCPlist())
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
167 self.Czcount = len(self.getCZlist())
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
168
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
169
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
170 def getPairsWCP(self):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
171 lists = []
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
172 for j in self.pairs:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
173 if(j.hasCP):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
174 lists.append(j.num)
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
175 return lists
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
176
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
177 def getPairsWCZ(self):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
178 lists = []
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
179 for j in self.pairs:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
180 if(j.hasCZ):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
181 lists.append(j.num)
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
182 return lists
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
183
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
184 def getCPlist(self,indicatorThreshold=99999):
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
185 lists = []
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
186 for j in self.pairs:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
187 if(j.hasCP):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
188 for k in j.CP:
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
189 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
190 lists.append([k,j.CP[k][0]])
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
191 return lists
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
192
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
193 def getCZlist(self,indicatorThreshold=99999):
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
194 lists = []
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
195 for j in self.pairs:
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
196 if(j.hasCZ):
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
197 for k in j.CZ:
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
198 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
199 lists.append([k,j.CZ[k][0]])
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
200 return lists
292
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
201
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
202 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
203 if(not CPlist):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
204 CPlist = self.getCPlist()
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
205 if(not CPlist):
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
206 return False
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
207 TTC_list = []
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
208 for i in CPlist:
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
209 TTC_list.append(i[1].indicator)
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
210 histo = np.histogram(TTC_list,bins=bins)
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
211 histo += (histo[0].astype(float)/np.sum(histo[0]),)
8b2c8a4015f1 class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents: 291
diff changeset
212 return histo
291
9f81218e497a class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents: 283
diff changeset
213
59
f955e83da499 developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 56
diff changeset
214 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
215 '''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
216
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
217 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
218 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
219 carac traversee
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
220 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
221 carac interaction'''
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
222
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
223 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
224 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
225 self.roaduserNum = roaduserNum
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
226
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
227
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
228
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
229 if __name__ == "__main__":
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
230 import doctest
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
231 import unittest
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
232 #suite = doctest.DocFileSuite('tests/moving.txt')
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
233 suite = doctest.DocTestSuite()
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
234 unittest.TextTestRunner().run(suite)
61fe73df2d36 created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
235