Skip to content

Commit 9f796b6

Browse files
committed
fix ggplot tests
1 parent 1766915 commit 9f796b6

File tree

6 files changed

+477
-442
lines changed

6 files changed

+477
-442
lines changed

R/checks.R

Lines changed: 115 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,115 @@
1-
# Author: Roozbeh Valavi
2-
# contact: valavi.r@gmail.com
3-
# Date : May 2023
4-
# Version 0.2
5-
# Licence GPL v3
6-
7-
# check for x
8-
.check_x <- function(x, name = "x"){
9-
if(!methods::is(x, "sf")){
10-
tryCatch(
11-
{
12-
x <- sf::st_as_sf(x)
13-
},
14-
error = function(cond) {
15-
message(sprintf("'%s' is not convertible to an sf object!", name))
16-
message(sprintf("'%s' must be an sf or spatial* object.", name))
17-
}
18-
)
19-
}
20-
return(x)
21-
}
22-
23-
# check for column matching colnames(x)
24-
.check_column <- function(column, x){
25-
if(!is.null(column)){
26-
if(!column %in% colnames(x)){
27-
warning(sprintf("There is no column named '%s' in 'x'. Column is ignored!\n", column))
28-
column <- NULL
29-
}
30-
}
31-
return(column)
32-
}
33-
34-
# column should be binary or categorical
35-
.check_classes <- function(clen, column, th = 15){
36-
if(clen > th){
37-
warning(
38-
sprintf(
39-
paste(
40-
"The are too many unique values in '%s'.",
41-
"Use 'column' only for binary or categorical responses (ignore this if it is).\n"
42-
),
43-
column
44-
)
45-
)
46-
}
47-
}
48-
49-
# check for r
50-
.check_r <- function(r, name = "r"){
51-
if(!methods::is(r, "SpatRaster")){
52-
tryCatch(
53-
{
54-
r <- terra::rast(r)
55-
},
56-
error = function(cond) {
57-
message(sprintf("'%s' is not convertible to a terra SpatRaster object!", name))
58-
message(sprintf("'%s' must be a SpatRaster, stars, Raster* object, or path to a raster file on disk.", name))
59-
}
60-
)
61-
}
62-
return(r)
63-
}
64-
65-
# check for required packages
66-
.check_pkgs <- function(pkg){
67-
pkgna <- names(which(sapply(sapply(pkg, find.package, quiet = TRUE), length) == 0))
68-
if(length(pkgna) > 0){
69-
nm <- paste(pkgna, collapse = ", ")
70-
message("This function requires these packages: ", nm, "\nWould you like to install them now?\n1: yes\n2: no")
71-
user <- readline(prompt = paste0("Selection: "))
72-
if(tolower(user) %in% c("1", "yes", "y")){
73-
utils::install.packages(pkgna)
74-
} else{
75-
stop("Please install these packages for function to work: ", nm)
76-
}
77-
}
78-
}
1+
# Author: Roozbeh Valavi
2+
# contact: valavi.r@gmail.com
3+
# Date : May 2023
4+
# Version 0.2
5+
# Licence GPL v3
6+
7+
# check points fall within the raster layer
8+
.check_within <- function(x, r) {
9+
bbox <- sf::st_bbox(x)
10+
ex <- terra::ext(r)
11+
12+
y <- bbox[1] >= terra::xmin(ex) &
13+
bbox[3] <= terra::xmax(ex) &
14+
bbox[2] >= terra::ymin(ex) &
15+
bbox[4] <= terra::ymax(ex)
16+
17+
if(!y) stop("The x's bounding box lies outside the raster extent.")
18+
}
19+
20+
21+
# check for x
22+
.check_x <- function(x, name = "x"){
23+
if(!methods::is(x, "sf")){
24+
tryCatch(
25+
{
26+
x <- sf::st_as_sf(x)
27+
},
28+
error = function(cond) {
29+
message(sprintf("'%s' is not convertible to an sf object!", name))
30+
message(sprintf("'%s' must be an sf or spatial* object.", name))
31+
}
32+
)
33+
}
34+
return(x)
35+
}
36+
37+
# check for column matching colnames(x)
38+
.check_column <- function(column, x){
39+
if(!is.null(column)){
40+
if(!column %in% colnames(x)){
41+
warning(sprintf("There is no column named '%s' in 'x'. Column is ignored!\n", column))
42+
column <- NULL
43+
}
44+
}
45+
return(column)
46+
}
47+
48+
# column should be binary or categorical
49+
.check_classes <- function(clen, column, th = 15){
50+
if(clen > th){
51+
warning(
52+
sprintf(
53+
paste(
54+
"The are too many unique values in '%s'.",
55+
"Use 'column' only for binary or categorical responses (ignore this if it is).\n"
56+
),
57+
column
58+
)
59+
)
60+
}
61+
}
62+
63+
64+
# check raster extent is valid
65+
.check_ext <- function(r) {
66+
tryCatch(
67+
{
68+
e <- terra::ext(r)
69+
vals <- e[1:4]
70+
},
71+
error = function(cond) {
72+
stop("Failed to extract raster extent: ", conditionMessage(cond))
73+
}
74+
)
75+
76+
y <- all(is.finite(vals)) &&
77+
(terra::xmax(e) > terra::xmin(e)) &&
78+
(terra::ymax(e) > terra::ymin(e))
79+
80+
if (!y) stop("Invalid raster extent: values are non-finite, degenerate, or out of range.")
81+
}
82+
83+
# check for r
84+
.check_r <- function(r, name = "r"){
85+
if(!methods::is(r, "SpatRaster")){
86+
tryCatch(
87+
{
88+
r <- terra::rast(r)
89+
},
90+
error = function(cond) {
91+
message(sprintf("'%s' is not convertible to a terra SpatRaster object!", name))
92+
message(sprintf("'%s' must be a SpatRaster, stars, Raster* object, or path to a raster file on disk.", name))
93+
}
94+
)
95+
}
96+
.check_ext(r) # check for valid extent
97+
98+
return(r)
99+
}
100+
101+
# check for required packages
102+
.check_pkgs <- function(pkg){
103+
pkgna <- names(which(sapply(sapply(pkg, find.package, quiet = TRUE), length) == 0))
104+
if(length(pkgna) > 0){
105+
nm <- paste(pkgna, collapse = ", ")
106+
message("This function requires these packages: ", nm, "\nWould you like to install them now?\n1: yes\n2: no")
107+
user <- readline(prompt = paste0("Selection: "))
108+
if(tolower(user) %in% c("1", "yes", "y")){
109+
utils::install.packages(pkgna)
110+
} else{
111+
stop("Please install these packages for function to work: ", nm)
112+
}
113+
}
114+
}
115+

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ status](https://github.com/rvalavi/blockCV/workflows/R-CMD-check/badge.svg)](htt
55
[![codecov](https://codecov.io/gh/rvalavi/blockCV/branch/master/graph/badge.svg)](https://codecov.io/gh/rvalavi/blockCV)
66
[![CRAN
77
version](https://www.r-pkg.org/badges/version/blockCV)](https://CRAN.R-project.org/package=blockCV)
8-
[![total](https://cranlogs.r-pkg.org/badges/grand-total/blockCV)](https://www.rpackages.io/package/blockCV)
8+
[![total](https://cranlogs.r-pkg.org/badges/grand-total/blockCV)](https://CRAN.R-project.org/package=blockCV)
99
[![License](https://img.shields.io/badge/license-GPL%20(%3E=%203)-lightgrey.svg?style=flat)](http://www.gnu.org/licenses/gpl-3.0.html)
1010
[![DOI](https://zenodo.org/badge/116337503.svg)](https://zenodo.org/badge/latestdoi/116337503)
11+
[![Methods in Ecology & Evolution](https://img.shields.io/badge/Methods%20in%20Ecology%20&%20Evolution-10,%20225-232-blue.svg)](https://doi.org/10.1111/2041-210X.13107)
1112

1213
### Spatial and environmental blocking for k-fold and LOO cross-validation
1314

tests/testthat/test-cv_plot.R

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
pa_data <- read.csv(system.file("extdata/", "species.csv", package = "blockCV")) |>
2-
sf::st_as_sf(coords = c("x", "y"), crs = 7845)
3-
4-
5-
test_that("test that cv_plot function works",
6-
{
7-
scv <- cv_spatial(x = pa_data,
8-
size = 450000,
9-
k = 5,
10-
selection = "random",
11-
iteration = 1,
12-
biomod2 = FALSE,
13-
plot = FALSE,
14-
progress = FALSE)
15-
16-
plt <- cv_plot(cv = scv, x = pa_data)
17-
18-
expect_true(exists("plt"))
19-
expect_s3_class(plt, "ggplot")
20-
expect_type(plt, "list")
21-
22-
})
1+
pa_data <- read.csv(system.file("extdata/", "species.csv", package = "blockCV")) |>
2+
sf::st_as_sf(coords = c("x", "y"), crs = 7845)
3+
4+
5+
test_that("test that cv_plot function works",
6+
{
7+
scv <- cv_spatial(x = pa_data,
8+
size = 450000,
9+
k = 5,
10+
selection = "random",
11+
iteration = 1,
12+
biomod2 = FALSE,
13+
plot = FALSE,
14+
progress = FALSE)
15+
16+
plt <- cv_plot(cv = scv, x = pa_data)
17+
18+
expect_true(exists("plt"))
19+
expect_true(ggplot2::is_ggplot(plt))
20+
21+
})

tests/testthat/test-cv_similarity.R

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
1-
aus <- system.file("extdata/au/", package = "blockCV") |>
2-
list.files(full.names = TRUE) |>
3-
terra::rast()
4-
5-
pa_data <- read.csv(system.file("extdata/", "species.csv", package = "blockCV")) |>
6-
sf::st_as_sf(coords = c("x", "y"), crs = 7845)
7-
pa_data <- pa_data[1:200, ]
8-
9-
test_that("test that cv_similarity function works with cv_spatil",
10-
{
11-
scv <- cv_spatial(x = pa_data,
12-
selection = "random",
13-
iteration = 1,
14-
biomod2 = FALSE,
15-
plot = FALSE,
16-
report = FALSE,
17-
progress = FALSE)
18-
19-
plt <- cv_similarity(cv = scv, x = pa_data, r = aus)
20-
21-
expect_true(exists("plt"))
22-
expect_s3_class(plt, "ggplot")
23-
expect_type(plt, "list")
24-
25-
})
26-
27-
28-
test_that("test that cv_similarity function works with cv_buffer",
29-
{
30-
bloo <- cv_buffer(x = pa_data,
31-
size = 250000,
32-
progress = FALSE,
33-
report = FALSE)
34-
35-
plt <- cv_similarity(cv = bloo, x = pa_data, r = aus)
36-
37-
expect_true(exists("plt"))
38-
expect_s3_class(plt, "ggplot")
39-
expect_type(plt, "list")
40-
41-
})
1+
aus <- system.file("extdata/au/", package = "blockCV") |>
2+
list.files(full.names = TRUE) |>
3+
terra::rast()
4+
5+
pa_data <- read.csv(system.file("extdata/", "species.csv", package = "blockCV")) |>
6+
sf::st_as_sf(coords = c("x", "y"), crs = 7845)
7+
pa_data <- pa_data[1:200, ]
8+
9+
test_that("test that cv_similarity function works with cv_spatil",
10+
{
11+
scv <- cv_spatial(x = pa_data,
12+
selection = "random",
13+
iteration = 1,
14+
biomod2 = FALSE,
15+
plot = FALSE,
16+
report = FALSE,
17+
progress = FALSE)
18+
19+
plt <- cv_similarity(cv = scv, x = pa_data, r = aus)
20+
21+
expect_true(exists("plt"))
22+
expect_true(ggplot2::is_ggplot(plt))
23+
24+
})
25+
26+
27+
test_that("test that cv_similarity function works with cv_buffer",
28+
{
29+
bloo <- cv_buffer(x = pa_data,
30+
size = 250000,
31+
progress = FALSE,
32+
report = FALSE)
33+
34+
plt <- cv_similarity(cv = bloo, x = pa_data, r = aus)
35+
36+
expect_true(exists("plt"))
37+
expect_true(ggplot2::is_ggplot(plt))
38+
39+
})

0 commit comments

Comments
 (0)