{"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":13874099,"sourceType":"competition"}],"dockerImageVersionId":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport duckdb","metadata":{"_uuid":"c8d73a78-ad23-47b8-8255-98bb4dcd0660","_cell_guid":"0062cca4-0000-4799-87e8-e8a0c5d0bf3d","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-10-20T07:22:27.247462Z","iopub.execute_input":"2025-10-20T07:22:27.247830Z","iopub.status.idle":"2025-10-20T07:22:27.253790Z","shell.execute_reply.started":"2025-10-20T07:22:27.247807Z","shell.execute_reply":"2025-10-20T07:22:27.252526Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"con = duckdb.connect(database = \":memory:\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.279579Z","iopub.execute_input":"2025-10-20T07:22:27.280521Z","iopub.status.idle":"2025-10-20T07:22:27.462563Z","shell.execute_reply.started":"2025-10-20T07:22:27.280489Z","shell.execute_reply":"2025-10-20T07:22:27.461388Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"con.sql(\"CREATE OR REPLACE TABLE train AS SELECT * FROM read_csv('/kaggle/input/MABe-mouse-behavior-detection/train.csv')\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.466577Z","iopub.execute_input":"2025-10-20T07:22:27.466871Z","iopub.status.idle":"2025-10-20T07:22:27.650401Z","shell.execute_reply.started":"2025-10-20T07:22:27.466849Z","shell.execute_reply":"2025-10-20T07:22:27.649437Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Number of rows\ncon.sql(\"SELECT count(*) FROM train\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.651683Z","iopub.execute_input":"2025-10-20T07:22:27.652142Z","iopub.status.idle":"2025-10-20T07:22:27.665729Z","shell.execute_reply.started":"2025-10-20T07:22:27.652103Z","shell.execute_reply":"2025-10-20T07:22:27.664541Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"con.sql(\"SUMMARIZE train; \").df()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.666894Z","iopub.execute_input":"2025-10-20T07:22:27.667218Z","iopub.status.idle":"2025-10-20T07:22:27.771521Z","shell.execute_reply.started":"2025-10-20T07:22:27.667192Z","shell.execute_reply":"2025-10-20T07:22:27.770243Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"lab_id - The pseudonym of the lab that provided the data. The CalMS21, CRIM13, and MABe22 datasets are copies of publicly available datasets provided as additional training data. Some tracking files in CalMS21 set are largely duplicated-- these were annotated by multiple individuals for different sets of behaviors.","metadata":{}},{"cell_type":"code","source":"# lab_id\ncon.sql(\"SELECT DISTINCT lab_id FROM train\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.774470Z","iopub.execute_input":"2025-10-20T07:22:27.774799Z","iopub.status.idle":"2025-10-20T07:22:27.789573Z","shell.execute_reply.started":"2025-10-20T07:22:27.774775Z","shell.execute_reply":"2025-10-20T07:22:27.788442Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"con.sql(\"SELECT lab_id, COUNT(*) as values FROM train GROUP BY lab_id ORDER BY values DESC;\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.790697Z","iopub.execute_input":"2025-10-20T07:22:27.791258Z","iopub.status.idle":"2025-10-20T07:22:27.814084Z","shell.execute_reply.started":"2025-10-20T07:22:27.791223Z","shell.execute_reply":"2025-10-20T07:22:27.812666Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"video_id - A unique identifier for the video.","metadata":{}},{"cell_type":"code","source":"con.sql(\"SELECT COUNT(video_id) FROM train;\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.815343Z","iopub.execute_input":"2025-10-20T07:22:27.815644Z","iopub.status.idle":"2025-10-20T07:22:27.828501Z","shell.execute_reply.started":"2025-10-20T07:22:27.815618Z","shell.execute_reply":"2025-10-20T07:22:27.827276Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(con.sql(\"SELECT DISTINCT mouse1_strain from train ORDER BY mouse1_strain\"))\nprint(con.sql(\"SELECT DISTINCT mouse2_strain from train ORDER BY mouse2_strain\"))\nprint(con.sql(\"SELECT DISTINCT mouse3_strain from train ORDER BY mouse3_strain\"))\nprint(con.sql(\"SELECT DISTINCT mouse4_strain from train ORDER BY mouse4_strain\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.829449Z","iopub.execute_input":"2025-10-20T07:22:27.829785Z","iopub.status.idle":"2025-10-20T07:22:27.857663Z","shell.execute_reply.started":"2025-10-20T07:22:27.829732Z","shell.execute_reply":"2025-10-20T07:22:27.856333Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"con.sql(\"SELECT mouse1_strain, count(*) AS counts FROM train GROUP BY mouse1_strain ORDER BY counts DESC\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.858897Z","iopub.execute_input":"2025-10-20T07:22:27.859219Z","iopub.status.idle":"2025-10-20T07:22:27.873845Z","shell.execute_reply.started":"2025-10-20T07:22:27.859195Z","shell.execute_reply":"2025-10-20T07:22:27.872371Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### C57Bl/6J      \n![](https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTyCsmJnNL5EMT7podASllF5YX76lOmio-XqKstClgCe8MPVK0RKTEe37-yO5LoUMS_XPWF0FkjSBCgQJLIiHQSHj9HPJyeSzBiLWdYdg)\n\n\n\n### BTBR\n![BTBR](https://media.jax.org/m/57190339fb1108b2/original/002282.png)\n\n\n###  C57Bl/6N  \n![](https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQncuZ1zz9D6Mvg8fQqGkrqN2aebUKL_gFpW0DGIWxqTQSZP7bVcDPS9AS1jUXW2MeHBAP-mBZR1o7eeyg6-uRneAYCAICc5brBK4YxsdI)\n\n### CD1       \n![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQfwBnnnC46c39disTjb2PA0pzNRnPUxom4oQ&s)\n\n\n\n###  C57Bl/6J x Ai148 \n![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRrrFDFTUWNwKf7zppGqCwgQ7JTolG0J_fTNg&s)\n\n###  CD-1 (ICR)       \n![](https://www.inotiv.com/hs-fs/hubfs/resources/model-images/ICR.png?width=350&height=181&name=ICR.png)\n\n### 129/SvEvTac\n![129/SvEvTac](https://assets.taconic.com/is/image/taconic/129s6-inbred-mouse-model-1?ts=1695185333723&dpr=off)\n\n### CFW    \n![](https://criver.widen.net/content/kzrrkzsckz/jpeg/RM-001459-white-mouse.jpeg?position=c&color=ffffffff&quality=80&u=dxbf0u)\n\n### BALB/c\n![](https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQIZj_eGKVrLhNpGjfClEdVEgCYzgNGPWJc4jX7-JhClFXI-Igb3Ov7TDfu0N2Vsmvmyg5ITH5WMe1xhgY6c3IvkyPklIuRn8M4LNBZMZk)","metadata":{}},{"cell_type":"markdown","source":"frames per second","metadata":{}},{"cell_type":"code","source":"con.sql(\"SELECT frames_per_second, COUNT(*) AS counts FROM train GROUP BY frames_per_second ORDER BY counts DESC\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.875016Z","iopub.execute_input":"2025-10-20T07:22:27.875443Z","iopub.status.idle":"2025-10-20T07:22:27.897982Z","shell.execute_reply.started":"2025-10-20T07:22:27.875410Z","shell.execute_reply":"2025-10-20T07:22:27.896448Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"con.sql(\"SELECT video_duration_sec, COUNT(*) AS counts FROM train GROUP BY video_duration_sec ORDER BY counts DESC\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.899376Z","iopub.execute_input":"2025-10-20T07:22:27.899750Z","iopub.status.idle":"2025-10-20T07:22:27.914278Z","shell.execute_reply.started":"2025-10-20T07:22:27.899716Z","shell.execute_reply":"2025-10-20T07:22:27.913151Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"arena","metadata":{}},{"cell_type":"code","source":"con.sql(\"SELECT arena_shape, COUNT(*) AS counts  FROM train GROUP BY arena_shape ORDER BY counts DESC\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.915381Z","iopub.execute_input":"2025-10-20T07:22:27.915666Z","iopub.status.idle":"2025-10-20T07:22:27.934108Z","shell.execute_reply.started":"2025-10-20T07:22:27.915642Z","shell.execute_reply":"2025-10-20T07:22:27.932982Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"con.sql(\"SELECT arena_type, COUNT(*) AS counts  FROM train GROUP BY arena_type ORDER BY counts DESC\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.935282Z","iopub.execute_input":"2025-10-20T07:22:27.935544Z","iopub.status.idle":"2025-10-20T07:22:27.951343Z","shell.execute_reply.started":"2025-10-20T07:22:27.935525Z","shell.execute_reply":"2025-10-20T07:22:27.950081Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"con.sql(\"SELECT body_parts_tracked, COUNT(*) AS counts  FROM train GROUP BY body_parts_tracked ORDER BY counts DESC\").df()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.955561Z","iopub.execute_input":"2025-10-20T07:22:27.955842Z","iopub.status.idle":"2025-10-20T07:22:27.972079Z","shell.execute_reply.started":"2025-10-20T07:22:27.955821Z","shell.execute_reply":"2025-10-20T07:22:27.970641Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Tracking Method\n\n### **Custom HRNet**\nThis refers to a modified version of the **High-Resolution Network (HRNet)**, a deep learning model originally designed for human pose estimation. It's adapted for tracking specific keypoints on animals or objects by maintaining high-resolution feature maps throughout the network, which allows for very precise spatial localization.\n\n***\n\n### **MARS**\n**MARS (Mask-based Automatic Rater)** is a machine learning method specifically developed for tracking the poses of mice in a social context. It uses computer vision to automatically identify and follow multiple body parts of interacting animals, which helps researchers analyze social behavior without manual annotation.\n\n***\n\n### **DeepLabCut**\n**DeepLabCut** is a popular open-source toolbox that enables markerless pose estimation of any animal or object. It leverages a pre-trained deep neural network (ResNet) and requires only a small number of labeled images to train a custom tracker for specific keypoints, making it highly versatile for scientific research.\n\n***\n\n### **SLEAP**\n**SLEAP (Social Leap Estimates of Animal Poses)** is a deep learning framework for multi-animal pose tracking that is optimized for speed and accuracy, even in complex scenes with many interacting individuals. It can work with a single camera and is designed to handle social interactions where animals might block or touch each other.","metadata":{}},{"cell_type":"code","source":"con.sql(\"SELECT tracking_method, COUNT(*) AS counts  FROM train GROUP BY tracking_method ORDER BY counts DESC\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.973154Z","iopub.execute_input":"2025-10-20T07:22:27.973513Z","iopub.status.idle":"2025-10-20T07:22:27.988548Z","shell.execute_reply.started":"2025-10-20T07:22:27.973477Z","shell.execute_reply":"2025-10-20T07:22:27.987452Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The tracking methods listed (custom HRnet, MARS, DeepLabCut, SLEAP) are all computer vision-based techniques. They analyze video footage to estimate the pose and track the body parts of mice, typically without the need for physical sensors attached to the animals. This allows for the study of free-moving mice in their environment.","metadata":{}},{"cell_type":"code","source":"con.sql(\"CREATE OR REPLACE TABLE test AS SELECT * FROM read_csv('/kaggle/input/MABe-mouse-behavior-detection/test.csv')\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:27.990343Z","iopub.execute_input":"2025-10-20T07:22:27.990639Z","iopub.status.idle":"2025-10-20T07:22:28.007464Z","shell.execute_reply.started":"2025-10-20T07:22:27.990614Z","shell.execute_reply":"2025-10-20T07:22:28.006331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"con.sql(\"SUMMARIZE test\").df()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:28.008638Z","iopub.execute_input":"2025-10-20T07:22:28.009202Z","iopub.status.idle":"2025-10-20T07:22:28.071945Z","shell.execute_reply.started":"2025-10-20T07:22:28.009167Z","shell.execute_reply":"2025-10-20T07:22:28.069992Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Create train_tracking view as we are just looking at the data, we can create external table to save memory\n> add lab_id and video_id as features","metadata":{}},{"cell_type":"code","source":"con.sql(\"\"\"\nCREATE OR REPLACE VIEW train_tracking AS \nSELECT split_part(filename, '/', 6) AS lab_id,\n       replace(regexp_extract(filename, '([^/]+)$', 1), '.parquet', '') AS video_id,\n       * EXCLUDE filename,\nFROM read_parquet(\n    '/kaggle/input/MABe-mouse-behavior-detection/train_tracking/**/*.parquet',\n    filename=true,\n    hive_partitioning=false\n);\n\"\"\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:28.073571Z","iopub.execute_input":"2025-10-20T07:22:28.073971Z","iopub.status.idle":"2025-10-20T07:22:29.108308Z","shell.execute_reply.started":"2025-10-20T07:22:28.073938Z","shell.execute_reply":"2025-10-20T07:22:29.107181Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"con.sql(\"DESCRIBE train_tracking;\").df()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:29.109529Z","iopub.execute_input":"2025-10-20T07:22:29.109834Z","iopub.status.idle":"2025-10-20T07:22:29.690341Z","shell.execute_reply.started":"2025-10-20T07:22:29.109809Z","shell.execute_reply":"2025-10-20T07:22:29.689132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"con.sql(\"SELECT * FROM train_tracking LIMIT 5;\").df()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:29.691420Z","iopub.execute_input":"2025-10-20T07:22:29.691773Z","iopub.status.idle":"2025-10-20T07:22:30.228582Z","shell.execute_reply.started":"2025-10-20T07:22:29.691715Z","shell.execute_reply":"2025-10-20T07:22:30.226900Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"con.sql(\"SELECT MAX(video_frame) AS total_frames FROM train_tracking WHERE video_id=1212811043;\").df()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:30.230093Z","iopub.execute_input":"2025-10-20T07:22:30.230400Z","iopub.status.idle":"2025-10-20T07:22:41.049037Z","shell.execute_reply.started":"2025-10-20T07:22:30.230374Z","shell.execute_reply":"2025-10-20T07:22:41.048093Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Validating one video for total frames","metadata":{}},{"cell_type":"code","source":"con.sql(\"\"\"\nSELECT  frames_per_second, \n        video_duration_sec, \n        frames_per_second*video_duration_sec AS total_frames\nFROM train\nWHERE video_id=1212811043;\n\"\"\").df()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:41.050378Z","iopub.execute_input":"2025-10-20T07:22:41.050673Z","iopub.status.idle":"2025-10-20T07:22:41.067721Z","shell.execute_reply.started":"2025-10-20T07:22:41.050649Z","shell.execute_reply":"2025-10-20T07:22:41.066477Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"train annotation","metadata":{}},{"cell_type":"code","source":"con.sql(\"\"\"\nCREATE OR REPLACE VIEW train_annotation AS \nSELECT split_part(filename, '/', 6) AS lab_id,\n       replace(regexp_extract(filename, '([^/]+)$', 1), '.parquet', '') AS video_id,\n       * EXCLUDE filename,\nFROM read_parquet(\n    '/kaggle/input/MABe-mouse-behavior-detection/train_annotation/**/*.parquet',\n    filename=true,\n    hive_partitioning=false\n);\n\"\"\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:41.069120Z","iopub.execute_input":"2025-10-20T07:22:41.069946Z","iopub.status.idle":"2025-10-20T07:22:41.433332Z","shell.execute_reply.started":"2025-10-20T07:22:41.069903Z","shell.execute_reply":"2025-10-20T07:22:41.432047Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"con.sql(\"DESCRIBE train_annotation;\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:41.434385Z","iopub.execute_input":"2025-10-20T07:22:41.434673Z","iopub.status.idle":"2025-10-20T07:22:41.568665Z","shell.execute_reply.started":"2025-10-20T07:22:41.434651Z","shell.execute_reply":"2025-10-20T07:22:41.567543Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"con.sql(\"SELECT * FROM train_annotation LIMIT 5;\").df()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T07:22:41.570014Z","iopub.execute_input":"2025-10-20T07:22:41.570468Z","iopub.status.idle":"2025-10-20T07:22:41.695257Z","shell.execute_reply.started":"2025-10-20T07:22:41.570436Z","shell.execute_reply":"2025-10-20T07:22:41.693814Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"To be continued...","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}