Mercurial > hg > nsaunier > traffic-intelligence
comparison python/ubc_utils.py @ 57:b8b3768f8d54
added functions to load interactions and indicators
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 29 Oct 2010 01:53:38 -0400 |
| parents | 0a5bdbf0d1b4 |
| children | a8c6d544f015 |
comparison
equal
deleted
inserted
replaced
| 56:61fe73df2d36 | 57:b8b3768f8d54 |
|---|---|
| 14 | 14 |
| 15 fileTypeNames = ['feature', | 15 fileTypeNames = ['feature', |
| 16 'object', | 16 'object', |
| 17 'prototype', | 17 'prototype', |
| 18 'contoursequence'] | 18 'contoursequence'] |
| 19 | |
| 20 severityIndicatorNames = ['Distance', | |
| 21 'Cosine', | |
| 22 'Velocity Cosine', | |
| 23 'Speed Differential', | |
| 24 'Collision Probability', | |
| 25 'Severity Index', | |
| 26 'TTC'] | |
| 27 | |
| 28 # severityIndicator = {'Distance': 0, | |
| 29 # 'Cosine': 1, | |
| 30 # 'Velocity Cosine': 2, | |
| 31 # 'Speed Differential': 3, | |
| 32 # 'Collision Probability': 4, | |
| 33 # 'Severity Index': 5, | |
| 34 # 'TTC': 6} | |
| 35 | |
| 36 mostSevereIsMax = [False, | |
| 37 False, | |
| 38 True, | |
| 39 True, | |
| 40 True, | |
| 41 True, | |
| 42 False] | |
| 43 | |
| 44 ignoredValue = [None, None, None, None, None, None, -1] | |
| 19 | 45 |
| 20 def getFileType(s): | 46 def getFileType(s): |
| 21 'Finds the type in fileTypeNames' | 47 'Finds the type in fileTypeNames' |
| 22 for fileType in fileTypeNames: | 48 for fileType in fileTypeNames: |
| 23 if s.find(fileType)>0: | 49 if s.find(fileType)>0: |
| 102 lines = utils.getLines(file) | 128 lines = utils.getLines(file) |
| 103 | 129 |
| 104 file.close() | 130 file.close() |
| 105 return objects | 131 return objects |
| 106 | 132 |
| 133 def loadInteractions(filename, nInteractions = -1): | |
| 134 'Loads interactions from the old UBC traffic event format' | |
| 135 from event import Interaction | |
| 136 from moving import SeverityIndicator | |
| 137 file = utils.openCheck(filename) | |
| 138 if (not file): | |
| 139 return [] | |
| 140 | |
| 141 interactions = [] | |
| 142 interactionNum = 0 | |
| 143 lines = utils.getLines(file) | |
| 144 while (lines != []) and ((nInteractions<0) or (interactionNum<nInteractions)): | |
| 145 parsedLine = [int(n) for n in lines[0].split(' ')] | |
| 146 inter = Interaction(interactionNum, TimeInterval(parsedLine[1],parsedLine[2]), parsedLine[3], parsedLine[4], parsedLine[5]) | |
| 147 | |
| 148 indicatorFrameNums = [int(n) for n in lines[1].split(' ')] | |
| 149 indicators = [] | |
| 150 for indicatorNum,line in enumerate(lines[2:]): | |
| 151 values = {} | |
| 152 for i,v in enumerate([float(n) for n in line.split(' ')]): | |
| 153 values[indicatorFrameNums[i]] = v | |
| 154 indicators.append(SeverityIndicator(severityIndicatorNames[indicatorNum], values, mostSevereIsMax[indicatorNum], ignoredValue[indicatorNum])) | |
| 155 inter.indicators = indicators | |
| 156 | |
| 157 interactions.append(inter) | |
| 158 interactionNum+=1 | |
| 159 lines = utils.getLines(file) | |
| 160 | |
| 161 file.close() | |
| 162 return interactions | |
| 163 | |
| 107 def loadCollisionPoints(filename, nPoints = -1): | 164 def loadCollisionPoints(filename, nPoints = -1): |
| 108 '''Loads collision points and returns a dict | 165 '''Loads collision points and returns a dict |
| 109 with keys as a pair of the numbers of the two interacting objects''' | 166 with keys as a pair of the numbers of the two interacting objects''' |
| 110 file = utils.openCheck(filename) | 167 file = utils.openCheck(filename) |
| 111 if (not file): | 168 if (not file): |
