Download Processing_Code/attach_lane_annotations.py from InterestingITS/U-EASWS: direct link, hf CLI and curl.
- Browser
- Download file 2.03 kB
-
https://huggingface.co/datasets/InterestingITS/U-EASWS/resolve/main/Processing_Code/attach_lane_annotations.py
- Command line
-
hf download hf://datasets/InterestingITS/U-EASWS/Processing_Code/attach_lane_annotations.py
-
curl -L -o attach_lane_annotations.py https://huggingface.co/datasets/InterestingITS/U-EASWS/resolve/main/Processing_Code/attach_lane_annotations.py
2.03 kB
| """Join source-frame lane and direction fields by original frame and vehicle keys.""" | |
| from _common import lane_direction, numeric, parser, read_csv, require, save_csv, unique | |
| def attach(metric, annotations): | |
| metric, annotations = metric.copy(), annotations.copy() | |
| keys, fields = ["Frame", "Vehicle_ID"], ["Lane number", "Direction"] | |
| if set(fields) & set(metric.columns): | |
| raise ValueError("Metric input already has lane/direction fields; explicitly resolve them first") | |
| for table in (metric, annotations): | |
| numeric(table, keys, integer=True) | |
| unique(table, keys) | |
| require(annotations, fields) | |
| lane_direction(annotations) | |
| if not annotations.Direction.isin(["Left", "Right"]).all() or annotations["Lane number"].isna().any(): | |
| raise ValueError("Annotations require valid lane numbers and Left/Right directions") | |
| if "Source_ID" in metric: | |
| if metric.Source_ID.isna().any() or metric.Source_ID.nunique() != 1: | |
| raise ValueError("Attach lanes for one source at a time") | |
| if "Source_ID" in annotations and not annotations.Source_ID.eq(metric.Source_ID.iloc[0]).all(): | |
| raise ValueError("Annotation source differs from metric source") | |
| result = metric.merge(annotations[keys + fields], on=keys, how="left", validate="one_to_one", indicator=True) | |
| if result._merge.ne("both").any(): | |
| raise ValueError("Some metric observations lack verified lane/direction annotations") | |
| return result.drop(columns="_merge") | |
| def main(): | |
| p = parser(__doc__) | |
| p.add_argument("--input", required=True, help="One-source metric CSV with original Frame and Vehicle_ID") | |
| p.add_argument("--annotations", required=True, help="Verified source-frame CSV: Frame,Vehicle_ID,Lane number,Direction") | |
| p.add_argument("--output", required=True, help="New metric trajectory CSV with lane/direction fields") | |
| a = p.parse_args() | |
| save_csv(attach(read_csv(a.input), read_csv(a.annotations)), a.output) | |
| if __name__ == "__main__": | |
| main() | |