{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# A real collision: impact time, speed and crash pulse\n",
    "\n",
    "RoadData.AI sample data \u00b7 tested 2026-11-26 against the published files (Python 3.12.14, numpy 2.5.4, opencv-python 5.1.0, pyarrow 18.1.0). Outputs below are from that run."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The collision samples in **Real Driving Events and Scenarios** pair two minutes of road video with `route.json` (10 Hz GNSS + IMU records, the ~100 Hz crash pulse) and `velocity.csv` (speed per video frame). Times are seconds from the start of `road.mp4`.\n",
    "\n",
    "Run `01_download_sample.py real-driving-events-and-scenarios` first."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "East New York, Brooklyn, NY 2026-10-07\n",
      "t_s: seconds from the start of road.mp4 (negative = before it).\n",
      "x_m, y_m: metres east and north of the car's position at t_s = 0, in a local east-north-up plane (WGS 84); z_m: metres of GNSS altitude above the first good fix at or after t_s = 0.\n"
     ]
    }
   ],
   "source": [
    "import csv, json\n",
    "d = './real-driving-events-and-scenarios/events/collisions/brooklyn'\n",
    "rec = json.load(open(f'{d}/route.json'))\n",
    "info = next(r for r in rec if r['type'] == 'sample_info')\n",
    "print(info['neighbourhood'], info['date'])\n",
    "print(info['time']); print(info['position'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "{'sample_info': 1, 'accel': 94, 'gyro': 329, 'gps': 32, 'gnss_imu': 1200, 'crash_pulse': 1}\n"
     ]
    }
   ],
   "source": [
    "types = {}\n",
    "for r in rec: types[r['type']] = types.get(r['type'], 0) + 1\n",
    "print(types)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Find the impact on the video's clock from `velocity.csv`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "impact at video time 22.00 s\n",
      "speed 3 s before: 23.1 mph, at impact: 22.9 mph\n"
     ]
    }
   ],
   "source": [
    "v = list(csv.DictReader(open(f'{d}/velocity.csv', newline='')))\n",
    "imp = min(v, key=lambda r: abs(float(r['seconds_from_impact'])))\n",
    "before = min(v, key=lambda r: abs(float(r['seconds_from_impact']) + 3.0))\n",
    "print(f\"impact at video time {float(imp['video_time_s']):.2f} s\")\n",
    "print(f\"speed 3 s before: {float(before['fused_speed_mph']):.1f} mph, at impact: {float(imp['fused_speed_mph']):.1f} mph\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The crash pulse and its integration (peak g, duration, change in speed)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "units: accel m/s^2 gravity-removed; gyro rad/s; gravity m/s^2; t s from the start of the road video\n",
      "peak_horizontal_g    1.934\n",
      "pulse_duration_s     0.126\n",
      "delta_v_low_mph      1.78\n",
      "delta_v_high_mph     1.87\n",
      "shape                impulse\n"
     ]
    }
   ],
   "source": [
    "p = next(r for r in rec if r['type'] == 'crash_pulse')\n",
    "print('units:', p['units'])\n",
    "ig = p['integration']\n",
    "for k in ('peak_horizontal_g', 'pulse_duration_s', 'delta_v_low_mph', 'delta_v_high_mph', 'shape'):\n",
    "    print(f'{k:20s} {ig[k]}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.12.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}