Mercurial Hosting > traffic-intelligence
comparison python/poly-utils.py @ 614:5e09583275a4
Merged Nicolas/trafficintelligence into default
author | Mohamed Gomaa <eng.m.gom3a@gmail.com> |
---|---|
date | Fri, 05 Dec 2014 12:13:53 -0500 |
parents | python/poly_utils.py@11f96bd08552 python/poly_utils.py@1d90e9080cb2 |
children | dc2d0a0d7fe1 |
comparison
equal
deleted
inserted
replaced
598:11f96bd08552 | 614:5e09583275a4 |
---|---|
1 #! /usr/bin/env python | |
2 '''Various utilities to load data saved by the POLY new output(s)''' | |
3 import sys | |
4 import utils | |
5 from moving import TimeInterval | |
6 import numpy as np | |
7 | |
8 __metaclass__ = type | |
9 from indicators import SeverityIndicator | |
10 | |
11 | |
12 def loadNewInteractions(videoFilename,interactionType,dirname, extension, indicatorsNames, roaduserNum1,roaduserNum2, selectedIndicators=[]): | |
13 '''Loads interactions from the POLY traffic event format''' | |
14 from events import Interaction | |
15 filename= dirname + videoFilename + extension | |
16 #filename= dirname + interactionType+ '-' + videoFilename + extension # case of min distance todo: change the saving format to be matched with all outputs | |
17 file = utils.openCheck(filename) | |
18 if (not file): | |
19 return [] | |
20 #interactions = [] | |
21 interactionNum = 0 | |
22 data= np.loadtxt(filename) | |
23 indicatorFrameNums= data[:,0] | |
24 inter = Interaction(interactionNum, TimeInterval(indicatorFrameNums[0],indicatorFrameNums[-1]), roaduserNum1, roaduserNum2) | |
25 inter.addVideoFilename(videoFilename) | |
26 inter.addInteractionType(interactionType) | |
27 for key in indicatorsNames.keys(): | |
28 values= {} | |
29 for i,t in enumerate(indicatorFrameNums): | |
30 values[t] = data[i,key] | |
31 inter.addIndicator(SeverityIndicator(indicatorsNames[key], values)) | |
32 if selectedIndicators !=[]: | |
33 values= {} | |
34 for i,t in enumerate(indicatorFrameNums): | |
35 values[t] = [data[i,index] for index in selectedIndicators] | |
36 inter.addIndicator(SeverityIndicator('selectedIndicators', values)) | |
37 | |
38 #interactions.append(inter) | |
39 file.close() | |
40 #return interactions | |
41 return inter | |
42 | |
43 # Plotting results | |
44 | |
45 frameRate = 15. | |
46 | |
47 # To run in directory that contains the directories that contain the results (Miss-xx and Incident-xx) | |
48 #dirname = '/home/nicolas/Research/Data/kentucky-db/' | |
49 | |
50 interactingRoadUsers = {'Miss/0404052336': [(0,3)] # 0,2 and 1 vs 3 | |
51 #, | |
52 #'Incident/0306022035': [(1,3)] | |
53 #, | |
54 #'Miss/0208030956': [(4,5),(5,7)] | |
55 } | |
56 | |
57 | |
58 def getIndicatorName(filename, withUnit = False): | |
59 if withUnit: | |
60 unit = ' (s)' | |
61 else: | |
62 unit = '' | |
63 if 'collision-point' in filename: | |
64 return 'TTC'+unit | |
65 elif 'crossing' in filename: | |
66 return 'pPET'+unit | |
67 elif 'probability' in filename: | |
68 return 'P(UEA)' | |
69 | |
70 def getMethodName(fileprefix): | |
71 if fileprefix == 'constant-velocity': | |
72 return 'Con. Vel.' | |
73 elif fileprefix == 'normal-adaptation': | |
74 return 'Norm. Ad.' | |
75 elif fileprefix == 'point-set': | |
76 return 'Pos. Set' | |
77 elif fileprefix == 'evasive-action': | |
78 return 'Ev. Act.' | |
79 elif fileprefix == 'point-set-evasive-action': | |
80 return 'Pos. Set' | |
81 | |
82 indicator2TimeIdx = {'TTC':2,'pPET':2, 'P(UEA)':3} | |
83 | |
84 def getDataAtInstant(data, i): | |
85 return data[data[:,2] == i] | |
86 | |
87 def getPointsAtInstant(data, i): | |
88 return getDataAtInstant(i)[3:5] | |
89 | |
90 def getIndicator(data, roadUserNumbers, indicatorName): | |
91 if data.ndim ==1: | |
92 data.shape = (1,data.shape[0]) | |
93 | |
94 # find the order for the roadUserNumbers | |
95 uniqueObj1 = np.unique(data[:,0]) | |
96 uniqueObj2 = np.unique(data[:,1]) | |
97 found = False | |
98 if roadUserNumbers[0] in uniqueObj1 and roadUserNumbers[1] in uniqueObj2: | |
99 objNum1 = roadUserNumbers[0] | |
100 objNum2 = roadUserNumbers[1] | |
101 found = True | |
102 if roadUserNumbers[1] in uniqueObj1 and roadUserNumbers[0] in uniqueObj2: | |
103 objNum1 = roadUserNumbers[1] | |
104 objNum2 = roadUserNumbers[0] | |
105 found = True | |
106 | |
107 # get subset of data for road user numbers | |
108 if found: | |
109 roadUserData = data[np.logical_and(data[:,0] == objNum1, data[:,1] == objNum2),:] | |
110 if roadUserData.size > 0: | |
111 time = np.unique(roadUserData[:,indicator2TimeIdx[indicatorName]]) | |
112 values = {} | |
113 if indicatorName == 'P(UEA)': | |
114 tmp = roadUserData[:,4] | |
115 for k,v in zip(time, tmp): | |
116 values[k]=v | |
117 return SeverityIndicator(indicatorName, values, mostSevereIsMax = False, maxValue = 1.), roadUserData | |
118 else: | |
119 for i in xrange(time[0],time[-1]+1): | |
120 try: | |
121 tmp = getDataAtInstant(roadUserData, i) | |
122 values[i] = np.sum(tmp[:,5]*tmp[:,6])/np.sum(tmp[:,5])/frameRate | |
123 except IOError: | |
124 values[i] = np.inf | |
125 return SeverityIndicator(indicatorName, values, mostSevereIsMax = False), roadUserData | |
126 return None, None |