changeset 526:21bdeb29f855

corrected bug in initialization of lists and loading trajectories from vissim files
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 20 Jun 2014 17:45:32 -0400
parents 7124c7d2a663
children 37830a831818
files python/events.py python/ml.py python/moving.py python/storage.py
diffstat 4 files changed, 35 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/python/events.py	Fri Jun 20 00:20:29 2014 -0400
+++ b/python/events.py	Fri Jun 20 17:45:32 2014 -0400
@@ -303,7 +303,7 @@
                 lists.append(j.num)
         return lists
     
-    def getCPlist(self,indicatorThreshold=99999):
+    def getCPlist(self,indicatorThreshold=float('Inf')):
         lists = []
         for j in self.pairs:
             if(j.hasCP):
@@ -312,7 +312,7 @@
                         lists.append([k,j.CP[k][0]])
         return lists
      
-    def getCZlist(self,indicatorThreshold=99999):
+    def getCZlist(self,indicatorThreshold=float('Inf')):
         lists = []
         for j in self.pairs:
             if(j.hasCZ):
--- a/python/ml.py	Fri Jun 20 00:20:29 2014 -0400
+++ b/python/ml.py	Fri Jun 20 17:45:32 2014 -0400
@@ -58,7 +58,7 @@
     Either the initialCentroids or k are passed'''
     pass
 
-def assignCluster(data, similarFunc, initialCentroids = [], shuffleData = True):
+def assignCluster(data, similarFunc, initialCentroids = None, shuffleData = True):
     '''k-means algorithm with similarity function
     Two instances should be in the same cluster if the sameCluster function returns true for two instances. It is supposed that the average centroid of a set of instances can be computed, using the function. 
     The number of clusters will be determined accordingly
@@ -71,10 +71,10 @@
     localdata = copy(data) # shallow copy to avoid modifying data
     if shuffleData:
         shuffle(localdata)
-    if initialCentroids:
+    if initialCentroids == None:
+        centroids = [Centroid(localdata[0])]
+    else:
         centroids = deepcopy(initialCentroids)
-    else:
-        centroids = [Centroid(localdata[0])]
     for instance in localdata[1:]:
         i = 0
         while i<len(centroids) and not similarFunc(centroids[i].instance, instance):
--- a/python/moving.py	Fri Jun 20 00:20:29 2014 -0400
+++ b/python/moving.py	Fri Jun 20 17:45:32 2014 -0400
@@ -100,7 +100,11 @@
 
     def __getitem__(self, i):
         if not self.empty():
-            return self.first+i
+            if isinstance(i, int):
+                return self.first+i
+            else:
+                raise TypeError, "Invalid argument type."
+            #elif isinstance( key, slice ):
 
     def __iter__(self):
         self.iterInstantNum = -1
@@ -434,14 +438,18 @@
     def length(self):
         return self.__len__()
 
+    def __getitem__(self, i):
+        if isinstance(i, int):
+            return Point(self.positions[0][i], self.positions[1][i])
+        else:
+            raise TypeError, "Invalid argument type."
+            #elif isinstance( key, slice ):
+
     def __str__(self):
         return ' '.join([self.__getitem__(i).__str__() for i in xrange(self.length())])
 
     def __repr__(self):
-        return str(self)
-
-    def __getitem__(self, i):
-        return Point(self.positions[0][i], self.positions[1][i])
+        return self.__str__()
 
     def __iter__(self):
         self.iterInstantNum = 0
@@ -567,7 +575,6 @@
                 return True
         return False
 
-
     def wiggliness(self):
         return self.cumulatedDisplacement()/float(Point.distanceNorm2(self.__getitem__(0),self.__getitem__(self.length()-1)))
 
@@ -625,12 +632,22 @@
     longitudinal coordinate is stored as first coordinate (exterior name S)
     lateral coordiante is stored as second coordinate'''
 
-    def __init__(self, S = [], Y = [], lanes = []):
-        self.positions = [S,Y]
-        self.lanes = lanes
+    def __init__(self, S = None, Y = None, lanes = None):
+        if S == None or Y == None:
+            self.positions = [[],[]]
+        else:
+            self.positions = [S,Y]
+        if lanes == None:
+            self.lanes = []
+        else:
+            self.lanes = lanes
         
     def __getitem__(self,i): 
-        return [self.positions[0][i], self.positions[1][i], self.lanes[i]]
+        if isinstance(i, int):
+            return [self.positions[0][i], self.positions[1][i], self.lanes[i]]
+        else:
+            raise TypeError, "Invalid argument type."
+            #elif isinstance( key, slice ):
 
     def getSCoordinates(self):
         return self.getXCoordinates()
--- a/python/storage.py	Fri Jun 20 00:20:29 2014 -0400
+++ b/python/storage.py	Fri Jun 20 17:45:32 2014 -0400
@@ -444,6 +444,8 @@
             objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory()
         objects[objNum].timeInterval.last = instant
         objects[objNum].curvilinearPositions.addPosition(s, y, lane)
+        if nObjects > 0 and len(objects) > nObjects:
+            return objects.values()[:nObjects]
 
     return objects.values()