Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
Fixed error when no sdm.mod given.
  • Loading branch information
r-a-dobson committed Oct 10, 2023
1 parent da29ba2 commit 7555d71
Show file tree
Hide file tree
Showing 2 changed files with 241 additions and 3 deletions.
9 changes: 6 additions & 3 deletions R/dynamic_proj.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ dynamic_proj <- function(dates,
drive.folder,
user.email,
sdm.mod,
sdm.thresh = 0.5,
sdm.weight = 1,
sdm.thresh,
sdm.weight ,
sam.mod,
sam.weight = 1,
sam.weight,
save.directory,
save.drive.folder,
cov.file.type,
Expand All @@ -155,6 +155,7 @@ dynamic_proj <- function(dates,
# Set thresholds
if (!missing(sdm.mod) && missing(sdm.thresh)) {
message("No sdm.thresh. Default 0.5")
sdm.thresh <- 0.5
}

if (missing(proj.prj)) {
Expand All @@ -168,6 +169,7 @@ dynamic_proj <- function(dates,
}
if (!missing(sdm.mod) && missing(sdm.weight)) {
message("No sdm.weight specified. Default equal weighting.")
sdm.weight <- 1
}

if (!missing(sdm.mod)){
Expand All @@ -183,6 +185,7 @@ dynamic_proj <- function(dates,
if (missing(sam.weight)) {
message("No sam.weight specified. Default equal weighting")
}
sam.weight <- 1
if (!length(sam.weight) == length(sam.mod) && !length(sam.weight) == 1) {
stop(
"sam.weight should be of length(1) or equal to number of sam models."
Expand Down
235 changes: 235 additions & 0 deletions tests/testthat/test-spatiotemp_check.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@

data(sample_occ_data)
sample_occ_data <- convert_gbif(sample_occ_data)

test_that("stops if no occcurence data provided", {
expect_error(spatiotemp_check(na.handle = "exclude", duplicate.handle =
"exclude"))
})

test_that("stops if no occcurence data not a data frame", {
expect_error(
spatiotemp_check(
occ.data = "dataframe",
na.handle = "exclude",
duplicate.handle = "exclude"
)
)
})

test_that("duplicates removed", {
checked <- spatiotemp_check(
occ.data = sample_occ_data,
duplicate.handle = "exclude",
date.res = "day"
)
expect_equal(nrow(checked), 315)
})

test_that("NAs removed", {
checked <- spatiotemp_check(occ.data = sample_occ_data,
na.handle = "exclude",
date.res = "day")
expect_equal(nrow(checked), 594)
})

test_that("identifies missing year col", {
expect_error(spatiotemp_check(
occ.data = subset(sample_occ_data, select = -c(year)),
date.res = "day"
))
})
test_that("identifies missing month col", {
expect_error(spatiotemp_check(
occ.data = subset(sample_occ_data, select = -c(month)),
date.res = "day"
))
})
test_that("identifies missing day col", {
expect_error(spatiotemp_check(
occ.data = subset(sample_occ_data, select = -c(day)),
date.res = "day"
))
})
test_that("identifies missing x col", {
expect_error(spatiotemp_check(
occ.data = subset(sample_occ_data, select = -c(x)),
date.res = "day"
))
})
test_that("identifies missing y col", {
expect_error(spatiotemp_check(
occ.data = subset(sample_occ_data, select = -c(y)),
date.res = "day"
))
})

test_that("identifies wrong year class", {
wrong.class <- sample_occ_data
wrong.class$year <- as.character(wrong.class$year)
expect_error(spatiotemp_check(occ.data = wrong.class, date.res = "day"))
})

test_that("identifies wrong month class", {
wrong.class <- sample_occ_data
wrong.class$month <- as.character(wrong.class$month)
expect_error(spatiotemp_check(occ.data = wrong.class, date.res = "day"))
})

test_that("identifies wrong day class", {
wrong.class <- sample_occ_data
wrong.class$day <- as.character(wrong.class$day)
expect_error(spatiotemp_check(occ.data = wrong.class, date.res = "day"))
})

test_that("identifies wrong x class", {
wrong.class <- sample_occ_data
wrong.class$x <- as.character(wrong.class$x)
expect_error(spatiotemp_check(occ.data = wrong.class, date.res = "day"))
})

test_that("identifies wrong y class", {
wrong.class <- sample_occ_data
wrong.class$x <- as.character(wrong.class$x)
expect_error(spatiotemp_check(occ.data = wrong.class, date.res = "day"))
})

test_that("excludes invalid date", {
expect_equal(nrow(
spatiotemp_check(
occ.data = sample_occ_data,
date.handle = "exclude",
date.res = "day"
)
), 597)
})

test_that("ignores invalid date", {
expect_equal(nrow(
spatiotemp_check(
occ.data = sample_occ_data,
date.handle = "ignore",
date.res = "day"
)
), 600)
})

test_that("excludes invalid x", {
expect_equal(nrow(
spatiotemp_check(
occ.data = sample_occ_data,
coord.handle = "exclude",
date.res = "day"
)
), 594)
})

test_that("ignores invalid x", {
expect_equal(nrow(
spatiotemp_check(
occ.data = sample_occ_data,
coord.handle = "ignore",
date.res = "day"
)
), 600)
})

test_that("excludes invalid y", {
expect_equal(nrow(
spatiotemp_check(
occ.data = sample_occ_data,
coord.handle = "exclude",
date.res = "day"
)
), 594)
})

test_that("ignores invalid y", {
expect_equal(nrow(
spatiotemp_check(
occ.data = sample_occ_data,
coord.handle = "ignore",
date.res = "day"
)
), 600)
})

test_that("All work together", {
expect_equal(nrow(
spatiotemp_check(
occ.data = sample_occ_data,
coord.handle = "exclude",
date.res = "day",
date.handle = "exclude",
duplicate.handle = "exclude",
na.handle = "exclude"
)
), 311)
})

test_that("Returns all columns", {
expect_equal(ncol(
spatiotemp_check(
occ.data = sample_occ_data,
coord.handle = "exclude",
date.res = "day",
date.handle = "exclude",
duplicate.handle = "exclude",
na.handle = "exclude"
)
), 9)
})

testthat::test_that("CoordinateCleaner works (correct ncol)", {
testthat::skip_on_cran()
testthat::skip_if_offline()
expect_equal(ncol(
spatiotemp_check(
occ.data = sample_occ_data,
coord.handle = "exclude",
date.res = "day",
date.handle = "exclude",
duplicate.handle = "exclude",
na.handle = "exclude",
coordclean = T,
coordclean.species = "quelea",
coordclean.handle = "exclude"
)
), 9)
})

test_that("CoordinateCleaner works (correct nrow)", {
testthat::skip_on_cran()
testthat::skip_if_offline()
expect_equal(nrow(
spatiotemp_check(
occ.data = sample_occ_data,
coord.handle = "exclude",
date.res = "day",
date.handle = "exclude",
duplicate.handle = "exclude",
na.handle = "exclude",
coordclean = T,
coordclean.species = "quelea",
coordclean.handle = "exclude"
)
), 301)
})

test_that("CoordinateCleaner works return report", {
testthat::skip_on_cran()
testthat::skip_if_offline()
expect_equal(ncol(
spatiotemp_check(
occ.data = sample_occ_data,
coord.handle = "exclude",
date.handle = "exclude",
duplicate.handle = "exclude",
na.handle = "exclude",
date.res = "day",
coordclean = T,
coordclean.species = "quelea",
coordclean.handle = "report"
)
), ncol(sample_occ_data)+2)
})

0 comments on commit 7555d71

Please sign in to comment.