Mercurial Hosting > traffic-intelligence
changeset 293:ee3302528cdc
rearranged new code by Paul (works now)
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 08 Feb 2013 18:13:29 -0500 |
parents | 8b2c8a4015f1 |
children | 1f253f218b9f |
files | python/event.py python/ml.py |
diffstat | 2 files changed, 66 insertions(+), 81 deletions(-) [+] |
line wrap: on
line diff
--- a/python/event.py Wed Feb 06 20:39:14 2013 -0500 +++ b/python/event.py Fri Feb 08 18:13:29 2013 -0500 @@ -2,16 +2,75 @@ '''Libraries for events Interactions, pedestrian crossing...''' -## Native +import numpy as np + import multiprocessing import itertools -#import utils; -import numpy as np + import moving import prediction __metaclass__ = type +class Interaction(moving.STObject): + '''Class for an interaction between two road users + or a road user and an obstacle + + link to the moving objects + ''' + + categories = {'headon': 0, + 'rearend': 1, + 'side': 2, + 'parallel': 3} + + def __init__(self, num = None, timeInterval = None, roaduserNum1 = None, roaduserNum2 = None, movingObject1 = None, movingObject2 = None, categoryNum = None): + moving.STObject.__init__(self, num, timeInterval) + self.roaduserNumbers = set([roaduserNum1, roaduserNum2]) + self.movingObject1 = movingObject1 + self.movingObject2 = movingObject2 + self.categoryNum = categoryNum + + def getIndicator(self, indicatorName): + if hasattr(self, 'indicators'): + for i in self.indicators: + if i.name == indicatorName: + return i + else: + return None + + def computeIndicators(self): + '''Computes the collision course cosine only if the cosine is positive''' + collisionCourseDotProduct = [0]*int(self.timeInterval.length()) + collisionCourseCosine = {} + distances = [0]*int(self.timeInterval.length()) + for i,instant in enumerate(self.timeInterval): + deltap = self.movingObject1.getPositionAtInstant(instant)-self.movingObject2.getPositionAtInstant(instant) + deltav = self.movingObject2.getVelocityAtInstant(instant)-self.movingObject1.getVelocityAtInstant(instant) + collisionCourseDotProduct[i] = moving.Point.dot(deltap, deltav) + distances[i] = deltap.norm2() + if collisionCourseDotProduct[i] > 0: + collisionCourseCosine[instant] = collisionCourseDotProduct[i]/(distances[i]*deltav.norm2()) + self.indicators = [moving.SeverityIndicator('Collision Course Dot Product', collisionCourseDotProduct, self.timeInterval), + moving.SeverityIndicator('Distances', distances, self.timeInterval), + moving.SeverityIndicator('Collision Course Cosine', collisionCourseCosine)] + + +def createInteractions(objects): + '''Create all interactions of two co-existing road users + + todo add test to compute categories?''' + interactions = [] + num = 0 + for i in xrange(len(objects)): + for j in xrange(i): + commonTimeInterval = objects[i].commonTimeInterval(objects[j]) + if not commonTimeInterval.empty(): + interactions.append(Interaction(num, commonTimeInterval, objects[i].num, objects[j].num, objects[i], objects[j])) + num += 1 + return interactions + + # 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 @@ -46,23 +105,12 @@ return calculateIndicatorPipe(*a_b) class VehPairs(): - # Create a veh-pairs object from objects list + '''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? - ''' - self.pairs = [] + self.pairs = createInteractions(objects) self.interactionCount = 0 self.CPcount = 0 self.CZcount = 0 - num = 0 - for i in xrange(len(objects)): - for j in xrange(i): - commonTimeInterval = objects[i].commonTimeInterval(objects[j]) - if not commonTimeInterval.empty(): - self.pairs.append(event.Interaction(num, commonTimeInterval, objects[i].num, objects[j].num, objects[i], objects[j])) - num += 1 # Process indicator calculation with support for multi-threading def calculateIndicators(self,predParam,threads=1,timeHorizon=75,collisionDistanceThreshold=1.8): @@ -102,7 +150,6 @@ self.interactionCount = self.interactionCount + len(j.CP) self.CPcount = len(self.getCPlist()) self.Czcount = len(self.getCZlist()) - return def getPairsWCP(self): @@ -149,68 +196,6 @@ histo += (histo[0].astype(float)/np.sum(histo[0]),) return histo -class Interaction(moving.STObject): - '''Class for an interaction between two road users - or a road user and an obstacle - - link to the moving objects - ''' - - categories = {'headon': 0, - 'rearend': 1, - 'side': 2, - 'parallel': 3} - - def __init__(self, num = None, timeInterval = None, roaduserNum1 = None, roaduserNum2 = None, movingObject1 = None, movingObject2 = None, categoryNum = None): - moving.STObject.__init__(self, num, timeInterval) - self.roaduserNumbers = set([roaduserNum1, roaduserNum2]) - self.movingObject1 = movingObject1 - self.movingObject2 = movingObject2 - self.categoryNum = categoryNum - - def getIndicator(self, indicatorName): - if hasattr(self, 'indicators'): - for i in self.indicators: - if i.name == indicatorName: - return i - else: - return None - - def computeIndicators(self): - '''Computes the collision course cosine only if the cosine is positive''' - collisionCourseDotProduct = [0]*int(self.timeInterval.length()) - collisionCourseCosine = {} - distances = [0]*int(self.timeInterval.length()) - for i,instant in enumerate(self.timeInterval): - deltap = self.movingObject1.getPositionAtInstant(instant)-self.movingObject2.getPositionAtInstant(instant) - deltav = self.movingObject2.getVelocityAtInstant(instant)-self.movingObject1.getVelocityAtInstant(instant) - collisionCourseDotProduct[i] = moving.Point.dot(deltap, deltav) - distances[i] = deltap.norm2() - if collisionCourseDotProduct[i] > 0: - collisionCourseCosine[instant] = collisionCourseDotProduct[i]/(distances[i]*deltav.norm2()) - self.indicators = [moving.SeverityIndicator('Collision Course Dot Product', collisionCourseDotProduct, self.timeInterval), - moving.SeverityIndicator('Distances', distances, self.timeInterval), - moving.SeverityIndicator('Collision Course Cosine', collisionCourseCosine)] - - -######====>BEGIN LEGACY CODE -def createInteractions(objects): - '''Create all interactions of two co-existing road users - - todo add test to compute categories?''' - interactions = [] - num = 0 - for i in xrange(len(objects)): - for j in xrange(i): - commonTimeInterval = objects[i].commonTimeInterval(objects[j]) - if not commonTimeInterval.empty(): - interactions.append(Interaction(num, commonTimeInterval, objects[i].num, objects[j].num, objects[i], objects[j])) - num += 1 - return interactions -#<====END LEGACY CODE##### - - - class Crossing(moving.STObject): '''Class for the event of a street crossing
--- a/python/ml.py Wed Feb 06 20:39:14 2013 -0500 +++ b/python/ml.py Fri Feb 08 18:13:29 2013 -0500 @@ -58,7 +58,7 @@ return centroids -def spectralClustering(similarityMatrix,k): +def spectralClustering(similarityMatrix, k, iter=20): '''Spectral Clustering algorithm''' n = len(similarityMatrix) # create Laplacian matrix @@ -74,6 +74,6 @@ # k-means from scipy.cluster.vq import kmeans, whiten, vq features = whiten(features) - centroids,distortion = kmeans(features,k,iter=20) # default iter = 20 + centroids,distortion = kmeans(features,k, iter) code,distance = vq(features,centroids) # code starting from 0 (represent first cluster) to k-1 (last cluster) return code,sigma