Mercurial > hg > nsaunier > traffic-intelligence
comparison python/storage.py @ 729:dad99b86a104 dev
merge with default
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 10 Aug 2015 17:52:19 -0400 |
| parents | 49e99ca34a7d |
| children | 967d244968a4 |
comparison
equal
deleted
inserted
replaced
| 728:4e89341edd29 | 729:dad99b86a104 |
|---|---|
| 652 s = f.readline() | 652 s = f.readline() |
| 653 while (len(s) > 0) and s[0] in commentCharacters: | 653 while (len(s) > 0) and s[0] in commentCharacters: |
| 654 s = f.readline() | 654 s = f.readline() |
| 655 return s.strip() | 655 return s.strip() |
| 656 | 656 |
| 657 def getLines(f, commentCharacters = commentChar): | 657 def getLines(f, delimiterChar = delimiterChar, commentCharacters = commentChar): |
| 658 '''Gets a complete entry (all the lines) in between delimiterChar.''' | 658 '''Gets a complete entry (all the lines) in between delimiterChar.''' |
| 659 dataStrings = [] | 659 dataStrings = [] |
| 660 s = readline(f, commentCharacters) | 660 s = readline(f, commentCharacters) |
| 661 while len(s) > 0: | 661 while len(s) > 0 and s[0] != delimiterChar: |
| 662 dataStrings += [s.strip()] | 662 dataStrings += [s.strip()] |
| 663 s = readline(f, commentCharacters) | 663 s = readline(f, commentCharacters) |
| 664 return dataStrings | 664 return dataStrings |
| 665 | 665 |
| 666 def writeList(filename, l): | 666 def writeList(filename, l): |
| 699 else: return self.fp.readline() | 699 else: return self.fp.readline() |
| 700 | 700 |
| 701 def generatePDLaneColumn(data): | 701 def generatePDLaneColumn(data): |
| 702 data['LANE'] = data['LANE\LINK\NO'].astype(str)+'_'+data['LANE\INDEX'].astype(str) | 702 data['LANE'] = data['LANE\LINK\NO'].astype(str)+'_'+data['LANE\INDEX'].astype(str) |
| 703 | 703 |
| 704 def loadTrajectoriesFromVissimFile(filename, simulationStepsPerTimeUnit, nObjects = -1, warmUpLastInstant = None, usePandas = False, nDecimals = 2): | 704 def loadTrajectoriesFromVissimFile(filename, simulationStepsPerTimeUnit, nObjects = -1, warmUpLastInstant = None, usePandas = False, nDecimals = 2, lowMemory = True): |
| 705 '''Reads data from VISSIM .fzp trajectory file | 705 '''Reads data from VISSIM .fzp trajectory file |
| 706 simulationStepsPerTimeUnit is the number of simulation steps per unit of time used by VISSIM | 706 simulationStepsPerTimeUnit is the number of simulation steps per unit of time used by VISSIM |
| 707 for example, there seems to be 5 simulation steps per simulated second in VISSIM, | 707 for example, there seems to be 5 simulation steps per simulated second in VISSIM, |
| 708 so simulationStepsPerTimeUnit should be 5, | 708 so simulationStepsPerTimeUnit should be 5, |
| 709 so that all times correspond to the number of the simulation step (and can be stored as integers) | 709 so that all times correspond to the number of the simulation step (and can be stored as integers) |
| 714 Assumed to be sorted over time''' | 714 Assumed to be sorted over time''' |
| 715 objects = {} # dictionary of objects index by their id | 715 objects = {} # dictionary of objects index by their id |
| 716 | 716 |
| 717 if usePandas: | 717 if usePandas: |
| 718 from pandas import read_csv | 718 from pandas import read_csv |
| 719 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1) | 719 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, low_memory = lowMemory) |
| 720 generatePDLaneColumn(data) | 720 generatePDLaneColumn(data) |
| 721 data['TIME'] = data['$VEHICLE:SIMSEC']*simulationStepsPerTimeUnit | 721 data['TIME'] = data['$VEHICLE:SIMSEC']*simulationStepsPerTimeUnit |
| 722 if warmUpLastInstant is not None: | 722 if warmUpLastInstant is not None: |
| 723 data = data[data['TIME']>=warmUpLastInstant] | 723 data = data[data['TIME']>=warmUpLastInstant] |
| 724 grouped = data.loc[:,['NO','TIME']].groupby(['NO'], as_index = False) | 724 grouped = data.loc[:,['NO','TIME']].groupby(['NO'], as_index = False) |
| 780 (format as string x_y where x is link index and y is lane index)''' | 780 (format as string x_y where x is link index and y is lane index)''' |
| 781 from pandas import read_csv | 781 from pandas import read_csv |
| 782 columns = ['NO', '$VEHICLE:SIMSEC', 'POS'] | 782 columns = ['NO', '$VEHICLE:SIMSEC', 'POS'] |
| 783 if lanes is not None: | 783 if lanes is not None: |
| 784 columns += ['LANE\LINK\NO', 'LANE\INDEX'] | 784 columns += ['LANE\LINK\NO', 'LANE\INDEX'] |
| 785 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, usecols = columns) | 785 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, usecols = columns, low_memory = lowMemory) |
| 786 data = selectPDLanes(data, lanes) | 786 data = selectPDLanes(data, lanes) |
| 787 data.sort(['$VEHICLE:SIMSEC'], inplace = True) | 787 data.sort(['$VEHICLE:SIMSEC'], inplace = True) |
| 788 | 788 |
| 789 nStationary = 0 | 789 nStationary = 0 |
| 790 nVehicles = 0 | 790 nVehicles = 0 |
| 804 one checks when the sign of the position difference inverts | 804 one checks when the sign of the position difference inverts |
| 805 (if the time are closer than collisionTimeDifference) | 805 (if the time are closer than collisionTimeDifference) |
| 806 If lanes is not None, only the data for the selected lanes will be provided | 806 If lanes is not None, only the data for the selected lanes will be provided |
| 807 (format as string x_y where x is link index and y is lane index)''' | 807 (format as string x_y where x is link index and y is lane index)''' |
| 808 from pandas import read_csv, merge | 808 from pandas import read_csv, merge |
| 809 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, usecols = ['LANE\LINK\NO', 'LANE\INDEX', '$VEHICLE:SIMSEC', 'NO', 'POS']) | 809 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, usecols = ['LANE\LINK\NO', 'LANE\INDEX', '$VEHICLE:SIMSEC', 'NO', 'POS'], low_memory = lowMemory) |
| 810 data = selectPDLanes(data, lanes) | 810 data = selectPDLanes(data, lanes) |
| 811 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) | 811 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) |
| 812 merged = merged[merged['NO_x']>merged['NO_y']] | 812 merged = merged[merged['NO_x']>merged['NO_y']] |
| 813 | 813 |
| 814 nCollisions = 0 | 814 nCollisions = 0 |
