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')