changeset 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 f5806717cc36
files python/moving.py python/storage.py python/utils.py
diffstat 3 files changed, 170 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/moving.py	Sun Oct 18 22:29:24 2009 -0400
@@ -0,0 +1,8 @@
+#! /usr/bin/env python
+'''Libraries for moving objects, trajectories...'''
+
+import utils;
+
+__metaclass__ = type
+
+#class MovingObject:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/storage.py	Sun Oct 18 22:29:24 2009 -0400
@@ -0,0 +1,99 @@
+#! /usr/bin/env python
+'''Various utilities to save and load data'''
+
+import utils;
+
+__metaclass__ = type
+
+
+
+
+
+def loadNgsimFile(filename, nObjects = -1, sequenceNum = -1):
+    '''Reads data from the trajectory data provided by NGSIM project 
+    and returns the list of Feature objects'''
+    features = []
+
+    input = utils.open(filename)
+    if not input:
+        import sys
+        sys.exit()
+
+    def createFeature(numbers):
+        firstFrameNum = int(numbers[1])
+        lastFrameNum = firstFrameNum+int(numbers[2])-1
+        f = Feature(sequenceNum, firstFrameNum, lastFrameNum, int(numbers[0]))
+        f.sizeLength = float(numbers[8])
+        f.sizeWidth = float(numbers[9])
+        f.userType = int(numbers[10])
+        f.positions = [[float(numbers[6])],[float(numbers[7])]]
+        f.speeds = [float(numbers[11])]
+        return f
+
+    numbers = input.readline().strip().split()
+    if (len(numbers) > 0):
+        f = createFeature(numbers)
+
+    for line in input:
+        numbers = line.strip().split()
+        if f.num != int(numbers[0]):
+            # check and adapt the length to deal with issues in NGSIM data
+            objLength = f.length()
+            if (objLength != len(f.positions[0])):
+                print 'length pb with object %s (%d,%d)' % (f.num,objLength,len(f.positions[0]))
+                f.lastFrameNum = f.getFirstFrameNum()+len(f.positions[0])-1
+            f.velocities = utils.computeVelocities(f.positions) # compare norm to speeds ?
+            features.append(f)
+            if (nObjects>0) and (len(features)>=nObjects):
+                break
+            f = createFeature(numbers)
+        else:
+            f.positions[0] += [float(numbers[6])]
+            f.positions[1] += [float(numbers[7])]
+            f.speeds += [float(numbers[11])]
+#         if (f.sizeWidth != float(numbers[9])): 
+#             print 'changed width obj %d' % (f.num)
+#         if (f.sizeLength != float(numbers[8])):
+#             print 'changed length obj %d' % (f.num)
+    
+    input.close()
+    return features
+
+def convertNgsimFile(inFile, outFile, append = False, nObjects = -1, sequenceNum = 0):
+    '''Reads data from the trajectory data provided by NGSIM project
+    and converts to our current format.'''
+    if append:
+        out = open(outFile,'a')
+    else:
+        out = open(outFile,'w')
+    nObjectsPerType = [0,0,0]
+
+    features = loadNgsimFile(inFile, sequenceNum)
+    for f in features:
+        nObjectsPerType[f.userType-1] += 1
+        f.write(out)
+
+    print nObjectsPerType
+        
+    out.close()
+
+# other functions
+def getLines(f):
+    '''Gets a complete entry (all the lines) in between delimiterChar.'''
+    dataStrings = []
+    s = utils.myreadline(f)
+    while (len(s) > 0) and (not s.startswith(delimiterChar)):
+        dataStrings += [s.strip()]
+        s = utils.myreadline(f)
+    return dataStrings
+
+
+
+
+if __name__ == "__main__":
+    import doctest
+    import unittest
+    suite = doctest.DocFileSuite('tests/ubc_utils.txt')
+    unittest.TextTestRunner().run(suite)
+    #doctest.testmod()
+    #doctest.testfile("example.txt")
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/utils.py	Sun Oct 18 22:29:24 2009 -0400
@@ -0,0 +1,63 @@
+#! /usr/bin/env python
+''' Generic utilities.'''
+
+#from numpy import *
+#from pylab import *
+
+__metaclass__ = type
+
+commentChar = '#';
+
+def openCheck(filename, option = 'r', quit = False):
+    '''Open file filename in read mode by default
+    and checks it is open
+
+    >>> f = openCheck('non_existant_file.txt')
+    File non_existant_file.txt could not be opened.
+    '''
+    try:
+        return open(filename, option)
+    except IOError:
+        print 'File %s could not be opened.' % filename
+        if quit:
+            from sys import exit
+            exit()
+        return None
+
+def readline(f):
+    '''Modified readline function to skip comments.'''
+    s = f.readline()
+    while (len(s) > 0) and s.startswith(commentChar):
+        s = f.readline()
+    return s.strip()
+
+def removeExtension(filename, delimiter = '.'):
+    '''Returns the filename minus the extension (all characters after last .)
+    >>> removeExtension('test-adfasdf.asdfa.txt')
+    'test-adfasdf.asdfa'
+    >>> removeExtension('test-adfasdf')
+    'test-adfasdf'
+    '''
+    i = filename.rfind(delimiter)
+    if i>0:
+        return filename[:i]
+    else:
+        return filename
+
+def listfiles(dirname, extension, remove = False):
+    '''Returns the list of files with the extension in the directory dirname'''
+    from os import listdir
+    tmp = [f for f in listdir(dirname) if f.endswith(extension)]
+    tmp.sort()
+    if remove:
+        return [removeExtension(f, extension) for f in tmp]
+    else:
+        return tmp
+
+
+
+
+if __name__ == "__main__":
+    import doctest
+    doctest.testmod()
+    #doctest.testfile("example.txt")