Mercurial > hg > nsaunier > traffic-intelligence
comparison python/ubc_utils.py @ 111:48e3de4acb65
corrected bugs in file copying functions and added utility to get concatenate feature numbers for a set of objects
| author | Nicolas Saunier <nico@confins.net> |
|---|---|
| date | Fri, 15 Jul 2011 19:51:31 -0400 |
| parents | fae55a4c7a5a |
| children | 41a5853ec495 |
comparison
equal
deleted
inserted
replaced
| 110:fae55a4c7a5a | 111:48e3de4acb65 |
|---|---|
| 83 '''Reads filenameIn, replaces the first line with the result of modifyLines and writes the result in filenameOut''' | 83 '''Reads filenameIn, replaces the first line with the result of modifyLines and writes the result in filenameOut''' |
| 84 #sortByNum(objects) | 84 #sortByNum(objects) |
| 85 fileIn = utils.openCheck(filenameIn, 'r', True) | 85 fileIn = utils.openCheck(filenameIn, 'r', True) |
| 86 fileOut = utils.openCheck(filenameOut, "w", True) | 86 fileOut = utils.openCheck(filenameOut, "w", True) |
| 87 | 87 |
| 88 lines = getLines(fileIn) | 88 lines = utils.getLines(fileIn) |
| 89 trajNum = 0 | 89 trajNum = 0 |
| 90 while (lines != []): | 90 while (lines != []): |
| 91 modifiedLines = modifyLines(trajNum, lines) | 91 modifiedLines = modifyLines(trajNum, lines) |
| 92 if modifiedLines: | 92 if modifiedLines: |
| 93 for l in modifiedLines: | 93 for l in modifiedLines: |
| 94 fileOut.write(l+"\n") | 94 fileOut.write(l+"\n") |
| 95 fileOut.write(utils.delimiterChar+"\n") | 95 fileOut.write(utils.delimiterChar+"\n") |
| 96 lines = getLines(fileIn) | 96 lines = utils.getLines(fileIn) |
| 97 trajNum += 1 | 97 trajNum += 1 |
| 98 | 98 |
| 99 fileIn.close() | 99 fileIn.close() |
| 100 fileOut.close() | 100 fileOut.close() |
| 101 | 101 |
| 103 '''Reads filenameIn, keeps the trajectories for which the function keepTrajectory(trajNum, lines) is True | 103 '''Reads filenameIn, keeps the trajectories for which the function keepTrajectory(trajNum, lines) is True |
| 104 and writes the result in filenameOut''' | 104 and writes the result in filenameOut''' |
| 105 fileIn = utils.openCheck(filenameIn, 'r', True) | 105 fileIn = utils.openCheck(filenameIn, 'r', True) |
| 106 fileOut = utils.openCheck(filenameOut, "w", True) | 106 fileOut = utils.openCheck(filenameOut, "w", True) |
| 107 | 107 |
| 108 lines = getLines(fileIn) | 108 lines = utils.getLines(fileIn) |
| 109 trajNum = 0 | 109 trajNum = 0 |
| 110 while (lines != []): | 110 while (lines != []): |
| 111 if keepTrajectory(trajNum, lines): | 111 if keepTrajectory(trajNum, lines): |
| 112 for l in modifiedLines: | 112 for l in lines: |
| 113 fileOut.write(l+"\n") | 113 fileOut.write(l+"\n") |
| 114 fileOut.write(utils.delimiterChar+"\n") | 114 fileOut.write(utils.delimiterChar+"\n") |
| 115 lines = getLines(fileIn) | 115 lines = utils.getLines(fileIn) |
| 116 trajNum += 1 | 116 trajNum += 1 |
| 117 | 117 |
| 118 fileIn.close() | 118 fileIn.close() |
| 119 fileOut.close() | 119 fileOut.close() |
| 120 | 120 |
| 161 lines = utils.getLines(file) | 161 lines = utils.getLines(file) |
| 162 | 162 |
| 163 file.close() | 163 file.close() |
| 164 return objects | 164 return objects |
| 165 | 165 |
| 166 def getFeatureNumbers(objects): | |
| 167 featureNumbers=[] | |
| 168 for o in objects: | |
| 169 featureNumbers += o.featureNumbers | |
| 170 return featureNumbers | |
| 171 | |
| 166 def loadInteractions(filename, nInteractions = -1): | 172 def loadInteractions(filename, nInteractions = -1): |
| 167 'Loads interactions from the old UBC traffic event format' | 173 'Loads interactions from the old UBC traffic event format' |
| 168 from event import Interaction | 174 from event import Interaction |
| 169 from moving import SeverityIndicator | 175 from moving import SeverityIndicator |
| 170 file = utils.openCheck(filename) | 176 file = utils.openCheck(filename) |
