Mercurial > hg > nsaunier > traffic-intelligence
comparison python/storage.py @ 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 | bfaa6b95dae2 |
| children | 6680a69d5bf3 |
comparison
equal
deleted
inserted
replaced
| 644:e54751e71d61 | 645:5ed2118c959d |
|---|---|
| 663 objects = {} # dictionary of objects index by their id | 663 objects = {} # dictionary of objects index by their id |
| 664 | 664 |
| 665 if usePandas: | 665 if usePandas: |
| 666 from pandas import read_csv | 666 from pandas import read_csv |
| 667 from numpy import min, max, round | 667 from numpy import min, max, round |
| 668 data = read_csv(filename, delimiter=';', skiprows=16) | 668 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1) |
| 669 data['LANE'] = data['LANE\LINK\NO'].astype(str)+'_'+data['LANE\INDEX'].astype(str) | 669 data['LANE'] = data['LANE\LINK\NO'].astype(str)+'_'+data['LANE\INDEX'].astype(str) |
| 670 data['TIME'] = data['$VEHICLE:SIMSEC']*simulationStepsPerTimeUnit | 670 data['TIME'] = data['$VEHICLE:SIMSEC']*simulationStepsPerTimeUnit |
| 671 grouped = data.loc[:,['NO','TIME']].groupby(['NO'], as_index = False) | 671 grouped = data.loc[:,['NO','TIME']].groupby(['NO'], as_index = False) |
| 672 instants = grouped['TIME'].agg({'first': min, 'last': max}) | 672 instants = grouped['TIME'].agg({'first': min, 'last': max}) |
| 673 if warmUpLastInstant is not None: | 673 if warmUpLastInstant is not None: |
| 702 objects[objNum].timeInterval.last = instant | 702 objects[objNum].timeInterval.last = instant |
| 703 objects[objNum].curvilinearPositions.addPositionSYL(s, y, lane) | 703 objects[objNum].curvilinearPositions.addPositionSYL(s, y, lane) |
| 704 line = readline(inputfile, '*$') | 704 line = readline(inputfile, '*$') |
| 705 | 705 |
| 706 return objects.values() | 706 return objects.values() |
| 707 | |
| 708 def countCollisionsVissim(filename, collisionTimeDifference = 0.2): | |
| 709 '''Counts the number of collisions per lane in a VISSIM trajectory file | |
| 710 | |
| 711 To distinguish between cars passing and collision, | |
| 712 one checks when the sign of the position difference inverts | |
| 713 (if the time are closer than collisionTimeDifference)''' | |
| 714 from pandas import read_csv, merge | |
| 715 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, usecols = ['LANE\LINK\NO', 'LANE\INDEX', '$VEHICLE:SIMSEC', 'NO', 'POS']) | |
| 716 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) | |
| 717 merged = merged[merged['NO_x']>merged['NO_y']] | |
| 718 | |
| 719 nCollisions = 0 | |
| 720 for name, group in merged.groupby(['LANE\LINK\NO', 'LANE\INDEX', 'NO_x', 'NO_y']): | |
| 721 diff = group['POS_x']-group['POS_y'] | |
| 722 if len(diff) >= 2 and min(diff) < 0 and max(diff) > 0: | |
| 723 xidx = diff[diff < 0].argmax() | |
| 724 yidx = diff[diff > 0].argmin() | |
| 725 if abs(group.loc[xidx, '$VEHICLE:SIMSEC'] - group.loc[yidx, '$VEHICLE:SIMSEC']) <= collisionTimeDifference: | |
| 726 nCollisions += 1 | |
| 727 return nCollisions | |
| 707 | 728 |
| 708 def loadTrajectoriesFromNgsimFile(filename, nObjects = -1, sequenceNum = -1): | 729 def loadTrajectoriesFromNgsimFile(filename, nObjects = -1, sequenceNum = -1): |
| 709 '''Reads data from the trajectory data provided by NGSIM project | 730 '''Reads data from the trajectory data provided by NGSIM project |
| 710 and returns the list of Feature objects''' | 731 and returns the list of Feature objects''' |
| 711 objects = [] | 732 objects = [] |
