Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/output/edgeDepartDelay.py
169674 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
4
# Copyright (C) 2012-2025 German Aerospace Center (DLR) and others.
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file edgeDepartDelay.py
16
# @author Jakob Erdmann
17
# @date 2025-01-15
18
19
"""
20
Compute departDelay per edge from tripinfo-output
21
"""
22
23
from __future__ import absolute_import
24
from __future__ import print_function
25
import os
26
import sys
27
from collections import defaultdict
28
sys.path.append(os.path.join(os.environ["SUMO_HOME"], 'tools'))
29
import sumolib # noqa
30
from sumolib.xml import parse_fast # noqa
31
from sumolib.miscutils import Statistics, parseTime # noqa
32
from sumolib.options import ArgumentParser # noqa
33
34
35
def get_options(args=None):
36
argParser = ArgumentParser()
37
argParser.add_argument("-t", "--tripinfo-file", dest="tripinfoFile",
38
help="tripinfo file written by the simulation")
39
argParser.add_argument("-o", "--output", help="the output file")
40
options = argParser.parse_args(args=args)
41
if not options.tripinfoFile:
42
sys.exit("Required argument --tripinfo-file is missing")
43
return options
44
45
46
def main(options):
47
edgeStats = defaultdict(lambda: Statistics())
48
departStats = Statistics("departure")
49
delayStats = Statistics("departDelay")
50
for trip in parse_fast(options.tripinfoFile, 'tripinfo', ['id', 'depart', 'departLane', 'departDelay']):
51
edge = trip.departLane.rsplit('_', 1)[0]
52
delay = parseTime(trip.departDelay)
53
edgeStats[edge].add(delay, trip.id)
54
departStats.add(parseTime(trip.depart), trip.id)
55
delayStats.add(delay, trip.id)
56
57
edgeDelayMax = Statistics("edgeDelay-max")
58
edgeDelayAvg = Statistics("edgeDelay-avg")
59
edgeDelayTot = Statistics("edgeDelay-total")
60
for edge, stats in edgeStats.items():
61
edgeDelayMax.add(stats.max, edge)
62
edgeDelayAvg.add(stats.avg(), edge)
63
edgeDelayTot.add(sum(stats.values), edge)
64
65
print(departStats)
66
print(delayStats)
67
print(edgeDelayMax)
68
print(edgeDelayAvg)
69
print(edgeDelayTot)
70
71
if options.output:
72
with open(options.output, 'w') as outf:
73
sumolib.writeXMLHeader(outf, "$Id$", "data", schemaPath="datamode_file.xsd", options=options) # noqa
74
outf.write(' <interval id="edgeDepartDelay" begin="%s" end="%s">\n' % (departStats.min, departStats.max))
75
for edge, stats in edgeStats.items():
76
outf.write(stats.toXML(tag="edge", label='', extraAttributes={'id': edge}))
77
outf.write(' </interval>\n')
78
outf.write('</data>\n')
79
80
81
if __name__ == "__main__":
82
options = get_options()
83
main(options)
84
85