sumolib.output.dump
1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 2# Copyright (C) 2008-2026 German Aerospace Center (DLR) and others. 3# This program and the accompanying materials are made available under the 4# terms of the Eclipse Public License 2.0 which is available at 5# https://www.eclipse.org/legal/epl-2.0/ 6# This Source Code may also be made available under the following Secondary 7# Licenses when the conditions for such availability set forth in the Eclipse 8# Public License 2.0 are satisfied: GNU General Public License, version 2 9# or later which is available at 10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html 11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later 12 13# @file dump.py 14# @author Daniel Krajzewicz 15# @author Michael Behrisch 16# @date 2011-06-15 17 18from __future__ import absolute_import 19 20import collections 21from xml.sax import parse, handler 22 23 24class DumpReader(handler.ContentHandler): 25 26 def __init__(self, attrsToCollect, edgesToCollect=None): 27 self._values = collections.defaultdict(list) 28 if isinstance(attrsToCollect, dict): 29 self._attrsToCollect = attrsToCollect 30 else: 31 self._attrsToCollect = {} 32 for a in attrsToCollect: 33 self._attrsToCollect[a] = a 34 if edgesToCollect is None or isinstance(edgesToCollect, dict): 35 self._edgesToCollect = edgesToCollect 36 else: 37 self._edgesToCollect = {} 38 for e in edgesToCollect: 39 self._edgesToCollect[e] = e 40 self._intervalBegins = [] 41 self._beginTime = None 42 43 def startElement(self, name, attrs): 44 if name == 'interval': 45 self._beginTime = float(attrs['begin']) 46 self._intervalBegins.append(self._beginTime) 47 for a in self._attrsToCollect.values(): 48 self._values[a].append(collections.defaultdict(int)) 49 if name == 'edge' or name == 'lane': 50 id = attrs['id'] 51 if self._edgesToCollect is not None: 52 if id in self._edgesToCollect: 53 id = self._edgesToCollect[id] 54 else: 55 return 56 for a in attrs.keys(): 57 if a not in self._attrsToCollect: 58 continue 59 self._values[ 60 self._attrsToCollect[a]][-1][id] += float(attrs[a]) 61 62 def join(self, what, how): 63 for a in what: 64 self._singleJoin(a, how) 65 66 def get(self, what): 67 return self._values[what] 68 69 def getIntervalStarts(self): 70 return self._intervalBegins 71 72 def _singleJoin(self, what, how): 73 ret = {} 74 no = {} 75 for i in self._values[what]: 76 for e in i: 77 if e not in ret: 78 ret[e] = 0 79 no[e] = 0 80 ret[e] = ret[e] + i[e] 81 no[e] = no[e] + 1 82 if how == "sum": 83 return ret 84 elif how == "average": 85 for e in i: 86 ret[e] = ret[e] / float(no[e]) 87 self._values[what] = [ret] 88 89 90def readDump(file, attrsToCollect, edgesToCollect=None): 91 dump = DumpReader(attrsToCollect, edgesToCollect) 92 parse(file, dump) 93 return dump
class
DumpReader(xml.sax.handler.ContentHandler):
25class DumpReader(handler.ContentHandler): 26 27 def __init__(self, attrsToCollect, edgesToCollect=None): 28 self._values = collections.defaultdict(list) 29 if isinstance(attrsToCollect, dict): 30 self._attrsToCollect = attrsToCollect 31 else: 32 self._attrsToCollect = {} 33 for a in attrsToCollect: 34 self._attrsToCollect[a] = a 35 if edgesToCollect is None or isinstance(edgesToCollect, dict): 36 self._edgesToCollect = edgesToCollect 37 else: 38 self._edgesToCollect = {} 39 for e in edgesToCollect: 40 self._edgesToCollect[e] = e 41 self._intervalBegins = [] 42 self._beginTime = None 43 44 def startElement(self, name, attrs): 45 if name == 'interval': 46 self._beginTime = float(attrs['begin']) 47 self._intervalBegins.append(self._beginTime) 48 for a in self._attrsToCollect.values(): 49 self._values[a].append(collections.defaultdict(int)) 50 if name == 'edge' or name == 'lane': 51 id = attrs['id'] 52 if self._edgesToCollect is not None: 53 if id in self._edgesToCollect: 54 id = self._edgesToCollect[id] 55 else: 56 return 57 for a in attrs.keys(): 58 if a not in self._attrsToCollect: 59 continue 60 self._values[ 61 self._attrsToCollect[a]][-1][id] += float(attrs[a]) 62 63 def join(self, what, how): 64 for a in what: 65 self._singleJoin(a, how) 66 67 def get(self, what): 68 return self._values[what] 69 70 def getIntervalStarts(self): 71 return self._intervalBegins 72 73 def _singleJoin(self, what, how): 74 ret = {} 75 no = {} 76 for i in self._values[what]: 77 for e in i: 78 if e not in ret: 79 ret[e] = 0 80 no[e] = 0 81 ret[e] = ret[e] + i[e] 82 no[e] = no[e] + 1 83 if how == "sum": 84 return ret 85 elif how == "average": 86 for e in i: 87 ret[e] = ret[e] / float(no[e]) 88 self._values[what] = [ret]
Interface for receiving logical document content events.
This is the main callback interface in SAX, and the one most important to applications. The order of events in this interface mirrors the order of the information in the document.
DumpReader(attrsToCollect, edgesToCollect=None)
27 def __init__(self, attrsToCollect, edgesToCollect=None): 28 self._values = collections.defaultdict(list) 29 if isinstance(attrsToCollect, dict): 30 self._attrsToCollect = attrsToCollect 31 else: 32 self._attrsToCollect = {} 33 for a in attrsToCollect: 34 self._attrsToCollect[a] = a 35 if edgesToCollect is None or isinstance(edgesToCollect, dict): 36 self._edgesToCollect = edgesToCollect 37 else: 38 self._edgesToCollect = {} 39 for e in edgesToCollect: 40 self._edgesToCollect[e] = e 41 self._intervalBegins = [] 42 self._beginTime = None
def
startElement(self, name, attrs):
44 def startElement(self, name, attrs): 45 if name == 'interval': 46 self._beginTime = float(attrs['begin']) 47 self._intervalBegins.append(self._beginTime) 48 for a in self._attrsToCollect.values(): 49 self._values[a].append(collections.defaultdict(int)) 50 if name == 'edge' or name == 'lane': 51 id = attrs['id'] 52 if self._edgesToCollect is not None: 53 if id in self._edgesToCollect: 54 id = self._edgesToCollect[id] 55 else: 56 return 57 for a in attrs.keys(): 58 if a not in self._attrsToCollect: 59 continue 60 self._values[ 61 self._attrsToCollect[a]][-1][id] += float(attrs[a])
Signals the start of an element in non-namespace mode.
The name parameter contains the raw XML 1.0 name of the element type as a string and the attrs parameter holds an instance of the Attributes class containing the attributes of the element.
def
readDump(file, attrsToCollect, edgesToCollect=None):