Skip to content

Commit

Permalink
Merge pull request #3298 from meetagrawal09/unit-testing-2
Browse files Browse the repository at this point in the history
Unit Testing functions in `meta-analysis` sub-workflow
  • Loading branch information
infotroph authored Aug 1, 2024
2 parents 535b826 + 59140ce commit e3e21ba
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 47 deletions.
2 changes: 1 addition & 1 deletion book_source/03_topical_pages/94_docker/04_models.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ It is important values for `type` and `version` are set correct. The PEcAn code

To build the docker image, we use a Dockerfile (see example below) and run the following command. This command will expect the Dockerfile to live in the model specific folder and the command is executed in the root pecan folder. It will copy the content of the pecan folder and make it available to the build process (in this example we do not need any additional files).

Since we can have multiple different versions of a model be available for PEcAn we ar using the following naming schema `pecan/model-<modeltype>-<version>:<pecan version`. For example the image below will be named pecan/model-ed2-git, since we do not specify the exact version it will be atomically be named `pecan/model-ed2-git:latest`.
Since we can have multiple different versions of a model be available for PEcAn we are using the following naming schema `pecan/model-<modeltype>-<version>:<pecan version`. For example the image below will be named pecan/model-ed2-git, since we do not specify the exact version it will be atomically be named `pecan/model-ed2-git:latest`.

````bash
docker build \
Expand Down
1 change: 1 addition & 0 deletions docker/depends/pecan_package_dependencies.csv
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
"mockery","*","base/visualization","Suggests",FALSE
"mockery","*","base/workflow","Suggests",FALSE
"mockery","*","modules/data.atmosphere","Suggests",FALSE
"mockery","*","modules/meta.analysis","Suggests",FALSE
"mockery",">= 0.3.0","models/biocro","Suggests",FALSE
"mockery",">= 0.4.3","base/db","Suggests",FALSE
"MODISTools",">= 1.1.0","modules/data.remote","Imports",FALSE
Expand Down
5 changes: 3 additions & 2 deletions modules/meta.analysis/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ Description: The Predictive Ecosystem Carbon Analyzer (PEcAn) is a scientific
Imports:
coda (>= 0.18),
lattice,
PEcAn.utils,
MASS,
PEcAn.DB,
PEcAn.logger,
MASS,
PEcAn.settings,
PEcAn.utils,
rjags
Suggests:
ggmcmc,
ggplot2,
knitr (>= 1.42),
mockery,
rmarkdown (>= 2.19),
testthat (>= 1.0.2)
SystemRequirements: JAGS
Expand Down
2 changes: 1 addition & 1 deletion modules/meta.analysis/R/meta.analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

##' Trait Meta-analysis
##'
##' Runs heirarchical meta-analysis of plant trait data
##' Runs hierarchical meta-analysis of plant trait data
##'
##' `pecan.ma` runs a hierarchical Bayesian meta-analytical model.
##' This model combines prior information with data from studies on the particular species or group of interest.
Expand Down
2 changes: 1 addition & 1 deletion modules/meta.analysis/R/rename_jags_columns.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#-------------------------------------------------------------------------------

