Mercurial > hg > nsaunier > traffic-intelligence
comparison python/event.py @ 292:8b2c8a4015f1
class VehPairs updated. Now supports primitive multithreading.
| author | Paul@BEAST-III |
|---|---|
| date | Wed, 06 Feb 2013 20:39:14 -0500 |
| parents | 9f81218e497a |
| children | ee3302528cdc |
comparison
equal
deleted
inserted
replaced
| 291:9f81218e497a | 292:8b2c8a4015f1 |
|---|---|
| 1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
| 2 '''Libraries for events | 2 '''Libraries for events |
| 3 Interactions, pedestrian crossing...''' | 3 Interactions, pedestrian crossing...''' |
| 4 | 4 |
| 5 ## Native | |
| 6 import multiprocessing | |
| 7 import itertools | |
| 5 #import utils; | 8 #import utils; |
| 9 import numpy as np | |
| 6 import moving | 10 import moving |
| 7 import prediction | 11 import prediction |
| 8 | 12 |
| 9 __metaclass__ = type | 13 __metaclass__ = type |
| 10 | 14 |
| 11 ## Subsumes createInteractions(objects) with a VehPair Object | 15 # TODO: |
| 16 #http://stackoverflow.com/questions/3288595/multiprocessing-using-pool-map-on-a-function-defined-in-a-class | |
| 17 #http://www.rueckstiess.net/research/snippets/show/ca1d7d90 | |
| 18 def calculateIndicatorPipe(pairs, predParam, timeHorizon=75,collisionDistanceThreshold=1.8): | |
| 19 collisionPoints, crossingZones = prediction.computeCrossingsCollisions(pairs.movingObject1, pairs.movingObject2, predParam, collisionDistanceThreshold, timeHorizon) | |
| 20 #print pairs.num | |
| 21 # Ignore empty collision points | |
| 22 empty = 1 | |
| 23 for i in collisionPoints: | |
| 24 if(collisionPoints[i] != []): | |
| 25 empty = 0 | |
| 26 if(empty == 1): | |
| 27 pairs.hasCP = 0 | |
| 28 else: | |
| 29 pairs.hasCP = 1 | |
| 30 pairs.CP = collisionPoints | |
| 31 | |
| 32 # Ignore empty crossing zones | |
| 33 empty = 1 | |
| 34 for i in crossingZones: | |
| 35 if(crossingZones[i] != []): | |
| 36 empty = 0 | |
| 37 if(empty == 1): | |
| 38 pairs.hasCZ = 0 | |
| 39 else: | |
| 40 pairs.hasCZ = 1 | |
| 41 pairs.CZ = crossingZones | |
| 42 return pairs | |
| 43 | |
| 44 def calculateIndicatorPipe_star(a_b): | |
| 45 """Convert `f([1,2])` to `f(1,2)` call.""" | |
| 46 return calculateIndicatorPipe(*a_b) | |
| 47 | |
| 12 class VehPairs(): | 48 class VehPairs(): |
| 49 # Create a veh-pairs object from objects list | |
| 13 def __init__(self,objects): | 50 def __init__(self,objects): |
| 14 ''' | 51 ''' |
| 15 Create all pairs of two co-existing road users | 52 Create all pairs of two co-existing road users |
| 16 TODO: add test to compute categories? | 53 TODO: add test to compute categories? |
| 17 ''' | 54 ''' |
| 18 #print(' -------------') #Disabled for traffic-intelligence trunk | |
| 19 #print(' Generating vehicle pairs...') #Disabled for traffic-intelligence trunk | |
| 20 | |
| 21 self.pairs = [] | 55 self.pairs = [] |
| 56 self.interactionCount = 0 | |
| 57 self.CPcount = 0 | |
| 58 self.CZcount = 0 | |
| 22 num = 0 | 59 num = 0 |
| 23 for i in xrange(len(objects)): | 60 for i in xrange(len(objects)): |
| 24 for j in xrange(i): | 61 for j in xrange(i): |
| 25 commonTimeInterval = objects[i].commonTimeInterval(objects[j]) | 62 commonTimeInterval = objects[i].commonTimeInterval(objects[j]) |
| 26 if not commonTimeInterval.empty(): | 63 if not commonTimeInterval.empty(): |
| 27 self.pairs.append(event.Interaction(num, commonTimeInterval, objects[i].num, objects[j].num, objects[i], objects[j])) | 64 self.pairs.append(event.Interaction(num, commonTimeInterval, objects[i].num, objects[j].num, objects[i], objects[j])) |
| 28 num += 1 | 65 num += 1 |
| 29 | 66 |
| 30 def calculateIndicators(self,predParam,frameRate=15,collisionDistanceThreshold=1.8,timeHorizonMultiplier=5): | 67 # Process indicator calculation with support for multi-threading |
| 31 #print(' -------------') #Disabled for traffic-intelligence trunk | 68 def calculateIndicators(self,predParam,threads=1,timeHorizon=75,collisionDistanceThreshold=1.8): |
| 32 #print(' Calculating time-to-collision...') #Disabled for traffic-intelligence trunk | 69 if(threads > 1): |
| 33 | 70 pool = multiprocessing.Pool(threads) |
| 34 timeHorizon = frameRate*timeHorizonMultiplier # prediction time Horizon = 1.5 s (reaction time) (5 second) | 71 self.pairs = pool.map(calculateIndicatorPipe_star, itertools.izip(self.pairs, itertools.repeat(predParam))) |
| 35 | 72 pool.close() |
| 36 for params in [predParam]: | 73 else: |
| 37 | 74 #prog = Tools.ProgressBar(0, len(self.pairs), 77) #Removed in traffic-intelligenc port |
| 38 #prog = Tools.ProgressBar(0, len(self.pairs), 77) #Disabled for traffic-intelligence trunk (PVA Tools dependancy) | |
| 39 for j in xrange(len(self.pairs)): | 75 for j in xrange(len(self.pairs)): |
| 40 #prog.updateAmount(j) #Disabled for traffic-intelligence trunk (PVA Tools dependancy) | 76 #prog.updateAmount(j) #Removed in traffic-intelligenc port |
| 41 | |
| 42 collisionPoints, crossingZones = prediction.computeCrossingsCollisions(self.pairs[j].movingObject1, self.pairs[j].movingObject2, predParam, collisionDistanceThreshold, timeHorizon) | 77 collisionPoints, crossingZones = prediction.computeCrossingsCollisions(self.pairs[j].movingObject1, self.pairs[j].movingObject2, predParam, collisionDistanceThreshold, timeHorizon) |
| 43 | 78 |
| 44 # Ignore empty collision points | 79 # Ignore empty collision points |
| 45 empty = 1 | 80 empty = 1 |
| 46 for i in collisionPoints: | 81 for i in collisionPoints: |
| 59 empty = 0 | 94 empty = 0 |
| 60 if(empty == 1): | 95 if(empty == 1): |
| 61 self.pairs[j].hasCZ = 0 | 96 self.pairs[j].hasCZ = 0 |
| 62 else: | 97 else: |
| 63 self.pairs[j].hasCZ = 1 | 98 self.pairs[j].hasCZ = 1 |
| 64 self.pairs[j].CZ = crossingZones | 99 self.pairs[j].CZ = crossingZones |
| 100 | |
| 101 for j in self.pairs: | |
| 102 self.interactionCount = self.interactionCount + len(j.CP) | |
| 103 self.CPcount = len(self.getCPlist()) | |
| 104 self.Czcount = len(self.getCZlist()) | |
| 65 return | 105 return |
| 106 | |
| 66 | 107 |
| 67 def getPairsWCP(self): | 108 def getPairsWCP(self): |
| 68 lists = [] | 109 lists = [] |
| 69 for j in self.pairs: | 110 for j in self.pairs: |
| 70 if(j.hasCP): | 111 if(j.hasCP): |
| 76 for j in self.pairs: | 117 for j in self.pairs: |
| 77 if(j.hasCZ): | 118 if(j.hasCZ): |
| 78 lists.append(j.num) | 119 lists.append(j.num) |
| 79 return lists | 120 return lists |
| 80 | 121 |
| 81 def getCPlist(self): | 122 def getCPlist(self,indicatorThreshold=99999): |
| 82 lists = [] | 123 lists = [] |
| 83 for j in self.pairs: | 124 for j in self.pairs: |
| 84 if(j.hasCP): | 125 if(j.hasCP): |
| 85 for k in j.CP: | 126 for k in j.CP: |
| 86 if(j.CP[k] != []): | 127 if(j.CP[k] != [] and j.CP[k][0].indicator < indicatorThreshold): |
| 87 lists.append(j.CP[k]) | 128 lists.append([k,j.CP[k][0]]) |
| 88 return lists | 129 return lists |
| 89 | 130 |
| 90 def getCZlist(self): | 131 def getCZlist(self,indicatorThreshold=99999): |
| 91 lists = [] | 132 lists = [] |
| 92 for j in self.pairs: | 133 for j in self.pairs: |
| 93 if(j.hasCZ): | 134 if(j.hasCZ): |
| 94 for k in j.CZ: | 135 for k in j.CZ: |
| 95 if(j.CZ[k] != []): | 136 if(j.CZ[k] != [] and j.CZ[k][0].indicator < indicatorThreshold): |
| 96 lists.append(j.CZ[k]) | 137 lists.append([k,j.CZ[k][0]]) |
| 97 return lists | 138 return lists |
| 139 | |
| 140 def genIndicatorHistogram(self, CPlist=False, bins=range(0,100,1)): | |
| 141 if(not CPlist): | |
| 142 CPlist = self.getCPlist() | |
| 143 if(not CPlist): | |
| 144 return False | |
| 145 TTC_list = [] | |
| 146 for i in CPlist: | |
| 147 TTC_list.append(i[1].indicator) | |
| 148 histo = np.histogram(TTC_list,bins=bins) | |
| 149 histo += (histo[0].astype(float)/np.sum(histo[0]),) | |
| 150 return histo | |
| 98 | 151 |
| 99 class Interaction(moving.STObject): | 152 class Interaction(moving.STObject): |
| 100 '''Class for an interaction between two road users | 153 '''Class for an interaction between two road users |
| 101 or a road user and an obstacle | 154 or a road user and an obstacle |
| 102 | 155 |
