{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# US Urban Driving Events: a clip next to its motion data\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": [
    "Each clip in **US Urban Driving Events** comes with its telemetry: gyroscope, accelerometer and satellite-positioning samples, timed in seconds from the clip start on the video's clock. A sample's video frame is `round(t \u00d7 fps)`.\n",
    "\n",
    "Run `01_download_sample.py us-urban-driving-events` first, from the same folder as this notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "54 clips; columns: file_name, clip_uid, label, label_display, label_source, device_trigger, cloud_event_type, school_bus_stop_arm_out, region, month, weather, time_of_day ...\n"
     ]
    }
   ],
   "source": [
    "import csv, json\n",
    "import cv2\n",
    "root = './us-urban-driving-events'\n",
    "rows = list(csv.DictReader(open(f'{root}/data/train/metadata.csv', newline='')))\n",
    "print(len(rows), 'clips; columns:', ', '.join(list(rows[0])[:12]), '...')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pick a clip and read its metadata row: label, place, conditions, video properties and the measured video-to-sensor clock offset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "label_display          Moped / motor scooter\n",
      "region                 Orlando\n",
      "time_of_day            dusk\n",
      "weather                clear\n",
      "width                  1280\n",
      "height                 720\n",
      "codec                  h264\n",
      "fps                    29.67\n",
      "duration_s             6.167\n",
      "video_sensor_offset_s  0.48\n"
     ]
    }
   ],
   "source": [
    "uid = 'rd_3f22a1f07655'\n",
    "row = next(r for r in rows if r['clip_uid'] == uid)\n",
    "for k in ('label_display', 'region', 'time_of_day', 'weather', 'width', 'height', 'codec', 'fps', 'duration_s', 'video_sensor_offset_s'):\n",
    "    print(f'{k:22s} {row[k]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Open the telemetry and the video."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "video: 186 frames at 30.00 fps\n",
      "gyro: 9 samples, first: {'t': 0.481, 'x': 0.05, 'y': 0.02, 'z': -0.01, 'unit': 'deg/s'}\n",
      "accel: 1 samples, first: {'t': 0.571, 'x': 0, 'y': -0.01, 'z': -0.01, 'unit': 'g'}\n",
      "gps_motion: 2 samples, first: {'t': 0.783, 'speed_mph': 1, 'heading_deg': 178.4, 'distance_m': 0.92}\n"
     ]
    }
   ],
   "source": [
    "tel = json.load(open(f'{root}/telemetry/{uid}.json'))\n",
    "cap = cv2.VideoCapture(f\"{root}/data/train/{row['file_name']}\")\n",
    "fps = cap.get(cv2.CAP_PROP_FPS); n = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))\n",
    "print(f'video: {n} frames at {fps:.2f} fps')\n",
    "for kind in ('gyro', 'accel', 'gps_motion'):\n",
    "    print(f'{kind}: {len(tel[kind])} samples, first: {tel[kind][0]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Put every satellite-positioning sample on its frame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "t  0.783 s -> frame   23   speed 1 mph   heading 178.4 deg\n",
      "t  1.783 s -> frame   53   speed 0 mph   heading None deg\n"
     ]
    }
   ],
   "source": [
    "for s in tel['gps_motion']:\n",
    "    print(f\"t {s['t']:6.3f} s -> frame {min(n - 1, round(s['t'] * fps)):4d}   speed {s['speed_mph']} mph   heading {s['heading_deg']} deg\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.12.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}