From 53e26395446a1908820862f45e4b181fc0eafef7 Mon Sep 17 00:00:00 2001
From: vwmaus <vwmaus1@gmail.com>
Date: Tue, 11 Oct 2022 14:51:34 +0200
Subject: [PATCH] Keep legacy in examples

---
 .Rbuildignore             |  4 +++-
 DESCRIPTION               |  3 +--
 NAMESPACE                 |  1 +
 R/class-twdtwMatches.R    |  5 +++--
 R/getInternals.R          |  3 ++-
 R/getTimeSeries.R         |  2 +-
 R/methods.R               |  4 ++--
 R/plotAccuracy.R          |  4 ++--
 R/plotAlignments.R        |  2 +-
 R/plotClassification.R    |  2 +-
 R/plotMatches.R           |  2 +-
 R/subset.R                |  3 ++-
 R/twdtwApply.R            |  6 +++---
 R/twdtwClassify.R         |  6 ++++--
 R/twdtw_reduce_time.R     |  2 +-
 R/zzz.R                   |  3 ++-
 cran-comments.md          | 10 ++++++++--
 man/get.Rd                |  3 ++-
 man/getTimeSeries.Rd      |  2 +-
 man/plotAlignments.Rd     |  2 +-
 man/plotClassification.Rd |  2 +-
 man/plotMatches.Rd        |  2 +-
 man/subset.Rd             |  3 ++-
 man/twdtwApply.Rd         |  2 +-
 man/twdtwClassify.Rd      | 17 +++++++++++++++++
 man/twdtwMatches-class.Rd |  5 +++--
 26 files changed, 67 insertions(+), 33 deletions(-)

diff --git a/.Rbuildignore b/.Rbuildignore
index 3980631..21385f2 100644
--- a/.Rbuildignore
+++ b/.Rbuildignore
@@ -31,5 +31,7 @@ dtwSat-Ex_x64.Rout
 examples_i386
 examples_x64
 vignettes/twdtw03-speed.Rmd
-  
+
+^data/mod13q1/
+^data/mod13q1.db
 ^CRAN-RELEASE$
diff --git a/DESCRIPTION b/DESCRIPTION
index 6a6f1a3..516c5b0 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -53,8 +53,7 @@ Imports:
     xtable,
     Rdpack,
     data.table,
-    foreach,
-    parallel
+    foreach
 Suggests:
     gridExtra,
     grid,
diff --git a/NAMESPACE b/NAMESPACE
index 65bd612..5c60500 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -123,6 +123,7 @@ importFrom(stats,sd)
 importFrom(stats,window)
 importFrom(stats,xtabs)
 importFrom(utils,flush.console)
+importFrom(utils,globalVariables)
 importFrom(utils,packageDescription)
 importFrom(xtable,print.xtable)
 importFrom(xtable,xtable)
diff --git a/R/class-twdtwMatches.R b/R/class-twdtwMatches.R
index 0500ac4..26b7861 100644
--- a/R/class-twdtwMatches.R
+++ b/R/class-twdtwMatches.R
@@ -74,7 +74,7 @@
 #' @examples 
 #' ts = twdtwTimeSeries(timeseries=MOD13Q1.ts.list)
 #' patterns = twdtwTimeSeries(timeseries=MOD13Q1.patterns.list)
-#' matches = twdtwApply(x = ts, y = patterns)
+#' matches = twdtwApply(x = ts, y = patterns, keep=TRUE, legacy=TRUE)
 #' class(matches)
 #' length(matches)
 #' matches 
