{"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.4.0"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":59156,"databundleVersionId":13719397,"sourceType":"competition"}],"dockerImageVersionId":30749,"isInternetEnabled":true,"language":"r","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### This project is for plotting pets' information","metadata":{}},{"cell_type":"code","source":"# This R environment comes with many helpful analytics packages installed\n# It is defined by the kaggle/rstats Docker image: https://github.com/kaggle/docker-rstats\n# For example, here's a helpful package to load\n\nlibrary(tidyverse) # metapackage of all tidyverse packages\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\nlist.files(path = \"../input\")\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":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:09:15.001584Z","iopub.execute_input":"2025-09-23T08:09:15.003804Z","iopub.status.idle":"2025-09-23T08:09:16.521040Z","shell.execute_reply":"2025-09-23T08:09:16.519167Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 1.order libraries","metadata":{}},{"cell_type":"code","source":"library(data.table) \nlibrary(arrow) \nlibrary(fs) \nlibrary(ggplot2) \nlibrary(gganimate)\nlibrary(dplyr)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:09:16.523838Z","iopub.execute_input":"2025-09-23T08:09:16.555588Z","iopub.status.idle":"2025-09-23T08:09:17.688677Z","shell.execute_reply":"2025-09-23T08:09:17.686706Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 2. read and show data","metadata":{}},{"cell_type":"code","source":"test_data <- read.csv('/kaggle/input/MABe-mouse-behavior-detection/test.csv') \ntrain_data <- read.csv('/kaggle/input/MABe-mouse-behavior-detection/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:09:17.692929Z","iopub.execute_input":"2025-09-23T08:09:17.694747Z","iopub.status.idle":"2025-09-23T08:09:17.889863Z","shell.execute_reply":"2025-09-23T08:09:17.887934Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"str(train_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:09:17.894218Z","iopub.execute_input":"2025-09-23T08:09:17.896048Z","iopub.status.idle":"2025-09-23T08:09:17.975074Z","shell.execute_reply":"2025-09-23T08:09:17.937134Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"column_names <- names(train_data) \nprint(column_names)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:09:17.978419Z","iopub.execute_input":"2025-09-23T08:09:17.979976Z","iopub.status.idle":"2025-09-23T08:09:17.996241Z","shell.execute_reply":"2025-09-23T08:09:17.994390Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"str(test_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:09:17.999091Z","iopub.execute_input":"2025-09-23T08:09:18.000682Z","iopub.status.idle":"2025-09-23T08:09:18.032875Z","shell.execute_reply":"2025-09-23T08:09:18.030851Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 3. fix data","metadata":{}},{"cell_type":"code","source":"# this line converts the train and test objects into a dataset. table objects\ntrain_data <- as.data.table(train_data)\ntest_data <- as.data.table(test_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:09:18.035841Z","iopub.execute_input":"2025-09-23T08:09:18.037401Z","iopub.status.idle":"2025-09-23T08:09:18.054073Z","shell.execute_reply":"2025-09-23T08:09:18.052197Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# build the table and check the number of rows and labs\n\nprint_data <- function(dt, name = \"Dataset\", n = NULL, max_width = 30) {\n      cat(name,sep = \"\")\n      cat(nrow(dt), \"rows,\", ncol(dt))\n      cat(uniqueN(dt$lab_id), \" labs \")\n      if (is.null(n)) n <- dt[, .N]\n      sample_dt <- dt[1:n]\n          sample_dt[, body_parts_tracked := substr(body_parts_tracked , 1 , max_width)] \n      sample_dt[, behaviors_labeled := substr(behaviors_labeled , 1 , max_width)]\n      head(sample_dt ,n)\n      }\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:09:18.057092Z","iopub.execute_input":"2025-09-23T08:09:18.058637Z","iopub.status.idle":"2025-09-23T08:09:18.073223Z","shell.execute_reply":"2025-09-23T08:09:18.071373Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print_data(train_data , 'Train', n = 4 , max_width  = 30  )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:09:18.076083Z","iopub.execute_input":"2025-09-23T08:09:18.077617Z","iopub.status.idle":"2025-09-23T08:09:18.161653Z","shell.execute_reply":"2025-09-23T08:09:18.152478Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\ndir = \"/kaggle/input/MABe-mouse-behavior-detection/\"\nfiles <- dir_ls(\n    file.path(dir , 'train_tracking') ,\n    recurse = TRUE , glob = '*.parquet' \n    \n    \n)\ntrain_tr <- rbindlist(\n    lapply(files[1:3], \\(f){\n        dt <- as.data.table(read_parquet(f)) \n        fn <- gsub('\\\\.parquet','',basename(f))\n        dt[,video_file := fn]\n    })\n)\ncat('number of files' , length(files)) \nhead(train_tr)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:09:18.165162Z","iopub.execute_input":"2025-09-23T08:09:18.166741Z","iopub.status.idle":"2025-09-23T08:09:43.077489Z","shell.execute_reply":"2025-09-23T08:09:43.075690Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\ndata_meta <- rbind(\n    train_data [, dataset:= 'train'],\n    test_data [, dataset:= 'test']\n)\ndata_meta[, has_ann := behaviors_labeled !=''] \nann_t <- data_meta[has_ann == TRUE , uniqueN(video_id) ] \nann_total <- data_meta[, uniqueN(video_id)] \ncat('col_n',names(data_meta)) \ncat('\\nVideos with annotations' , ann_t) \ncat('\\nvideos without annotations',ann_total - ann_t) \ncat('\\nTotal videos:',ann_total )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:09:43.081264Z","iopub.execute_input":"2025-09-23T08:09:43.082949Z","iopub.status.idle":"2025-09-23T08:09:43.129304Z","shell.execute_reply":"2025-09-23T08:09:43.127471Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with_vid <- data_meta[has_ann == TRUE & dataset == 'train', unique(video_id)] \nvideo_ch <- setdiff(with_vid , train_tr$video_file) \nvideo_ch","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:10:34.061104Z","iopub.execute_input":"2025-09-23T08:10:34.062964Z","iopub.status.idle":"2025-09-23T08:10:34.448507Z","shell.execute_reply":"2025-09-23T08:10:34.445994Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"id_cols <- c(\"dataset\", \"has_ann\", \"lab_id\", \"video_id\")\nvars <- c(\n  \"frames_per_second\",\n  \"video_duration_sec\",\n  \"pix_per_cm_approx\",\n  \"video_width_pix\",\n  \"video_height_pix\"\n)\ndt_video <- data_meta[, c(id_cols, vars), with = FALSE]\n\ndt_video[, `:=`(\n  aspect_ratio   = video_width_pix / video_height_pix,\n  pixel_area_mpx = video_width_pix * video_height_pix / 1e6\n)]\n\ndt_video[, (vars) := lapply(.SD, as.numeric), .SDcols = vars]\ndt <- melt(dt_video, id.vars = id_cols, variable.factor = FALSE)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:18:06.845649Z","iopub.execute_input":"2025-09-23T08:18:06.847681Z","iopub.status.idle":"2025-09-23T08:18:06.881186Z","shell.execute_reply":"2025-09-23T08:18:06.879229Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stats_data <- dt[\n    dataset == 'train', .(\n        n = .N , \n        min = min(value),\n        q25 = quantile(value , 0.25),\n        median = median(value),\n        q75 = quantile(value, 0.75),\n        max = max(value) \n        \n    ), by = (variable)] \nstats_data <- merge(\n    stats_data  , dt[dataset == 'test',.(variable, test_value = value)],\n    by = 'variable',all.x = TRUE , sort = FALSE\n)\nstats_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:20:18.492784Z","iopub.execute_input":"2025-09-23T08:20:18.494500Z","iopub.status.idle":"2025-09-23T08:20:18.544690Z","shell.execute_reply":"2025-09-23T08:20:18.542684Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 4. Plot data.","metadata":{}},{"cell_type":"code","source":"# this fuuction for control size plot's data\nfig <- function(width , heigth){\n    options(repr.plot.width = width , repr.plot.heigth = heigth)\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:27:33.383203Z","iopub.execute_input":"2025-09-23T08:27:33.384912Z","iopub.status.idle":"2025-09-23T08:27:33.397420Z","shell.execute_reply":"2025-09-23T08:27:33.395548Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig(20,10) \nplot_data <- dt[\n    !variable %in% c('video_height_pix','video_width_pix')\n]\nggplot(plot_data[dataset == 'train'], aes(x = value)) +\n    geom_density(fill = 'skyblue', alpha = 0.5) + \n    geom_vline( \n    data = plot_data[dataset == 'test'],aes(xintercept = value),\n        color = 'red',linewidth = 0.8 \n        \n    ) + \n    facet_wrap(~ variable , scales = 'free', ncol = 3)+\n    labs(\n        x = NULL,\n        y = 'robability',\n        titel = 'train (8790 videos) & example test videos'\n    ) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:36:40.903156Z","iopub.execute_input":"2025-09-23T08:36:40.904855Z","iopub.status.idle":"2025-09-23T08:36:42.157815Z","shell.execute_reply":"2025-09-23T08:36:42.154662Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"state_data <- dt[\n    dataset == 'train' & has_ann == TRUE , .(\n        min = min(value),\n        q25 = quantile(value , 0.25),\n        median = median(value),\n        q75 = quantile(value , 0.75),\n        max = max(value)\n    ), by = .(variable)] \nstate_data <- merge(\n    state_data , dt[dataset == 'test', .(variable , test_value = value)],\n    by = 'variable', all.x = TRUE , sort = FALSE\n    \n)\nstate_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T08:44:51.412571Z","iopub.execute_input":"2025-09-23T08:44:51.414260Z","iopub.status.idle":"2025-09-23T08:44:51.458275Z","shell.execute_reply":"2025-09-23T08:44:51.456240Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig(20, 10)\nplot_data <- dt[\n    !variable %in% c(\"video_height_pix\", \"video_width_pix\") &\n    has_ann == TRUE\n]\nggplot(plot_data[dataset == \"train\"], aes(x = value)) +\n  geom_density(fill = \"skyblue\", alpha = 0.5) +\n  geom_vline(\n    data = plot_data[dataset == \"test\"], aes(xintercept = value),\n    color = \"red\", linewidth = 0.8\n  ) +\n  facet_wrap(~ variable, scales = \"free\", ncol = 3) +\n  labs(\n    x = NULL,\n    y = \"robability\",\n    title = \"train distributions and test videos\"\n  ) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T09:03:20.609721Z","iopub.execute_input":"2025-09-23T09:03:20.611411Z","iopub.status.idle":"2025-09-23T09:03:21.461950Z","shell.execute_reply":"2025-09-23T09:03:21.459905Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 4. creat model","metadata":{}},{"cell_type":"code","source":"sum(is.na(state_data))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T09:23:08.757110Z","iopub.execute_input":"2025-09-23T09:23:08.766397Z","iopub.status.idle":"2025-09-23T09:23:08.786148Z","shell.execute_reply":"2025-09-23T09:23:08.783708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"library(caret) \n\ncontrol_cv <- trainControl(method ='cv',number = 6) \nmodel_cv <- train(min ~\tq25 , data = state_data, method = 'lm', trControl = control_cv ) \nprint(model_cv)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T09:27:42.297101Z","iopub.execute_input":"2025-09-23T09:27:42.300310Z","iopub.status.idle":"2025-09-23T09:27:42.643697Z","shell.execute_reply":"2025-09-23T09:27:42.641691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"summary(model_cv)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-23T09:28:13.060880Z","iopub.execute_input":"2025-09-23T09:28:13.062797Z","iopub.status.idle":"2025-09-23T09:28:13.177921Z","shell.execute_reply":"2025-09-23T09:28:13.174765Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}