diff python/storage.py @ 646:6680a69d5bf3

added fast function to detect VISSIM simulations with errors
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 14 Apr 2015 16:30:27 +0200
parents 5ed2118c959d
children 458890c0437c
line wrap: on
line diff
--- a/python/storage.py	Mon Apr 13 00:11:21 2015 +0200
+++ b/python/storage.py	Tue Apr 14 16:30:27 2015 +0200
@@ -705,6 +705,31 @@
 
         return objects.values()
 
+def countStoppedVehiclesVissim(filename, proportionStationaryTime = 0.7):
+    '''Counts the number of vehicles stopped for a long time in a VISSIM trajectory file
+    and the total number of vehicles
+
+    Vehicles are considered finally stationary
+    if more than proportionStationaryTime of their total time'''
+    from pandas import read_csv
+    from numpy import array, sum as npsum
+    data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, usecols = ['NO', '$VEHICLE:SIMSEC', 'POS']) # 'LANE\LINK\NO', 'LANE\INDEX', 
+    data.sort(['$VEHICLE:SIMSEC'], inplace = True)
+    #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']]
+
+    nStationary = 0
+    from matplotlib.pyplot import plot, figure
+    nVehicles = 0
+    for name, group in data.groupby(['NO'], sort = False):
+        nVehicles += 1
+        positions = array(group['POS'])
+        diff = positions[1:]-positions[:-1]
+        if npsum(diff == 0.) >= proportionStationaryTime*len(positions):
+            nStationary += 1
+
+    return nStationary, nVehicles
+
 def countCollisionsVissim(filename, collisionTimeDifference = 0.2):
     '''Counts the number of collisions per lane in a VISSIM trajectory file