Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/output/analyze_teleports.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_teleports.py
16
# @author Jakob Erdmann
17
# @author Michael Behrisch
18
# @date 2012-11-20
19
20
from __future__ import absolute_import
21
from __future__ import print_function
22
from __future__ import division
23
import os
24
import sys
25
import re
26
from collections import defaultdict
27
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
28
from sumolib.options import ArgumentParser # noqa
29
30
31
def parse_args():
32
optParser = ArgumentParser()
33
optParser.add_argument("logfile", type=optParser.file, category="input", help="log file")
34
return optParser.parse_args()
35
36
37
def parse_log(logfile, edges=True, aggregate=3600):
38
print("Parsing %s" % logfile)
39
reFrom = re.compile("lane='([^']*)'")
40
reFromMeso = re.compile("edge '([^']*)'")
41
reTime = re.compile(r"time.(\d*)\.")
42
reHRTime = re.compile(r"time.(\d):(\d\d):(\d\d):(\d*).")
43
# counts per lane
44
waitingCounts = defaultdict(lambda: 0)
45
collisionCounts = defaultdict(lambda: 0)
46
# counts per step
47
waitingStepCounts = defaultdict(lambda: 0)
48
collisionStepCounts = defaultdict(lambda: 0)
49
with open(logfile) as log:
50
for index, line in enumerate(log):
51
try:
52
if "Warning: Teleporting vehicle" in line:
53
# figure out whether its micro or meso
54
match = reFrom.search(line)
55
if match is None:
56
edge = reFromMeso.search(line).group(1)
57
else:
58
edge = match.group(1)
59
if edges:
60
edge = edge[:-2]
61
timeMatch = reTime.search(line)
62
if timeMatch:
63
time = int(timeMatch.group(1))
64
else:
65
timeMatch = reHRTime.search(line)
66
time = (24 * 3600 * int(timeMatch.group(1))
67
+ 3600 * int(timeMatch.group(2))
68
+ 60 * int(timeMatch.group(3))
69
+ int(timeMatch.group(4)))
70
if "collision" in line:
71
collisionCounts[edge] += 1
72
collisionStepCounts[time // aggregate] += 1
73
else:
74
waitingCounts[edge] += 1
75
waitingStepCounts[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 (waitingCounts, collisionCounts,
86
waitingStepCounts, collisionStepCounts)
87
88
89
def print_counts(countDict, label, num=10):
90
counts = sorted(countDict.items(), key=lambda k: -k[1])
91
print(label, "worst %s edges: " % num, counts[:num])
92
print(label, 'total:', sum(countDict.values()))
93
94
95
def main(logfile):
96
waitingCounts, collisionCounts, waitingStepCounts, collisionStepCounts = parse_log(logfile)
97
print_counts(waitingCounts, 'waiting')
98
print_counts(collisionCounts, 'collisions')
99
# generate plot
100
if len(waitingStepCounts) + len(collisionStepCounts) > 0:
101
min_step = min(list(waitingStepCounts.keys()) + list(collisionStepCounts.keys()))
102
max_step = max(list(waitingStepCounts.keys()) + list(collisionStepCounts.keys()))
103
plotfile = logfile + '.plot'
104
with open(plotfile, 'w') as f:
105
f.write(("# plot '%s' using 1:2 with lines title 'waiting', '%s' using 1:3 with lines title " +
106
"'collisions'\n") % (plotfile, plotfile))
107
for step in range(min_step, max_step + 1):
108
print(' '.join(
109
map(str, [step, waitingStepCounts[step], collisionStepCounts[step]])), file=f)
110
with open(logfile + "data.xml", 'w') as f:
111
print('<meandata>\n <interval begin="0" end="100:00:00:00">', file=f)
112
for item in waitingCounts.items():
113
print(' <edge id="%s" waiting_teleport="%s"/>' % item, file=f)
114
print(" </interval>\n</meandata>", file=f)
115
116
117
if __name__ == "__main__":
118
main(parse_args().logfile)
119
120