/usr/share/sumo/tools/output/generateITetrisIntersectionMetrics.py is in sumo-tools 0.15.0~dfsg-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | #!/usr/bin/env python
"""
@file generateITetrisIntersectionMetrics.py
@author Daniel Krajzewicz
@author Lena Kalleske
@author Michael Behrisch
@date 2007-10-25
@version $Id: generateITetrisIntersectionMetrics.py 11671 2012-01-07 20:14:30Z behrisch $
SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
Copyright (C) 2009-2012 DLR (http://www.dlr.de/) and contributors
All rights reserved
"""
from optparse import OptionParser
import os, sys
from numpy import mean
from xml.sax import parse, handler
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import sumolib.net
def getBasicStats(net, lanesInfo, T):
tlsInfo = {}
for tl in net._tlss:
tlID = tl._id
mQueueLen = []
# mWaitTime = []
nbStops = []
tWaitTime = []
seenLanes = set()
for conn in tl._connections:
lane = conn[0]
if lane in seenLanes:
continue
seenLanes.add(lane)
mQueueLenInfo = sum(lanesInfo[lane.getID()]['mQueueLen'])
mQueueLen.append(mQueueLenInfo)
# mWaitTimeInfo = mean(lanesInfo[lane.getID()]['mWaitTime'])
# mWaitTime.append(mWaitTimeInfo)
nbStopsInfo = sum(lanesInfo[lane.getID()]['nbStops'])
nbStops.append(nbStopsInfo)
tWaitTimeInfo = sum(lanesInfo[lane.getID()]['tWaitTime'])
tWaitTime.append(tWaitTimeInfo)
tlsInfo[tlID] = {}
tlsInfo[tlID]['mQueueLen'] = mean(mQueueLen) / T
tlsInfo[tlID]['mWaitTime'] = mean(tWaitTime) / T
tlsInfo[tlID]['nbStops'] = sum(nbStops)
tlsInfo[tlID]['tWaitTime'] = sum(tWaitTime)
return tlsInfo
def mergeInfos(tlsInfoAll, tlsInfoOne, metric):
for tl in tlsInfoOne.keys():
tlsInfoAll[tl][metric] = tlsInfoOne[tl]
def getStatisticsOutput(tlsInfo, outputfile):
opfile = file(outputfile, 'w')
for tl in tlsInfo.keys():
opfile.write('Traffic Light %s\n' % tl)
opfile.write('=================\n')
opfile.write('mean queue length in front of the junction: %s\n' % tlsInfo[tl]['mQueueLen'])
opfile.write('mean waiting time in front of the junction: %s\n' % tlsInfo[tl]['mWaitTime'])
if 'noise' in tlsInfo[tl]:
opfile.write('mean noise emission: %s\n' % tlsInfo[tl]['noise'])
if 'CO' in tlsInfo[tl]:
opfile.write('mean CO emission: %s\n' % tlsInfo[tl]['CO'])
opfile.write('mean CO2 emission: %s\n' % tlsInfo[tl]['CO2'])
opfile.write('mean HC emission: %s\n' % tlsInfo[tl]['HC'])
opfile.write('mean PMx emission: %s\n' % tlsInfo[tl]['PMx'])
opfile.write('mean NOx emission: %s\n' % tlsInfo[tl]['NOx'])
opfile.write('mean fuel consumption: %s\n' % tlsInfo[tl]['fuel'])
opfile.write('number of stops: %s\n' % tlsInfo[tl]['nbStops'])
opfile.write('total waiting time at junction: %s\n\n' % tlsInfo[tl]['tWaitTime'])
def tlsIDToNodeID(net):
tlsID2NodeID = {}
for tls in net._tlss:
tlsID = tls._id
tlsID2NodeID[tlsID] = []
seenNodes = set()
for conn in tls._connections:
lane = conn[0]
edge = lane._edge
node = edge._to
nodeID = node._id
if nodeID not in seenNodes:
tlsID2NodeID[tlsID].append(nodeID)
seenNodes.add(nodeID)
return tlsID2NodeID
class E2OutputReader(handler.ContentHandler):
def __init__(self):
self._lanes = {}
self._maxT = 0
def startElement(self, name, attrs):
if name == 'interval':
detID = attrs['id']
laneID = detID[6:len(detID)]
if not self._lanes.has_key(laneID):
self._lanes[laneID] = {}
self._lanes[laneID]['mQueueLen'] = []
# self._lanes[laneID]['mWaitTime'] = []
self._lanes[laneID]['nbStops'] = []
self._lanes[laneID]['tWaitTime'] = []
if float(attrs['end']) < 100000000:
self._lanes[laneID]['mQueueLen'].append(float(attrs['jamLengthInMetersSum']))
# self._lanes[laneID]['mWaitTime'].append(float(attrs['meanHaltingDuration']))
self._lanes[laneID]['nbStops'].append(float(attrs['startedHalts']))
self._lanes[laneID]['tWaitTime'].append(float(attrs['haltingDurationSum']))
self._maxT = max(float(attrs['end']), self._maxT)
class HarmonoiseReader(handler.ContentHandler):
def __init__(self, net, tlsID2NodeID):
self._nodeIntervalNoise = {}
self._maxT = 0
self._net = net
self._tlsNoise = {}
self._tlsID2NodeID = tlsID2NodeID
def startElement(self, name, attrs):
if name == 'interval':
self._maxT = max(float(attrs['end']), self._maxT)
if name == 'edge':
edgeID = attrs['id']
noiseStr = attrs['noise']
if len(noiseStr) < 10:
noise = float(noiseStr)
else:
noise = 0
if edgeID[0]==':':
nodeID = edgeID[1:edgeID.find('_')]
if nodeID not in self._nodeIntervalNoise:
self._nodeIntervalNoise[nodeID] = []
self._nodeIntervalNoise[nodeID].append(noise)
else:
fromNodeID = net.getEdge(edgeID)._from._id
if fromNodeID not in self._nodeIntervalNoise:
self._nodeIntervalNoise[fromNodeID] = []
self._nodeIntervalNoise[fromNodeID].append(noise)
toNodeID = net.getEdge(edgeID)._to._id
if toNodeID not in self._nodeIntervalNoise:
self._nodeIntervalNoise[toNodeID] = []
self._nodeIntervalNoise[toNodeID].append(noise)
def endElement(self, name):
if name == 'interval':
self.sumIntervalNoise()
if name == 'netstats':
self.sumNoise()
def sumIntervalNoise(self):
for tls in net._tlss:
sum = 0
tlsID = tls._id
if tlsID not in self._tlsNoise:
self._tlsNoise[tlsID] = []
for nodeID in self._tlsID2NodeID[tlsID]:
for noise in self._nodeIntervalNoise[nodeID]:
sum = sum + pow(10, noise/10)
self._tlsNoise[tlsID].append(10 * log(sum)/log(10))
def sumNoise(self):
for tls in net._tlss:
tlsID = tls._id
self._tlsNoise[tlsID] = sum(self._tlsNoise[tlsID]) / self._maxT
class HBEFAReader(handler.ContentHandler):
def __init__(self, net, tlsID2NodeID):
self._maxT = 0
self._net = net
self._nodeIntervalCO = {}
self._nodeIntervalCO2 = {}
self._nodeIntervalHC = {}
self._nodeIntervalPMx = {}
self._nodeIntervalNOx = {}
self._nodeIntervalfuel = {}
self._tlsCO = {}
self._tlsCO2 = {}
self._tlsHC = {}
self._tlsPMx = {}
self._tlsNOx = {}
self._tlsfuel = {}
self._tlsID2NodeID = tlsID2NodeID
def startElement(self, name, attrs):
if name == 'interval':
self._maxT = max(float(attrs['end']), self._maxT)
if name == 'edge':
edgeID = attrs['id']
CO = float(attrs['CO_perVeh'])
CO2 = float(attrs['CO2_perVeh'])
HC = float(attrs['HC_perVeh'])
PMx = float(attrs['PMx_perVeh'])
NOx = float(attrs['NOx_perVeh'])
fuel = float(attrs['fuel_perVeh'])
if edgeID[0]==':':
nodeIDs = edgeID[1:edgeID.find('_')]
else:
fromNodeID = net.getEdge(edgeID)._from._id
toNodeID = net.getEdge(edgeID)._to._id
nodeIDs = [fromNodeID, toNodeID]
for nodeID in nodeIDs:
if nodeID not in self._nodeIntervalCO:
self._nodeIntervalCO[nodeID] = []
self._nodeIntervalCO2[nodeID] = []
self._nodeIntervalHC[nodeID] = []
self._nodeIntervalPMx[nodeID] = []
self._nodeIntervalNOx[nodeID] = []
self._nodeIntervalfuel[nodeID] = []
self._nodeIntervalCO[nodeID].append(CO)
self._nodeIntervalCO2[nodeID].append(CO2)
self._nodeIntervalHC[nodeID].append(HC)
self._nodeIntervalPMx[nodeID].append(PMx)
self._nodeIntervalNOx[nodeID].append(NOx)
self._nodeIntervalfuel[nodeID].append(fuel)
def endElement(self, name):
if name == 'interval':
self.sumInterval()
if name == 'netstats':
self.sum()
def sumInterval(self):
for tls in net._tlss:
tlsID = tls._id
if tlsID not in self._tlsCO:
self._tlsCO[tlsID] = []
self._tlsCO2[tlsID] = []
self._tlsHC[tlsID] = []
self._tlsPMx[tlsID] = []
self._tlsNOx[tlsID] = []
self._tlsfuel[tlsID] = []
sum = 0
for nodeID in self._tlsID2NodeID[tlsID]:
for v in self._nodeIntervalCO[nodeID]:
sum = sum + v
self._tlsCO[tlsID].append(sum)
sum = 0
for nodeID in self._tlsID2NodeID[tlsID]:
for v in self._nodeIntervalCO2[nodeID]:
sum = sum + v
self._tlsCO2[tlsID].append(sum)
sum = 0
for nodeID in self._tlsID2NodeID[tlsID]:
for v in self._nodeIntervalHC[nodeID]:
sum = sum + v
self._tlsHC[tlsID].append(sum)
sum = 0
for nodeID in self._tlsID2NodeID[tlsID]:
for v in self._nodeIntervalPMx[nodeID]:
sum = sum + v
self._tlsPMx[tlsID].append(sum)
sum = 0
for nodeID in self._tlsID2NodeID[tlsID]:
for v in self._nodeIntervalNOx[nodeID]:
sum = sum + v
self._tlsNOx[tlsID].append(sum)
sum = 0
for nodeID in self._tlsID2NodeID[tlsID]:
for v in self._nodeIntervalfuel[nodeID]:
sum = sum + v
self._tlsfuel[tlsID].append(sum)
def sum(self):
for tls in net._tlss:
tlsID = tls._id
self._tlsCO[tlsID] = sum(self._tlsCO[tlsID]) / self._maxT
self._tlsCO2[tlsID] = sum(self._tlsCO2[tlsID]) / self._maxT
self._tlsHC[tlsID] = sum(self._tlsHC[tlsID]) / self._maxT
self._tlsPMx[tlsID] = sum(self._tlsPMx[tlsID]) / self._maxT
self._tlsNOx[tlsID] = sum(self._tlsNOx[tlsID]) / self._maxT
self._tlsfuel[tlsID] = sum(self._tlsfuel[tlsID]) / self._maxT
# initialise
optParser = OptionParser()
optParser.add_option("-n", "--netfile", dest="netfile",
help="name of the netfile (f.e. 'inputs\\pasubio\\a_costa.net.xml')", metavar="<FILE>", type="string")
optParser.add_option("-p", "--path", dest="path",
help="name of folder to work with (f.e. 'inputs\\a_costa\\')", metavar="<FOLDER>", type="string", default="./")
optParser.add_option("-o", "--harmonoiseFile", dest="harmonoiseFile",
help="name of the harmonoise file", metavar="<FOLDER>", type="string")
optParser.add_option("-e", "--HBEFAFile", dest="hbefaFile",
help="name of the HBEFA file", metavar="<FOLDER>", type="string")
optParser.set_usage('\n-n inputs\\pasubio\\pasubio.net.xml -p inputs\\pasubio\\')
# parse options
(options, args) = optParser.parse_args()
if not options.netfile:
print "Missing arguments"
optParser.print_help()
exit()
netfile = options.netfile
e2OutputFile = os.path.join(options.path, 'e2_output.xml')
net = sumolib.net.readNet(netfile)
e2Output = E2OutputReader()
parse(e2OutputFile, e2Output)
tlsID2NodeID = tlsIDToNodeID(net)
tlsInfo = getBasicStats(net, e2Output._lanes, e2Output._maxT)
if options.harmonoiseFile:
harmonoiseOutput = HarmonoiseReader(net, tlsID2NodeID)
parse(options.harmonoiseFile, harmonoiseOutput)
mergeInfos(tlsInfo, harmonoiseOutput._tlsNoise, 'noise')
if options.hbefaFile:
hbefaOutput = HBEFAReader(net, tlsID2NodeID)
parse(hbefaFile, hbefaOutput)
mergeInfos(tlsInfo, hbefaOutput._tlsCO, 'CO')
mergeInfos(tlsInfo, hbefaOutput._tlsCO2, 'CO2')
mergeInfos(tlsInfo, hbefaOutput._tlsHC, 'HC')
mergeInfos(tlsInfo, hbefaOutput._tlsPMx, 'PMx')
mergeInfos(tlsInfo, hbefaOutput._tlsNOx, 'NOx')
mergeInfos(tlsInfo, hbefaOutput._tlsfuel, 'fuel')
getStatisticsOutput(tlsInfo, os.path.join(options.path, "intersection_metrics_summary.txt"))
print 'The calculation is done!'
|