Mercurial > hg > nsaunier > traffic-intelligence
comparison python/storage.py @ 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 | f0a897d7f3a5 |
| children | 523eda2fafd4 |
comparison
equal
deleted
inserted
replaced
| 703:bee0e7407af7 | 704:f83d125d0c55 |
|---|---|
| 4 | 4 |
| 5 import utils, moving, events, indicators | 5 import utils, moving, events, indicators |
| 6 from base import VideoFilenameAddable | 6 from base import VideoFilenameAddable |
| 7 | 7 |
| 8 import sqlite3, logging | 8 import sqlite3, logging |
| 9 from numpy import log | 9 from numpy import log, min as npmin, max as npmax, round as npround, array, sum as npsum, loadtxt |
| 10 | 10 |
| 11 | 11 |
| 12 commentChar = '#' | 12 commentChar = '#' |
| 13 | 13 |
| 14 delimiterChar = '%'; | 14 delimiterChar = '%'; |
| 670 Assumed to be sorted over time''' | 670 Assumed to be sorted over time''' |
| 671 objects = {} # dictionary of objects index by their id | 671 objects = {} # dictionary of objects index by their id |
| 672 | 672 |
| 673 if usePandas: | 673 if usePandas: |
| 674 from pandas import read_csv | 674 from pandas import read_csv |
| 675 from numpy import min, max, round | |
| 676 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1) | 675 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1) |
| 677 generatePDLaneColumn(data) | 676 generatePDLaneColumn(data) |
| 678 data['TIME'] = data['$VEHICLE:SIMSEC']*simulationStepsPerTimeUnit | 677 data['TIME'] = data['$VEHICLE:SIMSEC']*simulationStepsPerTimeUnit |
| 679 if warmUpLastInstant is not None: | 678 if warmUpLastInstant is not None: |
| 680 data = data[data['TIME']>=warmUpLastInstant] | 679 data = data[data['TIME']>=warmUpLastInstant] |
| 681 grouped = data.loc[:,['NO','TIME']].groupby(['NO'], as_index = False) | 680 grouped = data.loc[:,['NO','TIME']].groupby(['NO'], as_index = False) |
| 682 instants = grouped['TIME'].agg({'first': min, 'last': max}) | 681 instants = grouped['TIME'].agg({'first': npmin, 'last': npmax}) |
| 683 for row_index, row in instants.iterrows(): | 682 for row_index, row in instants.iterrows(): |
| 684 objNum = int(row['NO']) | 683 objNum = int(row['NO']) |
| 685 tmp = data[data['NO'] == objNum] | 684 tmp = data[data['NO'] == objNum] |
| 686 objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(row['first'], row['last'])) | 685 objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(row['first'], row['last'])) |
| 687 # positions should be rounded to nDecimals decimals only | 686 # positions should be rounded to nDecimals decimals only |
| 688 objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory(S = round(tmp['POS'].tolist(), nDecimals), Y = round(tmp['POSLAT'].tolist(), nDecimals), lanes = tmp['LANE'].tolist()) | 687 objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory(S = npround(tmp['POS'].tolist(), nDecimals), Y = npround(tmp['POSLAT'].tolist(), nDecimals), lanes = tmp['LANE'].tolist()) |
| 688 if nObjects > 0 and len(objects) >= nObjects: | |
| 689 break | |
| 689 return objects.values() | 690 return objects.values() |
| 690 else: | 691 else: |
| 691 inputfile = openCheck(filename, quitting = True) | 692 inputfile = openCheck(filename, quitting = True) |
| 692 # data = pd.read_csv(filename, skiprows=15, delimiter=';') | 693 # data = pd.read_csv(filename, skiprows=15, delimiter=';') |
| 693 # skip header: 15 lines + 1 | 694 # skip header: 15 lines + 1 |
| 732 Vehicles are considered finally stationary | 733 Vehicles are considered finally stationary |
| 733 if more than proportionStationaryTime of their total time | 734 if more than proportionStationaryTime of their total time |
| 734 If lanes is not None, only the data for the selected lanes will be provided | 735 If lanes is not None, only the data for the selected lanes will be provided |
| 735 (format as string x_y where x is link index and y is lane index)''' | 736 (format as string x_y where x is link index and y is lane index)''' |
| 736 from pandas import read_csv | 737 from pandas import read_csv |
| 737 from numpy import array, sum as npsum | |
| 738 columns = ['NO', '$VEHICLE:SIMSEC', 'POS'] | 738 columns = ['NO', '$VEHICLE:SIMSEC', 'POS'] |
| 739 if lanes is not None: | 739 if lanes is not None: |
| 740 columns += ['LANE\LINK\NO', 'LANE\INDEX'] | 740 columns += ['LANE\LINK\NO', 'LANE\INDEX'] |
| 741 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, usecols = columns) | 741 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, usecols = columns) |
| 742 data = selectPDLanes(data, lanes) | 742 data = selectPDLanes(data, lanes) |
| 743 data.sort(['$VEHICLE:SIMSEC'], inplace = True) | 743 data.sort(['$VEHICLE:SIMSEC'], inplace = True) |
| 744 | 744 |
| 745 nStationary = 0 | 745 nStationary = 0 |
| 746 from matplotlib.pyplot import plot, figure | |
| 747 nVehicles = 0 | 746 nVehicles = 0 |
| 748 for name, group in data.groupby(['NO'], sort = False): | 747 for name, group in data.groupby(['NO'], sort = False): |
| 749 nVehicles += 1 | 748 nVehicles += 1 |
| 750 positions = array(group['POS']) | 749 positions = array(group['POS']) |
| 751 diff = positions[1:]-positions[:-1] | 750 diff = positions[1:]-positions[:-1] |
| 768 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) | 767 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) |
| 769 merged = merged[merged['NO_x']>merged['NO_y']] | 768 merged = merged[merged['NO_x']>merged['NO_y']] |
| 770 | 769 |
| 771 nCollisions = 0 | 770 nCollisions = 0 |
| 772 for name, group in merged.groupby(['LANE\LINK\NO', 'LANE\INDEX', 'NO_x', 'NO_y']): | 771 for name, group in merged.groupby(['LANE\LINK\NO', 'LANE\INDEX', 'NO_x', 'NO_y']): |
| 773 diff = group['POS_x']-group['POS_y'] | 772 diff = group['POS_x'].convert_objects(convert_numeric=True)-group['POS_y'].convert_objects(convert_numeric=True) |
| 774 if len(diff) >= 2 and min(diff) < 0 and max(diff) > 0: | 773 # 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 |
| 774 if len(diff) >= 2 and npmin(diff) < 0 and npmax(diff) > 0: | |
| 775 xidx = diff[diff < 0].argmax() | 775 xidx = diff[diff < 0].argmax() |
| 776 yidx = diff[diff > 0].argmin() | 776 yidx = diff[diff > 0].argmin() |
| 777 if abs(group.loc[xidx, '$VEHICLE:SIMSEC'] - group.loc[yidx, '$VEHICLE:SIMSEC']) <= collisionTimeDifference: | 777 if abs(group.loc[xidx, '$VEHICLE:SIMSEC'] - group.loc[yidx, '$VEHICLE:SIMSEC']) <= collisionTimeDifference: |
| 778 nCollisions += 1 | 778 nCollisions += 1 |
| 779 return nCollisions | 779 return nCollisions |
| 888 | 888 |
| 889 Note: framerate is already taken into account''' | 889 Note: framerate is already taken into account''' |
| 890 | 890 |
| 891 def loadConfigFile(self, filename): | 891 def loadConfigFile(self, filename): |
| 892 from ConfigParser import ConfigParser | 892 from ConfigParser import ConfigParser |
| 893 from numpy import loadtxt | |
| 894 from os import path | 893 from os import path |
| 895 | 894 |
| 896 config = ConfigParser() | 895 config = ConfigParser() |
| 897 config.readfp(FakeSecHead(openCheck(filename))) | 896 config.readfp(FakeSecHead(openCheck(filename))) |
| 898 self.sectionHeader = config.sections()[0] | 897 self.sectionHeader = config.sections()[0] |
