Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/output/tripinfoByType.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 tripinfoByType.py
16
# @author Jakob Erdmann
17
# @date 2021-11-19
18
19
"""
20
Aggregate tripinfo statistics by vehicle type.
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 # 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("-a", "--attribute", default="duration",
40
help="attribute to use for statistics")
41
argParser.add_argument("-o", "--output", help="the output file")
42
argParser.add_argument("-i", "--interval", help="custom aggregation interval (seconds or H:M:S)")
43
argParser.add_argument("--by-arrivals", action="store_true", default=False, dest="byArrivals",
44
help="When using --interval, aggregated by arrival time instead of depart time")
45
options = argParser.parse_args(args=args)
46
if not options.tripinfoFile:
47
sys.exit("Required argument --tripinfo-file is missing")
48
49
if options.interval is not None:
50
options.interval = parseTime(options.interval)
51
return options
52
53
54
def getAggregatedTime(options, elem):
55
if options.interval:
56
val = elem.arrival if options.byArrivals else elem.depart
57
return int(parseTime(val) / options.interval) * options.interval
58
else:
59
return None
60
61
62
def main(options):
63
intervals = defaultdict(dict) # time -> (type -> stats)
64
65
for trip in parse(options.tripinfoFile, 'tripinfo'):
66
typeStats = intervals[getAggregatedTime(options, trip)]
67
if trip.vType not in typeStats:
68
typeStats[trip.vType] = Statistics(trip.vType)
69
typeStats[trip.vType].add(parseTime(getattr(trip, options.attribute)), trip.id)
70
71
for person in parse(options.tripinfoFile, 'personinfo'):
72
for stage in person.getChildList():
73
if stage.hasAttribute(options.attribute):
74
typeStats = intervals[getAggregatedTime(options, stage)]
75
if stage.name not in typeStats:
76
typeStats[stage.name] = Statistics(stage.name)
77
typeStats[stage.name].add(parseTime(getattr(stage, options.attribute)), person.id)
78
79
if options.output:
80
with open(options.output, 'w') as outf:
81
sumolib.writeXMLHeader(outf, "$Id$", "tripinfosByType", options=options) # noqa
82
for time in sorted(intervals.keys()):
83
typeStats = intervals[time]
84
if time is not None:
85
outf.write(' <interval begin="%s" end="%s">\n' % (time, time + options.interval))
86
for vType, stats in sorted(typeStats.items()):
87
q1, median, q3 = stats.quartiles()
88
outf.write(' <typeInfo vType="%s" count="%s" min="%s" minVeh="%s"' %
89
(vType, stats.count(), stats.min, stats.min_label))
90
outf.write(' max="%s" maxVeh="%s" mean="%s" Q1="%s" median="%s" Q3="%s"/>\n' %
91
(stats.max, stats.max_label, stats.avg(), q1, median, q3))
92
if time is not None:
93
outf.write(' </interval>\n')
94
outf.write('</tripinfosByType>\n')
95
else:
96
for time in sorted(intervals.keys()):
97
typeStats = intervals[time]
98
if time is not None:
99
print("Interval: [%s, %s[" % (time, time + options.interval))
100
for vType, stats in sorted(typeStats.items()):
101
print(stats)
102
103
104
if __name__ == "__main__":
105
options = get_options()
106
main(options)
107
108