Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
restrictions[rID] = int(count)
routeUsage = defaultdict(int)
for flow in parse(options.emitters, 'flow'):
num = int(flow.number)
if flow.route is None:
dist = flow.routeDistribution[0]
probs = map(float, dist.probabilities.split())
probs = [p / sum(probs) for p in probs]
for rID, p in zip(dist.routes.split(), probs):
routeUsage[rID] += p * num
else:
routeUsage[flow.route] += num
usage = Statistics("routeUsage")
restrictUsage = Statistics("restrictedRouteUsage")
for rID, count in routeUsage.items():
usage.add(count, rID)
if rID in restrictions:
restrictUsage.add(count, rID)
print(usage)
print(restrictUsage, "total:", sum(restrictUsage.values))
if options.unused_output is not None:
with open(options.unused_output, 'w') as outf:
for rID, count in routeUsage.items():
if count <= options.threshold:
outf.write("%s\n" % rID)
if rID in restrictions and count > restrictions[rID]:
outf.write("%s %s %s\n" % (rID, count, restrictions[rID]))
assert(routeAlts[0].edges.split() == routeInfos[vehicle.id].edges)
routeInfos[vehicle.id].shortest_path_distance = getRouteLength(net, routeAlts[1].edges.split())
if oldCosts <= newCosts:
routeInfos[vehicle.id].detour = 0
routeInfos[vehicle.id].detourRatio = 1
if oldCosts < newCosts:
sys.stderr.write(("Warning: fastest route for '%s' is slower than original route " +
"(old=%s, new=%s). Check vehicle types\n") % (
vehicle.id, oldCosts, newCosts))
else:
routeInfos[vehicle.id].detour = oldCosts - newCosts
routeInfos[vehicle.id].detourRatio = oldCosts / newCosts
implausible = []
allRoutesStats = Statistics("overal implausiblity")
implausibleRoutesStats = Statistics("implausiblity above threshold")
for rID in sorted(routeInfos.keys()):
ri = routeInfos[rID]
ri.implausibility = (options.airdist_ratio_factor * ri.airDistRatio +
options.detour_factor * ri.detour +
options.detour_ratio_factor * ri.detourRatio +
max(0, options.min_dist / ri.shortest_path_distance - 1) +
max(0, options.min_air_dist / ri.airDist - 1))
allRoutesStats.add(ri.implausibility, rID)
if ri.implausibility > options.threshold:
implausible.append((ri.implausibility, rID, ri))
implausibleRoutesStats.add(ri.implausibility, rID)
# generate restrictions
if options.restrictions_output is not None:
with open(options.restrictions_output, 'w') as outf:
for score, rID, ri in sorted(implausible):
newCosts = float(routeAlts[1].cost)
assert(routeAlts[0].edges.split() == routeInfos[vehicle.id].edges)
routeInfos[vehicle.id].shortest_path_distance = getRouteLength(net, routeAlts[1].edges.split())
if oldCosts <= newCosts:
routeInfos[vehicle.id].detour = 0
routeInfos[vehicle.id].detourRatio = 1
if oldCosts < newCosts:
sys.stderr.write(("Warning: fastest route for '%s' is slower than original route " +
"(old=%s, new=%s). Check vehicle types\n") % (
vehicle.id, oldCosts, newCosts))
else:
routeInfos[vehicle.id].detour = oldCosts - newCosts
routeInfos[vehicle.id].detourRatio = oldCosts / newCosts
implausible = []
allRoutesStats = Statistics("overal implausiblity")
implausibleRoutesStats = Statistics("implausiblity above threshold")
for rID in sorted(routeInfos.keys()):
ri = routeInfos[rID]
ri.implausibility = (options.airdist_ratio_factor * ri.airDistRatio +
options.detour_factor * ri.detour +
options.detour_ratio_factor * ri.detourRatio +
max(0, options.min_dist / ri.shortest_path_distance - 1) +
max(0, options.min_air_dist / ri.airDist - 1))
allRoutesStats.add(ri.implausibility, rID)
if ri.implausibility > options.threshold:
implausible.append((ri.implausibility, rID, ri))
implausibleRoutesStats.add(ri.implausibility, rID)
# generate restrictions
if options.restrictions_output is not None:
with open(options.restrictions_output, 'w') as outf:
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("\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)]
for rID in routes[edges]:
restrictions[rID] = int(count)
routeUsage = defaultdict(int)
for flow in parse(options.emitters, 'flow'):
num = int(flow.number)
if flow.route is None:
dist = flow.routeDistribution[0]
probs = map(float, dist.probabilities.split())
probs = [p / sum(probs) for p in probs]
for rID, p in zip(dist.routes.split(), probs):
routeUsage[rID] += p * num
else:
routeUsage[flow.route] += num
usage = Statistics("routeUsage")
restrictUsage = Statistics("restrictedRouteUsage")
for rID, count in routeUsage.items():
usage.add(count, rID)
if rID in restrictions:
restrictUsage.add(count, rID)
print(usage)
print(restrictUsage, "total:", sum(restrictUsage.values))
if options.unused_output is not None:
with open(options.unused_output, 'w') as outf:
for rID, count in routeUsage.items():
if count <= options.threshold:
outf.write("%s\n" % rID)
if rID in restrictions and count > restrictions[rID]:
outf.write("%s %s %s\n" % (rID, count, restrictions[rID]))
odpairs = {}
for trip in parse(options.tripinfoFile, 'tripinfo'):
odpair = id2TAZ.get(trip.id)
tripID = trip.id
if odpair is None and '.' in trip.id:
flowID = trip.id[:trip.id.rfind('.')]
if flowID in flowIDs:
tripID = flowID
odpair = flowIds2TAZ.get(tripID)
if odpair is None:
fromEdge = trip.departLane[:trip.departLane.rfind('_')]
toEdge = trip.arrivalLane[:trip.arrivalLane.rfind('_')]
odpair = (sourceEdge2TAZ.get(fromEdge, '?'), sinkEdge2TAZ.get(toEdge, '?'))
if odpair not in odpairs:
odpairs[odpair] = Statistics(' '.join(odpair))
odpairs[odpair].add(parseTime(getattr(trip, options.attribute)), tripID)
if options.output:
with open(options.output, 'w') as outf:
outf.write('\n' % options.attribute)
for (fromTaz, toTaz), stats in sorted(odpairs.items()):
q1, median, q3 = stats.quartiles()
outf.write(' \n' %
(stats.max, stats.max_label, stats.avg(), q1, median, q3))
outf.write('\n')
else:
for (fromTaz, toTaz), stats in sorted(odpairs.items()):
print(stats)
def accelStats(netstate):
lastSpeed = {}
stats = Statistics(
"Accelerations", histogram=True, printMin=True, scale=0.2)
for vehicle in parse_fast(netstate, 'vehicle', ['id', 'speed']):
speed = float(vehicle.speed)
prevSpeed = lastSpeed.get(vehicle.id, speed)
stats.add(speed - prevSpeed, (vehicle.id, vehicle.speed))
lastSpeed[vehicle.id] = speed
print(stats)
def parse_dualog(dualog, limit):
print("Parsing %s" % dualog)
teleStats = Statistics('Teleports')
header = ['#Inserted', 'Running', 'Waiting', 'Teleports', 'Loaded']
step_values = [] # list of lists
step_counts = [] # list of edge teleport counters
reInserted = re.compile(r"Inserted: (\d*)")
reLoaded = re.compile(r"Loaded: (\d*)")
reRunning = re.compile(r"Running: (\d*)")
reWaiting = re.compile(r"Waiting: (\d*)")
reFrom = re.compile("from '([^']*)'") # mesosim
teleports = 0
inserted = None
loaded = None
running = None
waiting = None
haveMicrosim = None
counts = defaultdict(lambda: 0)
for line in open(dualog):
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("\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(' \n' % (p.id, diffAttrs))
del persons_orig[p.id]
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("\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(' \n' % (p.id, diffAttrs))