本文整理汇总了Python中sumolib.output.parse函数的典型用法代码示例。如果您正苦于以下问题:Python parse函数的具体用法?Python parse怎么用?Python parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_diff
def write_diff(options):
attrs = ["depart", "arrival", "timeLoss", "duration", "routeLength"]
# parseTime works just fine for floats
attr_conversions = dict([(a, parseTime) for a in attrs])
vehicles_orig = OrderedDict([(v.id, v) for v in parse(options.orig, 'tripinfo',
attr_conversions=attr_conversions)])
origDurations = Statistics('original durations', histogram=options.useHist, scale=options.histScale)
durations = Statistics('new durations', histogram=options.useHist, scale=options.histScale)
durationDiffs = Statistics('duration differences', histogram=options.useHist, scale=options.histScale)
with open(options.output, 'w') as f:
f.write("<tripDiffs>\n")
for v in parse(options.new, 'tripinfo', attr_conversions=attr_conversions):
if v.id in vehicles_orig:
vOrig = vehicles_orig[v.id]
diffs = [v.getAttribute(a) - vOrig.getAttribute(a) for a in attrs]
durations.add(v.duration, v.id)
origDurations.add(vOrig.duration, v.id)
durationDiffs.add(v.duration - vOrig.duration, v.id)
diffAttrs = ''.join([' %sDiff="%s"' % (a, x) for a, x in zip(attrs, diffs)])
f.write(' <vehicle id="%s"%s/>\n' % (v.id, diffAttrs))
del vehicles_orig[v.id]
else:
f.write(' <vehicle id="%s" comment="new"/>\n' % v.id)
for id in vehicles_orig.keys():
f.write(' <vehicle id="%s" comment="missing"/>\n' % id)
f.write("</tripDiffs>\n")
print(origDurations)
print(durations)
print(durationDiffs)
示例2: main
def main(args):
options = parse_args(args)
net = readNet(options.net)
known_ids = set()
def unique_id(cand, index=0):
cand2 = cand
if index > 0:
cand2 = "%s#%s" % (cand, index)
if cand2 in known_ids:
return unique_id(cand, index + 1)
else:
known_ids.add(cand2)
return cand2
with open(options.outfile, 'w') as outf:
outf.write('<polygons>\n')
for routefile in options.routefiles:
print("parsing %s" % routefile)
if options.standalone:
for route in parse(routefile, 'route'):
# print("found veh", vehicle.id)
generate_poly(net, unique_id(route.id), options.colorgen(),
options.layer, options.geo,
route.edges.split(), options.blur, outf)
else:
for vehicle in parse(routefile, 'vehicle'):
# print("found veh", vehicle.id)
generate_poly(net, unique_id(vehicle.id), options.colorgen(),
options.layer, options.geo,
vehicle.route[0].edges.split(), options.blur, outf)
outf.write('</polygons>\n')
示例3: main
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))
示例4: write_diff
def write_diff(orig, new, out):
diffStats = defaultdict(Statistics)
with open(out, 'w') as f:
f.write("<meandata>\n")
for interval_old, interval_new in zip(parse(orig, 'interval'), parse(new, 'interval')):
f.write(' <interval begin="%s" end="%s">\n' %
(interval_old.begin, interval_old.end))
for edge_old, edge_new in zip(interval_old.edge, interval_new.edge):
assert(edge_old.id == edge_new.id)
f.write(' <edge id="%s"' % edge_old.id)
for attr in edge_old._fields:
if attr == 'id':
continue
try:
delta = float(getattr(edge_new, attr)) - \
float(getattr(edge_old, attr))
diffStats[attr].add(delta, edge_old.id)
f.write(' %s="%s"' % (attr, delta))
except:
pass
f.write("/>\n")
f.write("</interval>\n")
f.write("</meandata>\n")
for attr, stats in diffStats.items():
stats.label = attr
print(stats)
示例5: write_persondiff
def write_persondiff(options):
attrs = ["depart", "arrival", "timeLoss", "duration", "routeLength", "waitingTime"]
attr_conversions = dict([(a, parseTime) for a in attrs])
persons_orig = OrderedDict([(p.id, p) for p in parse(options.orig, 'personinfo',
attr_conversions=attr_conversions)])
origDurations = Statistics('original durations')
durations = Statistics('new durations')
durationDiffs = Statistics('duration differences')
statAttrs = ["duration", "walkTimeLoss", "rideWait", "walks", "accesses", "rides", "stops"]
with open(options.output, 'w') as f:
f.write("<tripDiffs>\n")
for p in parse(options.new, 'personinfo', attr_conversions=attr_conversions):
if p.id in persons_orig:
pOrig = persons_orig[p.id]
stats = plan_stats(p)
statsOrig = plan_stats(pOrig)
diffs = [a - b for a, b in zip(stats, statsOrig)]
durations.add(stats[0], p.id)
origDurations.add(statsOrig[0], p.id)
durationDiffs.add(stats[0] - statsOrig[0], p.id)
diffAttrs = ''.join([' %sDiff="%s"' % (a, x) for a, x in zip(statAttrs, diffs)])
f.write(' <personinfo id="%s"%s/>\n' % (p.id, diffAttrs))
del persons_orig[p.id]
else:
f.write(' <personinfo id="%s" comment="new"/>\n' % p.id)
for id in persons_orig.keys():
f.write(' <personinfo id="%s" comment="missing"/>\n' % id)
f.write("</tripDiffs>\n")
print(origDurations)
print(durations)
print(durationDiffs)
示例6: main
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))
示例7: write_diff
def write_diff(orig, new, out, earliest_out=None):
attr_conversions = {"depart": parseTime, "arrival": parseTime}
earliest_diffs = defaultdict(lambda: (uMax, None)) # diff -> (time, veh)
vehicles_orig = dict([(v.id, v) for v in parse(orig, 'vehicle',
attr_conversions=attr_conversions)])
origDurations = Statistics('original durations')
durations = Statistics('new durations')
durationDiffs = Statistics('duration differences')
with open(out, 'w') as f:
f.write("<routeDiff>\n")
for v in parse(new, 'vehicle', attr_conversions=attr_conversions):
if v.id in vehicles_orig:
vOrig = vehicles_orig[v.id]
departDiff = v.depart - vOrig.depart
arrivalDiff = v.arrival - vOrig.arrival
if v.route[0].exitTimes is None:
sys.exit("Error: Need route input with 'exitTimes'\n")
exitTimes = map(parseTime, v.route[0].exitTimes.split())
origExitTimes = map(parseTime, vOrig.route[0].exitTimes.split())
exitTimesDiff = [
e - eOrig for e, eOrig in zip(exitTimes, origExitTimes)]
durations.add(v.arrival - v.depart, v.id)
origDurations.add(vOrig.arrival - vOrig.depart, v.id)
durationDiffs.add(arrivalDiff - departDiff, v.id)
update_earliest(
earliest_diffs, departDiff, vOrig.depart, v.id + ' (depart)')
for diff, timestamp in zip(exitTimesDiff, origExitTimes):
update_earliest(earliest_diffs, diff, timestamp, v.id)
f.write(''' <vehicle id="%s" departDiff="%s" arrivalDiff="%s" exitTimesDiff="%s"/>\n''' % (
v.id, departDiff, arrivalDiff, ' '.join(map(str, exitTimesDiff))))
del vehicles_orig[v.id]
else:
f.write(' <vehicle id="%s" comment="new"/>\n' % v.id)
for id in vehicles_orig.keys():
f.write(' <vehicle id="%s" comment="missing"/>\n' % id)
f.write("</routeDiff>\n")
if earliest_out is not None:
with open(earliest_out, 'w') as f:
for diff in reversed(sorted(earliest_diffs.keys())):
f.write("%s, %s\n" % (diff, earliest_diffs[diff]))
print(origDurations)
print(durations)
print(durationDiffs)
示例8: main
def main(tripinfos, lengthThreshold=0.1):
lengthThreshold = float(lengthThreshold)
stats = Statistics('walkfactor')
statsZeroDuration = Statistics('length of zero-duration walks')
statsShort = Statistics('duration of short walks (length <= %s)' % lengthThreshold)
numUnfinished = 0
for personinfo in parse(tripinfos, 'personinfo'):
if personinfo.hasChild('walk'):
for i, walk in enumerate(personinfo.walk):
if walk.arrival[0] == '-':
numUnfinished += 1
continue
walkID = "%s.%s" % (personinfo.id, i)
duration = float(walk.duration)
routeLength = float(walk.routeLength)
if duration > 0:
if routeLength <= lengthThreshold:
statsShort.add(duration, walkID)
else:
avgSpeed = routeLength / duration
walkFactor = avgSpeed / float(walk.maxSpeed)
stats.add(walkFactor, walkID)
else:
statsZeroDuration.add(routeLength, walkID)
print(stats)
if statsZeroDuration.count() > 0:
print(statsZeroDuration)
if statsShort.count() > 0:
print(statsShort)
if numUnfinished > 0:
print("unfinished walks: %s" % numUnfinished)
示例9: main
def main():
options = parse_args()
edges = set()
for routefile in options.routefiles:
for route in parse_fast(routefile, 'route', ['edges']):
edges.update(route.edges.split())
for walk in parse_fast(routefile, 'walk', ['edges']):
edges.update(walk.edges.split())
# warn about potentially missing edges
for trip in parse(routefile, ['trip', 'flow'], heterogeneous=True):
edges.update([trip.attr_from, trip.to])
if trip.via is not None:
edges.update(trip.via.split())
print(
"Warning: Trip %s is not guaranteed to be connected within the extracted edges." % trip.id)
for walk in parse_fast(routefile, 'walk', ['from', 'to']):
edges.update([walk.attr_from, walk.to])
print("Warning: Walk from %s to %s is not guaranteed to be connected within the extracted edges." % (
walk.attr_from, walk.to))
with open(options.outfile, 'w') as outf:
for e in sorted(list(edges)):
outf.write('edge:%s\n' % e)
示例10: write_diff
def write_diff(orig, new, out, earliest_out=None):
earliest_diffs = defaultdict(lambda: (uMax, None)) # diff -> (time, veh)
vehicles_orig = dict([(v.id, v) for v in parse(orig, "vehicle")])
origDurations = Statistics("original durations")
durations = Statistics("new durations")
durationDiffs = Statistics("duration differences")
with open(out, "w") as f:
f.write("<routeDiff>\n")
for v in parse(new, "vehicle"):
if v.id in vehicles_orig:
vOrig = vehicles_orig[v.id]
departDiff = float(v.depart) - float(vOrig.depart)
arrivalDiff = float(v.arrival) - float(vOrig.arrival)
if v.route[0].exitTimes is None:
sys.exit("Error: Need route input with 'exitTimes'\n")
exitTimes = map(float, v.route[0].exitTimes.split())
origExitTimes = map(float, vOrig.route[0].exitTimes.split())
exitTimesDiff = [e - eOrig for e, eOrig in zip(exitTimes, origExitTimes)]
durations.add(float(v.arrival) - float(v.depart), v.id)
origDurations.add(float(vOrig.arrival) - float(vOrig.depart), v.id)
durationDiffs.add(arrivalDiff - departDiff, v.id)
update_earliest(earliest_diffs, departDiff, vOrig.depart, v.id + " (depart)")
for diff, timestamp in zip(exitTimesDiff, origExitTimes):
update_earliest(earliest_diffs, diff, timestamp, v.id)
f.write(
""" <vehicle id="%s" departDiff="%s" arrivalDiff="%s" exitTimesDiff="%s"/>\n"""
% (v.id, departDiff, arrivalDiff, " ".join(map(str, exitTimesDiff)))
)
del vehicles_orig[v.id]
else:
f.write(' <vehicle id="%s" comment="new"/>\n' % v.id)
for id in vehicles_orig.keys():
f.write(' <vehicle id="%s" comment="missing"/>\n' % id)
f.write("</routeDiff>\n")
if earliest_out is not None:
with open(earliest_out, "w") as f:
for diff in reversed(sorted(earliest_diffs.keys())):
f.write("%s, %s\n" % (diff, earliest_diffs[diff]))
print(origDurations)
print(durations)
print(durationDiffs)
示例11: main
def main():
options = parse_args()
net = readNet(options.net)
with open(options.outfile, 'w') as outf:
outf.write('<polygons>\n')
for vehicle in parse(options.routefile, 'vehicle'):
generate_poly(net, vehicle.id, options.colorgen(), options.layer, vehicle.route[0].edges.split(), outf)
outf.write('</polygons>\n')
示例12: main
def main(options):
net = readNet(options.network)
edges = set([e.getID() for e in net.getEdges()])
if options.orig_net is not None:
orig_net = readNet(options.orig_net)
else:
orig_net = None
print("Valid area contains %s edges" % len(edges))
if options.trips:
output_type = 'trips'
writer = write_trip
else:
output_type = 'routes'
writer = write_route
busStopEdges = {}
if options.stops_output:
busStops = codecs.open(options.stops_output, 'w', encoding='utf8')
busStops.write(
'<additional xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/additional_file.xsd">\n')
if options.additional_input:
for busStop in parse(options.additional_input, 'busStop'):
edge = busStop.lane[:-2]
busStopEdges[busStop.id] = edge
if options.stops_output and edge in edges:
busStops.write(busStop.toXML(' '))
if options.stops_output:
busStops.write('</additional>\n')
busStops.close()
def write_to_file(vehicles, f):
f.write('<!-- generated with %s for %s from %s -->\n' %
(os.path.basename(__file__), options.network, options.routeFiles))
f.write(
('<%s xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">\n') % output_type)
num_routes = 0
for _, v in vehicles:
num_routes += 1
writer(f, v)
f.write('</%s>\n' % output_type)
print("Wrote %s %s" % (num_routes, output_type))
if options.big:
# write output unsorted
tmpname = options.output + ".unsorted"
with codecs.open(tmpname, 'w', encoding='utf8') as f:
write_to_file(
cut_routes(edges, orig_net, options, busStopEdges), f)
# sort out of memory
sort_routes.main([tmpname, '--big', '--outfile', options.output])
else:
routes = list(cut_routes(edges, orig_net, options, busStopEdges))
routes.sort(key=lambda v: v[0])
with codecs.open(options.output, 'w', encoding='utf8') as f:
write_to_file(routes, f)
示例13: cut_routes
def cut_routes(areaEdges, orig_net, options):
num_vehicles = 0
num_returned = 0
missingEdgeOccurences = defaultdict(lambda: 0)
multiAffectedRoutes = 0 # routes which enter the sub-scenario multiple times
teleportFactorSum = 0.0
too_short = 0
for routeFile in options.routeFiles:
print "Parsing routes from %s" % routeFile
for vehicle in parse(routeFile, "vehicle"):
num_vehicles += 1
edges = vehicle.route[0].edges.split()
fromIndex = getFirstIndex(areaEdges, edges)
if fromIndex is None:
continue # route does not touch the area
toIndex = len(edges) - 1 - getFirstIndex(areaEdges, reversed(edges))
# check for minimum length
if toIndex - fromIndex + 1 < options.min_length:
too_short += 1
continue
# check for connectivity
if missingEdges(areaEdges, edges[fromIndex:toIndex], missingEdgeOccurences):
multiAffectedRoutes += 1
if options.disconnected_action == "discard":
continue
# compute new departure
if vehicle.route[0].exitTimes is None:
if orig_net is not None:
# extrapolate new departure using default speed
newDepart = float(vehicle.depart) + sum(
[
(orig_net.getEdge(e).getLength() / (orig_net.getEdge(e).getSpeed() * options.speed_factor))
for e in edges[:fromIndex]
]
)
else:
print "Could not reconstruct new departure for vehicle '%s'. Using old departure" % vehicle.id
newDepart = vehicle.depart
else:
exitTimes = vehicle.route[0].exitTimes.split()
departTimes = [vehicle.depart] + exitTimes[:-1]
teleportFactor = len(departTimes) / float(len(edges))
teleportFactorSum += teleportFactor
# assume teleports were spread evenly across the vehicles route
newDepart = int(float(departTimes[int(fromIndex * teleportFactor)]))
yield (newDepart, vehicle.id, vehicle.type, edges[fromIndex : toIndex + 1])
num_returned += 1
if teleportFactorSum > 0:
teleports = " (avg teleportFactor %s)" % (1 - teleportFactorSum / num_returned)
else:
teleports = ""
print "Parsed %s vehicles and kept %s routes%s" % (num_vehicles, num_returned, teleports)
if too_short > 0:
print "Discared %s routes because they have less than %s edges" % (too_short, options.min_length)
print "Number of disconnected routes: %s. Most frequent missing edges:" % multiAffectedRoutes
printTop(missingEdgeOccurences)
示例14: main
def main():
random.seed(42)
options = parse_args()
net = readNet(options.net)
with open(options.outfile, "w") as outf:
outf.write("<polygons>\n")
for taz in parse(options.routefile, "taz"):
generate_poly(net, taz.id, options.colorgen(), options.layer, taz.edges.split(), outf)
outf.write("</polygons>\n")
示例15: write_diff
def write_diff(options):
diffStats = defaultdict(Statistics)
with open(options.out, 'w') as f:
f.write("<meandata>\n")
for interval_old, interval_new in zip(
parse(options.orig, 'interval', heterogeneous=True),
parse(options.new, 'interval', heterogeneous=True)):
f.write(' <interval begin="%s" end="%s">\n' %
(interval_old.begin, interval_old.end))
interval_new_edges = dict([(e.id, e) for e in interval_new.edge])
for edge_old in interval_old.edge:
edge_new = interval_new_edges.get(edge_old.id, None)
if edge_new is None:
continue
assert(edge_old.id == edge_new.id)
f.write(' <edge id="%s"' % edge_old.id)
for attr in edge_old._fields:
if attr == 'id':
continue
try:
val_new = float(getattr(edge_new, attr))
val_old = float(getattr(edge_old, attr))
delta = val_new - val_old
if options.relative:
if val_old != 0:
delta /= abs(val_old)
else:
delta = options.undefined
elif options.geh:
delta = geh(val_new, val_old)
diffStats[attr].add(delta, edge_old.id)
f.write(' %s="%s"' % (attr, delta))
except Exception:
pass
f.write("/>\n")
f.write("</interval>\n")
f.write("</meandata>\n")
for attr, stats in diffStats.items():
stats.label = attr
print(stats)