Skip to content

Commit

Permalink
Added a test on model wrapper results format
Browse files Browse the repository at this point in the history
  • Loading branch information
sbuis committed Feb 17, 2020
1 parent 326159f commit fdbcd40
Show file tree
Hide file tree
Showing 106 changed files with 713 additions and 75 deletions.
129 changes: 129 additions & 0 deletions R/is_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#' @title Check format of data storage (for simulation results or observations)
#'
#' @param data_list List of values to check
#'
#' @return TRUE if data_list has the expected format, FALSE otherwise (+ warnings)
#'
#' @details data is expected to be stored in a vector of named list containing for
#' each situation a data.frame with a column "Date" plus one column per variable
#'
#' @examples
#'
#' data_list <- list(sit1=data.frame(Date=as.POSIXct(c("2009-11-30","2009-12-10")),var1=c(1.1,1.5),var2=c(NA,2.1)),
#' sit2=data.frame(Date=as.POSIXct(c("2009-11-30","2009-12-5")),var1=c(1.3,2)))
#' CroptimizR:::is.data(data_list)
#'
#' # Missing Date column
#' data_list <- list(sit1=data.frame(var1=c(1.1,1.5),var2=c(NA,2.1)),
#' sit2=data.frame(Date=as.POSIXct(c("2009-11-30","2009-12-5")),var1=c(1.3,2)))
#' CroptimizR:::is.data(data_list)
#'
#' # Bad Date format
#' data_list <- list(sit1=data.frame(Date=c("2009-11-30","2009-12-10"),var1=c(1.1,1.5),var2=c(NA,2.1)),
#' sit2=data.frame(Date=c("2009-11-30","2009-12-5"),var1=c(1.3,2)))
#' CroptimizR:::is.data(data_list)
#'
#' @keywords internal
#'
is.data <- function(data_list) {

# Check data_list format
if (!is.list(data_list) || !all(sapply(data_list, function(x) is.data.frame(x))) ) {
warning("Incorrect format: Should be a named list containing data.frames.")
return(FALSE)
}
# Check existence of Date column
if (!all(sapply(data_list, function(x) is.element("Date",colnames(x))))) {
warning("Incorrect format: all data.frames in the list must contain a column named Date.")
return(FALSE)
}
# Check format of Date column
if (!all(sapply(data_list, function(x) all(lubridate::is.POSIXct(x$Date)))) &&
!all(sapply(data_list, function(x) all(lubridate::is.Date(x$Date)))) ) {
warning("Incorrect format: Date column in data.frame must contain values in Date or POSIXct format.")
return(FALSE)
}

return(TRUE)

}


#' @title Check format of simulated data list
#'
#' @param sim_list List of simulation results to check
#'
#' @return TRUE if sim_list has the good format, FALSE otherwise (+ warnings)
#'
#' @examples
#'
#' sim_list <- vector("list",2)
#' sim_list[[1]] <- list(sit1=data.frame(Date=as.POSIXct(c("2009-11-30","2009-12-10")),var1=c(1.1,1.5),var2=c(NA,2.1)),
#' sit2=data.frame(Date=as.POSIXct(c("2009-11-30","2009-12-5")),var1=c(1.3,2)))
#' sim_list[[2]] <- list(sit1=data.frame(Date=as.POSIXct(c("2009-11-30","2009-12-10")),var1=c(1.1,1.5),var2=c(NA,2.1)),
#' sit2=data.frame(Date=as.POSIXct(c("2009-11-30","2009-12-5")),var1=c(1.3,2)))
#' CroptimizR:::is.sim(sim_list)
#'
#' # Missing Date column
#' sim_list <- vector("list",2)
#' sim_list[[1]] <- list(sit1=data.frame(Date=as.POSIXct(c("2009-11-30","2009-12-10")),var1=c(1.1,1.5),var2=c(NA,2.1)),
#' sit2=data.frame(Date=as.POSIXct(c("2009-11-30","2009-12-5")),var1=c(1.3,2)))
#' sim_list[[2]] <- list(sit1=data.frame(var1=c(1.1,1.5),var2=c(NA,2.1)),
#' sit2=data.frame(Date=as.POSIXct(c("2009-11-30","2009-12-5")),var1=c(1.3,2)))
#' CroptimizR:::is.sim(sim_list)
#'
#' # Bad Date format
#' sim_list <- vector("list",1)
#' sim_list[[1]] <- list(sit1=data.frame(Date=c("2009-11-30","2009-12-10"),var1=c(1.1,1.5),var2=c(NA,2.1)),
#' sit2=data.frame(Date=c("2009-11-30","2009-12-5"),var1=c(1.3,2)))
#' CroptimizR:::is.sim(sim_list)
#'
#' @keywords internal
#'
is.sim <- function(sim_list) {

if (!all(sapply(sim_list, CroptimizR:::is.data))) {
warning("Variable storing simulated data has an incorrect format.")
return(FALSE)
}

return(TRUE)

}


