changeset 57:b8b3768f8d54

added functions to load interactions and indicators
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 29 Oct 2010 01:53:38 -0400
parents 61fe73df2d36
children 40e1508380ed
files python/ubc_utils.py
diffstat 1 files changed, 57 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/python/ubc_utils.py	Wed Oct 27 18:32:27 2010 -0400
+++ b/python/ubc_utils.py	Fri Oct 29 01:53:38 2010 -0400
@@ -17,6 +17,32 @@
                  'prototype',
                  'contoursequence']
 
+severityIndicatorNames = ['Distance',
+                          'Cosine',
+                          'Velocity Cosine',
+                          'Speed Differential',
+                          'Collision Probability',
+                          'Severity Index',
+                          'TTC']
+
+# severityIndicator = {'Distance': 0,
+#                      'Cosine': 1,
+#                      'Velocity Cosine': 2,
+#                      'Speed Differential': 3,
+#                      'Collision Probability': 4,
+#                      'Severity Index': 5,
+#                      'TTC': 6}
+
+mostSevereIsMax = [False, 
+                   False, 
+                   True, 
+                   True, 
+                   True, 
+                   True, 
+                   False]
+
+ignoredValue = [None, None, None, None, None, None, -1]
+
 def getFileType(s):
     'Finds the type in fileTypeNames'
     for fileType in fileTypeNames:
@@ -104,6 +130,37 @@
     file.close()
     return objects
    
+def loadInteractions(filename, nInteractions = -1):
+    'Loads interactions from the old UBC traffic event format'
+    from event import Interaction 
+    from moving import SeverityIndicator
+    file = utils.openCheck(filename)
+    if (not file):
+        return []
+
+    interactions = []
+    interactionNum = 0
+    lines = utils.getLines(file)
+    while (lines != []) and ((nInteractions<0) or (interactionNum<nInteractions)):
+        parsedLine = [int(n) for n in lines[0].split(' ')]
+        inter = Interaction(interactionNum, TimeInterval(parsedLine[1],parsedLine[2]), parsedLine[3], parsedLine[4], parsedLine[5])
+        
+        indicatorFrameNums = [int(n) for n in lines[1].split(' ')]
+        indicators = []
+        for indicatorNum,line in enumerate(lines[2:]):
+            values = {}
+            for i,v in enumerate([float(n) for n in line.split(' ')]):
+                values[indicatorFrameNums[i]] = v
+            indicators.append(SeverityIndicator(severityIndicatorNames[indicatorNum], values, mostSevereIsMax[indicatorNum], ignoredValue[indicatorNum]))
+        inter.indicators = indicators
+
+        interactions.append(inter)
+        interactionNum+=1
+        lines = utils.getLines(file)
+
+    file.close()
+    return interactions
+
 def loadCollisionPoints(filename, nPoints = -1):
     '''Loads collision points and returns a dict
     with keys as a pair of the numbers of the two interacting objects'''