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