#' @title Check format of observation list
#'
#' @param obs_list List of observed values to check
#'
#' @return TRUE if obs_list has the good format, FALSE otherwise (+ warnings)
#'
#' @examples
#'
#' obs_list <- list(sit1=data.frame(Date=as.POSIXct(c("2009-11-30","2009-12-10")),var1=c(1.1,1.5),var2=c(NA,2.1)),
#' sit2=data.frame(Date=as.POSIXct(c("2009-11-30","2009-12-5")),var1=c(1.3,2)))
#' CroptimizR:::is.obs(obs_list)
#'
#' # Missing Date column
#' obs_list <- list(sit1=data.frame(var1=c(1.1,1.5),var2=c(NA,2.1)),
#' sit2=data.frame(Date=as.POSIXct(c("2009-11-30","2009-12-5")),var1=c(1.3,2)))
#' CroptimizR:::is.obs(obs_list)
#'
#' # Bad Date format
#' obs_list <- list(sit1=data.frame(Date=c("2009-11-30","2009-12-10"),var1=c(1.1,1.5),var2=c(NA,2.1)),
#' sit2=data.frame(Date=c("2009-11-30","2009-12-5"),var1=c(1.3,2)))
#' CroptimizR:::is.obs(obs_list)
#'
#' @keywords internal
#'
is.obs <- function(obs_list) {

if (!CroptimizR:::is.data(obs_list)) {
warning("Variable storing observed data has an incorrect format.")
return(FALSE)
}

return(TRUE)

}

43 changes: 0 additions & 43 deletions R/is_obs.R

This file was deleted.

6 changes: 5 additions & 1 deletion R/main_crit.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,16 @@ main_crit <- function(param_values, crit_options) {
param_values = param_values_array,
sit_var_dates_mask = obs_list)

if (model_results$error) {
# Check results
if (!is.null(model_results$error) && model_results$error) {
warning("Error in model simulations")
}
if (is.null(model_results$sim_list[[1]]) || length(model_results$sim_list[[1]])==0) {
stop("Error: model wrapper returned an empty list!")
}
if (!is.sim(model_results$sim_list)) {
stop("Error: format of results returned by the model wrapper is incorrect!")
}

# intersect sim and obs
obs_sim_list <- intersect_sim_obs(model_results$sim_list[[1]], obs_list)
Expand Down
2 changes: 1 addition & 1 deletion docs/articles/Designing_a_model_wrapper.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file removed docs/reference/Rplots.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots1.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots10.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots11.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots12.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots13.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots14.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots15.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots16.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots17.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots18.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots19.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots2.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots20.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots21.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots22.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots23.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots24.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots25.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots26.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots27.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots28.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots29.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots3.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots30.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots31.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots32.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots33.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots34.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots35.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots36.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots37.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots38.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots39.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots4.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots40.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots41.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots42.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots43.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots44.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots45.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots46.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots47.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots48.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots49.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots5.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots50.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots51.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots52.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots53.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots54.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots55.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots56.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots57.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots58.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots59.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots6.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots60.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots61.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots62.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots63.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots64.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots65.pdf
Binary file not shown.
Empty file removed docs/reference/Rplots66.pdf
Empty file.
Binary file removed docs/reference/Rplots67.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots68.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots69.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots7.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots70.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots71.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots72.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots73.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots74.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots75.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots76.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots77.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots78.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots79.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots8.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots80.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots81.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots82.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots83.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots84.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots85.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots86.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots87.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots88.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots89.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots9.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots90.pdf
Binary file not shown.
Binary file removed docs/reference/Rplots91.pdf
Binary file not shown.
6 changes: 0 additions & 6 deletions docs/reference/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fdbcd40

Please sign in to comment.