R/Pre-process_data.R

# Pre-process experimental data
#
# Functions to pre-process data from experiments to merge together, or convert to more useful format than MATLAB provides/ etc.

#find all matching files
find_relevant_files <- function(recent_date = NULL, look_back_date = NULL, search_string = NULL, directory = NULL, monkey = NULL){
  #find all the files in the directory specified
  all_data_files <- dir(directory)

  #if recent_data is null load every file from the directory
  if(!is.null(recent_date)){
    if(!is.null(look_back_date)){
      #use a selected range of dates
      dates <- strftime(seq(as.Date(look_back_date, "%d-%b-%Y"), as.Date(recent_date, "%d-%b-%Y"), "days"), "%d-%b-%Y")
    } else {
      #else use the last week of data
      dates <- strftime(seq(as.Date(recent_date, "%d-%b-%Y") -7, as.Date(recent_date, "%d-%b-%Y"), "days"), "%d-%b-%Y")
    }

    #create a string to search for in the filename
    search_string <- paste0(dates, ".*", monkey, "COMPACT")

    #find the specific data fils you want to look at
    specific_files <- unlist(sapply(search_string, grep, x = data_files))

  } else {
    #otherwise just try to load every file
    specific_files <- all_data_files
  }

  return(specific_files)
}

#sometimes we don't output the actual magnitude of the rewards, just their ranking (e.g. 1,2,3...)
#this will find the reward given out for each of these assuming a monkey 'wins' at least one trial per offer_value
#depreceated in MATisse2.0 now we just output everything
merge_reward_magnitudes <- function(task_data){
  for(sub_block in 1:length(unique(task_data$block_no))){
    data_sub <- task_data[block_no == sub_block]
    for(sub_offer in 1:length(unique(data_sub$offer_value))){
      data_subbed <- data_sub[reward == sub_offer]

      row <- data.frame(block_no = sub_block,
                        offer_value = sub_offer,
                        juice_offered = max(unique(data_subbed$reward_liquid), na.rm = TRUE))
      if(sub_block == 1 & sub_offer == 1){
        merge_df <- row
      } else {
        merge_df <- rbind(merge_df, row)
      }
    }
  }
  task_data <- merge(task_data, merge_df, by = c("block_no", "offer_value"))
  return(task_data)
}

#select the correct columns to keep based on the task being performed
#also sets a good order
#needs to be modified for MATisse2.0
select_relevant_columns <- function(task_data, task){
  if(task == "BCb"){
    task_data <- merge(task_data, merge_df, by = c("block_no", "offer_value")) %>%
      #finally select and rearrange useful columns
      .[,c("date", "block_no", "trial",
           "bundle_position", "water_offered", "juice_offered",
           "fractal_choice", "bundle_distance", "task_failure")] %>%
      setcolorder(., c("date", "block_no", "trial",
                       "bundle_position", "water_offered", "juice_offered",
                       "fractal_choice", "bundle_distance", "task_failure"))
  } else if(task == "BDM"){
    task_data <- merge(task_data, merge_df, by = c("block_no", "offer_value")) %>%
      #finally select and rearrange useful columns
      .[,c("date", "block_no", "trial", "juice_offered",
           "computer_bid", "monkey_final_bid", "adjust", "start_position",
           "win", "task_failure", "reward_liquid", "budget_liquid",
           "correct_trials", "rolling_mean", "rolling_sd", "rolling_se")] %>%
      setcolorder(., c("date", "block_no", "trial", "juice_offered",
                       "computer_bid", "monkey_final_bid", "adjust", "start_position",
                       "win", "task_failure", "reward_liquid", "budget_liquid",
                       "correct_trials", "rolling_mean", "rolling_sd", "rolling_se"))
  }
}
RobWHickman/RobeRt documentation built on May 17, 2019, 6:34 p.m.