Mercurial > hg > nsaunier > traffic-intelligence
comparison python/storage.py @ 978:184f1dd307f9
corrected print and exception statements for Python 3
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Thu, 08 Feb 2018 05:53:50 -0500 |
| parents | 7f5cbdc107c5 |
| children | cc89267b5ff9 |
comparison
equal
deleted
inserted
replaced
| 977:9c27a0315c4d | 978:184f1dd307f9 |
|---|---|
| 878 '''Open file filename in read mode by default | 878 '''Open file filename in read mode by default |
| 879 and checks it is open''' | 879 and checks it is open''' |
| 880 try: | 880 try: |
| 881 return open(filename, option) | 881 return open(filename, option) |
| 882 except IOError: | 882 except IOError: |
| 883 print 'File {} could not be opened.'.format(filename) | 883 print('File {} could not be opened.'.format(filename)) |
| 884 if quitting: | 884 if quitting: |
| 885 from sys import exit | 885 from sys import exit |
| 886 exit() | 886 exit() |
| 887 return None | 887 return None |
| 888 | 888 |
| 950 headers += ['flow{}'.format(i+1), 'occupancy{}'.format(i+1), 'speed{}'.format(i+1)] | 950 headers += ['flow{}'.format(i+1), 'occupancy{}'.format(i+1), 'speed{}'.format(i+1)] |
| 951 f.close() | 951 f.close() |
| 952 return read_csv(filename, delimiter = ',', names = headers) | 952 return read_csv(filename, delimiter = ',', names = headers) |
| 953 | 953 |
| 954 def generatePDLaneColumn(data): | 954 def generatePDLaneColumn(data): |
| 955 data['LANE'] = data['LANE\LINK\NO'].astype(str)+'_'+data['LANE\INDEX'].astype(str) | 955 data['LANE'] = data['LANE\\LINK\\NO'].astype(str)+'_'+data['LANE\\INDEX'].astype(str) |
| 956 | 956 |
| 957 def convertTrajectoriesVissimToSqlite(filename): | 957 def convertTrajectoriesVissimToSqlite(filename): |
| 958 '''Relies on a system call to sqlite3 | 958 '''Relies on a system call to sqlite3 |
| 959 sqlite3 [file.sqlite] < import_fzp.sql''' | 959 sqlite3 [file.sqlite] < import_fzp.sql''' |
| 960 sqlScriptFilename = "import_fzp.sql" | 960 sqlScriptFilename = "import_fzp.sql" |
| 1096 If lanes is not None, only the data for the selected lanes will be provided | 1096 If lanes is not None, only the data for the selected lanes will be provided |
| 1097 (format as string x_y where x is link index and y is lane index)''' | 1097 (format as string x_y where x is link index and y is lane index)''' |
| 1098 if filename.endswith(".fzp"): | 1098 if filename.endswith(".fzp"): |
| 1099 columns = ['NO', '$VEHICLE:SIMSEC', 'POS'] | 1099 columns = ['NO', '$VEHICLE:SIMSEC', 'POS'] |
| 1100 if lanes is not None: | 1100 if lanes is not None: |
| 1101 columns += ['LANE\LINK\NO', 'LANE\INDEX'] | 1101 columns += ['LANE\\LINK\\NO', 'LANE\\INDEX'] |
| 1102 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, usecols = columns, low_memory = lowMemory) | 1102 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, usecols = columns, low_memory = lowMemory) |
| 1103 data = selectPDLanes(data, lanes) | 1103 data = selectPDLanes(data, lanes) |
| 1104 data.sort(['$VEHICLE:SIMSEC'], inplace = True) | 1104 data.sort(['$VEHICLE:SIMSEC'], inplace = True) |
| 1105 | 1105 |
| 1106 nStationary = 0 | 1106 nStationary = 0 |
| 1126 To distinguish between cars passing and collision, | 1126 To distinguish between cars passing and collision, |
| 1127 one checks when the sign of the position difference inverts | 1127 one checks when the sign of the position difference inverts |
| 1128 (if the time are closer than collisionTimeDifference) | 1128 (if the time are closer than collisionTimeDifference) |
| 1129 If lanes is not None, only the data for the selected lanes will be provided | 1129 If lanes is not None, only the data for the selected lanes will be provided |
| 1130 (format as string x_y where x is link index and y is lane index)''' | 1130 (format as string x_y where x is link index and y is lane index)''' |
| 1131 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, usecols = ['LANE\LINK\NO', 'LANE\INDEX', '$VEHICLE:SIMSEC', 'NO', 'POS'], low_memory = lowMemory) | 1131 data = read_csv(filename, delimiter=';', comment='*', header=0, skiprows = 1, usecols = ['LANE\\LINK\\NO', 'LANE\\INDEX', '$VEHICLE:SIMSEC', 'NO', 'POS'], low_memory = lowMemory) |
| 1132 data = selectPDLanes(data, lanes) | 1132 data = selectPDLanes(data, lanes) |
| 1133 data = data.convert_objects(convert_numeric=True) | 1133 data = data.convert_objects(convert_numeric=True) |
| 1134 | 1134 |
| 1135 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) | 1135 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) |
| 1136 merged = merged[merged['NO_x']>merged['NO_y']] | 1136 merged = merged[merged['NO_x']>merged['NO_y']] |
| 1137 | 1137 |
| 1138 nCollisions = 0 | 1138 nCollisions = 0 |
| 1139 for name, group in merged.groupby(['LANE\LINK\NO', 'LANE\INDEX', 'NO_x', 'NO_y']): | 1139 for name, group in merged.groupby(['LANE\\LINK\\NO', 'LANE\\INDEX', 'NO_x', 'NO_y']): |
| 1140 diff = group['POS_x']-group['POS_y'] | 1140 diff = group['POS_x']-group['POS_y'] |
| 1141 # 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 | 1141 # 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 |
| 1142 if len(diff) >= 2 and npmin(diff) < 0 and npmax(diff) > 0: | 1142 if len(diff) >= 2 and npmin(diff) < 0 and npmax(diff) > 0: |
| 1143 xidx = diff[diff < 0].argmax() | 1143 xidx = diff[diff < 0].argmax() |
| 1144 yidx = diff[diff > 0].argmin() | 1144 yidx = diff[diff > 0].argmin() |
| 1186 for line in inputfile: | 1186 for line in inputfile: |
| 1187 numbers = line.strip().split() | 1187 numbers = line.strip().split() |
| 1188 if obj.getNum() != int(numbers[0]): | 1188 if obj.getNum() != int(numbers[0]): |
| 1189 # check and adapt the length to deal with issues in NGSIM data | 1189 # check and adapt the length to deal with issues in NGSIM data |
| 1190 if (obj.length() != obj.positions.length()): | 1190 if (obj.length() != obj.positions.length()): |
| 1191 print 'length pb with object %s (%d,%d)' % (obj.getNum(),obj.length(),obj.positions.length()) | 1191 print('length pb with object {} ({},{})'.format(obj.getNum(),obj.length(),obj.positions.length())) |
| 1192 obj.last = obj.getFirstInstant()+obj.positions.length()-1 | 1192 obj.last = obj.getFirstInstant()+obj.positions.length()-1 |
| 1193 #obj.velocities = utils.computeVelocities(f.positions) # compare norm to speeds ? | 1193 #obj.velocities = utils.computeVelocities(f.positions) # compare norm to speeds ? |
| 1194 objects.append(obj) | 1194 objects.append(obj) |
| 1195 if (nObjects>0) and (len(objects)>=nObjects): | 1195 if (nObjects>0) and (len(objects)>=nObjects): |
| 1196 break | 1196 break |
| 1204 obj.followingVehicles.append(int(numbers[15])) | 1204 obj.followingVehicles.append(int(numbers[15])) |
| 1205 obj.spaceHeadways.append(float(numbers[16])) | 1205 obj.spaceHeadways.append(float(numbers[16])) |
| 1206 obj.timeHeadways.append(float(numbers[17])) | 1206 obj.timeHeadways.append(float(numbers[17])) |
| 1207 | 1207 |
| 1208 if (obj.size[0] != float(numbers[8])): | 1208 if (obj.size[0] != float(numbers[8])): |
| 1209 print 'changed length obj %d' % (obj.getNum()) | 1209 print('changed length obj {}'.format(obj.getNum())) |
| 1210 if (obj.size[1] != float(numbers[9])): | 1210 if (obj.size[1] != float(numbers[9])): |
| 1211 print 'changed width obj %d' % (obj.getNum()) | 1211 print('changed width obj {}'.format(obj.getNum())) |
| 1212 | 1212 |
| 1213 inputfile.close() | 1213 inputfile.close() |
| 1214 return objects | 1214 return objects |
| 1215 | 1215 |
| 1216 def convertNgsimFile(inputfile, outputfile, append = False, nObjects = -1, sequenceNum = 0): | 1216 def convertNgsimFile(inputfile, outputfile, append = False, nObjects = -1, sequenceNum = 0): |
| 1225 features = loadNgsimFile(inputfile, sequenceNum) | 1225 features = loadNgsimFile(inputfile, sequenceNum) |
| 1226 for f in features: | 1226 for f in features: |
| 1227 nObjectsPerType[f.userType-1] += 1 | 1227 nObjectsPerType[f.userType-1] += 1 |
| 1228 f.write(out) | 1228 f.write(out) |
| 1229 | 1229 |
| 1230 print nObjectsPerType | 1230 print(nObjectsPerType) |
| 1231 | 1231 |
| 1232 out.close() | 1232 out.close() |
| 1233 | 1233 |
| 1234 def loadPinholeCameraModel(filename, tanalystFormat = True): | 1234 def loadPinholeCameraModel(filename, tanalystFormat = True): |
| 1235 '''Loads the data from a file containing the camera parameters | 1235 '''Loads the data from a file containing the camera parameters |
