From dd63a63d9d948d5210c9a9dfff75e1e7c2a0d9d8 Mon Sep 17 00:00:00 2001 From: Ethan Bass Date: Sun, 18 Aug 2024 16:52:28 -0400 Subject: [PATCH] style: whitespace, use seq_len instead of `1:n` syntax --- R/VPdtw.R | 112 ++++++++++++++++++++++++------------------- R/dilation.R | 2 +- R/plot.VPdtw.R | 10 ++-- R/signalMatchABand.R | 6 +-- 4 files changed, 72 insertions(+), 58 deletions(-) diff --git a/R/VPdtw.R b/R/VPdtw.R index dd83d7f..6c5daa8 100644 --- a/R/VPdtw.R +++ b/R/VPdtw.R @@ -140,8 +140,8 @@ #' #' @export VPdtw -VPdtw <- function(reference, query, penalty=0, maxshift=50, - Reference.type=c("random", "median", "mean", "trimmed")) { +VPdtw <- function(reference, query, penalty = 0, maxshift = 50, + Reference.type = c("random", "median", "mean", "trimmed")) { ## We assume Sakoe Chiba DTW to allow for faster computation times if(!is.numeric(maxshift)) @@ -163,7 +163,7 @@ VPdtw <- function(reference, query, penalty=0, maxshift=50, ## the value of type as specified by the user. type <- match.arg(Reference.type, c("random", "median", "mean", "trimmed")) - ss <- sample(1:ncol(query), 1) + ss <- sample(seq_len(ncol(query)), 1) reference <- switch(type, random = query[,ss], median = apply(query, 1, median, na.rm = TRUE), @@ -221,12 +221,12 @@ VPdtw <- function(reference, query, penalty=0, maxshift=50, if(is.vector(reference) & is.vector(query) & is.vector(penalty)) { information <- "Reference is supplied by the user.\n" information <- paste(information, "Query vector is of length ", - length(query), ".\n", sep="") + length(query), ".\n", sep = "") information <- paste(information, - "Single Penalty vector supplied by user.\n", sep="") + "Single Penalty vector supplied by user.\n", sep = "") information <- paste(information, "Max allowed shift is ", - maxshift, ".\n", sep="") + maxshift, ".\n", sep = "") reference <- na.omit(reference) if(length(penalty) == 1) @@ -296,7 +296,8 @@ VPdtw <- function(reference, query, penalty=0, maxshift=50, ## result is now a list of length ncol(penalty) each part is a matrix xlim <- NULL - for(ii in 1:length(result)) xlim <- c(xlim, range(result[[ii]][,1])) + for(ii in seq_len(length(result))) + xlim <- c(xlim, range(result[[ii]][,1])) xlim <- range(xlim) xVals <- seq(xlim[1], xlim[2], by = 1) @@ -316,18 +317,18 @@ VPdtw <- function(reference, query, penalty=0, maxshift=50, output$penalty <- penalty output$warpedQuery <- matrix(NA, length(xVals), ncol(query)) - colnames(output$warpedQuery) <- paste("warped query", 1:ncol(query)) + colnames(output$warpedQuery) <- paste("warped query", seq_len(ncol(query))) output$shift <- matrix(NA, length(xVals), ncol(query)) - colnames(output$shift) <- paste("shift", 1:ncol(query)) + colnames(output$shift) <- paste("shift", seq_len(ncol(query))) - for(ii in 1:ncol(query)) { - colName <- paste("warped query",ii) - str <- which(xVals == result[[ii]][1, 1]) + for(ii in seq_len(ncol(query))) { + colName <- paste("warped query", ii) + str <- which(xVals == result[[ii]][1,1]) end <- which(xVals == result[[ii]][nrow(result[[ii]]), 1]) - output$warpedQuery[seq(str, end, by = 1), colName] <- result[[ii]][, 3] + output$warpedQuery[seq(str, end, by = 1), colName] <- result[[ii]][,3] colName <- paste("shift",ii) - output$shift[seq(str, end, by = 1), colName] <- result[[ii]][, 4] + output$shift[seq(str, end, by = 1), colName] <- result[[ii]][,4] } class(output) <- "VPdtw" @@ -337,8 +338,8 @@ VPdtw <- function(reference, query, penalty=0, maxshift=50, cost2 <- function(x,ii) { ret <- c(sum(abs(x$warpedQuery[,ii] - x$reference), na.rm = TRUE) + - sum(x$penalty[x$xVals[which(diff(x$shift[,ii])==1)+1]], - na.rm=TRUE) + + sum(x$penalty[x$xVals[which(diff(x$shift[,ii]) == 1) + 1]], + na.rm = TRUE) + 2*sum(x$penalty[x$xVals[which(diff(x$shift[,ii]) == -1) + 1]], na.rm = TRUE), sum(!is.na(x$warpedQuery[,ii] * x$reference), na.rm = TRUE), @@ -347,20 +348,24 @@ VPdtw <- function(reference, query, penalty=0, maxshift=50, sum(diff(x$shift[,ii]) == 1, na.rm = TRUE), sum(diff(x$shift[,ii]) == -1, na.rm = TRUE)) names(ret) <- c("Cost", "Overlap", "Max Obs Shift", "# Diag Moves", - "# Expanded","# Dropped") + "# Expanded", "# Dropped") ret } output$summary <- NULL - for(ii in 1:ncol(output$warpedQuery)) output$summary <- rbind(output$summary,cost2(output,ii)) - rownames(output$summary) <- paste("Query #",1:ncol(output$warpedQuery),":",sep="") + for(ii in seq_len(ncol(output$warpedQuery))){ + output$summary <- rbind(output$summary, cost2(output,ii)) + } + rownames(output$summary) <- paste("Query #", + seq_len(ncol(output$warpedQuery)), + ":", sep = "") output$information <- information return(invisible(output)) } ## For doing alignment for many different penalties - DoAlignmentP <- function(penalty,query,reference,maxshift) { + DoAlignmentP <- function(penalty, query, reference, maxshift) { ## Drop NAs reference <- na.omit(reference) query <- na.omit(query) @@ -372,16 +377,17 @@ VPdtw <- function(reference, query, penalty=0, maxshift=50, } ## Scenario 3: vector query and penalty matrix - if(is.vector(query) & is.matrix(penalty)) { + if(is.vector(query) & is.matrix(penalty)){ information <- paste(information, "Query vector of length ", - length(query), ".\n", sep="") + length(query), ".\n", sep = "") - information <- paste(information,"Penalty matrix made up of ", - ncol(penalty)," penalties supplied by user.\n",sep="") + information <- paste(information, "Penalty matrix made up of ", + ncol(penalty), " penalties supplied by user.\n", + sep = "") - information <- paste(information,"Max allowed shift is ", maxshift, - ".\n", sep="") + information <- paste(information, "Max allowed shift is ", maxshift, + ".\n", sep = "") reference <- na.omit(reference) ## Align query to reference using each of the penalties separately penaltyL <- as.list(as.data.frame(penalty)) ## to ensure I get a list in the next round, can't apply to matrix @@ -390,61 +396,67 @@ VPdtw <- function(reference, query, penalty=0, maxshift=50, ## result is now a list of length ncol(penalty) each part is a matrix xlim <- NULL - for(ii in 1:length(result)) xlim <- c(xlim,range(result[[ii]][,1])) + for(ii in seq_len(length(result))) + xlim <- c(xlim, range(result[[ii]][,1])) xlim <- range(xlim) xVals <- seq(xlim[1], xlim[2], by=1) output <- vector("list", 6) - names(output) <- c("xVals","reference","query","penalty","warpedQuery","shift") + names(output) <- c("xVals", "reference", "query", + "penalty", "warpedQuery", "shift") output$xVals <- xVals - str <- which(xVals==1) - end <- which(xVals==length(reference)) - output$reference <- rep(NA,length(xVals)) - output$reference[seq(str,end,by=1)] <- reference + str <- which(xVals == 1) + end <- which(xVals == length(reference)) + output$reference <- rep(NA, length(xVals)) + output$reference[seq(str, end, by = 1)] <- reference output$query <- query output$penalty <- penalty - output$warpedQuery <- matrix(NA,length(xVals),ncol(penalty)) - colnames(output$warpedQuery) <- paste("warped query penalty",1:ncol(penalty)) + output$warpedQuery <- matrix(NA, length(xVals), ncol(penalty)) + colnames(output$warpedQuery) <- paste("warped query penalty", seq_len(ncol(penalty))) - output$shift <- matrix(NA,length(xVals),ncol(penalty)) - colnames(output$shift) <- paste("shift penalty",1:ncol(penalty)) + output$shift <- matrix(NA, length(xVals), ncol(penalty)) + colnames(output$shift) <- paste("shift penalty", seq_len(ncol(penalty))) - for(ii in 1:ncol(penalty)) { - colName <- paste("warped query penalty",ii) + for(ii in seq_len(ncol(penalty))) { + colName <- paste("warped query penalty", ii) str <- which(xVals == result[[ii]][1,1]) - end <- which(xVals == result[[ii]][nrow(result[[ii]]),1]) + end <- which(xVals == result[[ii]][nrow(result[[ii]]), 1]) output$warpedQuery[seq(str, end, by=1), colName] <- result[[ii]][,3] colName <- paste("shift penalty", ii) - output$shift[seq(str, end, by=1), colName] <- result[[ii]][,4] + output$shift[seq(str, end, by = 1), colName] <- result[[ii]][,4] } class(output) <- "VPdtw" ## Summary Statistics for each query separately cost3 <- function(x,ii) { - ret <- c(sum(abs(x$warpedQuery[,ii] - x$reference), na.rm=TRUE) + + ret <- c(sum(abs(x$warpedQuery[,ii] - x$reference), na.rm = TRUE) + sum(x$penalty[,ii][x$xVals[which(diff(x$shift[,ii]) == 1) + 1]], - na.rm=TRUE) + + na.rm = TRUE) + 2*sum(x$penalty[,ii][x$xVals[which(diff(x$shift[,ii]) == -1) + 1]], - na.rm=TRUE), - sum(!is.na(x$warpedQuery[,ii] * x$reference), na.rm=TRUE), - max(abs(x$shift[,ii]), na.rm=TRUE), - sum(diff(x$shift[,ii]) == 0, na.rm=TRUE)+1, - sum(diff(x$shift[,ii]) == 1, na.rm=TRUE), - sum(diff(x$shift[,ii]) == -1, na.rm=TRUE)) + na.rm = TRUE), + sum(!is.na(x$warpedQuery[,ii] * x$reference), na.rm = TRUE), + max(abs(x$shift[,ii]), na.rm = TRUE), + sum(diff(x$shift[,ii]) == 0, na.rm = TRUE) + 1, + sum(diff(x$shift[,ii]) == 1, na.rm = TRUE), + sum(diff(x$shift[,ii]) == -1, na.rm = TRUE)) names(ret) <- c("Cost", "Overlap", "Max Obs Shift", "# Diag Moves", "# Expanded","# Dropped") ret } output$summary <- NULL - for(ii in 1:ncol(output$warpedQuery)) output$summary <- rbind(output$summary,cost3(output,ii)) - rownames(output$summary) <- paste("Penalty #", 1:ncol(output$warpedQuery), ":", sep="") + for(ii in seq_len(ncol(output$warpedQuery))){ + output$summary <- rbind(output$summary, cost3(output, ii)) + } + rownames(output$summary) <- paste("Penalty #", + seq_len(ncol(output$warpedQuery)), ":", + sep = "") output$information <- information return(invisible(output)) diff --git a/R/dilation.R b/R/dilation.R index 1dc8f64..95c07ee 100644 --- a/R/dilation.R +++ b/R/dilation.R @@ -43,7 +43,7 @@ #' #' @export dilation -dilation <- function(y,span){ +dilation <- function(y, span){ y <- na.omit(y) nmes <- names(y) res <- .C("dilation", diff --git a/R/plot.VPdtw.R b/R/plot.VPdtw.R index 951fa37..8fff102 100644 --- a/R/plot.VPdtw.R +++ b/R/plot.VPdtw.R @@ -90,7 +90,8 @@ plot.VPdtw <- function(x, type = c("All", "Before", "After", plot(xlim, ylim, type = "n", xlab = "Index", ylab = "Intensity", main = "Query and Reference after Alignment") lines(x$xVals, x$reference, lwd = 2, col = 1) - matplot(x$xVals, x$warpedQuery, type = "l", lty = 1, add = TRUE, col = 2:(ncols+1)) + matplot(x$xVals, x$warpedQuery, type = "l", lty = 1, add = TRUE, + col = (seq_len(ncols)+1)) legend("topright", legend = c("Reference", paste("penalty #", seq_len(ncols), sep = "")), @@ -104,13 +105,14 @@ plot.VPdtw <- function(x, type = c("All", "Before", "After", ncols <- ncol(x$shift) matplot(x$xVals, x$shift, type="l", lty=1, xlab = "Index", ylab = "Shift", - main = "Shifts required for Alignment", col = 2:(ncols+1), xlim = xlim) + main = "Shifts required for Alignment", col = seq_len(ncols)+1, + xlim = xlim) if (is.matrix(x$penalty)) legend("topleft", legend = paste("penalty #", seq_len(ncols), sep = ""), - col=2:(ncols+1), lty = rep(1, ncols)) + col = seq_len(ncols)+1, lty = rep(1, ncols)) if (is.matrix(x$query)) legend("topleft", legend = paste("query #", seq_len(ncols), sep = ""), - col=2:(ncols+1), lty = rep(1, ncols)) + col = seq_len(ncols)+1, lty = rep(1, ncols)) abline(h = 0, lty = 2, col = grey(0.75)) } if(is.vector(x$shift)) { diff --git a/R/signalMatchABand.R b/R/signalMatchABand.R index d94e30b..5750504 100644 --- a/R/signalMatchABand.R +++ b/R/signalMatchABand.R @@ -42,9 +42,9 @@ signalMatchABand <- function(reference, query, maxp <- max(path, na.rm = TRUE) xIndices <- path - xVals <- 1:length(path) + xVals <- seq_len(length(path)) if (minp > 1) { - xIndices <- c(1:(minp - 1), xIndices) + xIndices <- c(seq_len(minp - 1), xIndices) xVals <- c(seq(to = 0, len = minp - 1, by = 1), xVals) } if(maxp < length(query)) { @@ -54,7 +54,7 @@ signalMatchABand <- function(reference, query, } if (FALSE) { - plot(reference, type="l", lwd=2, xlim = c(1-maxshift, nr + maxshift)) + plot(reference, type = "l", lwd = 2, xlim = c(1 - maxshift, nr + maxshift)) lines(which(!is.na(path)), query[na.omit(path)], col = 2) lines(xVals, query[xIndices], col = 3, lty = 2) }