Skip to content

Example: lane assignment and lane changes on NGSIM

This example takes one period of the public NGSIM data from the raw trajectory file to a CSV table of lane changes. Along the way, trajlab assigns a lane to every vehicle at every time step, and finally compares these lanes with the lane numbers (Lane_ID) that come with the data. The script examples/ngsim_lane_changes.py runs all steps. This page shows the workflow; it does not evaluate the result.

The data are not part of the repository, so the documentation build does not run the code on this page.

Data

NGSIM (Next Generation Simulation) is a public dataset of the U.S. Department of Transportation, Federal Highway Administration. It holds vehicle trajectories at 10 Hz, recorded by cameras on buildings next to two freeways: I-80 in Emeryville, California, and US-101 in Los Angeles. Download the vehicle-trajectory files from data.transportation.gov. Use the original per-site files, one whitespace-separated .txt file per 15-minute period, not a .csv export: some .csv exports were cut at a spreadsheet row limit (readers). Keep the folder layout of the originals below one folder of your own, the NGSIM root:

<NGSIM root>/i-80/data/0400pm-0415pm/trajectories-0400-0415.txt
<NGSIM root>/us-101/data/0750am-0805am/trajectories-0750am-0805am.txt

The script downloads nothing. Give it the NGSIM root with --ngsim-dir, or set the environment variable TRAJLAB_NGSIM_DIR to it:

python examples/ngsim_lane_changes.py --ngsim-dir path/to/NGSIM               # I-80
python examples/ngsim_lane_changes.py --ngsim-dir path/to/NGSIM --site us-101 # US-101
python examples/ngsim_lane_changes.py --ngsim-dir path/to/NGSIM --preset ngsim  # experimental preset for NGSIM

To keep the run short, the script processes only the vehicles that enter in the first 5 minutes of the period; change this with --minutes. It writes its tables and figures to the folder ngsim_lane_changes/; change this with --out.

The positions of NGSIM contain measurement errors. Speeds and accelerations differentiated from them are noisy and partly implausible, which is why Montanino and Punzo (2015) reconstructed the I-80 trajectories, and the provided lanes contain errors as well (Coifman & Li 2017). trajlab therefore reads only the positions and computes speed, acceleration and lanes itself.

Read and process

import trajlab

raw = trajlab.read_dataset("ngsim", "path/to/NGSIM/i-80/data/0400pm-0415pm/trajectories-0400-0415.txt")
site = trajlab.site_config_from_attrs(raw)  # 6 main lanes and the on-ramp lane, reported by the reader
config = trajlab.load_preset("roadside_camera", site=site)
result = trajlab.run_pipeline_detailed(raw, None, config)  # positions already across the road: no reference line
tracks, events = result.tracks, result.events
  • Units. NGSIM gives feet. The reader converts every length to meters, and all outputs are in SI units (positions in m, acceleration in m/s², times as timestamps or seconds), except the speed, which is in km/h in all trajlab outputs. pos_lon runs along the road, pos_lat across it, positive to the left.
  • Lanes. The reader reports the number of main lanes (I-80: 6, US-101: 5) and the extent of the provider's auxiliary lane (I-80: the on-ramp lane; US-101: the auxiliary lane between the on-ramp and the off-ramp). It stores them with the data (in raw.attrs), and site_config_from_attrs turns them into the site settings, so roadside_camera needs no site file. On US-101 the provider's two ramp lanes (7 and 8) are left out: the reader gives them lane −1 in lane_gt, and they are not in its auxiliary-lane zone. Only the site file used with the preset ngsim (below) includes them.
  • Preset. roadside_camera is the processing chain for oblique cameras. It has no perspective correction (trajlab has no model of the offset per vehicle class for oblique views) and no stitching (NGSIM provides one track per vehicle). It reconstructs the kinematics after Montanino and Punzo, estimates the lane centers from the data with a restricted Gaussian mixture, a statistical model that finds one cluster of lateral positions per lane (lane geometry), and assigns the lanes with a hidden Markov model, which chooses the most likely sequence of lanes per vehicle and so suppresses short jumps (HMM lane assignment).
  • Kinematics. The reconstruction after Montanino and Punzo (2015) smooths the positions so that speed and acceleration are physically plausible (kinematics); the lane assignment uses the smoothed lateral position.
  • Lane_ID is not an input. The reader keeps it as lane_gt, renumbered from the right (lane 1 = rightmost, 0 = auxiliary lane), for the comparison below only.

The preset ngsim (experimental)

roadside_camera describes oblique roadside cameras in general and stays the recommended preset for NGSIM. The experimental preset ngsim is opt-in and not yet evaluated. It adapts roadside_camera to the properties of NGSIM, following Adapting the configuration to a dataset. Use it with the site file of the site instead of site_config_from_attrs:

from trajlab.config import site_pipeline_config

config = trajlab.load_preset("ngsim", site=site_pipeline_config("ngsim_i80"))  # US-101: "ngsim_us101"
result = trajlab.run_pipeline_detailed(raw, None, config)

