Skip to content

Commit

Permalink
update spectral tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adelegem committed Nov 18, 2024
1 parent 77b7f5d commit 25bd447
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
1 change: 0 additions & 1 deletion R/calculate_field_diversity.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#' field_diversity <- calculate_field_diversity(my.data)
#'


calculate_field_diversity <- function(survey_data){
# get unique site names
ausplot_sites <- unique(survey_data$site_unique)
Expand Down
70 changes: 40 additions & 30 deletions tests/testthat/test-test_spectral.R
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
set.seed(123)
df <- data.frame(
site_name = rep(c("site_one", "site_two", "site_three", "site_four"), each = 5000),
aoi_id = 1,
blue = runif(20000, min = 0, max = 1),
green = runif(20000, min = 0, max = 1),
red = runif(20000, min = 0, max = 1),
red_edge = runif(20000, min = 0, max = 1),
nir = runif(20000, min = 0, max = 1))
set.seed(9)
df_test <- data.frame(
aoi_id = rep(1:2, each = 10),
blue = c(runif(10, 0.1, 0.4), runif(10, 0.4, 0.7)),
green = c(runif(10, 0.2, 0.5), runif(10, 0.5, 0.8)),
red = c(runif(10, 0.3, 0.6), runif(10, 0.6, 0.9))
)

test_that("calculate_cv works", {
pixelvalues <- calculate_cv(df,
wavelengths = c('blue','green','red','red_edge','nir'),
rarefaction = TRUE, min_points = 100, n = 20)
expect_type(pixelvalues,"list")
expect_true(pixelvalues$CV<6)
expect_true(pixelvalues$aoi_id==1)
test_that('calculate_cv works', {
cv <- calculate_cv(df_test,
wavelengths = colnames(df_test[, 2:4]),
rarefaction = F)
expect_equal(round(cv$CV[1], 3), 0.296)
expect_equal(round(cv$CV[2], 3), 0.143)
})

test_that('calculate_sv works', {
sv <- calculate_sv(df_test,
wavelengths = colnames(df_test[, 2:4]))
expect_equal(round(sv$SV[1], 5), 0.02631)
expect_equal(round(sv$SV[2], 5), 0.02660)
})

test_that('calculate_chv_nopca works', {
chv <- calculate_chv_nopca(df_test,
wavelengths = colnames(df_test[, 2:4]),
rarefaction = F)
expect_equal(round(chv$CHV_nopca[1], 5), 0.00612)
expect_equal(round(chv$CHV_nopca[2], 5), 0.00663)
})

test_that("calculate_spectral_metrics works", {
spectral_metrics <- calculate_spectral_metrics(df,
wavelengths = c('blue','green','red','red_edge','nir'),
rarefaction = TRUE, min_points = 100, n = 20)
expect_type(spectral_metrics,"list")
expect_true(all(spectral_metrics$CV<1))
expect_true(all(spectral_metrics$SV<0.5))
expect_true(all(spectral_metrics$CHV_nopca<0.4))
expect_true(all(spectral_metrics$aoi_id==1))
expect_true(all(spectral_metrics$image_type=='masked'))
spectral_metrics_no_rare <- calculate_spectral_metrics(df,
wavelengths = c('blue','green','red','red_edge','nir'),
rarefaction = FALSE, min_points = 100, n = 20)
expect_type(spectral_metrics_no_rare,"list")
test_that('calculate_spectral_metrics works', {
metrics <- calculate_spectral_metrics(df_test,
wavelengths = colnames(df_test[, 2:4]))
expect_equal(round(metrics$CV[1], 3), 0.296)
expect_equal(round(metrics$CV[2], 3), 0.143)
expect_equal(round(metrics$SV[1], 5), 0.02631)
expect_equal(round(metrics$SV[2], 5), 0.02660)
expect_equal(round(metrics$CHV_nopca[1], 5), 0.00612)
expect_equal(round(metrics$CHV_nopca[2], 5), 0.00663)
expect_true(metrics$aoi_id[1] == 1)
expect_true(metrics$aoi_id[2] == 2)
expect_true(all(metrics$image_type == 'masked'))
expect_true(all(metrics$site == 'site1'))
})


Expand Down

0 comments on commit 25bd447

Please sign in to comment.