Skip to content

Adapting the configuration to a dataset

Use this page when you take trajlab to a dataset of your own and want the lane assignment and lane changes to be right for it. It shows how to change a setting, which settings to check for a new dataset, how to check them without ground truth, and how the experimental preset ngsim was derived for NGSIM I-80 and US-101.

Changing a setting

A preset is a ready-made configuration for one kind of recording. load_preset takes the preset name, your site settings (here only the number of main lanes) and any number of sections to change. Each section is a dict with the options you want to set; everything else keeps the preset's value:

import trajlab

config = trajlab.load_preset(
    "drone_topdown",
    site={"lanes": {"n_main_lanes": 3}},  # site settings: the number of main lanes
    alignment={"enabled": False},  # section "alignment", option "enabled"
    lane_change={"lat_speed_threshold_mps": 0.15},  # section "lane_change", option "lat_speed_threshold_mps"
)
print(config.alignment.enabled, config.lane_change.lat_speed_threshold_mps)  # False 0.15

A misspelled option raises an error that names it. The options of every section, with their defaults, are listed in Configuration.

Pick the preset by the kind of recording, not by the dataset:

  • drone_topdown: a drone looking straight down on the road (near-nadir), as in highD or exiD;
  • roadside_camera: cameras that look at the road at an angle from the side (oblique), as in NGSIM;
  • drone_hgv_paper: the processing chain of the ANT 2027 paper, for drone data with many heavy goods vehicles; the quickstart uses it;
  • default: the library defaults.

A dataset can still differ from its kind in ways that change the result. The positions may already be measured from the road edge, the lateral position may be noisier, and ramps or auxiliary lanes may lie where the reader does not report them. The checklist below covers these cases.

Checklist

Go through the list once for every new dataset, and again when a dataset brings a new site or sensor. Each item names the setting (as section.option), when it applies and how to check it.

Perspective correction (perspective.correction). Use it only for oblique views of tall vehicles that the provider did not correct, for example drone recordings at an angle. Do not use it for near-nadir drones, and not for roadside cameras, for which trajlab has no model of the offset per vehicle class. To check: compare the median lateral position of heavy vehicles and cars per lane. An offset of the heavy vehicles that grows with the distance from the camera is perspective (step 02).

Road-edge alignment (alignment.enabled). Turn it off when the positions are already measured across the road, for example as a distance from the road edge in the direction of travel. Keep it on when the positions were projected onto a reference line that does not follow the lanes. To check: plot the median pos_lat (the lateral position) per lane along pos_lon (the position along the road) before the alignment. Flat medians mean that the positions are already aligned, and the alignment can only bend them, mostly where the first bins have few samples (step 03).

Auxiliary-lane zones (lanes.auxiliary_lane_zones). Give a zone for every auxiliary lane, on-ramp and off-ramp to the right of lane 1, whether or not the reader reports it. A lane that continues from an on-ramp into an off-ramp is one zone. To check: plot how often pos_lat right of lane 1 is occupied along pos_lon, or plot the provider's ramp lanes. The lane assignment with the hidden Markov model (HMM, step 09) ignores zones shorter than lanes.hmm.min_aux_zone_m (50 m). Without a zone, ramp vehicles are pushed into lane 1 and appear to change lanes at the zone edges.

Detector threshold (lane_change.lat_speed_threshold_mps). Raise it when the lateral position is noisier than on the data the default of 0.1 m/s was set for. To check: measure the lateral speed of the vehicles that never change lane with compute_lateral_speed_noise (below). If many of them exceed the threshold, the detector marks lane changes that are not there, and real lane changes start early and end late.

Conflict resolution (lanes.resolve_conflicts). Turn it off where the positions of neighboring vehicles overlap in dense queues, as with oblique views or long vehicles in congestion. To check: look in the lane-change events (result.events, rows with event_type == "lane_change") for changes with detected_by == "hmm" and a duration of 0 s, and in the tracks for label changes over several lanes within a second while pos_lat stays at one lane center. Both are re-assignments, not lane changes.

Stitching (stitching.enabled). Stitching joins the pieces of one vehicle's track that the provider's software cut apart, for example where one camera hands the vehicle over to the next. Use it only for such data, not for data with one track per vehicle. To check: count tracks that end inside the section and tracks that start inside it at the same time and place (step 04).

Diagnosis without ground truth