It differs from roadside_camera in four settings:

  • No road-edge alignment. NGSIM gives the distance from the left road edge, so the positions are already measured across the road; the alignment would only bend the lanes where the first meters of the section hold few samples.
  • All ramps as zones. The site files hold the on-ramp of I-80 and, on US-101, one zone from the on-ramp through the auxiliary lane to the off-ramp (provider lanes 7, 6 and 8); the reader reports the auxiliary lane only.
  • No conflict resolution between vehicles. In dense queues the positions of neighboring vehicles overlap, and the resolution would move vehicles across several lanes while they keep their lateral position.
  • Detector threshold above the noise. The lateral speed of NGSIM is noisier than that of drone data. The threshold is the 95th percentile of the smoothed lateral speed of the vehicles that keep their lane (compute_lateral_speed_noise), measured on the first 5 minutes of I-80 4:00–4:15 pm and US-101 7:50–8:05 am.

The lane-change table below has the same columns with either preset; with ngsim, start and end use the higher threshold.

The same run step by step

The run can also be written with the single steps (Using single processing steps). Their defaults for the kinematics, the lane geometry, the lane-change detection and the lane assignment are the same as in roadside_camera:

from trajlab.processing.kinematics import reconstruct_kinematics
from trajlab.processing.lanes import assign_lanes, estimate_lane_geometry
from trajlab.road_layout import AuxLaneZone, LaneLayout

zones = [AuxLaneZone(**zone) for zone in raw.attrs["auxiliary_lane_zones"]]  # the lane layout stored by the reader
layout = LaneLayout(n_main_lanes=raw.attrs["n_lanes"], auxiliary_lane_zones=zones)
kinematics = reconstruct_kinematics(raw)
geometry = estimate_lane_geometry(kinematics.tracks, layout=layout)
lanes = assign_lanes(
    geometry.tracks, layout=layout, lane_centers=geometry.centers, road_length_m=float(raw["pos_lon"].max())
)
tracks, events = lanes.tracks, lanes.events

This short version leaves out steps that run_pipeline also runs (the pipeline overview lists them), so its lanes are close to, not equal to, those of the preset. Use run_pipeline for results, and the single steps to look at an intermediate result or to change one step.

The lane changes

Every change of the assigned lane is one row of the event table with event_type == "lane_change". The lines below keep these rows with all event columns under their original names:

lane_changes = events[events["event_type"] == "lane_change"]
lane_changes.to_csv("lane_changes.csv", index=False)

The script keeps only the columns of the following table and renames them, as in the first column, before it writes lane_changes.csv:

Script column Event column Unit Meaning
vehicle id – NGSIM Vehicle_ID
start, end time_start, time_end timestamp start and end of the maneuver: the smoothed lateral speed is above the threshold (lane_change.lat_speed_threshold_mps, 0.1 m/s in roadside_camera)
crossing time_cross timestamp first sample in the new lane
from_lane, to_lane lane_before, lane_after – assigned lane before and after (1 = rightmost, 0 = auxiliary lane)
duration_s duration_sec s end − start
direction lc_direction – +1 = left (to a higher lane number), −1 = right
detected_by detected_by – how the lane change was found (see below)

detected_by is detector+hmm when the lane change lies in an interval of the lane-change detector, which gives its start and end. It is hmm when the lane assignment changes the lane without such an interval, for example in a slow drift; start and end are then where the lateral speed falls below the threshold. All columns of the event table are described in Output format.

Comparison with the provided lanes

Neither labeling is ground truth: both come from the same video positions. The comparison shows where they agree.

The script writes three measures per labeling (trajlab's lanes and Lane_ID) to comparison.csv:

  • Agreement is the share of samples with the same lane in both labelings (method_agreement).
  • Label changes count every change of the lane label of a vehicle, and flicker counts the changes that are undone within 2 s (A → B → A), both with consistency_metrics. They are also given per vehicle-km, so that they do not depend on how much data the run covers.

Both functions are in trajlab.analysis.quality.lane_metrics; the function compare_with_provider of the script shows the calls.

Lateral position of one NGSIM I-80 vehicle during a lane change, with the provided Lane_ID and the trajlab lane over time

One vehicle of I-80 during a lane change to the right: lateral position (top) with the lane change found by trajlab (shaded), and the lane over time (bottom), Lane_ID of the provider (thick, gray) and trajlab (thin, teal).

The duration of a lane change depends on the definition of its start and end: here the lateral speed is above 0.1 m/s. Thiemann, Treiber and Kesting (2008) report a mode of about 3 s for NGSIM with their own definition (limitations). Compare durations only with values of the same definition.

References

  • Coifman, B., & Li, L. (2017). A critical evaluation of the Next Generation Simulation (NGSIM) vehicle trajectory dataset. Transportation Research Part B, 105, 362–377. https://doi.org/10.1016/j.trb.2017.09.018
  • Montanino, M., & Punzo, V. (2015). Trajectory data reconstruction and simulation-based validation against macroscopic traffic patterns. Transportation Research Part B, 80, 82–106. https://doi.org/10.1016/j.trb.2015.06.010
  • Thiemann, C., Treiber, M., & Kesting, A. (2008). Estimating acceleration and lane-changing dynamics from Next Generation Simulation trajectory data. Transportation Research Record, 2088, 90–101. https://doi.org/10.3141/2088-10
  • U.S. Department of Transportation, Federal Highway Administration (2017). Next Generation Simulation (NGSIM) Vehicle Trajectories and Supporting Data. https://doi.org/10.21949/1504477