Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/output/instantOutToEdgeCounts.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 instantOutToEdgeCounts.py
16
# @author Jakob Erdmann
17
# @date 2025-09-06
18
19
"""
20
Convert instantInductionLoop output to edge counts
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.miscutils import Statistics, parseTime # noqa
31
from sumolib.options import ArgumentParser # noqa
32
from sumolib.net import lane2edge # noqa
33
34
35
def get_options(args=None):
36
ap = ArgumentParser()
37
ap.add_argument("-i", "--detector-data", category="input", dest="detdata", required=True, type=ap.file,
38
help="the detector data to read")
39
ap.add_argument("-d", "--detector-file", category="input", dest="detfile", type=ap.additional_file,
40
help="the detector definitions to read")
41
ap.add_argument("-o", "--output-file", category="output", dest="outfile", type=ap.additional_file,
42
help="define the output filename")
43
ap.add_argument("-b", "--begin", type=ap.time,
44
help="begin time")
45
ap.add_argument("-e", "--end", type=ap.time,
46
help="end time")
47
ap.add_argument("-p", "--period", metavar="FLOAT",
48
help="The interval duration for grouping counts")
49
ap.add_argument("-t", "--event-type", dest="eType", default="leave",
50
help="The event type to read (default 'leavel')")
51
options = ap.parse_args(args=args)
52
return options
53
54
55
def parseDetectors(options):
56
det2edge = dict()
57
for det in sumolib.xml.parse(options.detfile, "instantInductionLoop"):
58
det2edge[det.id] = lane2edge(det.lane)
59
return det2edge
60
61
62
def parseTimes(options):
63
times = Statistics("times")
64
for event in sumolib.xml.parse_fast(options.detdata, 'instantOut', ['id', 'time', 'state']):
65
if event.state == options.eType:
66
times.add(parseTime(event.time), event.id)
67
return times
68
69
70
def writeEdges(outf, edgeCounts):
71
for edge in sorted(edgeCounts.keys()):
72
outf.write(' ' * 8 + '<edge id="%s" count="%s"/>\n' % (edge, edgeCounts[edge]))
73
edgeCounts.clear()
74
outf.write(' ' * 4 + '</interval>\n')
75
76
77
def main(options):
78
times = parseTimes(options)
79
det2edge = parseDetectors(options)
80
81
begin = times.min if options.begin is None else options.begin
82
absEnd = times.max if options.end is None else options.end
83
period = absEnd - begin if options.period is None else options.period
84
85
end = -1
86
87
with sumolib.openz(options.outfile, 'w') as outf:
88
sumolib.writeXMLHeader(outf, "$Id$", "edgeData", options=options, rootAttrs=None)
89
90
edgeCounts = defaultdict(lambda: 0)
91
92
for event in sumolib.xml.parse_fast(options.detdata, 'instantOut', ['id', 'time', 'state']):
93
if event.state == options.eType:
94
t = parseTime(event.time)
95
if t > end:
96
if end > 0:
97
writeEdges(outf, edgeCounts)
98
begin = end
99
if t > absEnd:
100
break
101
end = begin + period
102
outf.write(' ' * 4 + '<interval id="%s" begin="%s" end="%s">\n' % (
103
options.detdata, begin, begin + period))
104
edgeCounts[det2edge[event.id]] += 1
105
writeEdges(outf, edgeCounts)
106
outf.write('</edgeData>\n')
107
108
109
if __name__ == "__main__":
110
options = get_options()
111
main(options)
112
113