Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/output/analyze_pedestrian_jam.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 analyze_pedestrian_jam.py
16
# @author Jakob Erdmann
17
# @date 2020-01-06
18
19
from __future__ import absolute_import
20
from __future__ import print_function
21
from __future__ import division
22
import os
23
import sys
24
import re
25
from collections import defaultdict
26
if "SUMO_HOME" in os.environ:
27
sys.path += [os.path.join(os.environ["SUMO_HOME"], "tools")]
28
import sumolib # noqa
29
30
31
def get_options(args=None):
32
op = sumolib.options.ArgumentParser()
33
op.add_argument("logfile", type=op.file, category="input", help="log file")
34
op.add_option("-a", "--aggregation", type=op.time, default=3600,
35
help="define the time aggregation in seconds")
36
op.add_option("-p", "--gnuplot-output", type=op.file, dest="plotfile", category="output",
37
help="define the gnuplot output file")
38
op.add_option("-e", "--edgedata-output", type=op.file, dest="edgedata", category="output",
39
help="define the edgedata output file")
40
options = op.parse_args()
41
42
if options.plotfile is None:
43
options.plotfile = options.logfile + '.plot'
44
45
if options.edgedata is None:
46
options.edgedata = options.logfile + "data.xml"
47
return options
48
49
50
def parse_log(logfile, aggregate=3600):
51
print("Parsing %s" % logfile)
52
reEdge = re.compile("edge '([^']*)'")
53
reTime = re.compile(r"time.(\d*)\.")
54
reHRTime = re.compile(r"time.(\d):(\d\d):(\d\d):(\d*).")
55
# counts per edge
56
jamCounts = defaultdict(lambda: 0)
57
# counts per step
58
jamStepCounts = defaultdict(lambda: 0)
59
with open(logfile) as log:
60
for index, line in enumerate(log):
61
try:
62
if "is jammed on edge" in line:
63
match = reEdge.search(line)
64
edge = match.group(1)
65
timeMatch = reTime.search(line)
66
if timeMatch:
67
time = int(timeMatch.group(1))
68
else:
69
timeMatch = reHRTime.search(line)
70
time = (24 * 3600 * int(timeMatch.group(1))
71
+ 3600 * int(timeMatch.group(2))
72
+ 60 * int(timeMatch.group(3))
73
+ int(timeMatch.group(4)))
74
jamCounts[edge] += 1
75
jamStepCounts[time // aggregate] += 1
76
except Exception:
77
print(sys.exc_info())
78
sys.exit("error when parsing line '%s'" % line)
79
if index % 1000 == 0:
80
sys.stdout.write(".")
81
sys.stdout.flush()
82
print()
83
print("read %s lines" % index)
84
85
return jamCounts, jamStepCounts
86
87
88
def print_counts(countDict, label, num=10):
89
counts = sorted(countDict.items(), key=lambda k: -k[1])
90
print(label, "worst %s edges: " % num, counts[:num])
91
print(label, 'total:', sum(countDict.values()))
92
93
94
def main(options):
95
jamCounts, jamStepCounts = parse_log(options.logfile, aggregate=int(options.aggregation))
96
print_counts(jamCounts, 'waiting')
97
# generate plot
98
if len(jamStepCounts) > 0:
99
min_step = min(jamStepCounts.keys())
100
max_step = max(jamStepCounts.keys())
101
with open(options.plotfile, 'w') as f:
102
if options.aggregation >= 3600:
103
xfactor = options.aggregation / 3600.0
104
xlabel = "%s hours" % xfactor
105
else:
106
xfactor = options.aggregation / 60.0
107
xlabel = "%s minute" % xfactor
108
if xfactor != 1:
109
xlabel += "s"
110
f.write(("# plot '%s' using 1:2 with lines title 'jammed per %s'\n") % (options.plotfile, xlabel))
111
for step in range(min_step, max_step + 1):
112
print(' '.join(
113
map(str, [xfactor * step, jamStepCounts[step]])), file=f)
114
with open(options.edgedata, 'w') as f:
115
print('<meandata>\n <interval begin="0" end="100:00:00:00">', file=f)
116
for item in jamCounts.items():
117
print(' <edge id="%s" jammed="%s"/>' % item, file=f)
118
print(" </interval>\n</meandata>", file=f)
119
120
121
if __name__ == "__main__":
122
main(get_options())
123
124