changeset 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
files python/event.py
diffstat 1 files changed, 77 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/python/event.py	Tue Feb 05 15:45:33 2013 -0500
+++ b/python/event.py	Wed Feb 06 20:39:14 2013 -0500
@@ -2,23 +2,60 @@
 '''Libraries for events
 Interactions, pedestrian crossing...'''
 
+## Native
+import multiprocessing
+import itertools
 #import utils;
+import numpy as np
 import moving
 import prediction
 
 __metaclass__ = type
 
-## Subsumes createInteractions(objects) with a VehPair Object
+# TODO:
+#http://stackoverflow.com/questions/3288595/multiprocessing-using-pool-map-on-a-function-defined-in-a-class
+#http://www.rueckstiess.net/research/snippets/show/ca1d7d90
+def calculateIndicatorPipe(pairs, predParam, timeHorizon=75,collisionDistanceThreshold=1.8):  
+    collisionPoints, crossingZones = prediction.computeCrossingsCollisions(pairs.movingObject1, pairs.movingObject2, predParam, collisionDistanceThreshold, timeHorizon)      
+    #print pairs.num    
+    # Ignore empty collision points
+    empty = 1
+    for i in collisionPoints:
+        if(collisionPoints[i] != []):
+            empty = 0
+    if(empty == 1):
+        pairs.hasCP = 0
+    else:
+        pairs.hasCP = 1
+    pairs.CP = collisionPoints
+    
+    # Ignore empty crossing zones
+    empty = 1
+    for i in crossingZones:
+        if(crossingZones[i] != []):
+            empty = 0
+    if(empty == 1):
+        pairs.hasCZ = 0
+    else:
+        pairs.hasCZ = 1
+    pairs.CZ = crossingZones
+    return pairs
+
+def calculateIndicatorPipe_star(a_b):
+    """Convert `f([1,2])` to `f(1,2)` call."""
+    return calculateIndicatorPipe(*a_b)
+
 class VehPairs():
+    # Create a veh-pairs object from objects list
     def __init__(self,objects):
         '''
         Create all pairs of two co-existing road users
         TODO: add test to compute categories?
-        '''
-        #print('    -------------') #Disabled for traffic-intelligence trunk
-        #print('    Generating vehicle pairs...') #Disabled for traffic-intelligence trunk
-        
+        '''       
         self.pairs = []
+        self.interactionCount = 0
+        self.CPcount = 0
+        self.CZcount = 0
         num = 0
         for i in xrange(len(objects)):
             for j in xrange(i):
@@ -27,18 +64,16 @@
                     self.pairs.append(event.Interaction(num, commonTimeInterval, objects[i].num, objects[j].num, objects[i], objects[j]))
                     num += 1
     
-    def calculateIndicators(self,predParam,frameRate=15,collisionDistanceThreshold=1.8,timeHorizonMultiplier=5):
-        #print('    -------------') #Disabled for traffic-intelligence trunk
-        #print('    Calculating time-to-collision...') #Disabled for traffic-intelligence trunk
-        
-        timeHorizon = frameRate*timeHorizonMultiplier # prediction time Horizon = 1.5 s (reaction time) (5 second)
-        
-        for params in [predParam]:
-            
-            #prog = Tools.ProgressBar(0, len(self.pairs), 77)  #Disabled for traffic-intelligence trunk (PVA Tools dependancy)
+    # Process indicator calculation with support for multi-threading
+    def calculateIndicators(self,predParam,threads=1,timeHorizon=75,collisionDistanceThreshold=1.8):       
+        if(threads > 1):
+            pool = multiprocessing.Pool(threads)
+            self.pairs = pool.map(calculateIndicatorPipe_star, itertools.izip(self.pairs, itertools.repeat(predParam)))
+            pool.close()
+        else:
+            #prog = Tools.ProgressBar(0, len(self.pairs), 77) #Removed in traffic-intelligenc port
             for j in xrange(len(self.pairs)):
-                #prog.updateAmount(j)  #Disabled for traffic-intelligence trunk (PVA Tools dependancy)
-                
+                #prog.updateAmount(j) #Removed in traffic-intelligenc port
                 collisionPoints, crossingZones = prediction.computeCrossingsCollisions(self.pairs[j].movingObject1, self.pairs[j].movingObject2, predParam, collisionDistanceThreshold, timeHorizon)      
                 
                 # Ignore empty collision points
@@ -61,9 +96,15 @@
                     self.pairs[j].hasCZ = 0
                 else:
                     self.pairs[j].hasCZ = 1
-                self.pairs[j].CZ = crossingZones
+                self.pairs[j].CZ = crossingZones       
+                
+        for j in self.pairs:
+            self.interactionCount = self.interactionCount + len(j.CP)
+        self.CPcount = len(self.getCPlist())
+        self.Czcount = len(self.getCZlist())
         return
     
+    
     def getPairsWCP(self):
         lists = []
         for j in self.pairs:
@@ -78,23 +119,35 @@
                 lists.append(j.num)
         return lists
     
-    def getCPlist(self):
+    def getCPlist(self,indicatorThreshold=99999):
         lists = []
         for j in self.pairs:
             if(j.hasCP):
                 for k in j.CP:
-                    if(j.CP[k] != []):
-                        lists.append(j.CP[k])
+                    if(j.CP[k] != [] and j.CP[k][0].indicator < indicatorThreshold):
+                        lists.append([k,j.CP[k][0]])
         return lists
-        
-    def getCZlist(self):
+     
+    def getCZlist(self,indicatorThreshold=99999):
         lists = []
         for j in self.pairs:
             if(j.hasCZ):
                 for k in j.CZ:
-                    if(j.CZ[k] != []):
-                        lists.append(j.CZ[k])
+                    if(j.CZ[k] != [] and j.CZ[k][0].indicator < indicatorThreshold):
+                        lists.append([k,j.CZ[k][0]])
         return lists
+        
+    def genIndicatorHistogram(self, CPlist=False, bins=range(0,100,1)):
+        if(not CPlist):
+            CPlist = self.getCPlist()
+        if(not CPlist):
+            return False
+        TTC_list = []
+        for i in CPlist:
+            TTC_list.append(i[1].indicator)
+        histo = np.histogram(TTC_list,bins=bins)
+        histo += (histo[0].astype(float)/np.sum(histo[0]),)
+        return histo
 
 class Interaction(moving.STObject):
     '''Class for an interaction between two road users