matiasandina / homecage_quantification

Code for quantifying homecage activity
https://github.com/homecage_quantification
0 stars 0 forks source link

Improve `pool_movement_data` on ShinyApp #11

Closed matiasandina closed 4 years ago

matiasandina commented 4 years ago

Current function is

pool_movement_data <- function(date){
    pattern_to_find <- paste0(date,".*_opt_flow.csv")
    # TODO: improve this so that is not recursive
    files <- list.files(pattern = pattern_to_find, recursive = TRUE, full.names = TRUE)
    names(files) <- files
    pooled_data <- purrr::map(files,
                              function(tt) read_csv(tt, col_names = "total_flow") %>% 
                                  # replace first element with zero
                                  mutate(total_flow = replace(total_flow, 1, 0))
    ) %>%
        bind_rows(.id="source") 

    if(nrow(pooled_data) > 0){
        pooled_data <- pooled_data %>%
            # clean
            mutate(mac = str_extract(source, "[a-z|0-9]{2}:.*/"),
                   mac = str_remove(mac, "/"),
            )
    }
    return(pooled_data)    
}
matiasandina commented 4 years ago

This was written when optic flow had only one value (the total_flow). The new way has more columns, so

flow_df <- purrr::map(flow_files,
          function(x)
          read_delim(x,
                     delim=",",
                     col_names = c("datetime", "movement", "x", "y")))

names(flow_df) <- basename(flow_files)
flow_df <- flow_df %>% bind_rows(.id="filename")

# clean
flow_df <- flow_df %>% 
  mutate(x = as.numeric(str_extract(x, "[0-9]+")),
         y = as.numeric(str_extract(y, "[0-9]+")))