changeset 704:f83d125d0c55 dev

cleaning of storage.py and addition of type conversion for VISSIM files (from Laurent Gauthier)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 22 Jul 2015 12:58:04 -0400
parents bee0e7407af7
children 0ceee3b1a96d
files python/storage.py
diffstat 1 files changed, 8 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/python/storage.py	Tue Jul 21 15:52:43 2015 -0400
+++ b/python/storage.py	Wed Jul 22 12:58:04 2015 -0400
@@ -6,7 +6,7 @@
 from base import VideoFilenameAddable
 
 import sqlite3, logging
-from numpy import log
+from numpy import log, min as npmin, max as npmax, round as npround, array, sum as npsum, loadtxt
 
 
 commentChar = '#'
@@ -672,20 +672,21 @@
 
     if usePandas:
         from pandas import read_csv
-        from numpy import min, max, round
         data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1)
         generatePDLaneColumn(data)
         data['TIME'] = data['$VEHICLE:SIMSEC']*simulationStepsPerTimeUnit
         if warmUpLastInstant is not None:
             data = data[data['TIME']>=warmUpLastInstant]
         grouped = data.loc[:,['NO','TIME']].groupby(['NO'], as_index = False)
-        instants = grouped['TIME'].agg({'first': min, 'last': max})
+        instants = grouped['TIME'].agg({'first': npmin, 'last': npmax})
         for row_index, row in instants.iterrows():
             objNum = int(row['NO'])
             tmp = data[data['NO'] == objNum]
             objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(row['first'], row['last']))
             # positions should be rounded to nDecimals decimals only
-            objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory(S = round(tmp['POS'].tolist(), nDecimals), Y = round(tmp['POSLAT'].tolist(), nDecimals), lanes = tmp['LANE'].tolist())
+            objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory(S = npround(tmp['POS'].tolist(), nDecimals), Y = npround(tmp['POSLAT'].tolist(), nDecimals), lanes = tmp['LANE'].tolist())
+            if nObjects > 0 and len(objects) >= nObjects:
+                break
         return objects.values()
     else:
         inputfile = openCheck(filename, quitting = True)
@@ -734,7 +735,6 @@
     If lanes is not None, only the data for the selected lanes will be provided
     (format as string x_y where x is link index and y is lane index)'''
     from pandas import read_csv
-    from numpy import array, sum as npsum
     columns = ['NO', '$VEHICLE:SIMSEC', 'POS']
     if lanes is not None:
         columns += ['LANE\LINK\NO', 'LANE\INDEX']
@@ -743,7 +743,6 @@
     data.sort(['$VEHICLE:SIMSEC'], inplace = True)
 
     nStationary = 0
-    from matplotlib.pyplot import plot, figure
     nVehicles = 0
     for name, group in data.groupby(['NO'], sort = False):
         nVehicles += 1
@@ -770,8 +769,9 @@
 
     nCollisions = 0
     for name, group in merged.groupby(['LANE\LINK\NO', 'LANE\INDEX', 'NO_x', 'NO_y']):
-        diff = group['POS_x']-group['POS_y']
-        if len(diff) >= 2 and min(diff) < 0 and max(diff) > 0:
+        diff = group['POS_x'].convert_objects(convert_numeric=True)-group['POS_y'].convert_objects(convert_numeric=True)
+        # diff = group['POS_x']-group['POS_y'] # to check the impact of convert_objects and the possibility of using type conversion in read_csv or function to convert strings if any
+        if len(diff) >= 2 and npmin(diff) < 0 and npmax(diff) > 0:
             xidx = diff[diff < 0].argmax()
             yidx = diff[diff > 0].argmin()
             if abs(group.loc[xidx, '$VEHICLE:SIMSEC'] - group.loc[yidx, '$VEHICLE:SIMSEC']) <= collisionTimeDifference:
@@ -890,7 +890,6 @@
 
     def loadConfigFile(self, filename):
         from ConfigParser import ConfigParser
-        from numpy import loadtxt
         from os import path
 
         config = ConfigParser()