@@ -128,7 +128,8 @@ setGeneric(name = "twdtwMatches",
 #' # Creating objects of class twdtwMatches 
 #' ts  = twdtwTimeSeries(MOD13Q1.ts.list)
 #' patt = twdtwTimeSeries(MOD13Q1.patterns.list)
-#' mat = twdtwApply(ts, patt, weight.fun = logisticWeight(-0.1, 100))
+#' mat = twdtwApply(ts, patt, weight.fun = logisticWeight(-0.1, 100), 
+#'                  keep=TRUE, legacy=TRUE)
 #' mat = twdtwMatches(ts, patterns=patt, alignments=mat)
 #' mat
 #' 
diff --git a/R/getInternals.R b/R/getInternals.R
index 95a835f..bbc0740 100644
--- a/R/getInternals.R
+++ b/R/getInternals.R
@@ -34,7 +34,8 @@ setGeneric("getMatches", function(object, timeseries.labels=NULL, patterns.label
 #' # Getting patterns from objects of class twdtwMatches
 #' patt = twdtwTimeSeries(MOD13Q1.patterns.list)
 #' ts = twdtwTimeSeries(MOD13Q1.ts.list)
-#' mat = twdtwApply(x=ts, y=patt, weight.fun=logisticWeight(-0.1,100), keep=TRUE)
+#' mat = twdtwApply(x=ts, y=patt, weight.fun=logisticWeight(-0.1,100), 
+#'                  keep=TRUE, legacy = TRUE)
 #' getPatterns(mat)
 #' getTimeSeries(mat)
 #' getAlignments(mat)
diff --git a/R/getTimeSeries.R b/R/getTimeSeries.R
index 039aeb8..2bec0d6 100644
--- a/R/getTimeSeries.R
+++ b/R/getTimeSeries.R
@@ -63,7 +63,7 @@ setGeneric("getPatterns", function(object, ...) standardGeneric("getPatterns"))
 #' # Getting time series from objects of class twdtwTimeSeries
 #' ts = twdtwTimeSeries(MOD13Q1.ts.list)
 #' patt = twdtwTimeSeries(MOD13Q1.patterns.list)
-#' mat = twdtwApply(x=ts, y=patt)
+#' mat = twdtwApply(x=ts, y=patt, keep=TRUE, legacy=TRUE)
 #' getTimeSeries(mat, 2)
 #' 
 #' ## This example creates a twdtwRaster object and extract time series from it. 
diff --git a/R/methods.R b/R/methods.R
index 174c8d9..8ecc054 100644
--- a/R/methods.R
+++ b/R/methods.R
@@ -358,10 +358,10 @@ setMethod("[", "twdtwMatches", function(x, i, j, drop=TRUE) {
   if(missing(i)) i = 1:length(x@alignments)
   # if(missing(j)) j = 2:length(x@patterns)
   if(any(is.na(i))) stop("NA index not permitted")
-  if(class(i)=="character") i = match(i, names(x@timeseries@timeseries))
+  if(is(i, "character")) i = match(i, names(x@timeseries@timeseries))
   res = x@alignments[i]
   if(missing(j)) j = 1:length(res[[1]])
-  if(class(j)=="character") j = match(j, names(x@patterns@timeseries))
+  if(is(j, "character")) j = match(j, names(x@patterns@timeseries))
   if(any(is.na(j))) stop("NA index not permitted")
   res = lapply(res, function(x) x[j])
   res = res[sapply(res, length)>0]
diff --git a/R/plotAccuracy.R b/R/plotAccuracy.R
index 336d208..1768cee 100644
--- a/R/plotAccuracy.R
+++ b/R/plotAccuracy.R
@@ -48,10 +48,10 @@
 plotAccuracy = function(x, perc=TRUE, conf.int=.95, time.labels=NULL, 
                         category.name=NULL, category.type=NULL){
   
-  if(class(x)=="twdtwCrossValidation"){
+  if(is(x, "twdtwCrossValidation")){
     gp = .plotCrossValidation(x, conf.int, perc, category.name, category.type)
   } else {
-    if(class(x)=="twdtwAssessment"){
+    if(is(x, "twdtwAssessment")){
       gp = .plotAssessmentAccuracy(x, perc, time.labels, category.name, category.type)
     } else {
       stop("Class of x is not twdtwAssessment or twdtwCrossValidation")
diff --git a/R/plotAlignments.R b/R/plotAlignments.R
index aa172fe..2ced5d6 100644
--- a/R/plotAlignments.R
+++ b/R/plotAlignments.R
@@ -49,7 +49,7 @@
 #' log_fun = logisticWeight(-0.1, 100)
 #' ts = twdtwTimeSeries(MOD13Q1.ts.list)
 #' patt = twdtwTimeSeries(MOD13Q1.patterns.list)
-#' mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun)
+#' mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun, keep=TRUE, legacy=TRUE)
 #' 
 #' plotAlignments(mat1)
 #' 
diff --git a/R/plotClassification.R b/R/plotClassification.R
index 26ba5c7..61a6b37 100644
--- a/R/plotClassification.R
+++ b/R/plotClassification.R
@@ -50,7 +50,7 @@
 #' log_fun = logisticWeight(-0.1, 100)
 #' ts = twdtwTimeSeries(MOD13Q1.ts.list)
 #' patt = twdtwTimeSeries(MOD13Q1.patterns.list)
-#' mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun)
+#' mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun, keep=TRUE, legacy=TRUE)
 #' 
 #' # Classify interval
 #' from = as.Date("2007-09-01")
diff --git a/R/plotMatches.R b/R/plotMatches.R
index d40e2e1..11a6825 100644
--- a/R/plotMatches.R
+++ b/R/plotMatches.R
@@ -53,7 +53,7 @@
 #' log_fun = logisticWeight(-0.1, 100)
 #' ts = twdtwTimeSeries(MOD13Q1.ts.list)
 #' patt = twdtwTimeSeries(MOD13Q1.patterns.list)
-#' mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun, keep=TRUE)
+#' mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun, keep=TRUE, legacy=TRUE)
 #' 
 #' plotMatches(mat1)
 #' 
diff --git a/R/subset.R b/R/subset.R
index d59521b..f834e33 100644
--- a/R/subset.R
+++ b/R/subset.R
@@ -51,7 +51,8 @@
 #' ts
 #' # Getting time series from objects of class twdtwTimeSeries
 #' patt = twdtwTimeSeries(MOD13Q1.patterns.list)
-#' mat = twdtwApply(x=ts, y=patt, weight.fun=logisticWeight(-0.1,100))
+#' mat = twdtwApply(x=ts, y=patt, weight.fun=logisticWeight(-0.1,100), 
+#'                  keep=TRUE, legacy=TRUE)
 #' mat = subset(mat, k=4)
 #' 
 #' ## This example creates a twdtwRaster object and extracts time series from it. 
diff --git a/R/twdtwApply.R b/R/twdtwApply.R
index bcf53a6..b27e4ae 100644
--- a/R/twdtwApply.R
+++ b/R/twdtwApply.R
@@ -102,7 +102,7 @@ setGeneric(name = "twdtwApply",
 #' log_fun = logisticWeight(-0.1, 100)
 #' ts = twdtwTimeSeries(MOD13Q1.ts.list)
 #' patt = twdtwTimeSeries(MOD13Q1.patterns.list)
-#' mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun)
+#' mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun, keep=TRUE, legacy=TRUE)
 #' mat1
 #' 
 #' \dontrun{
@@ -299,10 +299,10 @@ twdtwApply.twdtwRaster.fast = function(x,
     
     # Apply TWDTW analysis
     twdtw_results <- foreach(
-      i = ts, 
+      tsidopar = ts, 
       .combine = 'rbind'
     ) %dopar% {
-      res = twdtwReduceTime(x = i, y = y, breaks = breaks, fill = fill, alpha = alpha, beta = beta, keep = FALSE, ...)
+      res = twdtwReduceTime(x = tsidopar, y = y, breaks = breaks, fill = fill, alpha = alpha, beta = beta, keep = FALSE, ...)
       twdtw_label <- matrix(res$label, ncol = length(breaks)-1, byrow = TRUE)
       twdtw_distance <- matrix(res$distance, ncol = length(breaks)-1, byrow = TRUE)
       cbind(twdtw_label, twdtw_distance)
diff --git a/R/twdtwClassify.R b/R/twdtwClassify.R
index 4d78a21..7c47619 100644
--- a/R/twdtwClassify.R
+++ b/R/twdtwClassify.R
@@ -21,7 +21,7 @@
 #' @description This function classifies the intervals of a time series 
 #' based on the TWDTW results. 
 #' 
-#' @inheritParams get
+#' @inheritParams twdtwReduceTime
 #'
 #' @param x An object of class twdtw*. This is the target time series. 
 #' Usually, it is a set of unclassified time series. 
@@ -39,6 +39,8 @@
 #' between one match and the interval of classification. Default is 0.5, 
 #' \emph{i.e.} an overlap minimum of 50\%.
 #' 
+#' @param patterns.labels a vector with labels of the patterns.
+#' 
 #' @param thresholds A numeric vector the same length as \code{patterns.labels}. 
 #' The TWDTW dissimilarity thresholds, i.e. the maximum TWDTW cost for consideration 
 #' in the classification. Default is \code{Inf} for all \code{patterns.labels}.
@@ -104,7 +106,7 @@ setMethod("twdtwClassify", "twdtwTimeSeries",
           function(x, patterns.labels=NULL, from=NULL, to=NULL, by=NULL, breaks=NULL,
                 overlap=.5, thresholds=Inf, fill="unclassified", ...){
                     xm = twdtwApply(x = x, from = from, to = to, by = by, breaks = breaks, ...)
-                    if(class(xm) == "twdtwMatches"){
+                    if(is(xm, "twdtwMatches")){
                       x = xm 
                       if(is.null(patterns.labels)) patterns.labels = labels(x@patterns)
                       if( overlap < 0 & 1 < overlap )
diff --git a/R/twdtw_reduce_time.R b/R/twdtw_reduce_time.R
index 94c0800..f093430 100644
--- a/R/twdtw_reduce_time.R
+++ b/R/twdtw_reduce_time.R
@@ -242,7 +242,7 @@ twdtwReduceTime = function(x,
   } else {
     stop("Fortran bestmatches lib is not loaded")
   }
-  if(class(res) == "try-error"){
+  if(is(res, "try-error")){
     res = list(
       XM = matrix(as.integer(c(as.numeric(tx[x[,1]]), as.numeric(tx[x[,3]]))), ncol = 2),
       AM = matrix(as.double(.Machine$double.xmax), nrow = n, ncol = m), 
diff --git a/R/zzz.R b/R/zzz.R
index 8883a02..e36d046 100644
--- a/R/zzz.R
+++ b/R/zzz.R
@@ -47,13 +47,14 @@
 #' @importFrom lubridate month month<- day day<- year year<-
 #' @importFrom caret createDataPartition 
 #' @importFrom xtable xtable print.xtable
-#' @importFrom utils packageDescription flush.console 
+#' @importFrom utils packageDescription flush.console globalVariables
 #' @importFrom Rdpack reprompt 
 #' @importFrom data.table rbindlist
 #' @useDynLib dtwSat, .registration = TRUE
 #' 
 NULL
 
+if(getRversion() >= "2.15.1")  utils::globalVariables("tsidopar")
 
 ### Import and export functions from other packages
 
diff --git a/cran-comments.md b/cran-comments.md
index 84fad68..f1e1282 100644
--- a/cran-comments.md
+++ b/cran-comments.md
@@ -1,9 +1,9 @@
 ## Test environments
 
-* Local Ubuntu 20.04 (64-bit), R 4.1.1 
+* Local Ubuntu 22.04.1 LTS x86_64-pc-linux-gnu (64-bit), R 4.2.1 (2022-06-23)
   devtools::check(args = '--as-cran')
   devtools::submit_cran()
-
+  
 * win-builder 
   devtools::check_win_release()
   devtools::check_win_devel()
@@ -15,6 +15,12 @@
 
 ## REVIEWS
 
+# v0.2.8
+
+* Fixes errors from https://cran.r-project.org/web/checks/check_results_dtwSat.html
+
+* Speed improvements
+
 # v0.2.7
 
 * Fixes error in cost TWDTW weighting function
diff --git a/man/get.Rd b/man/get.Rd
index 909f4c7..a0aabd3 100644
--- a/man/get.Rd
+++ b/man/get.Rd
@@ -33,7 +33,8 @@ Get elements from \code{\link[dtwSat]{twdtwMatches-class}} objects.
 # Getting patterns from objects of class twdtwMatches
 patt = twdtwTimeSeries(MOD13Q1.patterns.list)
 ts = twdtwTimeSeries(MOD13Q1.ts.list)
-mat = twdtwApply(x=ts, y=patt, weight.fun=logisticWeight(-0.1,100), keep=TRUE)
+mat = twdtwApply(x=ts, y=patt, weight.fun=logisticWeight(-0.1,100), 
+                 keep=TRUE, legacy = TRUE)
 getPatterns(mat)
 getTimeSeries(mat)
 getAlignments(mat)
diff --git a/man/getTimeSeries.Rd b/man/getTimeSeries.Rd
index 4db4c50..2a0a834 100644
--- a/man/getTimeSeries.Rd
+++ b/man/getTimeSeries.Rd
@@ -57,7 +57,7 @@ getTimeSeries(ts, 2)
 # Getting time series from objects of class twdtwTimeSeries
 ts = twdtwTimeSeries(MOD13Q1.ts.list)
 patt = twdtwTimeSeries(MOD13Q1.patterns.list)
-mat = twdtwApply(x=ts, y=patt)
+mat = twdtwApply(x=ts, y=patt, keep=TRUE, legacy=TRUE)
 getTimeSeries(mat, 2)
 
 ## This example creates a twdtwRaster object and extract time series from it. 
diff --git a/man/plotAlignments.Rd b/man/plotAlignments.Rd
index 2e92fbe..d050cdb 100644
--- a/man/plotAlignments.Rd
+++ b/man/plotAlignments.Rd
@@ -38,7 +38,7 @@ dissimilarity measures.
 log_fun = logisticWeight(-0.1, 100)
 ts = twdtwTimeSeries(MOD13Q1.ts.list)
 patt = twdtwTimeSeries(MOD13Q1.patterns.list)
-mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun)
+mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun, keep=TRUE, legacy=TRUE)
 
 plotAlignments(mat1)
 
diff --git a/man/plotClassification.Rd b/man/plotClassification.Rd
index 065df8e..eee6911 100644
--- a/man/plotClassification.Rd
+++ b/man/plotClassification.Rd
@@ -38,7 +38,7 @@ subinterval of the time series based on TWDTW analysis.
 log_fun = logisticWeight(-0.1, 100)
 ts = twdtwTimeSeries(MOD13Q1.ts.list)
 patt = twdtwTimeSeries(MOD13Q1.patterns.list)
-mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun)
+mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun, keep=TRUE, legacy=TRUE)
 
 # Classify interval
 from = as.Date("2007-09-01")
diff --git a/man/plotMatches.Rd b/man/plotMatches.Rd
index 6904335..9098386 100644
--- a/man/plotMatches.Rd
+++ b/man/plotMatches.Rd
@@ -47,7 +47,7 @@ TWDTW analysis.
 log_fun = logisticWeight(-0.1, 100)
 ts = twdtwTimeSeries(MOD13Q1.ts.list)
 patt = twdtwTimeSeries(MOD13Q1.patterns.list)
-mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun, keep=TRUE)
+mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun, keep=TRUE, legacy=TRUE)
 
 plotMatches(mat1)
 
diff --git a/man/subset.Rd b/man/subset.Rd
index 60761c2..ef0fb5d 100644
--- a/man/subset.Rd
+++ b/man/subset.Rd
@@ -47,7 +47,8 @@ ts = subset(ts, 2)
 ts
 # Getting time series from objects of class twdtwTimeSeries
 patt = twdtwTimeSeries(MOD13Q1.patterns.list)
-mat = twdtwApply(x=ts, y=patt, weight.fun=logisticWeight(-0.1,100))
+mat = twdtwApply(x=ts, y=patt, weight.fun=logisticWeight(-0.1,100), 
+                 keep=TRUE, legacy=TRUE)
 mat = subset(mat, k=4)
 
 ## This example creates a twdtwRaster object and extracts time series from it. 
diff --git a/man/twdtwApply.Rd b/man/twdtwApply.Rd
index ed1614e..9b885a9 100644
--- a/man/twdtwApply.Rd
+++ b/man/twdtwApply.Rd
@@ -164,7 +164,7 @@ See \insertCite{Maus:2016,Maus:2019}{dtwSat} for details about the method.
 log_fun = logisticWeight(-0.1, 100)
 ts = twdtwTimeSeries(MOD13Q1.ts.list)
 patt = twdtwTimeSeries(MOD13Q1.patterns.list)
-mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun)
+mat1 = twdtwApply(x=ts, y=patt, weight.fun=log_fun, keep=TRUE, legacy=TRUE)
 mat1
 
 \dontrun{
diff --git a/man/twdtwClassify.Rd b/man/twdtwClassify.Rd
index ff28c85..3db968a 100644
--- a/man/twdtwClassify.Rd
+++ b/man/twdtwClassify.Rd
@@ -93,6 +93,12 @@ and other arguments to pass to \code{\link[raster]{writeRaster}} and
 \code{\link[dtwSat]{twdtwTimeSeries-class}} additional arguments passed to 
 \code{\link[dtwSat]{twdtwApply}}.}
 
+\item{y}{a list of data.frame objects similar to \code{x}. 
+The temporal patterns used to classify the time series in \code{x}.}
+
+\item{step.matrix}{See \code{\link[dtw]{stepPattern}} in package \pkg{dtw} 
+\insertCite{Giorgino:2009}{dtwSat}.}
+
 \item{breaks}{A vector of class \code{\link[base]{Dates}}. This replaces the arguments \code{from},
 \code{to}, and \code{by}.}
 
@@ -110,6 +116,17 @@ between one match and the interval of classification. Default is 0.5,
 For signature \code{twdtwTimeSeries} the default is \code{fill="unclassified"}, 
 for signature \code{twdtwRaster} the default is \code{fill="unclassified"}.}
 
+\item{alpha}{Numeric. The steepness of TWDTW logistic weight.}
+
+\item{beta}{Numeric. The midpoint of TWDTW logistic weight.}
+
+\item{time.window}{logical. TRUE will constrain the TWDTW computation to the 
+value of the parameter \code{beta} defined in the logistic weight function. 
+Default is FALSE.}
+
+\item{keep}{Preserves the cost matrix, inputs, and other internal structures. 
+Default is FALSE. For plot methods use \code{keep=TRUE}.}
+
 \item{patterns.labels}{a vector with labels of the patterns.}
 
 \item{thresholds}{A numeric vector the same length as \code{patterns.labels}. 
diff --git a/man/twdtwMatches-class.Rd b/man/twdtwMatches-class.Rd
index 2630193..7ad7654 100644
--- a/man/twdtwMatches-class.Rd
+++ b/man/twdtwMatches-class.Rd
@@ -103,14 +103,15 @@ Class for Time-Weighted Dynamic Time Warping results.
 \examples{
 ts = twdtwTimeSeries(timeseries=MOD13Q1.ts.list)
 patterns = twdtwTimeSeries(timeseries=MOD13Q1.patterns.list)
-matches = twdtwApply(x = ts, y = patterns)
+matches = twdtwApply(x = ts, y = patterns, keep=TRUE, legacy=TRUE)
 class(matches)
 length(matches)
 matches 
 # Creating objects of class twdtwMatches 
 ts  = twdtwTimeSeries(MOD13Q1.ts.list)
 patt = twdtwTimeSeries(MOD13Q1.patterns.list)
-mat = twdtwApply(ts, patt, weight.fun = logisticWeight(-0.1, 100))
+mat = twdtwApply(ts, patt, weight.fun = logisticWeight(-0.1, 100), 
+                 keep=TRUE, legacy=TRUE)
 mat = twdtwMatches(ts, patterns=patt, alignments=mat)
 mat