07 Lane-change detection and the event table (Step 5, both methods)
Entry functions:
from trajlab.processing.events import add_event_neighbors
from trajlab.processing.lane_changes import detect_lane_changes
events = detect_lane_changes(tracks, lat_speed_threshold_mps=0.1) # inside the pipeline: assign_lanes runs it
events = add_event_neighbors(events, tracks_with_surroundings) # after step 10
Purpose
Lateral manoeuvres are detected from the lateral velocity of each vehicle. The detector intervals feed both lane-assignment methods (08 heuristic, 09 HMM), which decide whether an interval is a lane change (the vehicle ends in another lane) or a lateral excursion (it returns, or moves within its lane). The result is an event table with kinematics and the surrounding vehicles at start, crossing and end of every manoeuvre.
Interface
| Pipeline step | Requires columns | Produces columns | Further output columns |
|---|---|---|---|
lane_correction |
id, datum, pos_lon, pos_lat, lane, speed |
lane_fixed |
is_changing_lane, is_lateral_excursion, lane_conflict_resolved, lane_fixed_unresolved, lane_confidence, lane_posterior_margin, suspected_duplicate_track, lane_conflict_unresolved |
lane_change_events |
id, datum |
— | — |
is_changing_lane marks the samples of a lane change, is_lateral_excursion those of an excursion.
The event table is returned by run_pipeline_detailed(...).events (and stored in the
context artifact lane_change_events). Columns:
| Column | Meaning |
|---|---|
id, class |
vehicle |
event_type |
lane_change or lateral_excursion |
lc_direction, direction |
+1 / "left" towards a higher lane ID, −1 / "right" |
time_start, time_lc, time_end, duration_sec, frame_lc |
detector interval and its peak |
time_cross |
time of the boundary crossing (lane changes) |
lane_start, lane_end, lane_before, lane_after |
Step 4 labels at the interval ends; corrected lanes before/after |
max_lateral_offset_m |
largest signed offset from the start-lane center (excursions) |
speed_start, speed_lc, speed_end, pos_lon_lc |
kinematics |
{id, clearance, headway, rel_speed}_{front, rear}_{start, lc, end} |
neighbors (target lane at start, same lane at crossing and end) |
detected_by |
HMM only: detector+hmm (inside a detector interval) or hmm |
near_road_end |
crossing (or peak of an excursion) within lane_change.edge_buffer_m of a road end |
| Parameter | Default | Description |
|---|---|---|
lane_change.enabled |
True |
Run the step. |
lane_change.lat_speed_threshold_mps |
0.1 |
Smoothed lateral speed in m/s above which a lane-change interval starts. |
lane_change.median_kernel_sec |
0.167 |
Median-filter width in seconds for the lateral velocity. Defaults to 0.167 s (5 samples at 30 Hz). |
lane_change.sewma_halfwidth_sec |
0.5 |
Half-width of the SEWMA kernel in seconds. Defaults to 0.5 s (15 samples at 30 Hz). |
lane_change.sewma_width_sec |
0.316374 |
Width T of the symmetric exponential kernel (Thiemann et al. 2008) in seconds; per vehicle alpha = exp(-dt / T). Defaults to 0.3164 s (the former alpha = 0.9 at 30 Hz). |
lane_change.sewma_alpha |
None |
Deprecated per-sample decay factor; if set, it is read at 30 Hz, converted to sewma_width_sec and a DeprecationWarning is emitted. |
lane_change.min_displacement_m |
1.5 |
Minimum lateral displacement in meters of a lane change. |
lane_change.edge_buffer_m |
30 |
Events whose crossing lies within this distance (m) of either end of the road get near_road_end = True (artefacts of the section ends are concentrated there; excluded from behavioral statistics by default). |
Method
- Lateral velocity: $\dot d = \partial d/\partial t$ (
np.gradient), median-filtered overmedian_kernel_sec(0.167 s). - SEWMA smoothing (symmetric exponential kernel, Thiemann, Treiber & Kesting 2008):
$$\mathrm{SEWMA}i = \frac{\sum{j=-w}^{w} \alpha^{|j|}\,\dot d_{i+j}}{\sum_{j=-w}^{w} \alpha^{|j|}},\qquad \alpha = e^{-\Delta t / T},$$
width $T$ =
sewma_width_sec(0.3164 s) and half-widthsewma_halfwidth_sec(0.5 s); $\alpha$ is derived per vehicle from its nominal sampling interval, so the smoothing is identical at 10, 25 and 30 Hz (a fixed per-sample $\alpha$ = 0.9 would mean 0.32 s at 30 Hz but 0.95 s at 10 Hz). Filter widths are converted to samples with each vehicle's median interval. - Intervals: $|\mathrm{SEWMA}| >$
lat_speed_threshold_mps(0.1 m/s) with a total lateral displacement abovemin_displacement_m(1.5 m); start and end at the threshold crossings (scheme of Hofinger & Fellendorf 2023). Increasingpos_latis a left movement. - Lane change vs. excursion: decided by the assignment method (08, 09).
- Event attributes: after Step 6 the last pipeline step (
lane_change_events) adds the neighbors from the output grid at the nearest grid time within half a grid step: attime_startthe target-lane neighbors, attime_lcandtime_endthe same-lane neighbors. - Road ends: events whose crossing (lane change) or peak (excursion) lies within
edge_buffer_m(30 m) of either end of the section getnear_road_end = True; artefacts of the section ends concentrate there, so they are excluded from behavioral statistics.
Design choices
The choices below are made for this library unless a paper is named; the evidence for each is given
under "Source" (a test, a script of applications/ant2027/ or a measurement named in the text).
- SEWMA width in seconds, α per vehicle. The detector must behave the same at every frame rate.
$T$ = 0.3164 s is the width of the per-sample α = 0.9 at 30 Hz ($T = -(1/30)/\ln 0.9$). α is
computed from the nominal interval (1 / median interval rounded to 0.01 Hz): timestamps quantised
to microseconds change α by about 2·10⁻⁷, which already moved peak frames. Source: Thiemann, Treiber
& Kesting (2008) for the kernel;
tests/processing/lane_changes/test_sewma_rate.py. - Kernel truncated at 0.5 s. Thiemann et al. truncate the kernel at three widths (≈ 0.95 s);
0.5 s ≈ 1.6T is narrower. This is a deviation from the paper; the half-width is the parameter
sewma_halfwidth_sec. - No separate thresholds for merges. On exiD (13 directions) merges have a median peak lateral speed only about 10 % above open-road lane changes (criterion for separate thresholds: > 20 % with a confidence interval excluding zero) and are longer, not shorter (median 8.8 vs. 6.4 s).
- Events near the section ends are flagged, not removed (
near_road_end). Artefacts of the section ends concentrate within 30 m of the ends (see the edge lane-change density in 03); the flag lets behavioral statistics exclude them while the lane labels stay complete.
Validation and limitations
tests/processing/lane_changes/test_events.py,tests/processing/lane_changes/test_sewma_rate.py,tests/processing/test_processing.py, synthetic battery (analysis/quality/synthetic_battery.py: lane changes, slow drifts, excursions, aborted changes, merges, dense traffic).- Lane-change durations on Sites 1 and 2: median 6.4–7.1 s for cars. A second mode at 1.5–4 s marks lateral jumps detected as manoeuvres: < 4 s for 2 % (campaign 1a cars), 7 % (campaign 2 cars), but 26 % (campaign 1b cars) and 48 % (campaign 1b HGVs). On data with camera hand-overs and a non-constant HGV offset the behavioral lane-change statistics need filtering.
- Slow drifts below the threshold are missed by the detector (the HMM can still switch, 09).