Skip to content

Commit

Permalink
add test for mindist
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoodbody committed Jun 12, 2023
1 parent 2235019 commit 949336a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/testthat/setup-testthat.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library(terra)
library(dplyr)
library(sf)

#--- metrics raster ---#
mr <- system.file("extdata", "mraster.tif", package = "sgsR")
Expand Down
36 changes: 36 additions & 0 deletions tests/testthat/test-sample_strat.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,39 @@ test_that("category column", {

expect_equal(ncol(sample_strat(xx, nSamp = 1000, method = "random")), 4)
})


test_that("mindist", {
samples <- vect(sample_strat(sraster = sraster, nSamp = 200, mindist = 200))

# Calculate pairwise distances
distances <- terra::distance(samples, samples)

# Exclude distances between the same point (diagonal entries)
diag(distances) <- Inf
distances[upper.tri(distances)] <- Inf

# Check if any distance is smaller than the threshold
any_close <- distances < 200

indices <- which(any_close, arr.ind = TRUE)

expect_equal(nrow(indices),0)

#--- random ---#
samples <- vect(sample_strat(sraster = sraster, nSamp = 200, mindist = 200, method = "random"))

# Calculate pairwise distances
distances <- terra::distance(samples, samples)

# Exclude distances between the same point (diagonal entries)
diag(distances) <- Inf
distances[upper.tri(distances)] <- Inf

# Check if any distance is smaller than the threshold
any_close <- distances < 200

indices <- which(any_close, arr.ind = TRUE)

expect_equal(nrow(indices),0)
})

0 comments on commit 949336a

Please sign in to comment.