comparison python/storage.py @ 0:aed8eb63cdde

initial commit with non-functional python code for NGSIM
author Nicolas Saunier <nico@confins.net>
date Sun, 18 Oct 2009 22:29:24 -0400
parents
children ffddccfab7f9
comparison
equal deleted inserted replaced
-1:000000000000 0:aed8eb63cdde
1 #! /usr/bin/env python
2 '''Various utilities to save and load data'''
3
4 import utils;
5
6 __metaclass__ = type
7
8
9
10
11
12 def loadNgsimFile(filename, nObjects = -1, sequenceNum = -1):
13 '''Reads data from the trajectory data provided by NGSIM project
14 and returns the list of Feature objects'''
15 features = []
16
17 input = utils.open(filename)
18 if not input:
19 import sys
20 sys.exit()
21
22 def createFeature(numbers):
23 firstFrameNum = int(numbers[1])
24 lastFrameNum = firstFrameNum+int(numbers[2])-1
25 f = Feature(sequenceNum, firstFrameNum, lastFrameNum, int(numbers[0]))
26 f.sizeLength = float(numbers[8])
27 f.sizeWidth = float(numbers[9])
28 f.userType = int(numbers[10])
29 f.positions = [[float(numbers[6])],[float(numbers[7])]]
30 f.speeds = [float(numbers[11])]
31 return f
32
33 numbers = input.readline().strip().split()
34 if (len(numbers) > 0):
35 f = createFeature(numbers)
36
37 for line in input:
38 numbers = line.strip().split()
39 if f.num != int(numbers[0]):
40 # check and adapt the length to deal with issues in NGSIM data
41 objLength = f.length()
42 if (objLength != len(f.positions[0])):
43 print 'length pb with object %s (%d,%d)' % (f.num,objLength,len(f.positions[0]))
44 f.lastFrameNum = f.getFirstFrameNum()+len(f.positions[0])-1
45 f.velocities = utils.computeVelocities(f.positions) # compare norm to speeds ?
46 features.append(f)
47 if (nObjects>0) and (len(features)>=nObjects):
48 break
49 f = createFeature(numbers)
50 else:
51 f.positions[0] += [float(numbers[6])]
52 f.positions[1] += [float(numbers[7])]
53 f.speeds += [float(numbers[11])]
54 # if (f.sizeWidth != float(numbers[9])):
55 # print 'changed width obj %d' % (f.num)
56 # if (f.sizeLength != float(numbers[8])):
57 # print 'changed length obj %d' % (f.num)
58
59 input.close()
60 return features
61
62 def convertNgsimFile(inFile, outFile, append = False, nObjects = -1, sequenceNum = 0):
63 '''Reads data from the trajectory data provided by NGSIM project
64 and converts to our current format.'''
65 if append:
66 out = open(outFile,'a')
67 else:
68 out = open(outFile,'w')
69 nObjectsPerType = [0,0,0]
70
71 features = loadNgsimFile(inFile, sequenceNum)
72 for f in features:
73 nObjectsPerType[f.userType-1] += 1
74 f.write(out)
75
76 print nObjectsPerType
77
78 out.close()
79
80 # other functions
81 def getLines(f):
82 '''Gets a complete entry (all the lines) in between delimiterChar.'''
83 dataStrings = []
84 s = utils.myreadline(f)
85 while (len(s) > 0) and (not s.startswith(delimiterChar)):
86 dataStrings += [s.strip()]
87 s = utils.myreadline(f)
88 return dataStrings
89
90
91
92
93 if __name__ == "__main__":
94 import doctest
95 import unittest
96 suite = doctest.DocFileSuite('tests/ubc_utils.txt')
97 unittest.TextTestRunner().run(suite)
98 #doctest.testmod()
99 #doctest.testfile("example.txt")