v1.0.1: Automatum house format (Sample_Data, doc, example_scripts, zip, overview) + Parquet
b35f113 verified Download example_scripts/01_lane_changes.py from AutomatumData/automatum-data-highway-area-pools: direct link, hf CLI and curl.
- Browser
- Download file 5.62 kB
-
https://huggingface.co/datasets/AutomatumData/automatum-data-highway-area-pools/resolve/main/example_scripts/01_lane_changes.py
- Command line
-
hf download hf://datasets/AutomatumData/automatum-data-highway-area-pools/example_scripts/01_lane_changes.py
-
curl -L -o 01_lane_changes.py https://huggingface.co/datasets/AutomatumData/automatum-data-highway-area-pools/resolve/main/example_scripts/01_lane_changes.py
5.62 kB
| """ | |
| Lane Change Analysis — Automatum Data Highway Area Pools (Multi-Drone) | |
| Analyzes lane change behavior across all vehicles in a recording. | |
| Usage: | |
| python 01_lane_changes.py <path_to_recording_folder> | |
| Example: | |
| python 01_lane_changes.py ../Sample_Data/Pool-Highway-A9-OffenbauRastanlage_c025-c02598c7-fa60-4302-8cee-12a05f4e0db6 | |
| Note: | |
| The exports of this release do not contain the per-sample lane assignment | |
| (`lane_id_vec`). If it is missing, the script estimates lanes from the | |
| vehicle's lateral position across the road: per driving direction a local road | |
| axis (median lateral position in 20 m slices) is computed, the lateral offsets are | |
| clustered into lanes (histogram peaks), and a lane change is counted when | |
| a vehicle stays at least 1 s on a new lane. | |
| """ | |
| import sys | |
| import os | |
| import numpy as np | |
| from openautomatumdronedata.dataset import droneDataset | |
| def _road_axis(dynWorld): | |
| """Principal axis of all positions (unit vector) and the centroid.""" | |
| pts = np.vstack([np.column_stack([o.x_vec, o.y_vec]) for o in dynWorld.dynamicObjects.values() | |
| if len(o.x_vec) > 1]) | |
| c = pts.mean(0) | |
| _, _, vt = np.linalg.svd(pts[::50] - c, full_matrices=False) | |
| return c, vt[0] | |
| def estimate_lane_ids(dynWorld, lane_min_gap_m=2.5, min_dwell_s=1.0): | |
| """Estimated lane index per sample for every object (dict UUID -> np.array).""" | |
| c, ax = _road_axis(dynWorld) | |
| nrm = np.array([-ax[1], ax[0]]) | |
| objs = [o for o in dynWorld.dynamicObjects.values() if len(o.x_vec) > 1] | |
| s, d, direction = {}, {}, {} | |
| for o in objs: | |
| p = np.column_stack([o.x_vec, o.y_vec]) - c | |
| s[o.UUID], d[o.UUID] = p @ ax, p @ nrm | |
| direction[o.UUID] = 1 if s[o.UUID][-1] >= s[o.UUID][0] else -1 | |
| lane_ids = {} | |
| dt = dynWorld.delta_t | |
| for dirn in (1, -1): | |
| ids = [u for u in s if direction[u] == dirn] | |
| if not ids: | |
| continue | |
| S = np.concatenate([s[u] for u in ids]); D = np.concatenate([d[u] for u in ids]) | |
| # local road axis of this direction: median lateral position in 20 m slices along the road | |
| edges_s = np.arange(S.min(), S.max() + 20.0, 20.0) | |
| idx = np.clip(np.digitize(S, edges_s) - 1, 0, len(edges_s) - 2) | |
| med = np.array([np.median(D[idx == k]) if np.any(idx == k) else np.nan for k in range(len(edges_s) - 1)]) | |
| mid = 0.5 * (edges_s[1:] + edges_s[:-1]); ok = ~np.isnan(med) | |
| res = {u: d[u] - np.interp(s[u], mid[ok], med[ok]) for u in ids} | |
| R = np.concatenate(list(res.values())) | |
| hist, edges = np.histogram(R, bins=np.arange(R.min() - 1, R.max() + 1, 0.25)) | |
| hist = np.convolve(hist, np.ones(5) / 5, mode="same") | |
| centers = 0.5 * (edges[1:] + edges[:-1]) | |
| peaks = [i for i in range(1, len(hist) - 1) | |
| if hist[i] >= hist[i - 1] and hist[i] > hist[i + 1] and hist[i] > 0.02 * hist.max()] | |
| lanes = [] | |
| for i in sorted(peaks, key=lambda i: -hist[i]): | |
| if all(abs(centers[i] - l) > lane_min_gap_m for l in lanes): | |
| lanes.append(centers[i]) | |
| lanes = np.sort(np.array(lanes)) | |
| min_len = max(1, int(round(min_dwell_s / dt))) | |
| for u in ids: | |
| raw = np.argmin(np.abs(res[u][:, None] - lanes[None, :]), axis=1) | |
| # hysteresis: only accept a new lane after min_dwell_s on it | |
| out = raw.copy(); cur = raw[0]; run_start = 0 | |
| for k in range(1, len(raw)): | |
| if raw[k] != raw[k - 1]: | |
| run_start = k | |
| if raw[k] != cur and k - run_start + 1 >= min_len: | |
| cur = raw[k] | |
| out[k] = cur | |
| lane_ids[u] = out | |
| return lane_ids | |
| def analyze_lane_changes(dataset_path): | |
| print(f"Loading dataset from: {dataset_path}") | |
| dataset = droneDataset(dataset_path) | |
| dynWorld = dataset.dynWorld | |
| print(f"Vehicles found: {len(dynWorld)}") | |
| have_lane_ids = any(getattr(o, "lane_id_vec", None) is not None and len(o.lane_id_vec) > 0 | |
| for o in dynWorld.dynamicObjects.values()) | |
| estimated = None | |
| if not have_lane_ids: | |
| print("lane_id_vec is not part of this export -> estimating lanes from the lateral position") | |
| estimated = estimate_lane_ids(dynWorld) | |
| lane_change_counts = [] | |
| for dynObj in dynWorld.dynamicObjects.values(): | |
| lane_ids = dynObj.lane_id_vec if have_lane_ids else estimated.get(dynObj.UUID) | |
| if lane_ids is None or len(lane_ids) == 0: | |
| continue | |
| changes = 0 | |
| current_lane = lane_ids[0] | |
| for lane in lane_ids[1:]: | |
| if lane != current_lane: | |
| changes += 1 | |
| current_lane = lane | |
| lane_change_counts.append({ | |
| "uuid": dynObj.UUID, | |
| "type": dynObj.type, | |
| "changes": changes, | |
| }) | |
| sorted_by_changes = sorted(lane_change_counts, key=lambda x: x["changes"], reverse=True) | |
| n_changing = sum(1 for x in lane_change_counts if x["changes"] > 0) | |
| total = sum(x["changes"] for x in lane_change_counts) | |
| print(f"Lane changes: {total} in total, {n_changing} of {len(lane_change_counts)} vehicles change lane at least once") | |
| print("\n--- Top 10 vehicles with most lane changes ---") | |
| for idx, item in enumerate(sorted_by_changes[:10]): | |
| print(f"{idx+1}. Vehicle {item['uuid']} ({item['type']}): {item['changes']} lane changes") | |
| if __name__ == "__main__": | |
| if len(sys.argv) < 2: | |
| print("Usage: python 01_lane_changes.py <path_to_recording_folder>") | |
| else: | |
| analyze_lane_changes(sys.argv[1]) | |