{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":59156,"databundleVersionId":13719397,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Display a short video from tracking parquet data\nimport os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.animation as animation\nfrom IPython.display import HTML, display\n\n# Paths\nDATA_DIR = \"/kaggle/input/MABe-mouse-behavior-detection/\"\nTRAIN_CSV = os.path.join(DATA_DIR, \"train.csv\")\nTRAIN_TRACKING = os.path.join(DATA_DIR, \"train_tracking\")\n\n# Load metadata\ntrain_df = pd.read_csv(TRAIN_CSV)\n\n# Pick a sample video\nsample_row = train_df.iloc[0]\nlab_id = sample_row['lab_id']\nvideo_id = sample_row['video_id']\nparquet_path = os.path.join(TRAIN_TRACKING, lab_id, f\"{video_id}.parquet\")\n\nprint(f\"Loading tracking data: {parquet_path}\")\ndf = pd.read_parquet(parquet_path)\n\n# Show first 100 frames\nframes = sorted(df['video_frame'].unique())[:100]\nbodyparts = df['bodypart'].unique()\nmice = df['mouse_id'].unique()\n\n# Prepare figure\nfig, ax = plt.subplots(figsize=(6,6))\ncolors = plt.cm.tab10.colors\n\ndef animate(i):\n    ax.clear()\n    frame = frames[i]\n    ax.set_title(f\"Frame {frame}\")\n    ax.set_xlim(df['x'].min(), df['x'].max())\n    ax.set_ylim(df['y'].min(), df['y'].max())\n    for j, mouse in enumerate(mice):\n        d = df[(df['video_frame']==frame) & (df['mouse_id']==mouse)]\n        ax.scatter(d['x'], d['y'], label=f\"Mouse {mouse}\", color=colors[j%len(colors)])\n    ax.legend()\n\nani = animation.FuncAnimation(fig, animate, frames=len(frames), interval=50)\nplt.close(fig)\ndisplay(HTML(ani.to_jshtml()))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-09-22T04:43:50.461509Z","iopub.execute_input":"2025-09-22T04:43:50.461788Z","iopub.status.idle":"2025-09-22T04:44:13.511828Z","shell.execute_reply.started":"2025-09-22T04:43:50.461759Z","shell.execute_reply":"2025-09-22T04:44:13.510570Z"}},"outputs":[],"execution_count":null}]}