Skip to content
Snippets Groups Projects
params-functions.R 4.43 KiB
Newer Older
#' Get parameters
#'
#' Get multiple parameter values from a Params object.
#'
#' @param params Params object to get values from.
#' @param names vector of names of the parameters to get.
#'
#' @return List with the values of the selected parameters.
#' @examples
Luke Zappia's avatar
Luke Zappia committed
#' params <- newSimpleParams()
#' getParams(params, c("nGenes", "nCells", "mean.rate"))
#' @export
Luke Zappia's avatar
Luke Zappia committed
getParams <- function(params, names) {

    checkmate::assertClass(params, classes = "Params")
    checkmate::assertCharacter(names, min.len = 1, any.missing = FALSE)

    sapply(names, getParam, object = params, simplify = FALSE)
}

#' Set parameters
#'
#' Set multiple parameters in a Params object.
#'
#' @param params Params object to set parameters in.
#' @param update list of parameters to set where \code{names(update)} are the
#'        names of the parameters to set and the items in the list are values.
#' @param ... additional parameters to set. These are combined with any
#'        parameters specified in \code{update}.
#'
#' @details
#' Each parameter is set by a call to \code{\link{setParam}}. If the same
#' parameter is specified multiple times it will be set multiple times.
#' Parameters can be specified using a list via \code{update} (useful when
#' collecting parameter values in some way) or individually (useful when setting
#' them manually), see examples.
#'
#' @return Params object with updated values.
#' @examples
Luke Zappia's avatar
Luke Zappia committed
#' params <- newSimpleParams()
#' params
#' # Set individually
#' params <- setParams(params, nGenes = 1000, nCells = 50)
#' params
#' # Set via update list
#' params <- setParams(params, list(mean.rate = 0.2, mean.shape = 0.8))
#' params
#' @export
Luke Zappia's avatar
Luke Zappia committed
setParams <- function(params, update = NULL, ...) {
Luke Zappia's avatar
Luke Zappia committed

    checkmate::assertClass(params, classes = "Params")
    checkmate::assertList(update, null.ok = TRUE)

    update <- c(update, list(...))

    if (length(update) > 0) {
        for (name in names(update)) {
            value <- update[[name]]
            params <- setParam(params, name, value)
Luke Zappia's avatar
Luke Zappia committed
        }
    }

    return(params)
}

#' Set parameters UNCHECKED
#'
#' Set multiple parameters in a Params object.
#'
#' @param params Params object to set parameters in.
#' @param update list of parameters to set where \code{names(update)} are the
#'        names of the parameters to set and the items in the list are values.
#' @param ... additional parameters to set. These are combined with any
#'        parameters specified in \code{update}.
#'
#' @details
#' Each parameter is set by a call to \code{\link{setParam}}. If the same
#' parameter is specified multiple times it will be set multiple times.
#' Parameters can be specified using a list via \code{update} (useful when
#' collecting parameter values in some way) or individually (useful when setting
#' them manually), see examples. THE FINAL OBJECT IS NOT CHECKED FOR VALIDITY!
#'
#' @return Params object with updated values.
Luke Zappia's avatar
Luke Zappia committed
setParamsUnchecked <- function(params, update = NULL, ...) {

    checkmate::assertClass(params, classes = "Params")
    checkmate::assertList(update, null.ok = TRUE)

    update <- c(update, list(...))

    if (length(update) > 0) {
        for (name in names(update)) {
            value <- update[[name]]
Luke Zappia's avatar
Luke Zappia committed
            params <- setParamUnchecked(params, name, value)
#' Show pretty print
#'
#' Function used for pretty printing params object.
#'
#' @param params object to show.
#' @param pp list specifying how the object should be displayed.
#' @return Print params object to console
#'
Luke Zappia's avatar
Luke Zappia committed
#' @importFrom utils head
Luke Zappia's avatar
Luke Zappia committed
showPP <- function(params, pp) {

    checkmate::assertClass(params, classes = "Params")
    checkmate::assertList(pp, types = "character", min.len = 1)

    default <- new(class(params))
    for (category in names(pp)) {
        parameters <- pp[[category]]
        values <- getParams(params, parameters)
Luke Zappia's avatar
Luke Zappia committed
        short.values <- sapply(values, function(x) {
            if (length(x) > 4) {
                paste0(paste(head(x, n = 4), collapse = ", "), ",...")
            } else {
                paste(x, collapse = ", ")
            }
        })
Luke Zappia's avatar
Luke Zappia committed
        values <- sapply(values, paste, collapse = ", ")
        default.values <- getParams(default, parameters)
        default.values <- sapply(default.values, paste, collapse = ", ")
        not.default <- values != default.values
Luke Zappia's avatar
Luke Zappia committed
        names(short.values)[not.default] <- toupper(names(values[not.default]))
Luke Zappia's avatar
Luke Zappia committed
        cat(category, "\n")
Luke Zappia's avatar
Luke Zappia committed
        print(noquote(short.values), print.gap = 2)
Luke Zappia's avatar
Luke Zappia committed
        cat("\n")
    }