{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "830e91cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "from tqdm import tqdm\n",
    "import numpy as np\n",
    "metadata_path = \"../droid/metadata_all_new.jsonl\"\n",
    "intrinsics_save_path = \"../droid/intrinsics_custom.json\"\n",
    "original_intrinsics_path = \"../droid/intrinsics.json\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2a80d0ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "# read the original intrinsics\n",
    "with open(original_intrinsics_path, \"r\") as f:\n",
    "    original_intrinsics = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dfde9cd1",
   "metadata": {},
   "outputs": [],
   "source": [
    "for episode_id, episode_meta in original_intrinsics.items():\n",
    "    for cam_serial, cam_meta in episode_meta.items():\n",
    "        if cam_meta['width'] != 1280 and cam_meta['width'] != 672 and cam_meta['width'] != 0:\n",
    "            print(f\"episode {episode_id} has a camera with width {cam_meta['width']}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c3b56e5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def parse_intrinsics(intrinsics):\n",
    "    if intrinsics is None:\n",
    "        return [0.0, 0.0, 0.0, 0.0]\n",
    "    return [intrinsics[0][0], intrinsics[0][2], intrinsics[1][1], intrinsics[1][2]]\n",
    "\n",
    "def pick_size_from_K(K, candidates=[(1280,720),(672,376)]):\n",
    "    if K is None:\n",
    "        return (0, 0)\n",
    "    fx, fy, cx, cy = K[0][0], K[1][1], K[0][2], K[1][2]\n",
    "    best = (1280,720)\n",
    "    best_score = float('inf')\n",
    "    for W,H in candidates:\n",
    "        # must lie inside image\n",
    "        if not (0 <= cx < W and 0 <= cy < H):\n",
    "            continue\n",
    "        # prefer principal point near the image center (scale-invariant distance)\n",
    "        dx = (cx - (W-1)/2)/W\n",
    "        dy = (cy - (H-1)/2)/H\n",
    "        score = dx*dx + dy*dy\n",
    "        if score < best_score:\n",
    "            best_score, best = score, (W,H)\n",
    "    # as a tiebreaker (optional), prefer the one where fx,fy roughly match expected scale\n",
    "    return best\n",
    "    \n",
    "def get_size(original_intrinsics, episode_meta, cam_name, key):\n",
    "    size_in_original = original_intrinsics[episode_meta[\"uuid\"]][episode_meta[cam_name]][key]\n",
    "    if size_in_original != 0:\n",
    "        return size_in_original\n",
    "    else:\n",
    "        if key == 'width':\n",
    "            return 1280\n",
    "        else:\n",
    "            return 720"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9be9f5d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "intrinsics_dict = {}\n",
    "with open(metadata_path, 'r') as f:\n",
    "    for line in f:\n",
    "        data = json.loads(line)\n",
    "        episode_meta = data[\n",
    "            list(data.keys())[0]\n",
    "        ]\n",
    "        if episode_meta is not None:\n",
    "            intrinsics_dict[episode_meta[\"uuid\"]] = {\n",
    "                episode_meta[\"ext1_cam_serial\"]: {\n",
    "                    \"cameraMatrix\": parse_intrinsics(episode_meta.get(\"ext1_cam_intrinsics\", None)),\n",
    "                    \"distCoeffs\": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\n",
    "                    \"width\": pick_size_from_K(episode_meta.get(\"ext1_cam_intrinsics\", None))[0],\n",
    "                    \"height\": pick_size_from_K(episode_meta.get(\"ext1_cam_intrinsics\", None))[1],\n",
    "                },\n",
    "                episode_meta[\"ext2_cam_serial\"]: {\n",
    "                    \"cameraMatrix\": parse_intrinsics(episode_meta.get(\"ext2_cam_intrinsics\", None)),\n",
    "                    \"distCoeffs\": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\n",
    "                    \"width\": pick_size_from_K(episode_meta.get(\"ext2_cam_intrinsics\", None))[0],\n",
    "                    \"height\": pick_size_from_K(episode_meta.get(\"ext2_cam_intrinsics\", None))[1],\n",
    "                },\n",
    "                episode_meta[\"wrist_cam_serial\"]: {\n",
    "                    \"cameraMatrix\": parse_intrinsics(episode_meta.get(\"wrist_cam_intrinsics\", None)),\n",
    "                    \"distCoeffs\": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\n",
    "                    \"width\": pick_size_from_K(episode_meta.get(\"wrist_cam_intrinsics\", None))[0],\n",
    "                    \"height\": pick_size_from_K(episode_meta.get(\"wrist_cam_intrinsics\", None))[1],\n",
    "                },\n",
    "            }\n",
    "\n",
    "with open(intrinsics_save_path, \"w\") as f:\n",
    "    json.dump(intrinsics_dict, f, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "19769054",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number of episodes in the original intrinsics:  72468\n",
      "number of episodes in the custom intrinsics:  74165\n",
      "number of episodes in the union:  74305\n"
     ]
    }
   ],
   "source": [
    "print(\"number of episodes in the original intrinsics: \", len(original_intrinsics))\n",
    "print(\"number of episodes in the custom intrinsics: \", len(intrinsics_dict))\n",
    "\n",
    "union_episode_ids = set(original_intrinsics.keys()) | set(intrinsics_dict.keys())\n",
    "print(\"number of episodes in the union: \", len(union_episode_ids))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4c2ae1b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "original_intrinsics_copy = original_intrinsics.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "852b0c66",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  5%|▍         | 3445/74305 [00:00<00:02, 34448.14it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode TRI+749baf62+2023-09-05-17h-04m-26s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-14h-14m-56s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-15h-54m-45s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-08m-10s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-47m-08s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-06m-43s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-14h-34m-23s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-05m-47s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-10m-40s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-15h-47m-53s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-38m-27s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-04m-49s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-42m-44s is not in the custom intrinsics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  9%|▉         | 6890/74305 [00:00<00:02, 32916.10it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode TRI+749baf62+2023-09-05-16h-14m-55s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-14m-07s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-01m-47s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-50m-19s is not in the custom intrinsics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 14%|█▍        | 10296/74305 [00:00<00:01, 33424.30it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode tri+a8754edb+2023-10-12-18h-18m-19s is not in the custom intrinsics\n",
      "episode tri+a8754edb+2023-10-12-18h-12m-39s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-53m-55s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-11m-03s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-17m-06s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-13m-02s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-35m-39s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-49m-52s is not in the custom intrinsics\n",
      "episode tri+7dfa2da3+2023-10-12-13h-13m-53s is not in the custom intrinsics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 18%|█▊        | 13643/74305 [00:00<00:01, 32870.08it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode TRI+749baf62+2023-09-06-13h-37m-34s is not in the custom intrinsics\n",
      "episode tri+a8754edb+2023-10-12-18h-13m-33s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-39m-52s is not in the custom intrinsics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 23%|██▎       | 16933/74305 [00:00<00:01, 32678.70it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode tri+7dfa2da3+2023-10-12-13h-19m-47s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-52m-19s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-52m-40s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-56m-59s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-23m-41s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-03m-55s is not in the custom intrinsics\n",
      "episode tri+a8754edb+2023-10-12-18h-08m-57s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-03m-53s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-59m-24s is not in the custom intrinsics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 27%|██▋       | 20203/74305 [00:00<00:01, 32541.73it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode tri+7dfa2da3+2023-10-12-13h-29m-37s is not in the custom intrinsics\n",
      "episode tri+7dfa2da3+2023-10-12-12h-59m-59s is not in the custom intrinsics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 36%|███▌      | 26929/74305 [00:00<00:01, 32468.11it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode TRI+749baf62+2023-09-06-13h-53m-13s is not in the custom intrinsics\n",
      "episode tri+a8754edb+2023-10-12-18h-17m-08s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-43m-55s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-14h-31m-34s is not in the custom intrinsics\n",
      "episode tri+a8754edb+2023-10-12-18h-11m-27s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-12h-58m-44s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-15h-59m-18s is not in the custom intrinsics\n",
      "episode tri+a8754edb+2023-10-12-18h-19m-18s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-47m-55s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-14h-16m-10s is not in the custom intrinsics\n",
      "episode tri+a8754edb+2023-10-12-18h-09m-23s is not in the custom intrinsics\n",
      "episode tri+7dfa2da3+2023-10-12-13h-23m-22s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-18m-43s is not in the custom intrinsics\n",
      "episode tri+a8754edb+2023-10-12-18h-20m-16s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-33m-57s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-01m-17s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-56m-00s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-14m-42s is not in the custom intrinsics\n",
      "episode tri+7dfa2da3+2023-10-12-13h-21m-02s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-35m-54s is not in the custom intrinsics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 45%|████▍     | 33334/74305 [00:01<00:01, 31344.98it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode TRI+749baf62+2023-09-05-16h-44m-06s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-19m-29s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-15h-45m-47s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-53m-10s is not in the custom intrinsics\n",
      "episode tri+7dfa2da3+2023-10-12-13h-21m-36s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-15h-57m-05s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-10m-14s is not in the custom intrinsics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 54%|█████▎    | 39772/74305 [00:01<00:01, 30835.68it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode TRI+749baf62+2023-09-06-13h-29m-42s is not in the custom intrinsics\n",
      "episode tri+7dfa2da3+2023-10-12-12h-20m-30s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-58m-41s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-05m-32s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-14h-30m-09s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-50m-46s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-29m-04s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-15h-50m-53s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-33m-51s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-38m-29s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-14h-24m-26s is not in the custom intrinsics\n",
      "episode tri+a8754edb+2023-10-12-18h-14m-32s is not in the custom intrinsics\n",
      "episode tri+a8754edb+2023-10-12-18h-09m-59s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-16m-48s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-47m-29s is not in the custom intrinsics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 63%|██████▎   | 46554/74305 [00:01<00:00, 32450.10it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode tri+7dfa2da3+2023-10-12-12h-59m-16s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-41m-34s is not in the custom intrinsics\n",
      "episode tri+7dfa2da3+2023-10-12-13h-14m-15s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-15m-57s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-27m-01s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-16m-54s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-15m-02s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-14h-31m-57s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-57m-58s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-59m-05s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-54m-18s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-11m-10s is not in the custom intrinsics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 72%|███████▏  | 53273/74305 [00:01<00:00, 32631.22it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode tri+7dfa2da3+2023-10-12-13h-26m-09s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-14h-32m-57s is not in the custom intrinsics\n",
      "episode tri+7dfa2da3+2023-10-12-13h-33m-28s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-11m-25s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-45m-54s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-49m-38s is not in the custom intrinsics\n",
      "episode tri+7dfa2da3+2023-10-12-13h-17m-46s is not in the custom intrinsics\n",
      "episode tri+a8754edb+2023-10-12-18h-17m-58s is not in the custom intrinsics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 76%|███████▌  | 56546/74305 [00:01<00:00, 31995.10it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode tri+7dfa2da3+2023-10-12-13h-42m-52s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-14h-33m-30s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-08m-55s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-09m-26s is not in the custom intrinsics\n",
      "episode tri+7dfa2da3+2023-10-12-13h-38m-16s is not in the custom intrinsics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 80%|████████  | 59786/74305 [00:01<00:00, 32111.51it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode tri+7dfa2da3+2023-10-12-12h-19m-54s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-42m-42s is not in the custom intrinsics\n",
      "episode tri+7dfa2da3+2023-10-12-13h-32m-05s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-34m-24s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-14h-34m-48s is not in the custom intrinsics\n",
      "episode tri+a8754edb+2023-10-12-18h-15m-17s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-14h-00m-11s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-36m-53s is not in the custom intrinsics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 85%|████████▍ | 63004/74305 [00:01<00:00, 31561.06it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode TRI+749baf62+2023-09-06-13h-55m-28s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-30m-24s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-12m-53s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-19m-13s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-05m-02s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-15h-52m-24s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-28m-01s is not in the custom intrinsics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 93%|█████████▎| 69281/74305 [00:02<00:00, 30363.84it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode TRI+749baf62+2023-09-06-13h-55m-06s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-56m-42s is not in the custom intrinsics\n",
      "episode tri+7dfa2da3+2023-10-12-13h-24m-22s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-19m-21s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-20m-51s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-14h-31m-09s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-15h-54m-21s is not in the custom intrinsics\n",
      "episode tri+52ca9b6a+2023-10-16-15h-05m-57s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-52m-36s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-13h-39m-54s is not in the custom intrinsics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 74305/74305 [00:02<00:00, 31616.56it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "episode TRI+749baf62+2023-09-05-17h-20m-26s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-01m-25s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-42m-11s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-17h-15m-32s is not in the custom intrinsics\n",
      "episode tri+7dfa2da3+2023-10-12-12h-14m-10s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-18m-27s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-05-16h-09m-02s is not in the custom intrinsics\n",
      "episode TRI+749baf62+2023-09-06-14h-33m-55s is not in the custom intrinsics\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# add missing episodes to original \n",
    "for episode_id in tqdm(union_episode_ids):\n",
    "    if episode_id not in intrinsics_dict:\n",
    "        print(f\"episode {episode_id} is not in the custom intrinsics\")\n",
    "        continue\n",
    "    if episode_id not in original_intrinsics:\n",
    "        original_intrinsics[episode_id] = intrinsics_dict[episode_id]\n",
    "        continue\n",
    "    \n",
    "    for cam_serial, cam_meta in intrinsics_dict[episode_id].items():\n",
    "        if cam_serial not in original_intrinsics[episode_id]:\n",
    "            original_intrinsics[episode_id][cam_serial] = cam_meta\n",
    "            pass\n",
    "        for key in cam_meta.keys():\n",
    "            if (np.array(cam_meta[key]) - np.array(original_intrinsics[episode_id][cam_serial][key])).sum() > 2:\n",
    "                if not np.array(original_intrinsics[episode_id][cam_serial][key]).sum() == 0:\n",
    "                    print(f\"episode {episode_id} cam {cam_serial} {key} is not the same in the original and custom intrinsics\")\n",
    "                    pass\n",
    "                original_intrinsics[episode_id][cam_serial][key] = cam_meta[key]\n",
    "                pass\n",
    "\n",
    "with open(original_intrinsics_path.replace(\".json\", \"_updated.json\"), \"w\") as f:\n",
    "    json.dump(original_intrinsics, f, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3658f34d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1280, 0, 672}\n",
      "{720, 0, 376}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "with open(\"/home/junjieye/workspace/sim-evals/submodules/droid/intrinsics_updated.json\", \"r\") as f:\n",
    "    intrinsics = json.load(f)\n",
    "\n",
    "cam_width=[]\n",
    "cam_height=[]\n",
    "for episode_id, episode_meta in intrinsics.items():\n",
    "    for cam_serial, cam_meta in episode_meta.items():\n",
    "        cam_width.append(cam_meta['width'])\n",
    "        cam_height.append(cam_meta['height'])\n",
    "\n",
    "print(set(cam_width))\n",
    "print(set(cam_height))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a50ca6d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "local_dataset_path = \"/home/junjieye/datasets/droid_raw/1.0.1\"\n",
    "test_episode_id = \"AUTOLab/success/2023-07-08/Sat_Jul__8_10:55:07_2023\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "569ce130",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "\"Unable to synchronously open object (object 'joint_positions' doesn't exist)\"",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[25], line 16\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mos\u001b[39;00m\n\u001b[1;32m      5\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m h5py\u001b[38;5;241m.\u001b[39mFile(os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(local_dataset_path, test_episode_id, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtrajectory.h5\u001b[39m\u001b[38;5;124m\"\u001b[39m), \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m f:\n\u001b[1;32m      6\u001b[0m     \u001b[38;5;66;03m# print(f.keys())\u001b[39;00m\n\u001b[1;32m      7\u001b[0m     \u001b[38;5;66;03m# print(f[\"action\"].keys())\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m     13\u001b[0m     \u001b[38;5;66;03m# print(f[\"observation\"][\"robot_state\"]['joint_positions'])\u001b[39;00m\n\u001b[1;32m     14\u001b[0m     \u001b[38;5;66;03m# print(f[\"observation\"][\"robot_state\"]['gripper_position'])\u001b[39;00m\n\u001b[0;32m---> 16\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[43mf\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mobservation\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mjoint_positions\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m[()])\n\u001b[1;32m     17\u001b[0m     \u001b[38;5;28mprint\u001b[39m(f[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mobservation\u001b[39m\u001b[38;5;124m\"\u001b[39m][\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrobot_state\u001b[39m\u001b[38;5;124m\"\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mjoint_positions\u001b[39m\u001b[38;5;124m'\u001b[39m][()])\n\u001b[1;32m     18\u001b[0m     \u001b[38;5;28mprint\u001b[39m(f[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mobservation\u001b[39m\u001b[38;5;124m\"\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mjoint_positions\u001b[39m\u001b[38;5;124m'\u001b[39m][()] \u001b[38;5;241m-\u001b[39m f[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mobservation\u001b[39m\u001b[38;5;124m\"\u001b[39m][\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrobot_state\u001b[39m\u001b[38;5;124m\"\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mjoint_positions\u001b[39m\u001b[38;5;124m'\u001b[39m][()])\n",
      "File \u001b[0;32mh5py/_objects.pyx:56\u001b[0m, in \u001b[0;36mh5py._objects.with_phil.wrapper\u001b[0;34m()\u001b[0m\n",
      "File \u001b[0;32mh5py/_objects.pyx:57\u001b[0m, in \u001b[0;36mh5py._objects.with_phil.wrapper\u001b[0;34m()\u001b[0m\n",
      "File \u001b[0;32m~/anaconda3/envs/robocasa/lib/python3.10/site-packages/h5py/_hl/group.py:360\u001b[0m, in \u001b[0;36mGroup.__getitem__\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m    358\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid HDF5 object reference\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m    359\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(name, (\u001b[38;5;28mbytes\u001b[39m, \u001b[38;5;28mstr\u001b[39m)):\n\u001b[0;32m--> 360\u001b[0m     oid \u001b[38;5;241m=\u001b[39m \u001b[43mh5o\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mopen\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mid\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_e\u001b[49m\u001b[43m(\u001b[49m\u001b[43mname\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlapl\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_lapl\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    361\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    362\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAccessing a group is done with bytes or str, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    363\u001b[0m                     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnot \u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mformat(\u001b[38;5;28mtype\u001b[39m(name)))\n",
      "File \u001b[0;32mh5py/_objects.pyx:56\u001b[0m, in \u001b[0;36mh5py._objects.with_phil.wrapper\u001b[0;34m()\u001b[0m\n",
      "File \u001b[0;32mh5py/_objects.pyx:57\u001b[0m, in \u001b[0;36mh5py._objects.with_phil.wrapper\u001b[0;34m()\u001b[0m\n",
      "File \u001b[0;32mh5py/h5o.pyx:257\u001b[0m, in \u001b[0;36mh5py.h5o.open\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mKeyError\u001b[0m: \"Unable to synchronously open object (object 'joint_positions' doesn't exist)\""
     ]
    }
   ],
   "source": [
    "import h5py\n",
    "import numpy as np\n",
    "import os\n",
    "\n",
    "with h5py.File(os.path.join(local_dataset_path, test_episode_id, \"trajectory.h5\"), \"r\") as f:\n",
    "    # print(f.keys())\n",
    "    # print(f[\"action\"].keys())\n",
    "    # print(f[\"observation\"].keys())\n",
    "    # print(f['action']['robot_state']['joint_positions'][()])\n",
    "    # print(f[\"observation\"][\"camera_type\"]['18026681'][()])\n",
    "    # print(f[\"observation\"][\"controller_info\"].items())\n",
    "    # print(f[\"observation\"]['joint_positions'])\n",
    "    # print(f[\"observation\"][\"robot_state\"]['joint_positions'])\n",
    "    # print(f[\"observation\"][\"robot_state\"]['gripper_position'])\n",
    "    \n",
    "    print(f[\"observation\"]['joint_positions'][()])\n",
    "    print(f[\"observation\"][\"robot_state\"]['joint_positions'][()])\n",
    "    print(f[\"observation\"]['joint_positions'][()] - f[\"observation\"][\"robot_state\"]['joint_positions'][()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b75e206",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51014d80",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "robocasa",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
