From 593b1c1ff968626cb07b0c324b1bf4bcacf88c72 Mon Sep 17 00:00:00 2001 From: Philipp Boersch-Supan Date: Sat, 6 May 2017 18:22:13 -0400 Subject: [PATCH] added registration of cpp functions --- DESCRIPTION | 2 +- NAMESPACE | 2 +- NEWS.md | 1 + man/rucrdtw.Rd | 1 - man/summary.ucrdtw.Rd | 1 - man/summary.ucred.Rd | 1 - man/synthetic_control.Rd | 1 - man/ucrdtw_ff.Rd | 1 - man/ucrdtw_fv.Rd | 1 - man/ucrdtw_mv.Rd | 1 - man/ucrdtw_vv.Rd | 1 - man/ucred_ff.Rd | 1 - man/ucred_fv.Rd | 1 - man/ucred_mv.Rd | 1 - man/ucred_vv.Rd | 1 - rucrdtw.Rproj | 2 +- src/init.c | 30 ++++++++ vignettes/using_rucrdtw.html | 140 ++++++++++++++--------------------- 18 files changed, 90 insertions(+), 99 deletions(-) create mode 100644 src/init.c diff --git a/DESCRIPTION b/DESCRIPTION index 9dad1de..5467407 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,7 +21,7 @@ Depends: R (>= 2.10) Imports: Rcpp SystemRequirements: C++11 -RoxygenNote: 5.0.1 +RoxygenNote: 6.0.1 Suggests: testthat, knitr, rmarkdown, diff --git a/NAMESPACE b/NAMESPACE index 9311420..6ade0f9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,4 +11,4 @@ export(ucred_fv) export(ucred_mv) export(ucred_vv) importFrom(Rcpp,sourceCpp) -useDynLib(rucrdtw) +useDynLib(rucrdtw, .registration=TRUE) diff --git a/NEWS.md b/NEWS.md index ede6880..693552f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,5 +2,6 @@ * Added a `NEWS.md` file to track changes to the package. * Updated CITATION to reflect publication in JOSS +* Native routines are registered now diff --git a/man/rucrdtw.Rd b/man/rucrdtw.Rd index 320733a..e660cb8 100644 --- a/man/rucrdtw.Rd +++ b/man/rucrdtw.Rd @@ -34,4 +34,3 @@ UCR Suite Website: \url{http://www.cs.ucr.edu/~eamonn/UCRsuite.html} \seealso{ \link[dtw]{dtw-package} } - diff --git a/man/summary.ucrdtw.Rd b/man/summary.ucrdtw.Rd index 2bb67a3..7947390 100644 --- a/man/summary.ucrdtw.Rd +++ b/man/summary.ucrdtw.Rd @@ -17,4 +17,3 @@ An unlisted version of the object is returned. \description{ Summary method for class ucrdtw } - diff --git a/man/summary.ucred.Rd b/man/summary.ucred.Rd index d0bb47c..65fb46f 100644 --- a/man/summary.ucred.Rd +++ b/man/summary.ucred.Rd @@ -17,4 +17,3 @@ An unlisted version of the object is returned. \description{ Summary method for class ucred } - diff --git a/man/synthetic_control.Rd b/man/synthetic_control.Rd index d2cfd49..d625a9f 100644 --- a/man/synthetic_control.Rd +++ b/man/synthetic_control.Rd @@ -23,4 +23,3 @@ There are six different classes of control charts: Alcock R.J. and Manolopoulos Y. Time-Series Similarity Queries Employing a Feature-Based Approach. 7th Hellenic Conference on Informatics. August 27-29. Ioannina, Greece 1999. \url{http://rexa.info/paper/3a312d0efa97cce988eecfe0645eac5b241c839e} } \keyword{data} - diff --git a/man/ucrdtw_ff.Rd b/man/ucrdtw_ff.Rd index ac00120..b0425db 100644 --- a/man/ucrdtw_ff.Rd +++ b/man/ucrdtw_ff.Rd @@ -45,4 +45,3 @@ Rakthanmanon, Thanawin, Bilson Campana, Abdullah Mueen, Gustavo Batista, Brandon Giorgino, Toni (2009). Computing and Visualizing Dynamic Time Warping Alignments in R: The dtw Package. Journal of Statistical Software, 31(7), 1-24, doi:\href{http://dx.doi.org/10.18637/jss.v031.i07}{10.18637/jss.v031.i07}. } - diff --git a/man/ucrdtw_fv.Rd b/man/ucrdtw_fv.Rd index 56ce29b..34aeaa3 100644 --- a/man/ucrdtw_fv.Rd +++ b/man/ucrdtw_fv.Rd @@ -43,4 +43,3 @@ Rakthanmanon, Thanawin, Bilson Campana, Abdullah Mueen, Gustavo Batista, Brandon Giorgino, Toni (2009). Computing and Visualizing Dynamic Time Warping Alignments in R: The dtw Package. Journal of Statistical Software, 31(7), 1-24, doi:\href{http://dx.doi.org/10.18637/jss.v031.i07}{10.18637/jss.v031.i07}. } - diff --git a/man/ucrdtw_mv.Rd b/man/ucrdtw_mv.Rd index e5a0259..c0e0711 100644 --- a/man/ucrdtw_mv.Rd +++ b/man/ucrdtw_mv.Rd @@ -47,4 +47,3 @@ ucrdtw_mv(synthetic_control, query, 0.05, byrow = TRUE) \references{ Giorgino, Toni (2009). Computing and Visualizing Dynamic Time Warping Alignments in R: The dtw Package. Journal of Statistical Software, 31(7), 1-24, doi:\href{http://dx.doi.org/10.18637/jss.v031.i07}{10.18637/jss.v031.i07}. } - diff --git a/man/ucrdtw_vv.Rd b/man/ucrdtw_vv.Rd index d4fcb9c..c892c01 100644 --- a/man/ucrdtw_vv.Rd +++ b/man/ucrdtw_vv.Rd @@ -45,4 +45,3 @@ Rakthanmanon, Thanawin, Bilson Campana, Abdullah Mueen, Gustavo Batista, Brandon Giorgino, Toni (2009). Computing and Visualizing Dynamic Time Warping Alignments in R: The dtw Package. Journal of Statistical Software, 31(7), 1-24, doi:\href{http://dx.doi.org/10.18637/jss.v031.i07}{10.18637/jss.v031.i07}. } - diff --git a/man/ucred_ff.Rd b/man/ucred_ff.Rd index 7fb49cf..80f010b 100644 --- a/man/ucred_ff.Rd +++ b/man/ucred_ff.Rd @@ -33,4 +33,3 @@ qlength <- length(scan(queryf)) #run query ucred_ff(dataf, queryf, qlength) } - diff --git a/man/ucred_fv.Rd b/man/ucred_fv.Rd index ec98691..14ef1d1 100644 --- a/man/ucred_fv.Rd +++ b/man/ucred_fv.Rd @@ -29,4 +29,3 @@ query <- scan(system.file("extdata/mid_sc.txt", package="rucrdtw")) #run query ucred_fv(dataf, query) } - diff --git a/man/ucred_mv.Rd b/man/ucred_mv.Rd index ebaee0d..290cb5f 100644 --- a/man/ucred_mv.Rd +++ b/man/ucred_mv.Rd @@ -36,4 +36,3 @@ query <- synthetic_control[5,] #run query ucred_mv(synthetic_control, query, byrow=TRUE) } - diff --git a/man/ucred_vv.Rd b/man/ucred_vv.Rd index 42b2603..226497f 100644 --- a/man/ucred_vv.Rd +++ b/man/ucred_vv.Rd @@ -31,4 +31,3 @@ query <- scan(system.file("extdata/mid_sc.txt", package="rucrdtw")) #run query ucred_vv(dataf, query) } - diff --git a/rucrdtw.Rproj b/rucrdtw.Rproj index f0d6187..d2d171a 100644 --- a/rucrdtw.Rproj +++ b/rucrdtw.Rproj @@ -18,4 +18,4 @@ StripTrailingWhitespace: Yes BuildType: Package PackageUseDevtools: Yes PackageInstallArgs: --no-multiarch --with-keep.source -PackageRoxygenize: rd,collate,namespace,vignette +PackageRoxygenize: rd,collate,vignette diff --git a/src/init.c b/src/init.c new file mode 100644 index 0000000..8d43352 --- /dev/null +++ b/src/init.c @@ -0,0 +1,30 @@ +#include +#include +#include // for NULL +#include + +/* .Call calls */ +extern SEXP rucrdtw_RcppExport_registerCCallable(); +extern SEXP rucrdtw_ucrdtw_ff(SEXP, SEXP, SEXP, SEXP); +extern SEXP rucrdtw_ucrdtw_fv(SEXP, SEXP, SEXP); +extern SEXP rucrdtw_ucrdtw_vv(SEXP, SEXP, SEXP, SEXP, SEXP); +extern SEXP rucrdtw_ucred_ff(SEXP, SEXP, SEXP); +extern SEXP rucrdtw_ucred_fv(SEXP, SEXP); +extern SEXP rucrdtw_ucred_vv(SEXP, SEXP, SEXP); + +static const R_CallMethodDef CallEntries[] = { + {"rucrdtw_RcppExport_registerCCallable", (DL_FUNC) &rucrdtw_RcppExport_registerCCallable, 0}, + {"rucrdtw_ucrdtw_ff", (DL_FUNC) &rucrdtw_ucrdtw_ff, 4}, + {"rucrdtw_ucrdtw_fv", (DL_FUNC) &rucrdtw_ucrdtw_fv, 3}, + {"rucrdtw_ucrdtw_vv", (DL_FUNC) &rucrdtw_ucrdtw_vv, 5}, + {"rucrdtw_ucred_ff", (DL_FUNC) &rucrdtw_ucred_ff, 3}, + {"rucrdtw_ucred_fv", (DL_FUNC) &rucrdtw_ucred_fv, 2}, + {"rucrdtw_ucred_vv", (DL_FUNC) &rucrdtw_ucred_vv, 3}, + {NULL, NULL, 0} +}; + +void R_init_rucrdtw(DllInfo *dll) +{ + R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); + R_useDynamicSymbols(dll, FALSE); +} diff --git a/vignettes/using_rucrdtw.html b/vignettes/using_rucrdtw.html index 62f63fa..0cc7038 100644 --- a/vignettes/using_rucrdtw.html +++ b/vignettes/using_rucrdtw.html @@ -8,11 +8,9 @@ - - - + Using rucrdtw @@ -20,46 +18,32 @@ + - - + @@ -67,11 +51,11 @@ - -

Using rucrdtw

+
@@ -83,64 +67,64 @@

Introduction

Installation

Install rucrdtw from GitHub:

-
install.packages("devtools")
-devtools::install_github("pboesu/rucrdtw")
+
install.packages("devtools")
+devtools::install_github("pboesu/rucrdtw")

Examples

Load rucrdtw package:

-
library("rucrdtw")
+
library("rucrdtw")

create a random long time series

-
set.seed(123)
-rwalk <- cumsum(runif(1e7, min = -0.5, max = 0.5))
+
set.seed(123)
+rwalk <- cumsum(runif(1e7, min = -0.5, max = 0.5))

Pick a random subsequence of 100 elements as a query

-
qstart <- sample(length(rwalk), 1)
-query <- rwalk[qstart:(qstart+100)]
+
qstart <- sample(length(rwalk), 1)
+query <- rwalk[qstart:(qstart+100)]

Since both query and data are R vectors, we use the vector-vector methods for the search.

-
system.time(dtw_search <- ucrdtw_vv(data = rwalk, query = query, dtwwindow = 0.05))
+
system.time(dtw_search <- ucrdtw_vv(data = rwalk, query = query, dtwwindow = 0.05))
##    user  system elapsed 
-##   1.072   0.011   1.201
-
all.equal(qstart, dtw_search$location)
+## 3.489 0.000 3.490 +
all.equal(qstart, dtw_search$location)
## [1] TRUE
-
system.time(ed_search <- ucred_vv(data = rwalk, query = query))
+
system.time(ed_search <- ucred_vv(data = rwalk, query = query))
##    user  system elapsed 
-##   1.113   0.004   1.127
-
all.equal(qstart, ed_search$location)
+## 3.778 0.000 3.780 +
all.equal(qstart, ed_search$location)
## [1] TRUE

And in a matter of seconds we have searched 10 million data points and rediscovered our query!

Searching for an exact match, however, is somewhat artificial. The real power of the similarity search is finding structurally similar subsequences in complex sets of time series. To demonstrate this we load an example data set:

-
data("synthetic_control")
+
data("synthetic_control")

This data set contains 600 time series of length 60 from 6 classes (Alcock et al. 1999). The data set documentation contains further information about these data. It can be displayed using the command ?synthetic_control. We can plot an example of each class

-
par(mfrow = c(3,2),
+
par(mfrow = c(3,2),
     mar = c(1,1,1,1))
 classes = c("Normal", "Cyclic", "Increasing", "Decreasing", "Upward shift", "Downward shift")
 for (i in 1:6){
   plot(synthetic_control[i*100-99,], type = "l", xaxt = "n", yaxt = "n", ylab="", xlab = "", bty="n", main=classes[i])
-}
-

+}
+

Since we are now comparing a query against a set of time series, we only need to do comparisons for non-overlapping data sequences. The matrix-vector methods ucrdtw_mv and ucred_mv provide this functionality.

We can demonstrate this by removing a query from the data set, and then searching for a closest match:

-
index <- 600
+
index <- 600
 query <- synthetic_control[index,]
 #microbenchmark::microbenchmark(
 dtw_search = ucrdtw_mv(synthetic_control[-index,], query, 0.05, byrow = TRUE)
 ed_search = ucred_mv(synthetic_control[-index,], query, byrow= TRUE)
-#times=50)
+#times=50)

And plot the results:

-
plot(synthetic_control[dtw_search$location,], type="l", ylim=c(0,55), ylab="")
+
plot(synthetic_control[dtw_search$location,], type="l", ylim=c(0,55), ylab="")
 lines(query, col="red")
 lines(synthetic_control[ed_search$location,], col="blue", lty=3, lwd=3)
-legend("topright", legend = c("query", "DTW match", "ED match"), col=c("red", "black", "blue"), lty=c(1,1,3), bty="n")
-

+legend("topright", legend = c("query", "DTW match", "ED match"), col=c("red", "black", "blue"), lty=c(1,1,3), bty="n")
+

-
+

References

-
-

Alcock, R. J., Y. Manolopoulos, Data Engineering Laboratory, and Department Of Informatics. 1999. “Time-Series Similarity Queries Employing a Feature-Based Approach.” In In 7 Th Hellenic Conference on Informatics, Ioannina, 27–29.

-
-

Berndt, Donald J, and James Clifford. 1994. “Using Dynamic Time Warping to Find Patterns in Time Series.” In KDD Workshop, 10:359–70. 16. AAAI. http://www.aaai.org/Library/Workshops/1994/ws94-03-031.php.

-
-
-

Eddelbuettel, Dirk, and Romain Francois. 2011. “Rcpp: Seamless R and C++ Integration.” Journal of Statistical Software 40 (1): 1–18. doi:10.18637/jss.v040.i08.

-
-
-

Giorgino, Toni. 2009. “Computing and Visualizing Dynamic Time Warping Alignments in R: The Dtw Package.” Journal of Statistical Software 31 (7): 1–24. doi:10.18637/jss.v031.i07.

-
-
-

Rakthanmanon, Thanawin, Bilson Campana, Abdullah Mueen, Gustavo Batista, Brandon Westover, Qiang Zhu, Jesin Zakaria, and Eamonn Keogh. 2012. “Searching and Mining Trillions of Time Series Subsequences Under Dynamic Time Warping.” In Proceedings of the 18th ACM SIGKDD International Conference on Knowledge Discovery and Data Mining, 262–70. ACM. doi:10.1145/2339530.2339576.

-
+

Eddelbuettel, Dirk, and Romain Francois. 2011. “Rcpp: Seamless R and C++ Integration.” Journal of Statistical Software 40 (1): 1–18. doi:10.18637/jss.v040.i08.

+

Giorgino, Toni. 2009. “Computing and Visualizing Dynamic Time Warping Alignments in R: The Dtw Package.” Journal of Statistical Software 31 (7): 1–24. doi:10.18637/jss.v031.i07.

+

Rakthanmanon, Thanawin, Bilson Campana, Abdullah Mueen, Gustavo Batista, Brandon Westover, Qiang Zhu, Jesin Zakaria, and Eamonn Keogh. 2012. “Searching and Mining Trillions of Time Series Subsequences Under Dynamic Time Warping.” In Proceedings of the 18th ACM SIGKDD International Conference on Knowledge Discovery and Data Mining, 262–70. ACM. doi:10.1145/2339530.2339576.