# Input and output contracts Paths below describe relative file locations and examples. Use command-line arguments to specify input and output files. Original inputs are read only; existing output files are not overwritten. Keep the image view used for pixel centers and bounding-box corners identical to the view used for their calibration. | Stage | Existing input to supply | Output and next consumer | | --- | --- | --- | | Coordinate conversion | One named `data/raw/SCENE_raw_detection_data_N.csv`, actual scene JSON and matching `videoN` | Original fields plus `Source_ID,x_center,y_center` in meters. Input remains one observed frame/vehicle per row. | | Lane join | Metric output plus a source-frame annotation CSV containing `Frame,Vehicle_ID,Lane number,Direction` | Metric rows with `Lane number,Direction`, joined one-to-one using the original source `Frame,Vehicle_ID` keys. | | Reconstruction | Metric centers with lanes/direction, actual FPS and explicit offsets | One row for every reconstructed source frame/vehicle, plus separate rejected rows and per-vehicle screening measurements. | | Length estimation | Original six-column source detections and same scene/source calibration | One row per original vehicle: fixed length, bbox lateral diagnostic, frame counts and quality. | | Length joining | Reconstructed trajectories with lineage and a length table; otherwise an existing verified ID mapping | Basic trajectories with one fixed `Vehicle_Length_m` per final vehicle. QC fields stay in the separate length table. | | Risk | Released Basic with valid lengths, lanes, direction and Time_s or Delta_t_s | Frame-wise TTC/DRAC, event-frame PET, risk target and risk level. Delta_t_s input additionally receives Time_s and Time_Reference_ID. | | Pair generation | One released observation-group Basic CSV, or one source-labelled reconstructed Basic CSV, with meter positions and velocities | Frame-sorted **directed** candidate pairs and a JSON runtime summary. | | Balanced fitting samples | Full frame-sorted candidate pairs | `Frame,EV_Vehicle_ID,POV_Vehicle_ID,x_local,y_local,v_rel,cangle,round_v,Sample_Group_ID`; the group summary records sample counts and discarded remainders. The fitting subset contains the listed fields. | | Parameter inference | Balanced fitting subset; default minimum and maximum used per category/speed key are 50000 | Parameter CSV with one accepted result per category/speed group; a separate JSON records group status and sample counts. The run stops if a requested group fails its acceptance checks. | | Interaction annotation | Original Basic, **full** candidate pairs and completed scenario/source-appropriate PR parameter CSV | Basic fields plus pair-relative states, PR, interaction flag and type; a repeated ego/frame can represent several targets. | ## Required fields - Raw detections: `Frame,Vehicle_ID,x_center_px,y_center_px,width_px,height_px`. Integers for Frame and ID; pixel units for centers and box dimensions; unique source-local `(Frame,Vehicle_ID)`. Each manifest row binds this local key to its scene/source. - Lane annotation: `Frame,Vehicle_ID,Lane number,Direction`. Keys must refer to the original source frames/IDs before reconstruction; one record per observed key, complete coverage; direction is `Left` or `Right`. Optional Source_ID must agree with the metric source. Apply the corresponding identifier mapping when transferring processed Basic annotations to the original detection keys. - Reconstruction output lineage: `Source_ID,Original_Vehicle_ID,Original_Frame,Frame,Ego_Vehicle_ID`. `Time_s` is seconds from the input recording origin plus explicit time offset; `Delta_t_s` is blank at each track start and 1/FPS thereafter. The example commands use zero offsets and therefore retain a source-local reference. - Kinematics: `x_center,y_center` m; `v_x,v_y` m/s; `a_x,a_y` m/s². The reconstruction outputs smoothed positions under x_center/y_center. Positions use road-aligned local metric coordinates obtained by master-image registration and measured scale conversion. - Length output: `Source_ID,Vehicle_ID,Vehicle_Length_m,BBox_Lateral_Extent_Q25_m,Input_Frames,Valid_Before_IQR,Retained_Frames,Removed_By_IQR,Quality`. Vehicle_Length_m is missing if fewer than five frames survive. Quality is good at >=20 retained frames, limited at 5–19, insufficient otherwise. - Optional processed-ID mapping: `Ego_Vehicle_ID,Source_ID,Original_Vehicle_ID`; both final ID and source/original identity map one-to-one. Compress frame-wise mapping rows only after verifying consistency. - Required pair fields: `Frame,EV_Vehicle_ID,POV_Vehicle_ID,x_local,y_local,v_rel,cangle`. x_local/y_local are local X/Y components in meters, v_rel is a positive scalar in m/s, and cangle is **binary**, not degrees. The generator also writes signed `cangle_deg`, `Distance_m`, and `Angle_Defined` fields. - PR parameters: `cangle,round_v,rx_plus,rx_minus,ry_plus,ry_minus,bx_plus,bx_minus,by_plus,by_minus`. Category/speed key unique; all eight parameters positive. Additional fit-status fields are allowed. ## Calibration fields and source matching Each measured JSON includes `master_source`, the two-coordinate `origin_px`, `rotation_rad=0`, two measured `reference_segments`, and `sources`. Each non-master source contains matched Nx2 fitting arrays `src_points` and `master_points`. The master source defines the reference view and uses the identity source-to-master mapping. Use the measured files in `calibrations/` for the supplied recordings. Field meanings and units are detailed in `CALIBRATION.md`. The sixteen filename/source mappings and frame rates are listed in `configs/source_manifest.csv`. Process one source per run. For combined sources, provide established offsets with `--id-offset`, `--frame-offset`, and `--time-offset`. Use source-frame annotation keys for the lane join; apply any final-ID mapping only at the appropriate later stage. Measured `--merge-x-m` and `--diverge-x-m` values must use the same metric frame as the trajectories. ## Error behavior and outputs Invalid numeric values, duplicate identity keys, source disagreements and missing required fields fail explicitly. Invalid frame-level bounding boxes are excluded and counted by the length estimator. A length table can contain a vehicle with a missing estimate, but a missing vehicle mapping fails. The main risk example uses the existing per-vehicle lengths. Fitting samples use relative speeds up to 30 m/s and complete sparse blocks; runtime summaries record excluded observations and block remainders. All supplied candidate pairs remain eligible for final annotation, including those excluded from **fitting** by the speed cutoff. Parameter lookup selects the nearest fitted relative-speed bin within each angle category, with no default maximum speed difference, including at the fitted-range boundaries. Set `--speed-tolerance` to require a bin within a specified speed difference; an unmatched lookup raises an error. ## Released Basic input and time references The primary downstream input is the released observation-group Basic CSV, with `Ego_Vehicle_ID,Frame,Delta_t_s,x_center,y_center,v_x,v_y,a_x,a_y,Direction,Lane number,Dist_to_Merge_m,Dist_to_Diverge_m,Vehicle_Length_m`. This entry point uses the lane and direction fields already present in the Basic file. The pair generator uses metric centers and velocities directly; annotation uses the existing processed IDs, lane and direction fields. For risk input without `Time_s`, positive `Delta_t_s` values define the elapsed time into the corresponding processed frame. Values for the same frame must agree. Each vehicle must have consecutive frames and a positive interval after its first record. The time builder accumulates these intervals across connected frames. A missing incoming interval or a gap in observed frames starts a new time reference at zero. `Time_Reference_ID` identifies that reference, and each vehicle must belong to one reference. This represents local sequence time without assigning an unobserved interval between recordings. Existing `Time_s` input retains its supplied reference; explicit `--fps` retains the constant-rate option. The Basic files use processed `Ego_Vehicle_ID,Frame`; raw detections use source-local `Vehicle_ID,Frame`. A source-frame annotation CSV for reconstruction is keyed by the original detection records. Transferring Basic labels uses the corresponding source/frame/vehicle mapping.