dmirman / gazer

Functions for reading and pre-processing eye tracking data.
43 stars 11 forks source link

Downsample XY for Pupil #14

Open jgeller112 opened 3 years ago

jgeller112 commented 3 years ago

To downsample XY coordinates you will have to modify the downsample_gaze function

#' This function will combine gaze samples into time bins
#' @param dataframe dataframe
#' @param bin.length Length of time bins
#' @param timevar time column
#' @param aggvars vector of variable names to group by for aggregation
#' @param type pupil for pupil data gaze for gaze data
#' @export
downsample_gaze <- function(dataframe, bin.length = 50, timevar = "time", aggvars = c("subject", "condition", "target", "trial", "object", "timebins"), type="gaze"){
  if(type=="gaze") {
  downsample <- dataframe %>%
    mutate(timebins = round(!!sym(timevar)/bin.length)*bin.length)
  # if there are aggregation variables, then aggregate
  if(length(aggvars > 0)){
    downsample <- downsample %>%
      dplyr::group_by_(.dots = aggvars) %>%
      dplyr::summarize(acc = unique(acc), rt = unique(rt),
                       Fix = mean(Fix) > 0.5) %>%
      dplyr::ungroup()
  }
  return(downsample)
}
  if (type=="pupil") {
    downsample <- dataframe %>%
      mutate(timebins = round(!!sym(timevar)/bin.length)*bin.length)
    # if there are aggregation variables, then aggregate
    if(length(aggvars > 0)){
      downsample <- downsample %>%
        dplyr::group_by(.dots = aggvars) %>%
        dplyr::summarize(aggbaseline=mean(pupilcol, na.rm=TRUE), mean_x=mean(x, na.rm=TRUE), mean_y=mean(y, na.rm=TRUE)) %>% 
        dplyr::ungroup()
    }
  }
  return(downsample)
}

You will probably have to make sure you interpolate the missing gaze values or delete them altogether before aggregating.