import sys
if sys.platform == "emscripten": # on the course website: install the course package
import micropip, js
await micropip.install(js.location.origin + "/lite/wheels/trafficlab-0.3.0-py3-none-any.whl")
import numpy as np
import pandas as pd
from trafficlab import data, plotsLab 2 · Real Traffic from Above
Practice · NGSIM trajectories · time-space diagrams · Edie’s definitions · a virtual loop detector
▶︎ Start this lab in your browser: nothing to install.
- New to notebooks? Read How to do a lab: 5 minutes, with pictures.
- Nothing to submit. Save your notebook: you may want to come back to it.
- Every task has a level: 🌶 Core, in class, everyone; 🌶🌶 Extend, optional, at home, with hints; 🌶🌶🌶 Challenge, optional and open-ended, for the curious.
In 2005 the Federal Highway Administration filmed US-101 in Los Angeles from a 36-storey building and turned the video into the position of every vehicle, ten times a second.
Predict first (not graded). A loop detector in the road and a camera above the same lane report the average speed of the same cars. Which reports the higher speed, and by roughly how much? Write down your guess.
A1 · Four minutes of one lane 🌶 Core
Lane 2 of US-101 from 8:00 a.m. Columns: vehicle, t (s), x (m along the road), v (m/s).
Do: run the cell. Each line is one car; red means slow.
us101 = data.load("ngsim-us101-0800-4min-v1")
df = us101[us101.lane == 2].reset_index(drop=True)
print(f"{len(df):,} records of {df.vehicle.nunique()} vehicles")
ax = plots.time_space(df, title="US-101, lane 2, from 8:00 a.m.")Wave hunt. Find the red band: a stop-and-go wave.
Predict first (not graded). Which way does it move, with the traffic or against it? How fast, in km/h?
Do: read two points on the band’s lower edge, each as (time s, position m), and run the cell.
(t_a, x_a), (t_b, x_b) = (None, None), (None, None) # YOUR CODE HERE: two points on the band
assert None not in (t_a, x_a, t_b, x_b), "read two points off the diagram first"
w = (x_b - x_a) / (t_b - t_a) * 3.6
print(f"wave speed: {w:.0f} km/h")
ax = plots.time_space(df, title="Your wave"); ax.plot([t_a, t_b], [x_a, x_b], "k--", lw=2)
assert -35 < w < -8, "not quite: pick both points on the same red band, far apart"
print("wave hunt: checks passed")Reveal. Negative: the wave travels backward, against the traffic, while every car in it moves forward. On most freeways, stop-and-go waves move upstream at roughly 15–20 km/h, whoever the drivers are; this one is a little faster, about 20 km/h. Did you guess the direction?
A2 · Edie’s definitions 🌶 Core
For a region A of the time-space diagram (Edie, 1965),
\begin{aligned} q(A) &= \frac{d(A)}{|A|}, \\ k(A) &= \frac{t(A)}{|A|}, \\ v(A) &= \frac{d(A)}{t(A)}, \end{aligned}
where d(A) is the total distance and t(A) the total time that vehicles spend in A.
Edie, L. C. (1965). Discussion of traffic stream measurements and definitions. Proceedings of the Second International Symposium on the Theory of Road Traffic Flow, London, 139–154.
Do: complete edie, then run the next cell. It measures the box on its first line.
def edie(df, x1, x2, t1, t2, dt=0.1):
"""Flow (veh/h), density (veh/km) and speed (km/h) in the region [x1, x2) x [t1, t2).
Each row of df is one vehicle at one instant, recorded every dt seconds.
Hint: a row inside the region stands for dt seconds of vehicle time,
and for v * dt metres of vehicle distance.
"""
# YOUR CODE HERE: compute total_time t(A), total_distance d(A) and the area |A|
return Noneassert edie(df, 0, 600, 0, 240) is not None, "complete edie() first"
x1, dx, t1, dt = 200, 100, 60, 60 # try other boxes
box = (x1, x1 + dx, t1, t1 + dt)
q, k, v = edie(df, *box)
print(f"box {box}: q = {q:.0f} veh/h, k = {k:.1f} veh/km, v = {v:.1f} km/h; k*v = {k * v:.0f}")
ax = plots.time_space(df); plots.region(ax, *box, label="your box")Think. q = k\,v holds exactly in this box, and in any box you try. Why?
A3 · A virtual loop detector 🌶 Core
A loop detector sits at one point and records each vehicle as it passes.
Do: complete virtual_loop, then run the next two cells.
def virtual_loop(df, x0, t1, t2):
"""What a loop detector at position x0 would report between t1 and t2.
Returns (count, flow in veh/h, time-mean speed in km/h, harmonic-mean speed in km/h).
Hint: for each vehicle, find its first record at or past x0; keep it if it falls in [t1, t2).
"""
# YOUR CODE HERE
return Noner = virtual_loop(df, 100, 0, 60)
assert r is not None, "complete virtual_loop first"
assert len(r) == 4 and r[0] > 0, "virtual_loop returns (count, flow, time-mean speed, harmonic-mean speed)"
print("virtual_loop: checks passed")x0 = 300 # where the loop sits, m
loop_n, loop_flow, loop_vt, loop_vh = virtual_loop(df, x0, 0, 240)
print(f"loop at {x0:.0f} m: {loop_n} vehicles, {loop_flow:.0f} veh/h")
print(f" time-mean speed {loop_vt:5.1f} km/h")
print(f" harmonic-mean speed {loop_vh:5.1f} km/h")
print(f" Edie space-mean speed, x0 ± 50 m: {edie(df, x0 - 50, x0 + 50, 0, 240)[2]:5.1f} km/h")Think. Which of the loop’s two speeds is closer to Edie’s space-mean speed, and why? Now check your prediction from the top: which sensor reported the higher speed?
B1 · Traffic detective 🌶🌶 Extend
A contest: bragging rights for the closest prediction, and for the first to find the slowest car.
Predict first (not graded). Of the cars in lane 2, how many came to a complete stop (speed under 0.5 m/s) somewhere on these 650 m? Write down a number.
Do: count them, and find the car that spent the longest on the road. Hint: df.groupby("vehicle") then .v.min() or .t.agg(...).
n_stopped = None # YOUR CODE HERE: cars whose lowest speed is under 0.5 m/s
slowest = None # YOUR CODE HERE: the vehicle id with the longest time on the road
assert n_stopped is not None and slowest is not None, "find both first"
print(f"{n_stopped} of {df.vehicle.nunique()} cars stopped completely")
car = df[df.vehicle == slowest]
print(f"vehicle {slowest} took {car.t.max() - car.t.min():.0f} s for {car.x.max() - car.x.min():.0f} m")
ax = plots.time_space(df, title=f"Vehicle {slowest}"); ax.plot(car.t, car.x, "k", lw=3)Think. How many stops did your slowest car make? Is it unlucky, or just in the wrong place at the wrong time? Then try lane 1 (us101[us101.lane == 1]): is the wave there too?
B2 · Speeds the way PeMS makes them 🌶🌶🌶 Challenge
Most freeway loops are single loops: they count vehicles and measure occupancy, the share of time the loop is covered, but not speed. PeMS estimates speed as flow × (loop length + vehicle length) ÷ occupancy (Lecture 2).
Do: make your virtual loop a single loop of length 1.8 m. Compute its occupancy from the trajectories (each vehicle covers the loop for (1.8 + its length) ÷ its speed), estimate speed with a 5 m vehicle length, and compare with your harmonic mean.
When is the estimate good, and when is it fooled?