Skip to content

Commit

Permalink
unit tests & documentation for mask_userSDM
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohnso005 committed Oct 10, 2024
1 parent ef9665d commit 1a06288
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 17 deletions.
58 changes: 45 additions & 13 deletions R/mask_userSDM.R
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@
#'
# Wallace EcoMod: a flexible platform for reproducible modeling of
# species niches and distributions.
#
# mask_userSDM.R
# File author: Wallace EcoMod Dev Team. 2023.
# --------------------------------------------------------------------------
# This file is part of the Wallace EcoMod application
# (hereafter “Wallace”).
#
# Wallace is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# Wallace is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Wallace. If not, see <http://www.gnu.org/licenses/>.
# --------------------------------------------------------------------------
#
#' @title mask_userSDM
#' @description Upload user-specified SDM prediction
#'
#' @details
#' See Examples.
#' @details This function uploads a raster of a species' range prediction to be
#' used in Wallace EcoMod. The file name needs to named in Genus_species format
#' (Ex: Bassaricyon_neblina.tif) to have Wallace functionality. The raster can
#' be binary or continuous, but may need to be binary for some further analyses
#' in Wallace. This function returns a list of the range prediction raster as
#' well as a spatial polygon of the extent of the range prediction.
#'
#' @param rasPath character of path to rasters, must be the full path including file name and extension.
#' @param rasPath character of path to raster, must be the full path including file name and extension.
#' Filename need to be name with genus_species format.
#' @param rasName character vector of raster names to be assigned to loaded rasters
#' @param rasName character vector of raster names to be assigned to loaded raster
#' @param logger stores all notification messages to be displayed in the Log Window of Wallace GUI. insert the logger reactive list here for running in shiny,
#' otherwise leave the default NULL
#' @param spN Species name
# @keywords
#'
# @examples
#'
#' @examples
#' \dontrun{
#' ### Set parameters
#' rasPath <- system.file("extdata/Bassaricyon_neblina.tif",package = "wallace")
#' rasName <- "Bassaricyon_neblina.tif"
#' ### Run function
#' sdm <- mask_userSDM(rasPath, rasName, logger = NULL, spN = NULL)
#' }
#'
# @return
#' @return A list of two: the prediction rasterlayer and a spatialpolygon of the extent
#' @author Gonzalo E. Pinilla-Buitrago <gpinillabuitrago@@gradcenter.cuny.edu>
#' @author Jamie Kass <jkass@@gradcenter.cuny.edu>
#' @author Bethany A. Johnson <bjohnso005@@citymail.cuny.edu>
#'
# @keywords
# @note

# @seealso
# @references
# @aliases - a list of additional topic names that will be mapped to
# this documentation when the user looks them up from the command
Expand All @@ -43,15 +75,15 @@ mask_userSDM <- function(rasPath, rasName, logger = NULL, spN = NULL) {
'See guidance text in this module for more details.')
return()
}
smartProgress(logger, message = "Uploading user-specified SDM (**)...", {
smartProgress(logger, message = "Uploading user-specified SDM...", {
r <- raster::trim(r)
names(r) <- fileNameNoExt(rasName)
extPoly <- raster::extent(r)
if (extPoly@xmin < -180 | extPoly@xmax > 180 |
extPoly@ymin < -90 | extPoly@ymax > 90) {
logger %>%
writeLog(
type = "error", "Wrong extent projection. '", rasName,"' cannot be uploaded. (**)")
type = "error", "Wrong extent projection. '", rasName,"' cannot be uploaded. ")
return()
}
extPoly <- methods::as(extPoly, 'SpatialPolygons')
Expand Down
2 changes: 1 addition & 1 deletion man/mask_tempExtract.Rd

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

26 changes: 23 additions & 3 deletions man/mask_userSDM.Rd

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

51 changes: 51 additions & 0 deletions tests/testthat/test_mask_userSDM.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#### COMPONENT mask: Calculate Indicators
#### MODULE: Upload user-specified SDM prediction
context("userSDM")

### Set parameters
rasPath <- system.file("extdata/Bassaricyon_neblina.tif",package = "wallace")
rasName <- "Bassaricyon_neblina.tif"

### Run function
sdm <- mask_userSDM(rasPath, rasName, logger = NULL, spN = NULL)

# to test errors
rasPath_error <- system.file("extdata/B_neblina_WCEA.tif",package = "wallace")
#rasPath_error2 <- system.file("something with the wrong extent. see note below")

################ Tests ####################################

### test if the error messages appear when they are supposed to
test_that("error checks", {
# unspecified CRS
expect_error(mask_userSDM(rasPath_error, rasName, logger = NULL, spN = NULL),
paste0("Input rasters have undefined coordinate reference system (CRS). Mapping functionality will not work. Please define their projections and upload again. See guidance text in this module for more details."),
fixed = TRUE)

# extents do not overlap
# BAJ 10/10/2024: need to upload a raster with xmax > 180 and xmin < -180 to get this error
# expect_error(mask_userSDM(rasPath_error2, rasName, logger = NULL, spN = NULL),
# paste0("Wrong extent projection. '", rasName,"' cannot be uploaded. "))
})


### test if the warning messages appear when they are supposed to
# No warning messages

### test output features
test_that("output checks", {
# list
expect_is(sdm, "list")
# list of 2
expect_equal(length(sdm),2)
# two items should be sdm$sdm & sdm$extSdm
expect_equal(names(sdm), c("sdm", "extSdm"))
# sdm$sdm should be a rasterlayer
expect_is(sdm$sdm, "RasterLayer")
# sdm$extSdm should be a spatialpolygon
expect_is(sdm$extSdm, "SpatialPolygons")
# same extents
expect_equal(raster::extent(sdm$sdm), raster::extent(sdm$extSdm))
})

### test function steps

0 comments on commit 1a06288

Please sign in to comment.