comparison python/moving.py @ 546:6c0923f1ce68

add some functions for behaviour analysis
author MohamedGomaa
date Thu, 03 Jul 2014 14:30:20 -0400
parents f012a8ad7a0e
children b5525249eda1
comparison
equal deleted inserted replaced
538:bd1ad468e928 546:6c0923f1ce68
447 def __str__(self): 447 def __str__(self):
448 return ' '.join([self.__getitem__(i).__str__() for i in xrange(self.length())]) 448 return ' '.join([self.__getitem__(i).__str__() for i in xrange(self.length())])
449 449
450 def __repr__(self): 450 def __repr__(self):
451 return self.__str__() 451 return self.__str__()
452
452 453
453 def __iter__(self): 454 def __iter__(self):
454 self.iterInstantNum = 0 455 self.iterInstantNum = 0
455 return self 456 return self
456 457
801 @staticmethod 802 @staticmethod
802 def minDistance(obj1, obj2, instant): 803 def minDistance(obj1, obj2, instant):
803 return MovingObject.distances(obj1, obj2, instant).min() 804 return MovingObject.distances(obj1, obj2, instant).min()
804 805
805 @staticmethod 806 @staticmethod
807 def distances2(obj1, obj2, instant1,instant2):
808 from scipy.spatial.distance import cdist
809 positions1 = [f.getPositionAtInstant(instant1).astuple() for f in obj1.features if f.existsAtInstant(instant1)]
810 positions2 = [f.getPositionAtInstant(instant2).astuple() for f in obj2.features if f.existsAtInstant(instant2)]
811 return cdist(positions1, positions2, metric = 'euclidean')
812
813 @staticmethod
814 def minDistance2(obj1, obj2, instant1,instant2):
815 return MovingObject.distances2(obj1, obj2, instant1,instant2).min()
816
817 @staticmethod
806 def maxDistance(obj1, obj2, instant): 818 def maxDistance(obj1, obj2, instant):
807 return MovingObject.distances(obj1, obj2, instant).max() 819 return MovingObject.distances(obj1, obj2, instant).max()
808 820
809 def maxSize(self): 821 def maxSize(self):
810 '''Returns the max distance between features 822 '''Returns the max distance between features
819 tMaxFeatures = t 831 tMaxFeatures = t
820 return MovingObject.maxDistance(self, self, tMaxFeatures) 832 return MovingObject.maxDistance(self, self, tMaxFeatures)
821 else: 833 else:
822 print('Load features to compute a maximum size') 834 print('Load features to compute a maximum size')
823 return None 835 return None
824 836
837 def setRoutes(self,startCode,endCode):
838 self.startRouteID=startCode
839 self.endRouteID=endCode
840
825 def getInstantsCrossingLane(self, p1, p2): 841 def getInstantsCrossingLane(self, p1, p2):
826 '''Returns the instant(s) 842 '''Returns the instant(s)
827 at which the object passes from one side of the segment to the other 843 at which the object passes from one side of the segment to the other
828 empty list if there is no crossing''' 844 empty list if there is no crossing'''
829 indices = self.positions.getIntersections(p1, p2) 845 indices = self.positions.getIntersections(p1, p2)
940 if areas[userTypename][p.x, p.y] != 0: 956 if areas[userTypename][p.x, p.y] != 0:
941 possibleUserTypes[userType2Enum[userTypename]] += 1 957 possibleUserTypes[userType2Enum[userTypename]] += 1
942 # what to do: threshold for most common type? self.setUserType() 958 # what to do: threshold for most common type? self.setUserType()
943 return possibleUserTypes 959 return possibleUserTypes
944 960
961
945 @staticmethod 962 @staticmethod
946 def collisionCourseDotProduct(movingObject1, movingObject2, instant): 963 def collisionCourseDotProduct(movingObject1, movingObject2, instant):
947 'A positive result indicates that the road users are getting closer' 964 'A positive result indicates that the road users are getting closer'
948 deltap = movingObject1.getPositionAtInstant(instant)-movingObject2.getPositionAtInstant(instant) 965 deltap = movingObject1.getPositionAtInstant(instant)-movingObject2.getPositionAtInstant(instant)
949 deltav = movingObject2.getVelocityAtInstant(instant)-movingObject1.getVelocityAtInstant(instant) 966 deltav = movingObject2.getVelocityAtInstant(instant)-movingObject1.getVelocityAtInstant(instant)