annotate python/poly-utils.py @ 334:1d90e9080cb2

moved python scripts to the scripts directory
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 14 Jun 2013 10:34:11 -0400
parents python/poly_utils.py@273c200ec32e
children 5e09583275a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
311
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
1 #! /usr/bin/env python
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
2 '''Various utilities to load data saved by the POLY new output(s)'''
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
3 import sys
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
4 import utils
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
5 from moving import TimeInterval
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
6 import numpy as np
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
7
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
8 __metaclass__ = type
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
9
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
10 # inputs variables
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
11 #dirname= 'G:/0-phdstart/Code/trial/indicatorsNew/'
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
12 #extension= '-indicatorsNew.csv'
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
13 #indicatorsNames= {1:'Distance',2:'Cosine',3:'collision Course Angle',4:'Velocity Cosine',5:'Velocity Angle',6:'Speed Differential',7:'Collision Probability',8:'Severity Index',9:'TTC'}
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
14 ''' min Distance case'''
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
15 dirname= 'G:/0-phdstart/Code/trial/minDistanceIndicator/'
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
16 extension= '-minDistanceInd.csv'
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
17 indicatorsNames= {1:'minDistance'}
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
18
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
19 def loadNewInteractions(videoFilename,interactionType, roaduserNum1,roaduserNum2, selectedIndicators=[]):
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
20 '''Loads interactions from the POLY traffic event format'''
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
21 from events import Interaction
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
22 from indicators import SeverityIndicator
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
23 #filename= dirname + videoFilename + extension
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
24 filename= dirname + interactionType+ '-' + videoFilename + extension # case of min distance todo: change the saving format to be matched with all outputs
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
25 file = utils.openCheck(filename)
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
26 if (not file):
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
27 return []
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
28 interactions = []
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
29 interactionNum = 0
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
30 data= np.loadtxt(filename)
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
31 indicatorFrameNums= data[:,0]
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
32 inter = Interaction(interactionNum, TimeInterval(indicatorFrameNums[0],indicatorFrameNums[-1]), roaduserNum1, roaduserNum2)
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
33 inter.addVideoFilename(videoFilename)
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
34 inter.addInteractionType(interactionType)
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
35 for key in indicatorsNames.keys():
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
36 values= {}
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
37 for i,t in enumerate(indicatorFrameNums):
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
38 values[t] = data[i,key]
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
39 inter.addIndicator(SeverityIndicator(indicatorsNames[key], values))
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
40 if selectedIndicators !=[]:
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
41 values= {}
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
42 for i,t in enumerate(indicatorFrameNums):
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
43 values[t] = [data[i,index] for index in selectedIndicators]
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
44 inter.addIndicator(SeverityIndicator('selectedIndicators', values))
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
45
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
46 interactions.append(inter)
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
47 file.close()
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
48 return interactions
273c200ec32e load data saved by the POLY new output
Mohamed Gomaa
parents:
diff changeset
49