Mercurial > hg > nsaunier > traffic-intelligence
comparison python/poly-utils.py @ 615:0954aaf28231
Merge
| author | MohamedGomaa |
|---|---|
| date | Wed, 10 Dec 2014 14:12:06 -0500 |
| parents | 5e09583275a4 |
| children | dc2d0a0d7fe1 |
comparison
equal
deleted
inserted
replaced
| 613:306db0f3c7a2 | 615:0954aaf28231 |
|---|---|
| 4 import utils | 4 import utils |
| 5 from moving import TimeInterval | 5 from moving import TimeInterval |
| 6 import numpy as np | 6 import numpy as np |
| 7 | 7 |
| 8 __metaclass__ = type | 8 __metaclass__ = type |
| 9 from indicators import SeverityIndicator | |
| 9 | 10 |
| 10 # inputs variables | |
| 11 #dirname= 'G:/0-phdstart/Code/trial/indicatorsNew/' | |
| 12 #extension= '-indicatorsNew.csv' | |
| 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'} | |
| 14 ''' min Distance case''' | |
| 15 dirname= 'G:/0-phdstart/Code/trial/minDistanceIndicator/' | |
| 16 extension= '-minDistanceInd.csv' | |
| 17 indicatorsNames= {1:'minDistance'} | |
| 18 | 11 |
| 19 def loadNewInteractions(videoFilename,interactionType, roaduserNum1,roaduserNum2, selectedIndicators=[]): | 12 def loadNewInteractions(videoFilename,interactionType,dirname, extension, indicatorsNames, roaduserNum1,roaduserNum2, selectedIndicators=[]): |
| 20 '''Loads interactions from the POLY traffic event format''' | 13 '''Loads interactions from the POLY traffic event format''' |
| 21 from events import Interaction | 14 from events import Interaction |
| 22 from indicators import SeverityIndicator | 15 filename= dirname + videoFilename + extension |
| 23 #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 |
| 24 filename= dirname + interactionType+ '-' + videoFilename + extension # case of min distance todo: change the saving format to be matched with all outputs | |
| 25 file = utils.openCheck(filename) | 17 file = utils.openCheck(filename) |
| 26 if (not file): | 18 if (not file): |
| 27 return [] | 19 return [] |
| 28 interactions = [] | 20 #interactions = [] |
| 29 interactionNum = 0 | 21 interactionNum = 0 |
| 30 data= np.loadtxt(filename) | 22 data= np.loadtxt(filename) |
| 31 indicatorFrameNums= data[:,0] | 23 indicatorFrameNums= data[:,0] |
| 32 inter = Interaction(interactionNum, TimeInterval(indicatorFrameNums[0],indicatorFrameNums[-1]), roaduserNum1, roaduserNum2) | 24 inter = Interaction(interactionNum, TimeInterval(indicatorFrameNums[0],indicatorFrameNums[-1]), roaduserNum1, roaduserNum2) |
| 33 inter.addVideoFilename(videoFilename) | 25 inter.addVideoFilename(videoFilename) |
| 41 values= {} | 33 values= {} |
| 42 for i,t in enumerate(indicatorFrameNums): | 34 for i,t in enumerate(indicatorFrameNums): |
| 43 values[t] = [data[i,index] for index in selectedIndicators] | 35 values[t] = [data[i,index] for index in selectedIndicators] |
| 44 inter.addIndicator(SeverityIndicator('selectedIndicators', values)) | 36 inter.addIndicator(SeverityIndicator('selectedIndicators', values)) |
| 45 | 37 |
| 46 interactions.append(inter) | 38 #interactions.append(inter) |
| 47 file.close() | 39 file.close() |
| 48 return interactions | 40 #return interactions |
| 41 return inter | |
| 49 | 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 |
