comparison python/storage.py @ 642:932f96c89212

added pandas to read vissim fzp (more robust with respect to column names
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 10 Apr 2015 14:54:05 +0200
parents 3058e00887bc
children bfaa6b95dae2
comparison
equal deleted inserted replaced
641:9fe254f11743 642:932f96c89212
650 if self.sechead: 650 if self.sechead:
651 try: return self.sechead 651 try: return self.sechead
652 finally: self.sechead = None 652 finally: self.sechead = None
653 else: return self.fp.readline() 653 else: return self.fp.readline()
654 654
655 def loadTrajectoriesFromVissimFile(filename, simulationStepsPerTimeUnit, nObjects = -1, warmUpLastInstant = None, usePandas = False): 655 def loadTrajectoriesFromVissimFile(filename, simulationStepsPerTimeUnit, nObjects = -1, warmUpLastInstant = None, usePandas = False, nDecimals = 2):
656 '''Reads data from VISSIM .fzp trajectory file 656 '''Reads data from VISSIM .fzp trajectory file
657 simulationStepsPerTimeUnit is the number of simulation steps per unit of time used by VISSIM 657 simulationStepsPerTimeUnit is the number of simulation steps per unit of time used by VISSIM
658 for example, there seems to be 5 simulation steps per simulated second in VISSIM, 658 for example, there seems to be 5 simulation steps per simulated second in VISSIM,
659 so simulationStepsPerTimeUnit should be 5, 659 so simulationStepsPerTimeUnit should be 5,
660 so that all times correspond to the number of the simulation step (and can be stored as integers) 660 so that all times correspond to the number of the simulation step (and can be stored as integers)
663 objects = {} # dictionary of objects index by their id 663 objects = {} # dictionary of objects index by their id
664 firstInstants = {} 664 firstInstants = {}
665 665
666 if usePandas: 666 if usePandas:
667 from pandas import read_csv 667 from pandas import read_csv
668 from numpy import min, max, round
668 data = read_csv(filename, delimiter=';', skiprows=16) 669 data = read_csv(filename, delimiter=';', skiprows=16)
669 print('Work in progress') 670 data['LANE'] = data['LANE\LINK\NO'].astype(str)+'_'+data['LANE\INDEX'].astype(str)
670 return [] 671 data['TIME'] = data['$VEHICLE:SIMSEC']*simulationStepsPerTimeUnit
672 grouped = data.loc[:,['NO','TIME']].groupby(['NO'], as_index = False)
673 instants = grouped['TIME'].agg({'first': min, 'last': max})
674 if warmUpLastInstant is not None:
675 instants = instants[instants['first'] >= warmUpLastInstant]
676 for row_index, row in instants.iterrows():
677 objNum = int(row['NO'])
678 tmp = data[data['NO'] == objNum]
679 objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(row['first'], row['last']))
680 # positions should be rounded to nDecimals decimals only
681 objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory(S = round(tmp['POS'].tolist(), nDecimals), Y = round(tmp['POSLAT'].tolist(), nDecimals), lanes = tmp['LANE'].tolist())
682 return objects.values()
671 else: 683 else:
672 inputfile = openCheck(filename, quitting = True) 684 inputfile = openCheck(filename, quitting = True)
673
674 # data = pd.read_csv(filename, skiprows=15, delimiter=';') 685 # data = pd.read_csv(filename, skiprows=15, delimiter=';')
675 # skip header: 15 lines + 1 686 # skip header: 15 lines + 1
676 line = readline(inputfile, '*$') 687 line = readline(inputfile, '*$')
677 while len(line) > 0:#for line in inputfile: 688 while len(line) > 0:#for line in inputfile:
678 data = line.strip().split(';') 689 data = line.strip().split(';')