Mercurial > hg > nsaunier > traffic-intelligence
annotate python/poly-utils.py @ 635:6ae68383071e
corrected issue with tests requiring shapely, adding a separate test file
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 24 Mar 2015 14:17:12 +0100 |
| parents | dc2d0a0d7fe1 |
| children | 15e244d2a1b5 |
| rev | line source |
|---|---|
| 311 | 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 | |
| 598 | 9 from indicators import SeverityIndicator |
| 311 | 10 |
| 11 | |
| 598 | 12 def loadNewInteractions(videoFilename,interactionType,dirname, extension, indicatorsNames, roaduserNum1,roaduserNum2, selectedIndicators=[]): |
| 311 | 13 '''Loads interactions from the POLY traffic event format''' |
| 14 from events import Interaction | |
| 598 | 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 | |
| 311 | 17 file = utils.openCheck(filename) |
| 18 if (not file): | |
| 19 return [] | |
| 598 | 20 #interactions = [] |
| 311 | 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] | |
|
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
|
36 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
|
37 |
| 598 | 38 #interactions.append(inter) |
| 311 | 39 file.close() |
| 598 | 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} | |
| 311 | 83 |
| 598 | 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: | |
|
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
|
121 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
|
122 values[i] = np.sum(tmp[:,5]*tmp[:,6])/np.sum(tmp[:,5])/frameRate |
| 598 | 123 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
|
124 values[i] = np.inf |
| 598 | 125 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
|
126 return None, None |
