diff --git a/.Rbuildignore b/.Rbuildignore new file mode 100644 index 0000000..75498dd --- /dev/null +++ b/.Rbuildignore @@ -0,0 +1,31 @@ +^.*\.Rproj$ +^\.Rproj\.user$ +^_pkgdown\.yml$ +.*\.tar\.gz$ +.*\.toc$ +.*\.zip$ +^\.lintr$ + + +CONTRIBUTING\.md +cran-comments\.md +^docs$ +^LICENSE$ +vignettes/.*_cache$ +vignettes/.*\.log$ + +^\.httr-oauth$ +^revdep$ +^\.github$ +^codecov\.yml$ +^CRAN-RELEASE$ +^data/* +^.git +^.gitignore + +^.gitmodules + +Biomass_borealDataPrep.* +.*zip +citation.* +figures diff --git a/.github/workflows/render-module-rmd.yaml b/.github/workflows/render-module-rmd.yaml index 7fa9d97..55626c7 100644 --- a/.github/workflows/render-module-rmd.yaml +++ b/.github/workflows/render-module-rmd.yaml @@ -3,6 +3,7 @@ on: branches: - main - master + - development push: branches: - main diff --git a/Biomass_borealDataPrep.R b/Biomass_borealDataPrep.R index 8ba0ac2..00d9377 100644 --- a/Biomass_borealDataPrep.R +++ b/Biomass_borealDataPrep.R @@ -18,7 +18,7 @@ defineModule(sim, list( loadOrder = list(after = c("Biomass_speciesData"), before = c("Biomass_core")), reqdPkgs = list("assertthat", "crayon", "data.table", "dplyr", "fasterize", "ggplot2", - "merTools", "plyr", "rasterVis", "sf", "terra", + "merTools", "plyr", "rasterVis", "sf", "terra", "Require", "reproducible (>= 2.1.0)", "SpaDES.core (>= 2.1.0)", "SpaDES.tools (>= 2.0.0)", "PredictiveEcology/LandR (>= 1.1.1)", @@ -1344,6 +1344,13 @@ plottingFn <- function(sim) { ) } +#' Save files +#' +#' Generic function using `saveFiles` +#' +#' @return For side effect, saved files as per sim +#' +#' @param sim A simList Save <- function(sim) { sim <- saveFiles(sim) return(invisible(sim)) diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..70a70d4 --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,48 @@ +Package: Biomass.borealDataPrep +Type: Package +Title: Biomass_borealDataPrep +Version: 1.5.7.9002 +Description: paste A data preparation module for parameterizing `Biomass_core` from open data sources, within the Boreal forest of Canada. +Date: 2024-06-13 +Authors@R: + c(person("Yong", "Luo", email = "Yong.Luo@gov.bc.ca", role = c("aut")), + person(c("Eliot", "J", "B"), "McIntire", email = "eliot.mcintire@nrcan-rncan.gc.ca", + role = c("aut", "cre")), person(c("Ceres"), "Barros", + email = "ceres.barros@ubc.ca", role = c("aut")), person(c("Alex", + "M."), "Chubaty", email = "achubaty@for-cast.ca", role = c("aut"))) +Imports: + assertthat, + crayon, + data.table, + dplyr, + fasterize, + ggplot2, + LandR (>= 1.1.1), + merTools, + pemisc, + plyr, + rasterVis, + reproducible (>= 2.1.0), + sf, + SpaDES.core (>= 2.1.0), + SpaDES.project (>= 0.0.8.9026), + SpaDES.tools (>= 2.0.0), + terra +Suggests: + knitr, + RCurl, + rmarkdown, + roxygen2, + testthat, + withr, + XML +Remotes: + PredictiveEcology/LandR, + PredictiveEcology/pemisc@development, + PredictiveEcology/SpaDES.project@development +Encoding: UTF-8 +License: GPL-3 +VignetteBuilder: knitr, rmarkdown +ByteCompile: yes +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.3.1 diff --git a/NAMESPACE b/NAMESPACE index cf8d424..d9d81f0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,19 @@ # Generated by roxygen2: do not edit by hand +export(spinUpPartial) +import(SpaDES.core) +import(SpaDES.project) +import(SpaDES.tools) +import(assertthat) +import(dplyr) +import(fasterize) +import(ggplot2) +import(pemisc) +import(plyr) +import(rasterVis) +import(reproducible) +import(sf) +import(terra) importFrom(LandR,asInteger) importFrom(LandR,subsetDT) importFrom(crayon,cyan) diff --git a/R/imports.R b/R/imports.R new file mode 100644 index 0000000..915528b --- /dev/null +++ b/R/imports.R @@ -0,0 +1,42 @@ +#' @import assertthat +NULL + +#' @import dplyr +NULL + +#' @import fasterize +NULL + + +#' @import ggplot2 +NULL + +#' @import plyr +NULL + +#' @import rasterVis +NULL + +#' @import sf +NULL + + +#' @import terra +NULL + +#' @import reproducible +NULL + +#' @import SpaDES.core +NULL + + +#' @import SpaDES.tools +NULL + +#' @import SpaDES.project +NULL + +#' @import pemisc +NULL + diff --git a/R/updateYoungBiomasses.R b/R/updateYoungBiomasses.R index ea45f62..e34608e 100644 --- a/R/updateYoungBiomasses.R +++ b/R/updateYoungBiomasses.R @@ -1,3 +1,5 @@ +#' Update young biomasses +#' #' @param young data.table not unlinke `cohortData` #' @param modelBiomass named list with items "mod", "pred", "rsq", "scaledVarsModelB". #' @param ... For anything, used for Cache. Not used internally here. @@ -190,7 +192,7 @@ spinUpPartial <- function(pixelCohortData, speciesEcoregion, maxAge, } else { modules <- "Biomass_core" } - + outputs <- data.frame(expand.grid(objectName = "cohortData", saveTime = unique(seq(times$start, times$end, by = 1)), eventPriority = 1, fun = "qs::qsave", diff --git a/man/spinUpPartial.Rd b/man/spinUpPartial.Rd new file mode 100644 index 0000000..04049a4 --- /dev/null +++ b/man/spinUpPartial.Rd @@ -0,0 +1,51 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/updateYoungBiomasses.R +\name{spinUpPartial} +\alias{spinUpPartial} +\title{Update Biomass using a spinup type approach} +\usage{ +spinUpPartial( + pixelCohortData, + speciesEcoregion, + maxAge, + minRelativeB, + species, + sppEquiv, + sppEquivCol, + sppColorVect, + paths, + currentModule, + modules +) +} +\arguments{ +\item{pixelCohortData}{A pixelCohortData object (must have columns pixelIndex, +ecoregionGroup, age, and B). Should only have rows where the +age <= maxAge. +It should have none where age = 0} + +\item{speciesEcoregion}{A speciesEcoregion object (must have ecoregionGroup, maxB, maxANPP)} + +\item{maxAge}{A numeric scalar, indicating the maximum age to simulation biomass. +This should correspond to the maximum age in pixelCohortData} + +\item{minRelativeB}{A minRelativeB object} + +\item{species}{A species table with species-level parameters} + +\item{sppColorVect}{A sppColorVect object (required, but not used)} + +\item{paths}{A list of spades paths e.g., from paths(sim)} + +\item{currentModule}{A character string of the current module e.g., from currentModule(sim)} + +\item{modules}{A list of character strings of the modules in the sim, e.g., from modules(sim)} +} +\description{ +This function takes a pixelCohortData, and updates all the B values according to +the succession and growth dynamics provided by \code{species} and \code{speciesEcoregion} +objects. It does this by setting B to zero in each pixelIndex, growing each pixelIndex +until it hits the age given by age, for each pixelIndex independently. This uses +Biomass_core module to do this. If this module does not exist in the modules(sim), +then it will download it and use the latest development branch version of Biomass_core. +} diff --git a/man/updateYoungBiomasses.Rd b/man/updateYoungBiomasses.Rd new file mode 100644 index 0000000..208b098 --- /dev/null +++ b/man/updateYoungBiomasses.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/updateYoungBiomasses.R +\name{updateYoungBiomasses} +\alias{updateYoungBiomasses} +\title{Update young biomasses} +\usage{ +updateYoungBiomasses(young, modelBiomass, ...) +} +\arguments{ +\item{young}{data.table not unlinke \code{cohortData}} + +\item{modelBiomass}{named list with items "mod", "pred", "rsq", "scaledVarsModelB".} + +\item{...}{For anything, used for Cache. Not used internally here.} +} +\description{ +Update young biomasses +} diff --git a/tests/test-all.R b/tests/test-all.R new file mode 100644 index 0000000..b915809 --- /dev/null +++ b/tests/test-all.R @@ -0,0 +1,2 @@ +library(testthat) +test_check("Biomass.borealDataPrep") diff --git a/tests/testthat/test-template.R b/tests/testthat/test-template.R index 7dda4d7..991ebca 100644 --- a/tests/testthat/test-template.R +++ b/tests/testthat/test-template.R @@ -7,73 +7,76 @@ # 2. Copy this file to the tests folder (i.e., `~/GitHub/LandWeb/Biomass_borealDataPrep/tests/testthat`). # 3. Modify the test description based on the content you are testing: -test_that("test Event1 and Event2.", { - module <- list("Biomass_borealDataPrep") - path <- list(modulePath = "C:/Users/yonluo/Documents/GitHub/LandWeb", - outputPath = file.path(tempdir(), "outputs")) - parameters <- list( - #.progress = list(type = "graphical", interval = 1), - .globals = list(verbose = FALSE), - Biomass_borealDataPrep = list(.saveInitialTime = NA) - ) - times <- list(start = 0, end = 1) - - # If your test function contains `time(sim)`, you can test the function at a - # particular simulation time by defining the start time above. - object1 <- "object1" # please specify - object2 <- "object2" # please specify - objects <- list("object1" = object1, "object2" = object2) - - mySim <- simInit(times = times, - params = parameters, - modules = module, - objects = objects, - paths = path) - - # You may need to set the random seed if your module or its functions use the - # random number generator. - set.seed(1234) - - # You have two strategies to test your module: - # 1. Test the overall simulation results for the given objects, using the - # sample code below: +test_that("test Event1 and Event2", { + origLibPaths <- .libPaths() + on.exit(.libPaths(origLibPaths)) + origDir <- getwd() + td <- reproducible::tempdir2() + moduleFile <- dir(file.path("..", ".."), pattern = "\\.R$", full.names = TRUE) + moduleName <- basename(gsub("\\.R", "", moduleFile)) + modulePathIn <- file.path(td, moduleName) + reproducible::checkPath(modulePathIn, create = TRUE) + modulePath <- dirname(modulePathIn) + file.copy(moduleFile, file.path(modulePathIn, basename(moduleFile))) + withr::local_options("SpaDES.project.updateRprofile" = FALSE) - output <- spades(mySim, debug = FALSE) - - # is output a simList? - expect_is(output, "simList") - - # does output have your module in it - expect_true(any(unlist(modules(output)) %in% c(unlist(module)))) - - # did it simulate to the end? - expect_true(time(output) == 1) - - # 2. Test the functions inside of the module using the sample code below: - # To allow the `moduleCoverage` function to calculate unit test coverage - # level, it needs access to all functions directly. - # Use this approach when using any function within the simList object - # (i.e., one version as a direct call, and one with `simList` object prepended). + warns <- capture_warning( + out <- SpaDES.project::setupProject(paths = list(projectPath = td, + modulePath = modulePath, + packagePath = .libPaths()[1]), + modules = moduleName, + times = list(start = 0, end = 1)) + ) + sa <- setupStudyArea(list(NAME_1 = "Alberta", "NAME_2" = "Division No. 17", level = 2)) + expect_error(outFinal <- SpaDES.core::simInitAndSpades2(out), regexp = "Please provide a.*polygon") + out$studyArea <- sa + expect_error(outFinal <- SpaDES.core::simInitAndSpades2(out), regexp = "Please provide a.*Large.*polygon") + out$studyAreaLarge <- terra::buffer(sa, 1000) + outFinal <- SpaDES.core::simInitAndSpades2(out) - if (exists("Biomass_coreDataPrepEvent1", envir = .GlobalEnv)) { - simOutput <- Biomass_coreDataPrepEvent1(mySim) - } else { - simOutput <- mySim$Biomass_coreDataPrepEvent1(mySim) - } + skip("rest is not yet tested") - expectedOutputEvent1Test1 <- " this is test for event 1. " # please define your expection of your output - expect_is(class(simOutput$event1Test1), "character") - expect_equal(simOutput$event1Test1, expectedOutputEvent1Test1) # or other expect function in testthat package. - expect_equal(simOutput$event1Test2, as.numeric(999)) # or other expect function in testthat package. + # module <- list(moduleName) + # path <- list(modulePath = modulePath, + # outputPath = file.path(td, "outputs")) + # parameters <- list( + # #.progress = list(type = "graphical", interval = 1), + # .globals = list(verbose = FALSE), + # Biomass_borealDataPrep = list(.saveInitialTime = NA) + # ) + # times <- list(start = 0, end = 1) - if (exists("Biomass_coreDataPrepEvent2", envir = .GlobalEnv)) { - simOutput <- Biomass_coreDataPrepEvent2(mySim) - } else { - simOutput <- mySim$Biomass_coreDataPrepEvent2(mySim) - } + # If your test function contains `time(sim)`, you can test the function at a + # particular simulation time by defining the start time above. + # object1 <- "object1" # please specify + # object2 <- "object2" # please specify + # objects <- list("object1" = object1, "object2" = object2) + # + # mySim <- simInit(times = times, + # params = parameters, + # modules = module, + # objects = objects, + # paths = path) - expectedOutputEvent2Test1 <- " this is test for event 2. " # please define your expection of your output - expect_is(class(simOutput$event2Test1), "character") - expect_equal(simOutput$event2Test1, expectedOutputEvent2Test1) # or other expect function in testthat package. - expect_equal(simOutput$event2Test2, as.numeric(777)) # or other expect function in testthat package. + # if (exists("Biomass_coreDataPrepEvent1", envir = .GlobalEnv)) { + # simOutput <- Biomass_coreDataPrepEvent1(mySim) + # } else { + # simOutput <- mySim$Biomass_coreDataPrepEvent1(mySim) + # } + # + # expectedOutputEvent1Test1 <- " this is test for event 1. " # please define your expection of your output + # expect_is(class(simOutput$event1Test1), "character") + # expect_equal(simOutput$event1Test1, expectedOutputEvent1Test1) # or other expect function in testthat package. + # expect_equal(simOutput$event1Test2, as.numeric(999)) # or other expect function in testthat package. + # + # if (exists("Biomass_coreDataPrepEvent2", envir = .GlobalEnv)) { + # simOutput <- Biomass_coreDataPrepEvent2(mySim) + # } else { + # simOutput <- mySim$Biomass_coreDataPrepEvent2(mySim) + # } + # + # expectedOutputEvent2Test1 <- " this is test for event 2. " # please define your expection of your output + # expect_is(class(simOutput$event2Test1), "character") + # expect_equal(simOutput$event2Test1, expectedOutputEvent2Test1) # or other expect function in testthat package. + # expect_equal(simOutput$event2Test2, as.numeric(777)) # or other expect function in testthat package. }) diff --git a/tests/unitTests.R b/tests/unitTests.R deleted file mode 100644 index b49b901..0000000 --- a/tests/unitTests.R +++ /dev/null @@ -1,9 +0,0 @@ - -# Please build your own test file from test-Template.R, and place it in tests folder -# please specify the package you need to run the sim function in the test files. - -# to test all the test files in the tests folder: -test_dir("C:/Users/yonluo/Documents/GitHub/LandWeb/Biomass_borealDataPrep/tests/testthat") - -# Alternative, you can use test_file to test individual test file, e.g.: -test_file("C:/Users/yonluo/Documents/GitHub/LandWeb/Biomass_borealDataPrep/tests/testthat/test-template.R")