def main(): options = get_options() vals = defaultdict(list) stats = Statistics("%s %ss" % (options.element, options.attribute), histogram=options.binwidth > 0, scale=options.binwidth) missingAttr = set() invalidType = set() if options.fast: def elements(): for element in parse_fast(options.datafile, options.element, [options.idAttr, options.attribute]): yield getattr(element, options.idAttr), getattr(element, options.attribute) else: def elements(): for element in parse(options.datafile, options.element, heterogeneous=True): elementID = None if element.hasAttribute(options.idAttr): elementID = element.getAttribute(options.idAttr) stringVal = None if element.hasAttribute(options.attribute): stringVal = element.getAttribute(options.attribute) yield elementID, stringVal for elementID, stringVal in elements(): if stringVal is not None: try: val = sumolib.miscutils.parseTime(stringVal) vals[elementID].append(val) stats.add(val, elementID) except Exception: invalidType.add(stringVal) else: missingAttr.add(elementID) print(stats.toString(options.precision)) if missingAttr: print("%s elements did not provide attribute '%s' Example ids: '%s'" % (len(missingAttr), options.attribute, "', '".join(sorted(missingAttr)[:10]))) if invalidType: print(("%s distinct values of attribute '%s' could not be interpreted " + "as numerical value or time. Example values: '%s'") % (len(invalidType), options.attribute, "', '".join(sorted(invalidType)[:10]))) if options.hist_output is not None: with open(options.hist_output, 'w') as f: for bin, count in stats.histogram(): f.write("%s %s\n" % (bin, count)) if options.full_output is not None: with open(options.full_output, 'w') as f: for id, data in sorted(vals.items()): for x in data: f.write(setPrecision("%.2f %s\n", options.precision) % (x, id)) if options.xml_output is not None: with open(options.xml_output, 'w') as f: sumolib.writeXMLHeader(f, "$Id$", "attributeStats") # noqa f.write(stats.toXML(options.precision)) f.write('</attributeStats>\n')
def main(): options = get_options() net = readNet(options.network) edges = set([e.getID() for e in net.getEdges()]) lengths1 = {} lengths2 = {} lengthDiffStats = Statistics( "route length difference", histogram=True, scale=options.binwidth) for vehicle in parse(options.routeFile1, 'vehicle'): lengths1[vehicle.id] = getRouteLength(net, vehicle) for vehicle in parse(options.routeFile2, 'vehicle'): lengths2[vehicle.id] = getRouteLength(net, vehicle) lengthDiffStats.add( lengths2[vehicle.id] - lengths1[vehicle.id], vehicle.id) print lengthDiffStats if options.hist_output is not None: with open(options.hist_output, 'w') as f: for bin, count in lengthDiffStats.histogram(): f.write("%s %s\n" % (bin, count)) if options.full_output is not None: with open(options.full_output, 'w') as f: differences = sorted( [(lengths2[id] - lengths1[id], id) for id in lengths1.keys()]) for diff, id in differences: f.write("%s %s\n" % (diff, id))
def main(): options = get_options() net = readNet(options.network) edges = set([e.getID() for e in net.getEdges()]) lengths1 = {} lengths2 = {} lengthDiffStats = Statistics("route length difference", histogram=True, scale=options.binwidth) for vehicle in parse(options.routeFile1, 'vehicle'): lengths1[vehicle.id] = getRouteLength(net, vehicle) for vehicle in parse(options.routeFile2, 'vehicle'): lengths2[vehicle.id] = getRouteLength(net, vehicle) lengthDiffStats.add(lengths2[vehicle.id] - lengths1[vehicle.id], vehicle.id) print(lengthDiffStats) if options.hist_output is not None: with open(options.hist_output, 'w') as f: for bin, count in lengthDiffStats.histogram(): f.write("%s %s\n" % (bin, count)) if options.full_output is not None: with open(options.full_output, 'w') as f: differences = sorted([(lengths2[id] - lengths1[id], id) for id in lengths1.keys()]) for diff, id in differences: f.write("%s %s\n" % (diff, id))
def main(): options = get_options() attribute_retriever = None def attribute_retriever(tripinfo): return vals = defaultdict(list) stats = Statistics("%s %ss" % (options.element, options.attribute), histogram=True, scale=options.binwidth) for tripinfo in parse(options.tripinfos, options.element): try: val = float(tripinfo.getAttribute(options.attribute)) except: val = sumolib.miscutils.parseTime(tripinfo.getAttribute(options.attribute)) vals[tripinfo.id].append(val) stats.add(val, tripinfo.id) print(stats) if options.hist_output is not None: with open(options.hist_output, 'w') as f: for bin, count in stats.histogram(): f.write("%s %s\n" % (bin, count)) if options.full_output is not None: with open(options.full_output, 'w') as f: for id, data in vals.items(): for x in data: f.write("%s %s\n" % (x, id))
def main(): options = get_options() attribute_retriever = None def attribute_retriever(tripinfo): return vals = {} stats = Statistics("%s %ss" % (options.element, options.attribute), histogram=True, scale=options.binwidth) for tripinfo in parse(options.tripinfos, options.element): val = float(tripinfo.getAttribute(options.attribute)) vals[tripinfo.id] = val stats.add(val, tripinfo.id) print(stats) if options.hist_output is not None: with open(options.hist_output, 'w') as f: for bin, count in stats.histogram(): f.write("%s %s\n" % (bin, count)) if options.full_output is not None: with open(options.full_output, 'w') as f: data = [(v, k) for k, v in vals.items()] for val, id in sorted(data): f.write("%s %s\n" % (val, id))
def main(): options = get_options() net = None attribute_retriever = None if options.attribute == "length": net = readNet(options.network) attribute_retriever = lambda vehicle: sum([ net.getEdge(e).getLength() for e in vehicle.route[0].edges.split() ]) elif options.attribute == "depart": attribute_retriever = lambda vehicle: float(vehicle.depart) elif options.attribute == "numEdges": attribute_retriever = lambda vehicle: len(vehicle.route[0].edges.split( )) else: sys.exit("Invalid value '%s' for option --attribute" % options.attribute) lengths = {} lengths2 = {} if options.routeFile2 is None: # write statistics on a single route file stats = Statistics("route %ss" % options.attribute, histogram=True, scale=options.binwidth) for vehicle in parse(options.routeFile, 'vehicle'): length = attribute_retriever(vehicle) if options.routeFile2 is None: stats.add(length, vehicle.id) lengths[vehicle.id] = length if options.routeFile2 is not None: # compare route lengths between two files stats = Statistics("route %s difference" % options.attribute, histogram=True, scale=options.binwidth) for vehicle in parse(options.routeFile2, 'vehicle'): lengths2[vehicle.id] = attribute_retriever(vehicle) stats.add(lengths2[vehicle.id] - lengths[vehicle.id], vehicle.id) print(stats) if options.hist_output is not None: with open(options.hist_output, 'w') as f: for bin, count in stats.histogram(): f.write("%s %s\n" % (bin, count)) if options.full_output is not None: with open(options.full_output, 'w') as f: if options.routeFile2 is None: data = [(v, k) for k, v in lengths.items()] else: data = [(lengths2[id] - lengths[id], id) for id in lengths.keys()] for val, id in sorted(data): f.write("%s %s\n" % (val, id))
def main(): options = get_options() net = None attribute_retriever = None if options.attribute == "length": net = readNet(options.network) def attribute_retriever(vehicle): return sum([net.getEdge(e).getLength() for e in vehicle.route[0].edges.split()]) elif options.attribute == "depart": def attribute_retriever(vehicle): return float(vehicle.depart) elif options.attribute == "numEdges": def attribute_retriever(vehicle): return len(vehicle.route[0].edges.split()) else: sys.exit("Invalid value '%s' for option --attribute" % options.attribute) lengths = {} lengths2 = {} if options.routeFile2 is None: # write statistics on a single route file stats = Statistics( "route %ss" % options.attribute, histogram=True, scale=options.binwidth) for vehicle in parse(options.routeFile, 'vehicle'): length = attribute_retriever(vehicle) if options.routeFile2 is None: stats.add(length, vehicle.id) lengths[vehicle.id] = length if options.routeFile2 is not None: # compare route lengths between two files stats = Statistics( "route %s difference" % options.attribute, histogram=True, scale=options.binwidth) for vehicle in parse(options.routeFile2, 'vehicle'): lengths2[vehicle.id] = attribute_retriever(vehicle) stats.add(lengths2[vehicle.id] - lengths[vehicle.id], vehicle.id) print(stats) if options.hist_output is not None: with open(options.hist_output, 'w') as f: for bin, count in stats.histogram(): f.write("%s %s\n" % (bin, count)) if options.full_output is not None: with open(options.full_output, 'w') as f: if options.routeFile2 is None: data = [(v, k) for k, v in lengths.items()] else: data = [(lengths2[id] - lengths[id], id) for id in lengths.keys()] for val, id in sorted(data): f.write("%s %s\n" % (val, id))
def main(): options = get_options() net = readNet(options.network) edges = set([e.getID() for e in net.getEdges()]) lengths = {} lengths2 = {} if options.routeFile2 is None: # write statistics on a single route file stats = Statistics("route lengths", histogram=True, scale=options.binwidth) for vehicle in parse(options.routeFile, 'vehicle'): length = getRouteLength(net, vehicle) if options.routeFile2 is None: stats.add(length, vehicle.id) lengths[vehicle.id] = length if options.routeFile2 is not None: # compare route lengths between two files stats = Statistics("route length difference", histogram=True, scale=options.binwidth) for vehicle in parse(options.routeFile2, 'vehicle'): lengths2[vehicle.id] = getRouteLength(net, vehicle) stats.add(lengths2[vehicle.id] - lengths[vehicle.id], vehicle.id) print(stats) if options.hist_output is not None: with open(options.hist_output, 'w') as f: for bin, count in stats.histogram(): f.write("%s %s\n" % (bin, count)) if options.full_output is not None: with open(options.full_output, 'w') as f: if options.routeFile2 is None: data = [(v, k) for k, v in lengths.items()] else: data = [(lengths2[id] - lengths[id], id) for id in lengths.keys()] for val, id in sorted(data): f.write("%s %s\n" % (val, id))
def main(): options = get_options() net = readNet(options.network) edges = set([e.getID() for e in net.getEdges()]) lengths = {} lengths2 = {} for vehicle in parse(options.routeFile, 'vehicle'): lengths[vehicle.id] = getRouteLength(net, vehicle) if options.routeFile2 is None: # write statistics on a single route file stats = Statistics( "route lengths", histogram=True, scale=options.binwidth) for id, length in lengths.items(): stats.add(length, id) else: # compare route lengths between two files stats = Statistics( "route length difference", histogram=True, scale=options.binwidth) for vehicle in parse(options.routeFile2, 'vehicle'): lengths2[vehicle.id] = getRouteLength(net, vehicle) stats.add(lengths2[vehicle.id] - lengths[vehicle.id], vehicle.id) print(stats) if options.hist_output is not None: with open(options.hist_output, 'w') as f: for bin, count in stats.histogram(): f.write("%s %s\n" % (bin, count)) if options.full_output is not None: with open(options.full_output, 'w') as f: if options.routeFile2 is None: data = [(v, k) for k, v in lengths.items()] else: data = [(lengths2[id] - lengths[id], id) for id in lengths.keys()] for val, id in sorted(data): f.write("%s %s\n" % (val, id))
def main(): options = get_options() net = None attribute_retriever = None if options.attribute == "length": net = sumolib.net.readNet(options.network) def attribute_retriever(vehicle): return sum([ net.getEdge(e).getLength() for e in vehicle.route[0].edges.split() ]) elif options.attribute == "depart": def attribute_retriever(vehicle): return parseTime(vehicle.depart) elif options.attribute == "numEdges": def attribute_retriever(vehicle): return len(vehicle.route[0].edges.split()) elif options.attribute == "duration": def attribute_retriever(vehicle): return parseTime(vehicle.arrival) - parseTime(vehicle.depart) elif options.attribute == "routeLength": def attribute_retriever(vehicle): return float(vehicle.routeLength) elif options.attribute == "speed": def attribute_retriever(vehicle): return float(vehicle.routeLength) / (parseTime(vehicle.arrival) - parseTime(vehicle.depart)) elif options.attribute == "speedKmh": def attribute_retriever(vehicle): return 3.6 * float(vehicle.routeLength) / ( parseTime(vehicle.arrival) - parseTime(vehicle.depart)) else: sys.exit("Invalid value '%s' for option --attribute" % options.attribute) lengths = {} lengths2 = {} if options.routeFile2 is None: # write statistics on a single route file stats = Statistics("route %ss" % options.attribute, histogram=True, scale=options.binwidth) if options.fast: def parse(routes): return sumolib.xml.parse_fast( routes, 'vehicle', ['id', 'depart', 'arrival', 'routeLength']) else: def parse(routes): return sumolib.xml.parse(routes, 'vehicle') for vehicle in parse(options.routeFile): if vehicle.routeLength is None or float( vehicle.routeLength) >= options.minlength: length = attribute_retriever(vehicle) if options.routeFile2 is None: stats.add(length, vehicle.id) lengths[vehicle.id] = length if options.routeFile2 is not None: # compare route lengths between two files stats = Statistics("route %s difference" % options.attribute, histogram=True, scale=options.binwidth) for vehicle in parse(options.routeFile2): if vehicle.routeLength is None or float( vehicle.routeLength) >= options.minlength: lengths2[vehicle.id] = attribute_retriever(vehicle) stats.add(lengths2[vehicle.id] - lengths[vehicle.id], vehicle.id) print(stats) if options.hist_output is not None: with open(options.hist_output, 'w') as f: for bin, count in stats.histogram(): f.write("%s %s\n" % (bin, count)) if options.full_output is not None: with open(options.full_output, 'w') as f: if options.routeFile2 is None: data = [(v, k) for k, v in lengths.items()] else: data = [(lengths2[id] - lengths[id], id) for id in lengths.keys()] for val, id in sorted(data): f.write("%s %s\n" % (val, id))