Mercurial Hosting > traffic-intelligence
annotate python/events.py @ 299:7e5fb4abd070
renaming event to events and correcting errors in indicator computation
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 12 Feb 2013 18:08:00 -0500 |
parents | python/event.py@1f253f218b9f |
children | 93d851d0d21e |
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 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
6 |
292
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
7 import multiprocessing |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
8 import itertools |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
9 |
59
f955e83da499
developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
56
diff
changeset
|
10 import moving |
291
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
11 import prediction |
299
7e5fb4abd070
renaming event to events and correcting errors in indicator computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
294
diff
changeset
|
12 import indicators |
56
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
13 |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
14 __metaclass__ = type |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
15 |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
16 class Interaction(moving.STObject): |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
17 '''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
|
18 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
|
19 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
20 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
|
21 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
|
22 ''' |
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 categories = {'headon': 0, |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
25 'rearend': 1, |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
26 'side': 2, |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
27 'parallel': 3} |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
28 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
29 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
|
30 moving.STObject.__init__(self, num, timeInterval) |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
31 self.roaduserNumbers = set([roaduserNum1, roaduserNum2]) |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
32 self.movingObject1 = movingObject1 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
33 self.movingObject2 = movingObject2 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
34 self.categoryNum = categoryNum |
294
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
35 self.indicators = {} |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
36 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
37 def getIndicator(self, indicatorName): |
294
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
38 return self.indicators[indicatorName] |
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
39 |
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
40 def addIndicator(self, indicator): |
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
41 self.indicators[indicator.name] = indicator |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
42 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
43 def computeIndicators(self): |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
44 '''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
|
45 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
|
46 collisionCourseCosines = {} |
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
47 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
|
48 speedDifferentials = {} |
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
49 for instant in self.timeInterval: |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
50 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
|
51 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
|
52 collisionCourseDotProducts[instant] = moving.Point.dot(deltap, deltav) |
294
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
53 distances[instant] = deltap.norm2() |
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
54 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
|
55 if collisionCourseDotProducts[instant] > 0: |
7e5fb4abd070
renaming event to events and correcting errors in indicator computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
294
diff
changeset
|
56 collisionCourseCosines[instant] = collisionCourseDotProducts[instant]/(distances[instant]*speedDifferentials[instant]) |
294
1f253f218b9f
evolution of indicators and their computation in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
293
diff
changeset
|
57 # 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
|
58 # todos change cosines to angles |
7e5fb4abd070
renaming event to events and correcting errors in indicator computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
294
diff
changeset
|
59 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
|
60 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
|
61 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
|
62 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
|
63 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
64 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
65 def createInteractions(objects): |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
66 '''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
|
67 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
68 todo add test to compute categories?''' |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
69 interactions = [] |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
70 num = 0 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
71 for i in xrange(len(objects)): |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
72 for j in xrange(i): |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
73 commonTimeInterval = objects[i].commonTimeInterval(objects[j]) |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
74 if not commonTimeInterval.empty(): |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
75 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
|
76 num += 1 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
77 return interactions |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
78 |
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
79 |
292
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
80 # TODO: |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
81 #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
|
82 #http://www.rueckstiess.net/research/snippets/show/ca1d7d90 |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
83 def calculateIndicatorPipe(pairs, predParam, timeHorizon=75,collisionDistanceThreshold=1.8): |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
84 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
|
85 #print pairs.num |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
86 # Ignore empty collision points |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
87 empty = 1 |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
88 for i in collisionPoints: |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
89 if(collisionPoints[i] != []): |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
90 empty = 0 |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
91 if(empty == 1): |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
92 pairs.hasCP = 0 |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
93 else: |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
94 pairs.hasCP = 1 |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
95 pairs.CP = collisionPoints |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
96 |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
97 # Ignore empty crossing zones |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
98 empty = 1 |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
99 for i in crossingZones: |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
100 if(crossingZones[i] != []): |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
101 empty = 0 |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
102 if(empty == 1): |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
103 pairs.hasCZ = 0 |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
104 else: |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
105 pairs.hasCZ = 1 |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
106 pairs.CZ = crossingZones |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
107 return pairs |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
108 |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
109 def calculateIndicatorPipe_star(a_b): |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
110 """Convert `f([1,2])` to `f(1,2)` call.""" |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
111 return calculateIndicatorPipe(*a_b) |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
112 |
291
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
113 class VehPairs(): |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
114 '''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
|
115 def __init__(self,objects): |
293
ee3302528cdc
rearranged new code by Paul (works now)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
292
diff
changeset
|
116 self.pairs = createInteractions(objects) |
292
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
117 self.interactionCount = 0 |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
118 self.CPcount = 0 |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
119 self.CZcount = 0 |
291
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
120 |
292
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
121 # Process indicator calculation with support for multi-threading |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
122 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
|
123 if(threads > 1): |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
124 pool = multiprocessing.Pool(threads) |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
125 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
|
126 pool.close() |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
127 else: |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
128 #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
|
129 for j in xrange(len(self.pairs)): |
292
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
130 #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
|
131 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
|
132 |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
133 # Ignore empty collision points |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
134 empty = 1 |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
135 for i in collisionPoints: |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
136 if(collisionPoints[i] != []): |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
137 empty = 0 |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
138 if(empty == 1): |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
139 self.pairs[j].hasCP = 0 |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
140 else: |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
141 self.pairs[j].hasCP = 1 |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
142 self.pairs[j].CP = collisionPoints |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
143 |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
144 # Ignore empty crossing zones |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
145 empty = 1 |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
146 for i in crossingZones: |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
147 if(crossingZones[i] != []): |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
148 empty = 0 |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
149 if(empty == 1): |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
150 self.pairs[j].hasCZ = 0 |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
151 else: |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
152 self.pairs[j].hasCZ = 1 |
292
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
153 self.pairs[j].CZ = crossingZones |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
154 |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
155 for j in self.pairs: |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
156 self.interactionCount = self.interactionCount + len(j.CP) |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
157 self.CPcount = len(self.getCPlist()) |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
158 self.Czcount = len(self.getCZlist()) |
291
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
159 |
292
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
160 |
291
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
161 def getPairsWCP(self): |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
162 lists = [] |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
163 for j in self.pairs: |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
164 if(j.hasCP): |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
165 lists.append(j.num) |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
166 return lists |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
167 |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
168 def getPairsWCZ(self): |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
169 lists = [] |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
170 for j in self.pairs: |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
171 if(j.hasCZ): |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
172 lists.append(j.num) |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
173 return lists |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
174 |
292
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
175 def getCPlist(self,indicatorThreshold=99999): |
291
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
176 lists = [] |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
177 for j in self.pairs: |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
178 if(j.hasCP): |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
179 for k in j.CP: |
292
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
180 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
|
181 lists.append([k,j.CP[k][0]]) |
291
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
182 return lists |
292
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
183 |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
184 def getCZlist(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.hasCZ): |
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
188 for k in j.CZ: |
292
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
189 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
|
190 lists.append([k,j.CZ[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 genIndicatorHistogram(self, CPlist=False, bins=range(0,100,1)): |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
194 if(not CPlist): |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
195 CPlist = self.getCPlist() |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
196 if(not CPlist): |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
197 return False |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
198 TTC_list = [] |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
199 for i in CPlist: |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
200 TTC_list.append(i[1].indicator) |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
201 histo = np.histogram(TTC_list,bins=bins) |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
202 histo += (histo[0].astype(float)/np.sum(histo[0]),) |
8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
Paul@BEAST-III
parents:
291
diff
changeset
|
203 return histo |
291
9f81218e497a
class VehPairs subsumes createInteractions(objects); legacy code remains
Paul@BEAST-III
parents:
283
diff
changeset
|
204 |
59
f955e83da499
developed indicators in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
56
diff
changeset
|
205 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
|
206 '''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
|
207 |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
208 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
|
209 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
|
210 carac traversee |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
211 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
|
212 carac interaction''' |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
213 |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
214 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
|
215 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
|
216 self.roaduserNum = roaduserNum |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
217 |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
218 |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
219 |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
220 if __name__ == "__main__": |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
221 import doctest |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
222 import unittest |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
223 #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
|
224 suite = doctest.DocTestSuite() |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
225 unittest.TextTestRunner().run(suite) |
61fe73df2d36
created new package, moved Interaction class and created Crossing class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
226 |