Most datasets come without lane ground truth. The following checks find configuration errors on the pipeline output alone. Each one is a symptom; the checklist above names the setting behind it. The example runs the pipeline on the synthetic motorway that ships with trajlab; replace raw, the reference line and the preset with your own:

import trajlab
from trajlab.analysis.quality.lane_metrics import consistency_metrics, label_transitions

raw = trajlab.examples.synthetic_motorway(duration_s=40.0)  # synthetic drone data, no download
ref_east, _ = trajlab.estimate_ref_lines(raw)  # one reference line per travel direction
config = trajlab.load_preset("drone_topdown", site={"lanes": {"n_main_lanes": 3}})
result = trajlab.run_pipeline_detailed(raw[raw["orientation"] > 0], ref_east, config)  # one direction

changes = label_transitions(result.tracks)  # one row per label change: id, time, lane_from, lane_to
print(len(changes), consistency_metrics(result.tracks)["flicker"])

If your positions are already measured across the road (as in NGSIM), pass None instead of the reference line.

  • Where the lane changes lie. Histogram the label changes (label_transitions) along pos_lon, and by the time since the start and until the end of the track. Real lane changes spread over the section. Clusters at the section start (alignment), at zone edges (missing or too short zones) or within 2 s of a track start or end are artifacts.
  • Lane changes of 0 s duration. duration_sec == 0 in the events means that the label changed without lateral motion above the threshold: a re-assignment by the conflict resolution, or a label that drifts across a lane boundary.
  • Flicker, label changes undone within 2 s (consistency_metrics, key flicker). The HMM keeps them rare, so many flickers point to overlapping positions or a missing zone.
  • Jumps over more than one lane. A change of two or more lanes between two samples is not a maneuver.
  • Unexplained leader changes. A vehicle whose leader changes although neither vehicle changes lane points to wrong labels of one of them (limitations).
  • Agreement with the provider's labels, where they exist (method_agreement against lane_gt). They are not ground truth, because a provider label usually comes from the same positions. Read the disagreements rather than the agreement rate: they show where one of the two labelings is wrong.

All three functions are in trajlab.analysis.quality.lane_metrics.

The noise of the lateral speed

The lane-change detector (step 07) marks a lane change where the smoothed lateral speed exceeds lane_change.lat_speed_threshold_mps. The threshold cannot be lower than the noise of the lateral speed of vehicles that do not change lane. compute_lateral_speed_noise measures this noise on the lane keepers, the vehicles without any label change over their whole track, with the smoothing of the detector. Continuing the example above:

from trajlab.analysis.quality.lateral_speed_noise import compute_lateral_speed_noise

noise = compute_lateral_speed_noise(result.tracks, config.lane_change)
print(noise.speed_mps, noise.share_above_threshold)  # 95th percentile of |v_lat| [m/s]; share above the threshold

The rule of the preset ngsim: the threshold is the 95th percentile of |v_lat| of the lane keepers, per site, rounded to 0.05 m/s; where the sites of one dataset differ, the larger value holds for all. Measure it on a run whose other settings are already adapted (alignment, zones, conflict resolution), because their artifacts would otherwise remove lane keepers or add lateral speed. Choose the setting on one part of the data and check the result on another.

Worked example: NGSIM

Example: lane changes on NGSIM runs the whole chain. The preset ngsim and the site files ngsim_i80 and ngsim_us101 follow the checklist. Each value is derived from a property of the data, not from the agreement with the provider's Lane_ID:

import trajlab
from trajlab.config import site_pipeline_config

config = trajlab.load_preset("ngsim", site=site_pipeline_config("ngsim_us101"))  # or "ngsim_i80"
  • Alignment off. NGSIM gives Local_X, the distance from the left road edge in the direction of travel, so the positions are already measured across the road.
  • Zones from the documentation and the data. The NGSIM documentation names the on-ramp of I-80 and, on US-101, the on-ramp, the auxiliary lane and the off-ramp. The extents are the 1 % and 99 % quantiles of pos_lon of the provider's ramp lanes. On US-101 the three form one zone, because the on-ramp continues into the auxiliary lane and that into the off-ramp. site_config_from_attrs reports only the auxiliary lane.
  • Conflict resolution off, as for dense queues seen by oblique cameras.
  • Detector threshold from the noise rule above, measured on the first 5 minutes of the periods I-80 4:00–4:15 pm and US-101 7:50–8:05 am (the preset file gives the values).