{"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"},{"sourceId":13192514,"sourceType":"datasetVersion","datasetId":8360375}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Published on September 27, 2025. By Prata, Marília (mpwolke)","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n#Two lines Required to Plot Plotly\nimport plotly.io as pio\npio.renderers.default = 'iframe'\n\nimport plotly.graph_objs as go\nimport plotly.offline as py\nimport plotly.express as px\n\n#Ignore warnings\nimport warnings\nwarnings.filterwarnings('ignore')\n\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-09-27T22:26:51.406799Z","iopub.execute_input":"2025-09-27T22:26:51.407258Z","iopub.status.idle":"2025-09-27T22:26:55.152935Z","shell.execute_reply.started":"2025-09-27T22:26:51.407218Z","shell.execute_reply":"2025-09-27T22:26:55.151702Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"_kg_hide-input":true,"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"![](https://media.tenor.com/AflzCeRpLWIAAAAM/lemmiwinks.gif)","metadata":{}},{"cell_type":"markdown","source":"## Mouse Action Recognition System (MARS)\n\nThe Mouse Action Recognition System (MARS) software pipeline for automated analysis of social behaviors in mice\n\n**Citation**: Cristina SegalinJalani WilliamsTomomi KarigoMay HuiMoriel ZelikowskyJennifer J SunPietro PeronaDavid J AndersonAnn Kennedy (2021) The Mouse Action Recognition System (MARS) software pipeline for automated analysis of social behaviors in mice eLife 10:e63720.\nhttps://doi.org/10.7554/eLife.63720\n\n\n\"The study of naturalistic social behavior requires quantification of animals’ interactions. This is generally done through manual annotation a highly time-consuming and tedious process. Recent advances in computer vision enable tracking the pose (posture) of freely behaving animals. However, automatically and accurately classifying complex social behaviors remains technically challenging.\"\n\n\"The authors introduced the **Mouse Action Recognition System (MARS**), an automated pipeline for pose estimation and behavior quantification in pairs of freely interacting mice. They compared MARS’s annotations to human annotations and find that MARS’s pose estimation and behavior classification achieve human-level performance.\"\n\n\"The authors also released the pose and annotation datasets used to train MARS to serve as community benchmarks and resources. Finally, they introduced the **Behavior Ensemble and Neural Trajectory Observatory (BENTO)**, a graphical user interface for analysis of multimodal neuroscience datasets. Together, MARS and BENTO provide an end-to-end pipeline for behavior data extraction and analysis in a package that is user-friendly and easily modifiable.\"\n\n\"In preliminary exploration, the authors found that high precision and recall values for individual binary behavior classifiers were achieved by gradient boosting using the XGBoost algorithm. Custom Python code to train novel behavior classifiers is included with the MARS_Developer software. Classifier hyperparameters may be set by the user, otherwise MARS will provide default values.\"\n\n\"Each trained classifier produces a predicted probability that the behavior occurred, as well as a binarized output created by thresholding that probability value. Following predictions by individual classifiers, MARS combines all classifier outputs to produce a single, multi-class label for each frame of a behavior video. To do so, the authors selected on each frame the behavior label that has the highest predicted probability of occurring; if no behavior has a predicted probability of >0.5, then the frame is labeled as ‘other’ (no behavior occurring).\"\n\n\"The advantage of this approach over training multi-class XGBoost is that it allows their ensemble of classifiers to be more easily expanded in the future to include additional behaviors of interest because it does not require the original training set to be fully re-annotated for the new behavior.\"\n\nhttps://elifesciences.org/articles/63720","metadata":{}},{"cell_type":"markdown","source":"## Competititon Citation:\n\n@misc{MABe-mouse-behavior-detection,\n\n    author = {Jennifer J. Sun and Markus Marks and Sam Golden and Talmo Pereira and Ann Kennedy and Sohier Dane and Addison Howard and Ashley Chow},\n    title = {MABe Challenge - Social Action Recognition in Mice},\n    year = {2025},\n    \n    howpublished = {\\url{https://kaggle.com/competitions/MABe-mouse-behavior-detection}},\n    note = {Kaggle}\n}","metadata":{}},{"cell_type":"markdown","source":"## train file","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/MABe-mouse-behavior-detection/train.csv')\npd.set_option('display.max_columns', None)\ntrain.tail(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T21:05:00.371293Z","iopub.execute_input":"2025-09-27T21:05:00.371597Z","iopub.status.idle":"2025-09-27T21:05:00.475929Z","shell.execute_reply.started":"2025-09-27T21:05:00.371578Z","shell.execute_reply":"2025-09-27T21:05:00.475066Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Missing values","metadata":{}},{"cell_type":"code","source":"#By Yulya Odintsova https://www.kaggle.com/code/yulyaodintsova/eda-logistic-regression-lgbmclassifier\n\ntrain_info = pd.DataFrame({\n    \"DataType\": train.dtypes,\n    \"MissingValues\": train.isnull().sum(),\n    \"UniqueValues\": train.nunique()\n}).sort_values(by=\"MissingValues\", ascending=False)\n\ntrain_info['MissingValuesRatio'] = round(train_info['MissingValues'] / train.shape[0] ,2)\n\ntrain_info","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T21:10:25.381549Z","iopub.execute_input":"2025-09-27T21:10:25.382276Z","iopub.status.idle":"2025-09-27T21:10:25.428258Z","shell.execute_reply.started":"2025-09-27T21:10:25.382239Z","shell.execute_reply":"2025-09-27T21:10:25.427409Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Describe method","metadata":{}},{"cell_type":"code","source":"train.describe().loc[['mean','min','max']].T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T21:14:48.590720Z","iopub.execute_input":"2025-09-27T21:14:48.591075Z","iopub.status.idle":"2025-09-27T21:14:48.685507Z","shell.execute_reply.started":"2025-09-27T21:14:48.591050Z","shell.execute_reply":"2025-09-27T21:14:48.684635Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Numerical List\n#only Integer: list(df.select_dtypes(include='int64').columns)\n#Below could be df.select_dtypes(include=[np.number])\n\nlist(train.select_dtypes(include=['int64', 'float64']).columns)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T21:24:44.160007Z","iopub.execute_input":"2025-09-27T21:24:44.160738Z","iopub.status.idle":"2025-09-27T21:24:44.168283Z","shell.execute_reply.started":"2025-09-27T21:24:44.160714Z","shell.execute_reply":"2025-09-27T21:24:44.167314Z"},"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numerical_cols = ['video_id','mouse1_id','mouse2_id', 'mouse3_id','mouse4_id',\n 'frames_per_second','video_duration_sec','pix_per_cm_approx','video_width_pix',\n 'video_height_pix','arena_width_cm','arena_height_cm']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T21:26:58.562007Z","iopub.execute_input":"2025-09-27T21:26:58.562721Z","iopub.status.idle":"2025-09-27T21:26:58.567450Z","shell.execute_reply.started":"2025-09-27T21:26:58.562689Z","shell.execute_reply":"2025-09-27T21:26:58.566525Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Correlation - Heatmap","metadata":{}},{"cell_type":"code","source":"# OutlierPandas https://www.kaggle.com/code/abhyudaya456/s5e6-eda-for-predicting-optimal-fertilizers/notebook \nplt.figure(figsize=(10,6))\nsns.heatmap(train[numerical_cols].corr(), annot=True, cmap='summer')\nplt.title(\"Correlation among Numerical Features\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T21:27:23.584089Z","iopub.execute_input":"2025-09-27T21:27:23.584399Z","iopub.status.idle":"2025-09-27T21:27:24.316446Z","shell.execute_reply.started":"2025-09-27T21:27:23.584378Z","shell.execute_reply":"2025-09-27T21:27:24.315446Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"list(train.select_dtypes(include='object').columns)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T21:18:30.408347Z","iopub.execute_input":"2025-09-27T21:18:30.408651Z","iopub.status.idle":"2025-09-27T21:18:30.418200Z","shell.execute_reply.started":"2025-09-27T21:18:30.408634Z","shell.execute_reply":"2025-09-27T21:18:30.417159Z"},"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Categorical cols distributions","metadata":{}},{"cell_type":"code","source":"#By H-Z-Ning  https://www.kaggle.com/code/hzning/top-10-solution-0-97525-esay-is-all-you\n\ncategorical_columns = [\"mouse1_sex\", \"mouse2_sex\",\"mouse2_strain\", \"mouse1_color\", \"mouse2_color\",\"mouse3_color\", \"mouse4_color\", \"arena_shape\", \"arena_type\"]\n\nplt.figure(figsize=(14, 12))\nfor i, column in enumerate(categorical_columns, 1):\n    plt.subplot(3, 3, i)\n    sns.countplot(x=column, data=train, palette='Set2')\n    plt.title(f'Distribution of {column}')\n    plt.xticks(rotation=45)\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T23:06:41.784477Z","iopub.execute_input":"2025-09-27T23:06:41.784829Z","iopub.status.idle":"2025-09-27T23:06:43.480760Z","shell.execute_reply.started":"2025-09-27T23:06:41.784798Z","shell.execute_reply":"2025-09-27T23:06:43.479626Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Tracking Methods","metadata":{}},{"cell_type":"code","source":"train['tracking_method'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T20:31:41.381233Z","iopub.execute_input":"2025-09-27T20:31:41.381583Z","iopub.status.idle":"2025-09-27T20:31:41.393955Z","shell.execute_reply.started":"2025-09-27T20:31:41.381561Z","shell.execute_reply":"2025-09-27T20:31:41.393063Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels = 'custom HRnet', 'MARS', 'DeepLabCut', 'SLEAP'\nsizes = [7926, 528, 211, 124]  #must have same number labels, sizes and explode\nexplode = (0, 0.2, 0, 0)  # only \"explode\" the 2nd slice \n\nfig1, ax1 = plt.subplots(figsize=(6,6))\nax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',\n        shadow=True, startangle=90)\nax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.\n\nplt.title('Tracking Methods')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T22:48:15.507775Z","iopub.execute_input":"2025-09-27T22:48:15.508587Z","iopub.status.idle":"2025-09-27T22:48:15.676693Z","shell.execute_reply.started":"2025-09-27T22:48:15.508555Z","shell.execute_reply":"2025-09-27T22:48:15.675611Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Code by Ducky https://www.kaggle.com/code/illgamhoduck/nfl-starter-eda\n\nENV_DIR = '../input'\nDATA_DIR = f'{ENV_DIR}/elif-mars'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T22:43:01.990994Z","iopub.execute_input":"2025-09-27T22:43:01.992892Z","iopub.status.idle":"2025-09-27T22:43:01.999422Z","shell.execute_reply.started":"2025-09-27T22:43:01.992858Z","shell.execute_reply":"2025-09-27T22:43:01.998228Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## MARS video with BENTO\n\n**Citation**: Cristina Segalin, Jalani Williams, Tomomi Karigo, May Hui, Moriel Zelikowsky, Jennifer J Sun, Pietro Perona, David J Anderson, Ann Kennedy (2021) The Mouse Action Recognition System (MARS) software pipeline for automated analysis of social behaviors in mice eLife 10:e63720 https://doi.org/10.7554/eLife.63720","metadata":{}},{"cell_type":"code","source":"#Code by Ducky https://www.kaggle.com/code/illgamhoduck/nfl-starter-eda\n\nfrom IPython.display import Video, display\n\ndef video(video_path, ratio=0.7):\n    nfl_video = Video(f\"{DATA_DIR}/{video_path}\",\n                      embed=True,\n                      height=int(720 * ratio),\n                      width=int(1280 * ratio))\n    return nfl_video\n    \nvideo('elife-63720-video2 (1).mp4')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T22:43:27.606380Z","iopub.execute_input":"2025-09-27T22:43:27.606721Z","iopub.status.idle":"2025-09-27T22:43:27.882286Z","shell.execute_reply.started":"2025-09-27T22:43:27.606697Z","shell.execute_reply":"2025-09-27T22:43:27.881005Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## test file","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/MABe-mouse-behavior-detection/test.csv')\ntest.tail()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T20:16:03.741568Z","iopub.execute_input":"2025-09-27T20:16:03.742466Z","iopub.status.idle":"2025-09-27T20:16:03.769850Z","shell.execute_reply.started":"2025-09-27T20:16:03.742435Z","shell.execute_reply":"2025-09-27T20:16:03.768975Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## submission file","metadata":{}},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/MABe-mouse-behavior-detection/sample_submission.csv')\nsub.tail()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T20:17:06.179364Z","iopub.execute_input":"2025-09-27T20:17:06.179737Z","iopub.status.idle":"2025-09-27T20:17:06.205535Z","shell.execute_reply.started":"2025-09-27T20:17:06.179710Z","shell.execute_reply":"2025-09-27T20:17:06.204493Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## One DeliriousFly parquet file","metadata":{}},{"cell_type":"code","source":"#Read One parquet file. \ndf = pd.read_parquet(\"../input/MABe-mouse-behavior-detection/train_tracking/DeliriousFly/1649549863.parquet\")\ndf.tail()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-27T20:20:28.523096Z","iopub.execute_input":"2025-09-27T20:20:28.523468Z","iopub.status.idle":"2025-09-27T20:20:28.947624Z","shell.execute_reply.started":"2025-09-27T20:20:28.523443Z","shell.execute_reply":"2025-09-27T20:20:28.946581Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Mice Ippon\n\nLemmiwinks vs. WikiLeaks: Sniff, sniff.\n\n![](https://pa1.aminoapps.com/6967/df93577746d0aec536c75fe5c8b6e5caf6f110e2r1-480-270_hq.gif)","metadata":{}},{"cell_type":"markdown","source":"## Draft Session: 2h:57m","metadata":{}},{"cell_type":"markdown","source":"#Acknowledgements:\n\nYulya Odintsova https://www.kaggle.com/code/yulyaodintsova/eda-logistic-regression-lgbmclassifier\n\nOutlierPandas https://www.kaggle.com/code/abhyudaya456/s5e6-eda-for-predicting-optimal-fertilizers/notebook\n\nH-Z-Ning  https://www.kaggle.com/code/hzning/top-10-solution-0-97525-esay-is-all-you","metadata":{}}]}