Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/output/netdumpmean.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file netdumpmean.py
15
# @author Daniel Krajzewicz
16
# @author Michael Behrisch
17
# @date 2007-10-25
18
19
"""
20
This script reads two network dumps,
21
computes the mean values
22
and writes the results into the output file
23
"""
24
from __future__ import absolute_import
25
from __future__ import print_function
26
from optparse import OptionParser
27
from xml.sax import make_parser, handler
28
from datetime import datetime
29
30
31
class WeightsReader(handler.ContentHandler):
32
33
"""Reads the dump file"""
34
35
def __init__(self):
36
self._id = ''
37
self._edgeValues = {}
38
self._intervalBegins = []
39
self._intervalEnds = []
40
41
def startElement(self, name, attrs):
42
if name == 'interval':
43
self._beginTime = int(attrs['begin'])
44
self._intervalBegins.append(self._beginTime)
45
self._intervalEnds.append(int(attrs['end']))
46
self._edgeValues[self._beginTime] = {}
47
if name == 'edge':
48
self._id = attrs['id']
49
self._edgeValues[self._beginTime][self._id] = {}
50
for attr in attrs.getQNames():
51
if attr != "id":
52
self._edgeValues[self._beginTime][
53
self._id][attr] = float(attrs[attr])
54
55
def sub(self, weights, exclude):
56
for t in self._edgeValues:
57
for e in self._edgeValues[t]:
58
for a in self._edgeValues[t][e]:
59
if a not in exclude:
60
self._edgeValues[t][e][a] = (
61
self._edgeValues[t][e][a] + weights._edgeValues[t][e][a]) / 2.
62
63
def write(self, options):
64
fd = open(options.output, "w")
65
fd.write("<?xml version=\"1.0\"?>\n\n")
66
fd.write("<!-- generated on %s by netdumpdiv.py \n" % datetime.now())
67
fd.write(" -1 %s\n" % options.dump1)
68
fd.write(" -2 %s\n" % options.dump2)
69
fd.write(" -o %s\n" % options.output)
70
fd.write("-->\n\n")
71
fd.write("<netstats>\n")
72
for i in range(0, len(self._intervalBegins)):
73
fd.write(" <interval begin=\"%s\" end=\"%s\">\n" %
74
(self._intervalBegins[i], self._intervalEnds[i]))
75
t = self._intervalBegins[i]
76
for e in self._edgeValues[t]:
77
fd.write(" <edge id=\"%s\"" % e)
78
for a in self._edgeValues[t][e]:
79
fd.write(" %s=\"%s\"" % (a, self._edgeValues[t][e][a]))
80
fd.write("/>\n")
81
fd.write(" </interval>\n")
82
fd.write("</netstats>\n")
83
84
85
# initialise
86
optParser = OptionParser()
87
optParser.add_option("-v", "--verbose", action="store_true", dest="verbose",
88
default=False, help="tell me what you are doing")
89
# i/o
90
optParser.add_option("-1", "--dump1", dest="dump1",
91
help="First dump (mandatory)", metavar="FILE")
92
optParser.add_option("-2", "--dump2", dest="dump2",
93
help="Second dump (mandatory)", metavar="FILE")
94
optParser.add_option("-o", "--output", dest="output",
95
help="Name for the output", metavar="FILE")
96
optParser.add_option("-e", "--exclude", dest="exclude",
97
help="Exclude these values from being changed (stay as in 1)", metavar="FILE")
98
# parse options
99
(options, args) = optParser.parse_args()
100
101
102
parser = make_parser()
103
# read dump1
104
if options.verbose:
105
print("Reading dump1...")
106
weights1 = WeightsReader()
107
parser.setContentHandler(weights1)
108
parser.parse(options.dump1)
109
# read dump2
110
if options.verbose:
111
print("Reading dump2...")
112
weights2 = WeightsReader()
113
parser.setContentHandler(weights2)
114
parser.parse(options.dump2)
115
# process
116
if options.verbose:
117
print("Computing mean...")
118
exclude = []
119
if options.exclude:
120
exclude = options.exclude.split(",")
121
weights1.mean(weights2, exclude)
122
# save
123
if options.verbose:
124
print("Writing...")
125
weights1.write(options)
126
127