diff --git a/tests/testthat/test_MosaicFreeCloud.R b/tests/testthat/test_MosaicFreeCloud.R deleted file mode 100644 index 43cfb64..0000000 --- a/tests/testthat/test_MosaicFreeCloud.R +++ /dev/null @@ -1,56 +0,0 @@ -# library(testthat) -# library(ForesToolboxRS) -# library(stars) -# library(raster) -# context("ForesToolboxRS MosaicFreeCloud") -# rasterio <- list(nXOff = 50, nYOff = 50) -# rasterio_4D <- list(nXOff = 50, nYOff = 50,bands=c(4,3,2)) -# time <- as.Date(c("2016-07-30","2016-08-15","2016-09-16")) -# -# test_that("MosaicFreeCloud3D-character", { -# img <- system.file("simple_mosaic", package="ForesToolboxRS") %>% -# list.files("\\.tif$",full.names = TRUE) -# free_img <- MosaicFreeCloud(img, time, RasterIO=rasterio) -# expect_equal(mean(getValues(free_img),na.rm=TRUE),178.6082,tolerance=0.0001) -# }) -# -# test_that("MosaicFreeCloud3D-RasterStack", { -# img <- system.file("simple_mosaic", package="ForesToolboxRS") %>% -# list.files("\\.tif$",full.names = TRUE) %>% -# stack() -# free_img <- MosaicFreeCloud(img, time) -# expect_equal(mean(getValues(free_img),na.rm=TRUE), 192.016, tolerance=0.0001) -# }) -# -# test_that("MosaicFreeCloud3D-RasterBrick", { -# img <- system.file("simple_mosaic", package="ForesToolboxRS") %>% -# list.files("\\.tif$",full.names = TRUE) %>% -# stack() %>% -# brick() -# free_img <- MosaicFreeCloud(img, time, RasterIO=rasterio) -# expect_equal(mean(getValues(free_img),na.rm=TRUE),192.016,tolerance=0.0001) -# }) -# -# test_that("MosaicFreeCloud3D-stars", { -# img <- system.file("simple_mosaic", package="ForesToolboxRS") %>% -# list.files("\\.tif$",full.names = TRUE) %>% -# read_stars(RasterIO=rasterio) -# free_img <- MosaicFreeCloud(img, time) -# expect_equal(mean(getValues(free_img),na.rm=TRUE), 178.6082,tolerance=0.0001) -# }) -# -# -# test_that("MosaicFreeCloud4D-stars", { -# img <- system.file("mosaic", package="ForesToolboxRS") %>% -# list.files("\\.tif$",full.names = TRUE) %>% -# read_stars(RasterIO=rasterio_4D) -# free_img <- MosaicFreeCloud(img, time) -# expect_equal(mean(getValues(free_img),na.rm=TRUE), 285.6287,tolerance=0.0001) -# }) -# -# test_that("MosaicFreeCloud4D-character", { -# img <- system.file("mosaic", package="ForesToolboxRS") %>% -# list.files("\\.tif$",full.names = TRUE) -# free_img <- MosaicFreeCloud(img, time,RasterIO=rasterio_4D) -# expect_equal(mean(getValues(free_img),na.rm=TRUE), 285.6287,tolerance=0.0001) -# }) diff --git a/tests/testthat/test_mla.R b/tests/testthat/test_mla.R index ce50f4e..fde05b7 100644 --- a/tests/testthat/test_mla.R +++ b/tests/testthat/test_mla.R @@ -1,11 +1,9 @@ library(testthat) library(ForesToolboxRS) -library(stars) library(raster) library(caret) data(endm) -context("ForesToolboxRS ndfiSMA") - +context("ForesToolboxRS mla") training_split = 80 vegt <- extract(img_l8, endm) @@ -57,3 +55,14 @@ MC <- errorMatrix(prediction = prediction, reference = testing[,dim(endm)[2]]) results <- list(Overall_accuracy = (confusionMatrix(MC$MC_ini)$overall[1:6])*100, Confusion_matrix = MC$MCf, Classification = raster_class) + +test_that("try mla", { + ndfi <- c( + 0.86, 0.93, 0.97, 0.91, 0.95, 0.96, 0.91, 0.88, 0.92, 0.89, + 0.90, 0.89, 0.91, 0.92, 0.89, 0.90, 0.92, 0.84, 0.46, 0.20, + 0.27, 0.22, 0.52, 0.63, 0.61, 0.67, 0.64, 0.86 + ) + ndfi_ts <- ts(ndfi, start = 1990, end = 2017, frequency = 1) + cd <- pvts(x = ndfi_ts, startm = 2008, endm = 2008, threshold = 5) + expect_equal(as.vector(cd$Threshold[2]), test_pvts_ts(ndfi_ts, startm = 2008, endm = 2008, threshold = 5)) +}) diff --git a/tests/testthat/test_pvts.R b/tests/testthat/test_pvts.R index 55fba81..8c6b74e 100644 --- a/tests/testthat/test_pvts.R +++ b/tests/testthat/test_pvts.R @@ -1,5 +1,46 @@ library(testthat) library(ForesToolboxRS) -library(stars) library(raster) context("ForesToolboxRS pvts") + +test_pvts_vector <- function(x, startm, endm, threshold = 5) { + + mean.pvts <- mean(x[1:(startm - 1)]) + + std.pvts <- sd(x[1:(startm - 1)]) + + li <- mean.pvts - threshold * std.pvts + + return(li) +} + +test_that("try pvts", { + x <- c(80, 78, 79, 88, 86, 80, 82, 76, 81, 25, 76) + cd <- pvts(x, startm = 10, endm = 10, threshold = 5) + expect_equal(as.vector(cd$Threshold[2]), test_pvts_vector(x, startm = 10, endm = 10, threshold = 5)) +}) + +test_pvts_ts <- function(x, startm, endm, threshold = 5) { + + startm.pvts <- which(time(x) == startm) + endm.pvts <- which(time(x) == endm) + + mean.pvts <- mean(x[1:(startm.pvts - 1)]) + + std.pvts <- sd(x[1:(startm.pvts - 1)]) + + li <- mean.pvts - threshold * std.pvts + + return(li) +} + +test_that("try pvts", { + ndfi <- c( + 0.86, 0.93, 0.97, 0.91, 0.95, 0.96, 0.91, 0.88, 0.92, 0.89, + 0.90, 0.89, 0.91, 0.92, 0.89, 0.90, 0.92, 0.84, 0.46, 0.20, + 0.27, 0.22, 0.52, 0.63, 0.61, 0.67, 0.64, 0.86 + ) + ndfi_ts <- ts(ndfi, start = 1990, end = 2017, frequency = 1) + cd <- pvts(x = ndfi_ts, startm = 2008, endm = 2008, threshold = 5) + expect_equal(as.vector(cd$Threshold[2]), test_pvts_ts(ndfi_ts, startm = 2008, endm = 2008, threshold = 5)) +}) diff --git a/tests/testthat/test_smootH.R b/tests/testthat/test_smootH.R index 3d9f289..fbd191b 100644 --- a/tests/testthat/test_smootH.R +++ b/tests/testthat/test_smootH.R @@ -1,6 +1,5 @@ library(testthat) library(ForesToolboxRS) -library(stars) library(raster) library(forecast) context("ForesToolboxRS smootH") @@ -21,7 +20,6 @@ test_smootH <- function(x, interp = "na.interp") { stop("Unsupported interpolation method") } - # We apply Hamunyela Smoothing for (j in 2:(length(x) - 1)) { x[j] <- ifelse(((x[j] - x[j - 1]) < -0.01 * x[j - 1]) & ((x[j] - x[j + 1]) < -0.01 * x[j + 1]), (x[j - 1] + x[j + 1]) / 2, x[j] @@ -44,7 +42,7 @@ test_smootH <- function(x, interp = "na.interp") { stop("Unsupported interpolation method") } } - # We apply Hamunyela Smoothing + for (i in 1:dim(x)[1]) { for (j in 2:(dim(x)[2] - 1)) { x[i, ][j] <- ifelse(((x[i, ][j] - x[i, ][j - 1]) < -0.01 * x[i, ][j - 1]) & ((x[i, ][j] - x[i, ][j + 1]) < -0.01 * x[i, ][j + 1]),