Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. function (x, y = NULL, dist.method = "Euclidean", step.pattern = symmetric2,
  2. window.type = "none", keep.internals = FALSE, distance.only = FALSE,
  3. open.end = FALSE, open.begin = FALSE, ...)
  4. lm <- NULL
  5. if (is.null(y)) {
  6. if (!is.matrix(x))
  7. stop("Single argument requires a global cost matrix")
  8. lm <- x
  9. }
  10. else if (is.character(dist.method)) {
  11. x <- as.matrix(x)
  12. y <- as.matrix(y)
  13. lm <- proxy::dist(x, y, method = dist.method)
  14. }
  15. else if (is.function(dist.method)) {
  16. stop("Unimplemented")
  17. }
  18. else {
  19. stop("dist.method should be a character method supported by proxy::dist()")
  20. }
  21. wfun <- .canonicalizeWindowFunction(window.type)
  22. dir <- step.pattern
  23. norm <- attr(dir, "norm")
  24. if (!is.null(list(...)$partial)) {
  25. warning("Argument `partial' is obsolete. Use `open.end' instead")
  26. open.end <- TRUE
  27. }
  28. n <- nrow(lm)
  29. m <- ncol(lm)
  30. if (open.begin) {
  31. if (is.na(norm) || norm != "N") {
  32. stop("Open-begin requires step patterns with 'N' normalization (e.g. asymmetric, or R-J types (c)). See papers in citation().")
  33. }
  34. lm <- rbind(0, lm)
  35. np <- n + 1
  36. precm <- matrix(NA, nrow = np, ncol = m)
  37. precm[1, ] <- 0
  38. }
  39. else {
  40. precm <- NULL
  41. np <- n
  42. }
  43. gcm <- globalCostMatrix(lm, step.matrix = dir, window.function = wfun,
  44. seed = precm, ...)
  45. gcm$N <- n
  46. gcm$M <- m
  47. gcm$call <- match.call()
  48. gcm$openEnd <- open.end
  49. gcm$openBegin <- open.begin
  50. gcm$windowFunction <- wfun
  51. lastcol <- gcm$costMatrix[np, ]
  52. if (is.na(norm)) {
  53. }
  54. else if (norm == "N+M") {
  55. lastcol <- lastcol/(n + (1:m))
  56. }
  57. else if (norm == "N") {
  58. lastcol <- lastcol/n
  59. }
  60. else if (norm == "M") {
  61. lastcol <- lastcol/(1:m)
  62. }
  63. gcm$jmin <- m
  64. if (open.end) {
  65. if (is.na(norm)) {
  66. stop("Open-end alignments require normalizable step patterns")
  67. }
  68. gcm$jmin <- which.min(lastcol)
  69. }
  70. gcm$distance <- gcm$costMatrix[np, gcm$jmin]
  71. if (is.na(gcm$distance)) {
  72. stop("No warping path exists that is allowed by costraints")
  73. }
  74. if (!is.na(norm)) {
  75. gcm$normalizedDistance <- lastcol[gcm$jmin]
  76. }
  77. else {
  78. gcm$normalizedDistance <- NA
  79. }
  80. if (!distance.only) {
  81. mapping <- backtrack(gcm)
  82. gcm <- c(gcm, mapping)
  83. }
  84. if (open.begin) {
  85. gcm$index1 <- gcm$index1[-1] - 1
  86. gcm$index2 <- gcm$index2[-1]
  87. lm <- lm[-1, ]
  88. gcm$costMatrix <- gcm$costMatrix[-1, ]
  89. gcm$directionMatrix <- gcm$directionMatrix[-1, ]
  90. }
  91. if (!keep.internals) {
  92. gcm$costMatrix <- NULL
  93. gcm$directionMatrix <- NULL
  94. }
  95. else {
  96. gcm$localCostMatrix <- lm
  97. if (!is.null(y)) {
  98. gcm$query <- x
  99. gcm$reference <- y
  100. }
  101. }
  102. class(gcm) <- "dtw"
  103. return(gcm)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement