comparison python/storage.py @ 636:3058e00887bc

removed all issues because of tests with None, using is instead of == or !=
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 24 Mar 2015 18:11:28 +0100
parents 977407c9f815
children 932f96c89212
comparison
equal deleted inserted replaced
635:6ae68383071e 636:3058e00887bc
343 obj = None 343 obj = None
344 objects = [] 344 objects = []
345 for row in cursor: 345 for row in cursor:
346 if row[0] != objId: 346 if row[0] != objId:
347 objId = row[0] 347 objId = row[0]
348 if obj != None and obj.length() == obj.positions.length(): 348 if obj is not None and obj.length() == obj.positions.length():
349 objects.append(obj) 349 objects.append(obj)
350 elif obj != None: 350 elif obj is not None:
351 print('Object {} is missing {} positions'.format(obj.getNum(), int(obj.length())-obj.positions.length())) 351 print('Object {} is missing {} positions'.format(obj.getNum(), int(obj.length())-obj.positions.length()))
352 obj = moving.MovingObject(row[0], timeInterval = moving.TimeInterval(row[1], row[1]), positions = moving.Trajectory([[row[2]],[row[3]]])) 352 obj = moving.MovingObject(row[0], timeInterval = moving.TimeInterval(row[1], row[1]), positions = moving.Trajectory([[row[2]],[row[3]]]))
353 else: 353 else:
354 obj.timeInterval.last = row[1] 354 obj.timeInterval.last = row[1]
355 obj.positions.addPositionXY(row[2],row[3]) 355 obj.positions.addPositionXY(row[2],row[3])
356 356
357 if obj != None and obj.length() == obj.positions.length(): 357 if obj is not None and obj.length() == obj.positions.length():
358 objects.append(obj) 358 objects.append(obj)
359 elif obj != None: 359 elif obj is not None:
360 print('Object {} is missing {} positions'.format(obj.getNum(), int(obj.length())-obj.positions.length())) 360 print('Object {} is missing {} positions'.format(obj.getNum(), int(obj.length())-obj.positions.length()))
361 361
362 return objects 362 return objects
363 363
364 def loadUserTypesFromTable(cursor, trajectoryType, objectNumbers): 364 def loadUserTypesFromTable(cursor, trajectoryType, objectNumbers):
496 createIndicatorTables(cursor) 496 createIndicatorTables(cursor)
497 for inter in interactions: 497 for inter in interactions:
498 saveInteraction(cursor, inter) 498 saveInteraction(cursor, inter)
499 for indicatorName in indicatorNames: 499 for indicatorName in indicatorNames:
500 indicator = inter.getIndicator(indicatorName) 500 indicator = inter.getIndicator(indicatorName)
501 if indicator != None: 501 if indicator is not None:
502 saveIndicator(cursor, inter.getNum(), indicator) 502 saveIndicator(cursor, inter.getNum(), indicator)
503 except sqlite3.OperationalError as error: 503 except sqlite3.OperationalError as error:
504 printDBError(error) 504 printDBError(error)
505 connection.commit() 505 connection.commit()
506 connection.close() 506 connection.close()
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): 655 def loadTrajectoriesFromVissimFile(filename, simulationStepsPerTimeUnit, nObjects = -1, warmUpLastInstant = None, usePandas = False):
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)
661 661
662 Assumed to be sorted over time''' 662 Assumed to be sorted over time'''
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 inputfile = openCheck(filename, quitting = True) 666 if usePandas:
667 667 from pandas import read_csv
668 # data = pd.read_csv(filename, skiprows=15, delimiter=';') 668 data = read_csv(filename, delimiter=';', skiprows=16)
669 # skip header: 15 lines + 1 669 print('Work in progress')
670 line = readline(inputfile, '*$') 670 return []
671 while len(line) > 0:#for line in inputfile: 671 else:
672 data = line.strip().split(';') 672 inputfile = openCheck(filename, quitting = True)
673 objNum = int(data[1]) 673
674 instant = int(float(data[0])*simulationStepsPerTimeUnit) 674 # data = pd.read_csv(filename, skiprows=15, delimiter=';')
675 s = float(data[4]) 675 # skip header: 15 lines + 1
676 y = float(data[5])
677 lane = data[2]+'_'+data[3]
678 if objNum not in firstInstants:
679 firstInstants[objNum] = instant
680 if warmUpLastInstant == None or firstInstants[objNum] >= warmUpLastInstant:
681 if nObjects < 0 or len(objects) < nObjects:
682 objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(instant, instant))
683 objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory()
684 if (warmUpLastInstant == None or firstInstants[objNum] >= warmUpLastInstant) and objNum in objects:
685 objects[objNum].timeInterval.last = instant
686 objects[objNum].curvilinearPositions.addPositionSYL(s, y, lane)
687 line = readline(inputfile, '*$') 676 line = readline(inputfile, '*$')
688 677 while len(line) > 0:#for line in inputfile:
689 return objects.values() 678 data = line.strip().split(';')
679 objNum = int(data[1])
680 instant = int(float(data[0])*simulationStepsPerTimeUnit)
681 s = float(data[4])
682 y = float(data[5])
683 lane = data[2]+'_'+data[3]
684 if objNum not in firstInstants:
685 firstInstants[objNum] = instant
686 if warmUpLastInstant is None or firstInstants[objNum] >= warmUpLastInstant:
687 if nObjects < 0 or len(objects) < nObjects:
688 objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(instant, instant))
689 objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory()
690 if (warmUpLastInstant is None or firstInstants[objNum] >= warmUpLastInstant) and objNum in objects:
691 objects[objNum].timeInterval.last = instant
692 objects[objNum].curvilinearPositions.addPositionSYL(s, y, lane)
693 line = readline(inputfile, '*$')
694
695 return objects.values()
690 696
691 def loadTrajectoriesFromNgsimFile(filename, nObjects = -1, sequenceNum = -1): 697 def loadTrajectoriesFromNgsimFile(filename, nObjects = -1, sequenceNum = -1):
692 '''Reads data from the trajectory data provided by NGSIM project 698 '''Reads data from the trajectory data provided by NGSIM project
693 and returns the list of Feature objects''' 699 and returns the list of Feature objects'''
694 objects = [] 700 objects = []
774 positions = obj.getPositions() 780 positions = obj.getPositions()
775 curvilinearPositions = obj.getCurvilinearPositions() 781 curvilinearPositions = obj.getCurvilinearPositions()
776 for i in xrange(int(obj.length())): 782 for i in xrange(int(obj.length())):
777 p1 = positions[i] 783 p1 = positions[i]
778 s = '{},{},{},{}'.format(obj.num,timeInterval[i],p1.x,p1.y) 784 s = '{},{},{},{}'.format(obj.num,timeInterval[i],p1.x,p1.y)
779 if curvilinearPositions != None: 785 if curvilinearPositions is not None:
780 p2 = curvilinearPositions[i] 786 p2 = curvilinearPositions[i]
781 s += ',{},{}'.format(p2[0],p2[1]) 787 s += ',{},{}'.format(p2[0],p2[1])
782 f.write(s+'\n') 788 f.write(s+'\n')
783 789
784 def writeTrajectoriesToCsv(filename, objects): 790 def writeTrajectoriesToCsv(filename, objects):
842 self.maxExtremeAcceleration = config.getfloat(self.sectionHeader, 'max-extreme-acceleration')/self.videoFrameRate**2 848 self.maxExtremeAcceleration = config.getfloat(self.sectionHeader, 'max-extreme-acceleration')/self.videoFrameRate**2
843 self.maxExtremeSteering = config.getfloat(self.sectionHeader, 'max-extreme-steering')/self.videoFrameRate 849 self.maxExtremeSteering = config.getfloat(self.sectionHeader, 'max-extreme-steering')/self.videoFrameRate
844 self.useFeaturesForPrediction = config.getboolean(self.sectionHeader, 'use-features-prediction') 850 self.useFeaturesForPrediction = config.getboolean(self.sectionHeader, 'use-features-prediction')
845 851
846 def __init__(self, filename = None): 852 def __init__(self, filename = None):
847 if filename != None: 853 if filename is not None:
848 self.loadConfigFile(filename) 854 self.loadConfigFile(filename)
849 855
850 class SceneParameters: 856 class SceneParameters:
851 def __init__(self, config, sectionName): 857 def __init__(self, config, sectionName):
852 from ConfigParser import NoOptionError 858 from ConfigParser import NoOptionError