Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/output/netdumpdiff.py
169673 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 netdumpdiff.py
15
# @author Daniel Krajzewicz
16
# @author Michael Behrisch
17
# @date 2007-10-25
18
19
"""
20
21
This script reads two network dumps
22
and subtracts the values of the second
23
from the first writing the results
24
into the output file
25
"""
26
from __future__ import absolute_import
27
from __future__ import print_function
28
from optparse import OptionParser
29
from xml.sax import make_parser, handler
30
from datetime import datetime
31
32
33
class WeightsReader(handler.ContentHandler):
34
35
"""Reads the dump file"""
36
37
def __init__(self):
38
self._id = ''
39
self._edgeValues = {}
40
self._intervalBegins = []
41
self._intervalEnds = []
42
43
def startElement(self, name, attrs):
44
if name == 'interval':
45
self._beginTime = int(attrs['begin'])
46
self._intervalBegins.append(self._beginTime)
47
self._intervalEnds.append(int(attrs['end']))
48
self._edgeValues[self._beginTime] = {}
49
if name == 'edge':
50
self._id = attrs['id']
51
self._edgeValues[self._beginTime][self._id] = {}
52
for attr in attrs.getQNames():
53
if attr != "id":
54
self._edgeValues[self._beginTime][
55
self._id][attr] = float(attrs[attr])
56
57
def sub(self, weights, exclude):
58
for t in self._edgeValues:
59
for e in self._edgeValues[t]:
60
for a in self._edgeValues[t][e]:
61
if a not in exclude:
62
self._edgeValues[t][e][a] = self._edgeValues[
63
t][e][a] - weights._edgeValues[t][e][a]
64
65
def write(self, options):
66
fd = open(options.output, "w")
67
fd.write("<?xml version=\"1.0\"?>\n\n")
68
fd.write("<!-- generated on %s by netdumpdiv.py \n" % datetime.now())
69
fd.write(" -1 %s\n" % options.dump1)
70
fd.write(" -2 %s\n" % options.dump2)
71
fd.write(" -o %s\n" % options.output)
72
fd.write("-->\n\n")
73
fd.write("<netstats>\n")
74
for i in range(0, len(self._intervalBegins)):
75
fd.write(" <interval begin=\"%s\" end=\"%s\">\n" %
76
(self._intervalBegins[i], self._intervalEnds[i]))
77
t = self._intervalBegins[i]
78
for e in self._edgeValues[t]:
79
fd.write(" <edge id=\"%s\"" % e)
80
for a in self._edgeValues[t][e]:
81
fd.write(" %s=\"%s\"" % (a, self._edgeValues[t][e][a]))
82
fd.write("/>\n")
83
fd.write(" </interval>\n")
84
fd.write("</netstats>\n")
85
86
87
# initialise
88
optParser = OptionParser()
89
optParser.add_option("-v", "--verbose", action="store_true", dest="verbose",
90
default=False, help="tell me what you are doing")
91
# i/o
92
optParser.add_option("-1", "--dump1", dest="dump1",
93
help="First dump (mandatory)", metavar="FILE")
94
optParser.add_option("-2", "--dump2", dest="dump2",
95
help="Second dump (mandatory)", metavar="FILE")
96
optParser.add_option("-o", "--output", dest="output",
97
help="Name for the output", metavar="FILE")
98
optParser.add_option("-e", "--exclude", dest="exclude",
99
help="Exclude these values from being changed (stay as in 1)", metavar="FILE")
100
# parse options
101
(options, args) = optParser.parse_args()
102
103
104
parser = make_parser()
105
# read dump1
106
if options.verbose:
107
print("Reading dump1...")
108
weights1 = WeightsReader()
109
parser.setContentHandler(weights1)
110
parser.parse(options.dump1)
111
# read dump2
112
if options.verbose:
113
print("Reading dump2...")
114
weights2 = WeightsReader()
115
parser.setContentHandler(weights2)
116
parser.parse(options.dump2)
117
# process
118
if options.verbose:
119
print("Computing diff...")
120
exclude = []
121
if options.exclude:
122
exclude = options.exclude.split(",")
123
weights1.sub(weights2, exclude)
124
# save
125
if options.verbose:
126
print("Writing...")
127
weights1.write(options)
128
129