Skip to content

Commit

Permalink
Register TWDTW as a distance measure of the proxy package
Browse files Browse the repository at this point in the history
  • Loading branch information
vwmaus committed Apr 25, 2017
1 parent e3869d8 commit af71b23
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ importFrom(mgcv,gam)
importFrom(mgcv,predict.gam)
importFrom(plyr,alply)
importFrom(proxy,dist)
importFrom(proxy,pr_DB)
importFrom(reshape2,melt)
importFrom(scales,date_format)
importFrom(scales,percent)
Expand Down
33 changes: 24 additions & 9 deletions R/twdtwDist.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@


.twdtwDist = function (object, ...) {
d = lapply(seq_along(object), function (i) {
x = subset(object, i)
y = subset(object, -i)
res = twdtwApply(x, y, n=1)
res[]
})
d
.twdtwDist = function (x, y, ...) {
d = do.call("rbind", lapply(x, function (xx) {
sapply(y, function (yy) {
res = twdtwApply(twdtwTimeSeries(xx), twdtwTimeSeries(yy), n=1, ...)
res[[1]]$distance[1]
})
}))
as.matrix(d)
}

# library(dtwSat)
# library(dtw)
# library(dtwclust)
# log_fun = logisticWeight(-0.1, 100)
# phi = proxy::dist(x=MOD13Q1.patterns.list, y=MOD13Q1.patterns.list[1:2], method="TWDTW", weight.fun=log_fun)
# phi
# a=twdtwApply(twdtwTimeSeries(MOD13Q1.patterns.list), twdtwTimeSeries(MOD13Q1.patterns.list), n=1, weight.fun=log_fun)
# a[[3]]
#
# aux = c(MOD13Q1.patterns.list, MOD13Q1.patterns.list, MOD13Q1.patterns.list)
# a1 = tsclust(series = aux, distance = "DTW", trace = TRUE, type = "hierarchical", k = 3)
# log_fun = logisticWeight(-0.1, 100)
# a2 = tsclust(series = aux, distance = "TWDTW", trace = TRUE, type = "hierarchical", k = 3, weight.fun=log_fun, span = 300)
# a1
# a2

11 changes: 10 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@
packageStartupMessage(
sprintf("Loaded dtwSat v%s. See ?dtwSat for help, citation(\"dtwSat\") for use in publication.\n",
utils::packageDescription("dtwSat")$Version) )

## Register TWDTW as a distance function into package proxy
pr_DB$set_entry(FUN = .twdtwDist,
names = c("TWDTW","twdtw"),
loop = FALSE,
type = "metric",
description = "Time-Weighted Dynamic Time Warping",
reference = "Maus V, Camara G, Cartaxo R, Sanchez A, Ramos FM, de Queiroz GR (2016). A Time-Weighted Dynamic Time Warping method for land use and land cover mapping. IEEE Journal of Selected Topics in Applied Earth Observations and Remote Sensing, 9 (8), pp. 3729--3739. <doi: 10.1109/JSTARS.2016.2517118>."
);
}

#' @import zoo
#' @import raster
#' @import ggplot2
#' @import methods
#' @import rgdal
#' @importFrom proxy dist
#' @importFrom proxy dist pr_DB
#' @importFrom reshape2 melt
#' @importFrom scales pretty_breaks date_format percent
#' @importFrom grDevices terrain.colors gray.colors
Expand Down

0 comments on commit af71b23

Please sign in to comment.