R/mapping.R

# This function retrieves the locations of all the parameters of the model in
# the 3 levels of the hierarchical list.
# Used by "map_par". "exp" should be a list.
map_mod_par <- function(exp) {
  get_names <- function(x) sapply(x, "[", "name")
  get_level3 <- function(level1, slot)
    sapply(exp[[level1]], function(x) which(names(x) == slot))
  par_names <- lapply(exp[c("Parameters", "Outputs")], get_names)
  level1 <- rep(names(par_names), sapply(par_names, length))
  level2 <- unlist(lapply(par_names, seq_along))
  level3 <- unlist(mapply(get_level3,
                          c("Parameters", "Outputs"),
                          c("value", "framerate")))
  data.frame(param  = unlist(par_names),level1, level2, level3,
             stringsAsFactors = FALSE)
}


# ------------------------------------------------------------------------------


# This function retrieves the locations of all the parameters of the experiment
# in the 3 levels of the hierarchical list.
# Used by "map_par". "exp" can be of class list or experiment.
map_exp_par <- function(exp) {
  level1 <- ".attrs"
  param <- names(exp[[level1]])
  data.frame(param, level1, level2 = 1, level3 = seq_along(param),
             stringsAsFactors = FALSE)
}


# ------------------------------------------------------------------------------


# This function uses "map_mod_par" and "map_exp_par" to retrieve the locations
# of all the model and experiment parameters in the 3 levels of the hierarchical
# list.
# Used by "as.list.experiment", "names.experiment" and "`[<-.experiment`".
map_par <- function(exp) {
  exp <- unclass(exp)  # required by "map_mod_par"
  out <- do.call(rbind, lapply(list(map_mod_par, map_exp_par),
                               function(f) f(exp)))
  rownames(out) <- NULL
  out <- data.frame(section = out$level1, out, stringsAsFactors = FALSE)
  level1 <- names(exp)
  lut <- setNames(seq_along(level1), level1)
  out$level1 <- lut[out$level1]
  out
}


# ------------------------------------------------------------------------------


# This function reformats the output of "map_par" in a data frame.
# Used by "as.list.experiment" and "names.experiment".
reformat <- function(x)
  setNames(as.data.frame(apply(x[, paste0("level", 1:3)], 1, "c")), x$param)
choisy/gamar3 documentation built on May 28, 2019, 7:17 p.m.