name instead of year; ipynb generator debugged, with logging
[talweg.git] / pkg / tests / testthat / test.computeFilaments.R
diff --git a/pkg/tests/testthat/test.computeFilaments.R b/pkg/tests/testthat/test.computeFilaments.R
new file mode 100644 (file)
index 0000000..ec39340
--- /dev/null
@@ -0,0 +1,87 @@
+context("Check that computeFilaments behaves as expected")
+
+#shorthand: map 1->1, 2->2, 3->3, 4->1, ..., 149->2, 150->3
+I = function(i)
+       (i-1) %% 3 + 1
+
+#MOCK data; NOTE: could be in inst/testdata as well
+getDataTest = function(n)
+{
+       data = Data$new()
+       x = seq(0,9.5,0.1)
+       L = length(x) #96 1/4h
+       s1 = cos(x)
+       s2 = sin(x)
+       s3 = c( s1[1:(L%/%2)] , s2[(L%/%2+1):L] )
+       #sum((s1-s2)^2) == 96
+       #sum((s1-s3)^2) == 58
+       #sum((s2-s3)^2) == 38
+       s = list(s1, s2, s3)
+       series = list()
+       for (i in seq_len(n))
+       {
+               serie = s[[I(i)]] + rnorm(L,sd=0.01)
+               level = mean(serie)
+               serie = serie - level
+               # 10 series with NAs for index 2
+               if (I(i) == 2 && i >= 60 && i<= 90)
+                       serie[sample(seq_len(L),1)] = NA
+               time = as.POSIXct(i*15*60, origin="2007-01-01", tz="GMT")
+               exo = runif(4)
+               exo_hat = runif(4)
+               data$append(time, serie, level, exo, exo_hat)
+       }
+       data
+}
+
+test_that("output is as expected on simulated series",
+{
+       data = getDataTest(150)
+
+       # index 143 : serie type 2
+       pred = computeForecast(data, 143, "Neighbors", "Zero",
+               horizon=length(data$getSerie(1)), simtype="endo", h_window=1)
+       f = computeFilaments(data, pred, 1, limit=60, plot=FALSE)
+
+       # Expected output: 50-3-10 series of type 2, then 23 series of type 3 (closest next)
+       expect_identical(length(f$neighb_indices), as.integer(60))
+       expect_identical(length(f$colors), as.integer(60))
+       expect_equal(f$index, 143)
+       expect_true(all(I(f$neighb_indices) >= 2))
+       for (i in 1:37)
+       {
+               expect_equal(I(f$neighb_indices[i]), 2)
+               expect_match(f$colors[i], f$colors[1])
+       }
+       for (i in 38:60)
+       {
+               expect_equal(I(f$neighb_indices[i]), 3)
+               expect_match(f$colors[i], f$colors[38])
+       }
+       expect_match(f$colors[1], "#1*")
+       expect_match(f$colors[38], "#E*")
+
+       # index 142 : serie type 1
+       pred = computeForecast(data, 142, "Neighbors", "Zero",
+               horizon=length(data$getSerie(1)), simtype="endo", h_window=1)
+       f = computeFilaments(data, pred, 1, limit=50, plot=FALSE)
+
+       # Expected output: 50-10-3 series of type 1, then 13 series of type 3 (closest next)
+       # NOTE: -10 because only past days with no-NAs tomorrow => exclude type 1 in [60,90[
+       expect_identical(length(f$neighb_indices), as.integer(50))
+       expect_identical(length(f$colors), as.integer(50))
+       expect_equal(f$index, 142)
+       expect_true(all(I(f$neighb_indices) != 2))
+       for (i in 1:37)
+       {
+               expect_equal(I(f$neighb_indices[i]), 1)
+               expect_match(f$colors[i], f$colors[1])
+       }
+       for (i in 38:50)
+       {
+               expect_equal(I(f$neighb_indices[i]), 3)
+               expect_match(f$colors[i], f$colors[38])
+       }
+       expect_match(f$colors[1], "#1*")
+       expect_match(f$colors[38], "#E*")
+})