##-----------------------------------------------------------------------------#
##' renames the variables within output data frame trait.data
##' Renames the variables within output data frame trait.data
##'
##' @param data data frame to with variables to rename
##'
Expand Down
2 changes: 1 addition & 1 deletion modules/meta.analysis/R/run.meta.analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ run.meta.analysis.pft <- function(pft, iterations, random = TRUE, threshold = 1.
##--------------------------------------------------------------------------------------------------##
##' Run meta analysis
##'
##' This will use the following items from setings:
##' This will use the following items from settings:
##' - settings$pfts
##' - settings$database$bety
##' - settings$database$dbfiles
Expand Down
2 changes: 1 addition & 1 deletion modules/meta.analysis/man/pecan.ma.Rd

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

4 changes: 2 additions & 2 deletions modules/meta.analysis/man/rename_jags_columns.Rd

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

2 changes: 1 addition & 1 deletion modules/meta.analysis/man/run.meta.analysis.Rd

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

23 changes: 23 additions & 0 deletions modules/meta.analysis/tests/testthat/test.jagify.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

test_that("jagify correctly assigns treatment index of 1 to all control treatments, regardless of alphabetical order", {
## generate test data; controls assigned to early alphabet and late alphabet trt names
testresult <- data.frame(citation_id = 1,
site_id = rep(1:2, each = 5),
name = rep(letters[1:5],2),
trt_id = as.character(rep(letters[1:5],2)),
control = c(1, rep(0,8), 1),
greenhouse = c(rep(0,5), rep(1,5)),
date = 1,
time = NA,
cultivar_id = 1,
specie_id = 1,
n = 2,
mean = sqrt(1:10),
stat = 1,
statname = "SE",
treatment_id = 1:10
)

jagged.data <- jagify(testresult)
expect_equal(jagged.data$trt_num[jagged.data$trt == "control"], c(1, 1))
})
65 changes: 28 additions & 37 deletions modules/meta.analysis/tests/testthat/test.run.meta.analysis.R
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
#-------------------------------------------------------------------------------
# Copyright (c) 2012 University of Illinois, NCSA.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the
# University of Illinois/NCSA Open Source License
# which accompanies this distribution, and is available at
# http://opensource.ncsa.illinois.edu/license.html
#-------------------------------------------------------------------------------

context("run.meta.analysis")

test_that("`runModule.run.meta.analysis` throws an error for incorrect input", {
expect_error(runModule.run.meta.analysis('test'), "only works with Settings or MultiSettings")
})

test_that("singleMA gives expected result for example inputs",{
## need to calculate x
## x <- singleMA(....)
#expect_equal(round(summary(x)$statistics["beta.o", "Mean"]), 5)
test_that("`run.meta.analysis` able to call run.meta.analysis.pft for each pft in the input list", {
mocked_res <- mockery::mock(1, cycle = TRUE)
mockery::stub(run.meta.analysis, 'run.meta.analysis.pft', mocked_res)
mockery::stub(run.meta.analysis, 'PEcAn.DB::db.open', 1)
mockery::stub(run.meta.analysis, 'PEcAn.DB::db.close', 1)
pfts <- list('ebifarm.salix', 'temperate.coniferous')
run.meta.analysis(pfts = pfts, iterations = 1, dbfiles = NULL, database = NULL)
mockery::expect_called(mocked_res, 2)
args <- mockery::mock_args(mocked_res)
expect_equal(args[[1]][[1]], "ebifarm.salix")
expect_equal(args[[2]][[1]], "temperate.coniferous")
})

test_that("jagify correctly assigns treatment index of 1 to all control treatments, regardless of alphabetical order", {
## generate test data; controls assigned to early alphabet and late alphabet trt names
testresult <- data.frame(citation_id = 1,
site_id = rep(1:2, each = 5),
name = rep(letters[1:5],2),
trt_id = as.character(rep(letters[1:5],2)),
control = c(1, rep(0,8), 1),
greenhouse = c(rep(0,5), rep(1,5)),
date = 1,
time = NA,
cultivar_id = 1,
specie_id = 1,
n = 2,
mean = sqrt(1:10),
stat = 1,
statname = "SE",
treatment_id = 1:10
test_that("`run.meta.analysis.pft` throws an error if it cannot find output from get.trait", {
pft <- list(outdir = "", name = "ebifarm.salix")
expect_error(
run.meta.analysis.pft(pft = pft, iterations = 1, dbfiles = NULL, dbcon = NULL),
"Could not find output from get.trait"
)
i <- sapply(testresult, is.factor)
testresult[i] <- lapply(testresult[i], as.character)

jagged.data <- jagify(testresult)
expect_equal(jagged.data$trt_num[jagged.data$trt == "control"], c(1, 1))
})

test_that("`run.meta.analysis.pft` throws an error for missing posteriorid", {
pft <- list(outdir = "test", name = "ebifarm.salix")
mockery::stub(run.meta.analysis.pft, 'file.exists', TRUE)
expect_error(
run.meta.analysis.pft(pft = pft, iterations = 1, dbfiles = NULL, dbcon = NULL, update = TRUE),
"Missing posteriorid"
)
})

0 comments on commit e3e21ba

Please sign in to comment.