R/ana_Methods.R

Defines functions curMin_ curMean_ curMax_ showresults showresults2

#' Cursor methods
#' 
#' Cursor methods are used in conjunction with \link{set_cursor} to 
#' specify what calculations should be applied to a region 
#' of interest of a data trace, and how these results are shown in a plot. 
#' 
#' 
#' Names of cursormethods start with "cur*" for easier lookup. 
#' 
#' @param name a name for the cursor
#' @param col optional colour argument for the points in the plot
#'
#' @return A list of methods that are used by the cursor to calculate its results. 
#' The list may have attributes specifying plot.fun.first and plot.fun.last methods
#' to be used to decorate data plots before and after plotting of the trace data, respectively.   
#' Additional attributes may specify plotting options, like color, of these decorations.
#' @name cursormethods
NULL

#' @describeIn cursormethods 
#' shows the minimum in the cursor region as an orange dot. 
#' @export
curMin_ <- function(name, col = "orange") {
    curMin <- function(cut, s) {
        min(cut$y[, s], na.rm = T)
    }
    curMin.x <- function(cut, s) {
        cut$x[which.min(cut$y[, s]), s]
    }
    methods <- list(curMin, curMin.x)
    names(methods) <- c(name, paste(name, ".x", sep = ""))
    attributes(methods[[1]]) <- list(col = col)
    attr(methods, "plot.fun.last") <- showresults
    attr(methods, "name") <- "Min"
    methods
}

#' @describeIn cursormethods 
#' shows the mean of the data in the cursor region as a green line
#' 
#' @export
curMean_ <- function(name, col = "green") {
    curMean <- function(cut, s) mean(cut$y[, s])
    curMean.x1 <- function(cut, s) {
        cut$x[1, s]
    }
    curMean.x2 <- function(cut, s) {
        cut$x[nrow(cut$x), s]
    }
    methods <- list(curMean, curMean.x1, curMean.x2)
    names(methods) <- c(name, paste(name, ".x1", sep = ""), paste(name, ".x2", sep = ""))
    attributes(methods[[1]]) <- list(col = col)
    attr(methods, "plot.fun.last") <- showresults2
    attr(methods, "name") <- "Mean"
    methods
}

#' @describeIn cursormethods 
#' shows the maximum in the cursor region as a blue dot
#' 
#' more stuff to tell 
#' 
#' @export
curMax_ <- function(name, col = "blue") {
    curMax <- function(cut, s) max(cut$y[, s])
    curMax.x <- function(cut, s) {
        cut$x[which.max(cut$y[, s]), s]
    }
    methods <- list(curMax, curMax.x)
    names(methods) <- c(name, paste(name, ".x", sep = ""))
    attributes(methods[[1]]) <- list(col = col)
    attr(methods, "plot.fun.last") <- showresults
    attr(methods, "name") <- "Max"
    methods
}

showresults <- function(cursor, results, sweeps) {
    valnames <- names(cursor$analyse.methods)
    valnames.y <- (valnames[!grepl("\\.x", valnames)])
    valnames.x <- (valnames[grepl("\\.x", valnames)])
    valnames.xstart <- (valnames[grepl("\\.x", valnames)])
    valnames.xend <- (valnames[grepl("\\.x", valnames)])
    # str(valnames.xend)
    col = attr(cursor$analyse.methods[[valnames.y]], which = "col")
    points(results[sweeps, valnames.x], results[sweeps, valnames.y], col = col, pch = 19)
}

showresults2 <- function(cursor, results, sweeps) {
    valnames <- names(cursor$analyse.methods)
    valnames.y <- (valnames[!grepl("\\.x", valnames)])
    valnames.x1 <- (valnames[grepl("\\.x1", valnames)])
    valnames.x2 <- (valnames[grepl("\\.x2", valnames)])
    
    col = attr(cursor$analyse.methods[[valnames.y]], which = "col")
    arrows(results[sweeps, valnames.x1], results[sweeps, valnames.y], results[, valnames.x2], 
        col = col, length = 0)
}
tdanker/ephys2 documentation built on Aug. 11, 2019, 12:12 p.m.