Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ytarazona committed Apr 2, 2021
1 parent 81ca933 commit 304fbd1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 63 deletions.
56 changes: 0 additions & 56 deletions tests/testthat/test_MosaicFreeCloud.R

This file was deleted.

15 changes: 12 additions & 3 deletions tests/testthat/test_mla.R
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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))
})
43 changes: 42 additions & 1 deletion tests/testthat/test_pvts.R
Original file line number Diff line number Diff line change
@@ -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))
})
4 changes: 1 addition & 3 deletions tests/testthat/test_smootH.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
library(testthat)
library(ForesToolboxRS)
library(stars)
library(raster)
library(forecast)
context("ForesToolboxRS smootH")
Expand All @@ -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]
Expand All @@ -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]),
Expand Down

0 comments on commit 304fbd1

Please sign in to comment.