changeset 645:5ed2118c959d

added function to count collisions in vissim
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 13 Apr 2015 00:11:21 +0200
parents e54751e71d61
children 6680a69d5bf3
files python/storage.py
diffstat 1 files changed, 22 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/python/storage.py	Fri Apr 10 16:09:13 2015 +0200
+++ b/python/storage.py	Mon Apr 13 00:11:21 2015 +0200
@@ -665,7 +665,7 @@
     if usePandas:
         from pandas import read_csv
         from numpy import min, max, round
-        data = read_csv(filename, delimiter=';', skiprows=16)
+        data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1)
         data['LANE'] = data['LANE\LINK\NO'].astype(str)+'_'+data['LANE\INDEX'].astype(str)
         data['TIME'] = data['$VEHICLE:SIMSEC']*simulationStepsPerTimeUnit
         grouped = data.loc[:,['NO','TIME']].groupby(['NO'], as_index = False)
@@ -704,6 +704,27 @@
             line = readline(inputfile, '*$')
 
         return objects.values()
+
+def countCollisionsVissim(filename, collisionTimeDifference = 0.2):
+    '''Counts the number of collisions per lane in a VISSIM trajectory file
+
+    To distinguish between cars passing and collision, 
+    one checks when the sign of the position difference inverts
+    (if the time are closer than collisionTimeDifference)'''
+    from pandas import read_csv, merge
+    data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, usecols = ['LANE\LINK\NO', 'LANE\INDEX', '$VEHICLE:SIMSEC', 'NO', 'POS'])
+    merged = merge(data, data, how='inner', left_on=['LANE\LINK\NO', 'LANE\INDEX', '$VEHICLE:SIMSEC'], right_on=['LANE\LINK\NO', 'LANE\INDEX', '$VEHICLE:SIMSEC'], sort = False)
+    merged = merged[merged['NO_x']>merged['NO_y']]
+
+    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:
+            xidx = diff[diff < 0].argmax()
+            yidx = diff[diff > 0].argmin()
+            if abs(group.loc[xidx, '$VEHICLE:SIMSEC'] - group.loc[yidx, '$VEHICLE:SIMSEC']) <= collisionTimeDifference:
+                nCollisions += 1
+    return nCollisions
     
 def loadTrajectoriesFromNgsimFile(filename, nObjects = -1, sequenceNum = -1):
     '''Reads data from the trajectory data provided by NGSIM project