From 3a64b8760765a93d3914cc2be8531b650123b660 Mon Sep 17 00:00:00 2001 From: siggismara Date: Tue, 7 Nov 2017 10:43:19 +0100 Subject: [PATCH 01/13] Adding weighted average smoothing function --- R/smoothIntensity-methods.R | 6 ++++++ R/smoothingFilters-functions.R | 19 +++++++++++++++++++ man/smoothIntensity-methods.Rd | 11 ++++++----- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/R/smoothIntensity-methods.R b/R/smoothIntensity-methods.R index 2721ecc..60efc1d 100644 --- a/R/smoothIntensity-methods.R +++ b/R/smoothIntensity-methods.R @@ -23,6 +23,12 @@ setMethod(f="smoothIntensity", halfWindowSize <- 2L } .movingAverage + }, + "MovingWeightedAverage" = { + if (missing(halfWindowSize)) { + halfWindowSize <- 2L + } + .movingWeightedAverage } ) diff --git a/R/smoothingFilters-functions.R b/R/smoothingFilters-functions.R index c8c556c..6ddce79 100644 --- a/R/smoothingFilters-functions.R +++ b/R/smoothingFilters-functions.R @@ -1,3 +1,22 @@ +## .movingWeightedAverage +## runs a weighted 2-side moving average where +## the weight is calculated as: +## w=1/(2**i) where i is the distance from the center of the window +## +## params: +## y: double, intensity values +## halfWindowSize integer, half window size. +## +## returns: +## double +## +.movingWeightedAverage<-function(y, halfWindowSize=2L) { + windowSize <- 2L * halfWindowSize + 1L + myweigths<-1/2**abs(-halfWindowSize:halfWindowSize) + myweigths<-myweigths/sum(myweigths) + .filter(y, hws=halfWindowSize, + coef=matrix(myweigths, nrow=windowSize, ncol=windowSize)) +} ## .movingAverage ## runs a simple 2-side moving average. diff --git a/man/smoothIntensity-methods.Rd b/man/smoothIntensity-methods.Rd index bdeb3c7..54a7af3 100644 --- a/man/smoothIntensity-methods.Rd +++ b/man/smoothIntensity-methods.Rd @@ -10,14 +10,14 @@ This method smoothes the intensity values of a } \usage{ \S4method{smoothIntensity}{MassSpectrum}(object, - method=c("SavitzkyGolay", "MovingAverage"), halfWindowSize, + method=c("SavitzkyGolay", "MovingAverage","MovingWeightedAverage"), halfWindowSize, \dots) } \arguments{ \item{object}{\code{\linkS4class{AbstractMassObject}} object or a \code{list} of \code{\linkS4class{AbstractMassObject}} objects.} \item{method}{used smoothing method, one of \code{"SavitzkyGolay"} or - \code{"MovingAverage"}.} + \code{"MovingAverage"} or \code{"MovingWeightedAverage"}.} \item{halfWindowSize}{half window size. The resulting window reaches from \code{mass[currentIndex-halfWindowSize]} to \code{mass[currentIndex+halfWindowSize]} (window size is @@ -26,15 +26,16 @@ This method smoothes the intensity values of a selected \code{method}.} \item{\dots}{arguments to be passed to \code{method}. \code{SavitzkyGolay} has an additional \code{polynomialOrder} argument (default: \code{3}) to control - the order of the filter. Unused for \code{MovingAverage}} + the order of the filter. Unused for \code{MovingAverage} and \code{MovingWeightedAverage}} } \details{ \code{halfWindowSize}: Depends on the selected \code{method}. For the \code{SavitzkyGolay} the \code{halfWindowSize} should be smaller than \emph{FWHM} of the peaks (full width at half maximum; please find details in Bromba and Ziegler 1981). - In general the \code{halfWindowSize} for the \code{MovingAverage} has to be - much smaller than for \code{SavitzkyGolay} to conserve the peak shape. + In general the \code{halfWindowSize} for the \code{MovingAverage} and the + \code{MovingWeightedAverage} has to be much smaller than for + \code{SavitzkyGolay} to conserve the peak shape. } \author{ Sebastian Gibb \email{mail@sebastiangibb.de} From 0f619da90fb56c68988589d4eaa38229e44569bc Mon Sep 17 00:00:00 2001 From: siggismara Date: Tue, 7 Nov 2017 10:48:24 +0100 Subject: [PATCH 02/13] Adding more explanation to the man pages for the weighted average smoothing --- man/smoothIntensity-methods.Rd | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/man/smoothIntensity-methods.Rd b/man/smoothIntensity-methods.Rd index 54a7af3..6bc14d1 100644 --- a/man/smoothIntensity-methods.Rd +++ b/man/smoothIntensity-methods.Rd @@ -26,7 +26,8 @@ This method smoothes the intensity values of a selected \code{method}.} \item{\dots}{arguments to be passed to \code{method}. \code{SavitzkyGolay} has an additional \code{polynomialOrder} argument (default: \code{3}) to control - the order of the filter. Unused for \code{MovingAverage} and \code{MovingWeightedAverage}} + the order of the filter. Unused for \code{MovingAverage} and + \code{MovingWeightedAverage}}. } \details{ \code{halfWindowSize}: Depends on the selected \code{method}. @@ -36,6 +37,10 @@ This method smoothes the intensity values of a In general the \code{halfWindowSize} for the \code{MovingAverage} and the \code{MovingWeightedAverage} has to be much smaller than for \code{SavitzkyGolay} to conserve the peak shape. + \code{MovingWeightedAverage} uses a simple weight function \code{1/2**i} where + i is the distance from the center of the window, calculated as + \code{1/2**abs(-halfWindowSize:halfWindowSize)} in R and normalized to the + total sum of the weights. } \author{ Sebastian Gibb \email{mail@sebastiangibb.de} From 078c7af368a4cac4319bdb8c0ddaedeadc5d9a65 Mon Sep 17 00:00:00 2001 From: siggismara Date: Tue, 7 Nov 2017 10:54:38 +0100 Subject: [PATCH 03/13] Minor edits to the man pages --- man/smoothIntensity-methods.Rd | 1 + 1 file changed, 1 insertion(+) diff --git a/man/smoothIntensity-methods.Rd b/man/smoothIntensity-methods.Rd index 6bc14d1..deefc7e 100644 --- a/man/smoothIntensity-methods.Rd +++ b/man/smoothIntensity-methods.Rd @@ -44,6 +44,7 @@ This method smoothes the intensity values of a } \author{ Sebastian Gibb \email{mail@sebastiangibb.de} +Sigurður Smárason (WeightedAverage smoothing) } \references{ A. Savitzky and M. J. Golay. 1964. From 6e4d408d786754a75c9e46642f9be6d0d302ee8c Mon Sep 17 00:00:00 2001 From: siggismara Date: Tue, 7 Nov 2017 11:48:34 +0100 Subject: [PATCH 04/13] Fixed allowed methods for smoothing --- R/smoothIntensity-methods.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/smoothIntensity-methods.R b/R/smoothIntensity-methods.R index 60efc1d..24a538a 100644 --- a/R/smoothIntensity-methods.R +++ b/R/smoothIntensity-methods.R @@ -2,7 +2,7 @@ setMethod(f="smoothIntensity", signature=signature(object="MassSpectrum"), definition=function(object, - method=c("SavitzkyGolay", "MovingAverage"), + method=c("SavitzkyGolay", "MovingAverage","MovingWeightedAverage"), halfWindowSize, ...) { ## empty spectrum? if (.isEmptyWarning(object)) { From 7626ea05f46b7b14364fa6d6f04c9d535a529929 Mon Sep 17 00:00:00 2001 From: siggismara Date: Tue, 7 Nov 2017 12:09:28 +0100 Subject: [PATCH 05/13] Fixing non-ascii charcters in the .Rd file --- man/smoothIntensity-methods.Rd | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/man/smoothIntensity-methods.Rd b/man/smoothIntensity-methods.Rd index deefc7e..a00e6c3 100644 --- a/man/smoothIntensity-methods.Rd +++ b/man/smoothIntensity-methods.Rd @@ -10,7 +10,8 @@ This method smoothes the intensity values of a } \usage{ \S4method{smoothIntensity}{MassSpectrum}(object, - method=c("SavitzkyGolay", "MovingAverage","MovingWeightedAverage"), halfWindowSize, + method=c("SavitzkyGolay", "MovingAverage","MovingWeightedAverage"), + halfWindowSize, \dots) } \arguments{ @@ -37,14 +38,14 @@ This method smoothes the intensity values of a In general the \code{halfWindowSize} for the \code{MovingAverage} and the \code{MovingWeightedAverage} has to be much smaller than for \code{SavitzkyGolay} to conserve the peak shape. - \code{MovingWeightedAverage} uses a simple weight function \code{1/2**i} where - i is the distance from the center of the window, calculated as + \code{MovingWeightedAverage} uses a simple weight function \code{1/2**i} + where i is the distance from the center of the window, calculated as \code{1/2**abs(-halfWindowSize:halfWindowSize)} in R and normalized to the total sum of the weights. } \author{ Sebastian Gibb \email{mail@sebastiangibb.de} -Sigurður Smárason (WeightedAverage smoothing) +Sigurdur Smarason (WeightedAverage smoothing) } \references{ A. Savitzky and M. J. Golay. 1964. @@ -71,6 +72,9 @@ data("fiedler2009subset", package="MALDIquant") s <- smoothIntensity(fiedler2009subset, method="MovingAverage", halfWindowSize=2) ## or +s <- smoothIntensity(fiedler2009subset, method="MovingWeightedAverage", + halfWindowSize=2) +## or s <- smoothIntensity(fiedler2009subset, method="SavitzkyGolay", halfWindowSize=10) } From 7958942b519b5516760f75319736604da8a98d9d Mon Sep 17 00:00:00 2001 From: siggismara Date: Tue, 7 Nov 2017 12:44:22 +0100 Subject: [PATCH 06/13] Added unit tests for weighted moving average smoothing --- tests/testthat/test_smoothIntensity-methods.R | 5 +++++ tests/testthat/test_smoothingFilters-functions.R | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/tests/testthat/test_smoothIntensity-methods.R b/tests/testthat/test_smoothIntensity-methods.R index 084fc4e..464da50 100644 --- a/tests/testthat/test_smoothIntensity-methods.R +++ b/tests/testthat/test_smoothIntensity-methods.R @@ -20,6 +20,11 @@ test_that("smoothIntensity", { expect_equal(intensity(smoothIntensity(s, method="MovingAverage", halfWindowSize=2)), MALDIquant:::.movingAverage(i, halfWindowSize=2)) + expect_equal(length(smoothIntensity(s, method="MovingWeightedAverage", + halfWindowSize=2)), 40) + expect_equal(intensity(smoothIntensity(s, method="MovingWeightedAverage", + halfWindowSize=2)), + MALDIquant:::MovingWeightedAverage(i, halfWindowSize=2)) }) test_that("smoothIntensity works with list of MassSpectrum objects", { diff --git a/tests/testthat/test_smoothingFilters-functions.R b/tests/testthat/test_smoothingFilters-functions.R index 1fd9afb..1961f60 100644 --- a/tests/testthat/test_smoothingFilters-functions.R +++ b/tests/testthat/test_smoothingFilters-functions.R @@ -8,6 +8,14 @@ test_that(".movingAverage", { expect_equal(MALDIquant:::.movingAverage(1:10, 3), values) }) +test_that(".movingWegithedAverage", { + values <- c(rep(3, 3), 4:7, rep(8, 3)) + expect_equal(MALDIquant:::.movingWeightedAverage(1:10, halfWindowSize=2), values) + + values <- c(rep(4, 4), 5:6, rep(7, 4)) + expect_equal(MALDIquant:::.movingWeightedAverage(1:10, 3), values) +}) + test_that(".movingAverage throws errors", { expect_error(MALDIquant:::.movingAverage(1:10, halfWindowSize=0), "too small") From fc5cedccb45bfe49c43998fd43aef3ac310d132d Mon Sep 17 00:00:00 2001 From: siggismara Date: Tue, 7 Nov 2017 14:14:59 +0100 Subject: [PATCH 07/13] Fixed weighted average smoothing function and related unit tests --- R/smoothingFilters-functions.R | 2 +- tests/testthat/test_smoothIntensity-methods.R | 2 +- tests/testthat/test_smoothingFilters-functions.R | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/smoothingFilters-functions.R b/R/smoothingFilters-functions.R index 6ddce79..7305b32 100644 --- a/R/smoothingFilters-functions.R +++ b/R/smoothingFilters-functions.R @@ -15,7 +15,7 @@ myweigths<-1/2**abs(-halfWindowSize:halfWindowSize) myweigths<-myweigths/sum(myweigths) .filter(y, hws=halfWindowSize, - coef=matrix(myweigths, nrow=windowSize, ncol=windowSize)) + coef=matrix(myweigths, nrow=windowSize, ncol=windowSize, byrow = TRUE)) } ## .movingAverage diff --git a/tests/testthat/test_smoothIntensity-methods.R b/tests/testthat/test_smoothIntensity-methods.R index 464da50..bfc46a5 100644 --- a/tests/testthat/test_smoothIntensity-methods.R +++ b/tests/testthat/test_smoothIntensity-methods.R @@ -24,7 +24,7 @@ test_that("smoothIntensity", { halfWindowSize=2)), 40) expect_equal(intensity(smoothIntensity(s, method="MovingWeightedAverage", halfWindowSize=2)), - MALDIquant:::MovingWeightedAverage(i, halfWindowSize=2)) + MALDIquant:::.movingWeightedAverage(i, halfWindowSize=2)) }) test_that("smoothIntensity works with list of MassSpectrum objects", { diff --git a/tests/testthat/test_smoothingFilters-functions.R b/tests/testthat/test_smoothingFilters-functions.R index 1961f60..df98dbd 100644 --- a/tests/testthat/test_smoothingFilters-functions.R +++ b/tests/testthat/test_smoothingFilters-functions.R @@ -8,8 +8,8 @@ test_that(".movingAverage", { expect_equal(MALDIquant:::.movingAverage(1:10, 3), values) }) -test_that(".movingWegithedAverage", { - values <- c(rep(3, 3), 4:7, rep(8, 3)) +test_that(".movingWeightedAverage", { + values <- c(rep(3,3),4:7,rep(8,3)) expect_equal(MALDIquant:::.movingWeightedAverage(1:10, halfWindowSize=2), values) values <- c(rep(4, 4), 5:6, rep(7, 4)) From c6418e03527f3910602d7644f3ecb9d2f283fda5 Mon Sep 17 00:00:00 2001 From: siggismara Date: Tue, 7 Nov 2017 14:25:07 +0100 Subject: [PATCH 08/13] Added one more test to see ensure that missing halfWindowSize results in 2L being used --- tests/testthat/test_smoothIntensity-methods.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/testthat/test_smoothIntensity-methods.R b/tests/testthat/test_smoothIntensity-methods.R index bfc46a5..b29a777 100644 --- a/tests/testthat/test_smoothIntensity-methods.R +++ b/tests/testthat/test_smoothIntensity-methods.R @@ -25,6 +25,9 @@ test_that("smoothIntensity", { expect_equal(intensity(smoothIntensity(s, method="MovingWeightedAverage", halfWindowSize=2)), MALDIquant:::.movingWeightedAverage(i, halfWindowSize=2)) + expect_equal(intensity(smoothIntensity(s, method="MovingWeightedAverage", + halfWindowSize=2)), + intensity(smoothIntensity(s, method="MovingWeightedAverage"))) }) test_that("smoothIntensity works with list of MassSpectrum objects", { From 74b2afd9e14295f7e46b3fc502df06075f51f3f3 Mon Sep 17 00:00:00 2001 From: siggismara Date: Wed, 8 Nov 2017 12:11:42 +0100 Subject: [PATCH 09/13] Changed MovingAverage to incorporate weigthed MovingAverage as well --- R/smoothIntensity-methods.R | 8 +---- R/smoothingFilters-functions.R | 34 ++++++------------- man/smoothIntensity-methods.Rd | 25 +++++++------- tests/testthat/test_smoothIntensity-methods.R | 20 ++++++----- .../test_smoothingFilters-functions.R | 10 ++---- 5 files changed, 38 insertions(+), 59 deletions(-) diff --git a/R/smoothIntensity-methods.R b/R/smoothIntensity-methods.R index 24a538a..2721ecc 100644 --- a/R/smoothIntensity-methods.R +++ b/R/smoothIntensity-methods.R @@ -2,7 +2,7 @@ setMethod(f="smoothIntensity", signature=signature(object="MassSpectrum"), definition=function(object, - method=c("SavitzkyGolay", "MovingAverage","MovingWeightedAverage"), + method=c("SavitzkyGolay", "MovingAverage"), halfWindowSize, ...) { ## empty spectrum? if (.isEmptyWarning(object)) { @@ -23,12 +23,6 @@ setMethod(f="smoothIntensity", halfWindowSize <- 2L } .movingAverage - }, - "MovingWeightedAverage" = { - if (missing(halfWindowSize)) { - halfWindowSize <- 2L - } - .movingWeightedAverage } ) diff --git a/R/smoothingFilters-functions.R b/R/smoothingFilters-functions.R index 7305b32..6f27895 100644 --- a/R/smoothingFilters-functions.R +++ b/R/smoothingFilters-functions.R @@ -1,40 +1,26 @@ -## .movingWeightedAverage -## runs a weighted 2-side moving average where -## the weight is calculated as: -## w=1/(2**i) where i is the distance from the center of the window -## -## params: -## y: double, intensity values -## halfWindowSize integer, half window size. -## -## returns: -## double -## -.movingWeightedAverage<-function(y, halfWindowSize=2L) { - windowSize <- 2L * halfWindowSize + 1L - myweigths<-1/2**abs(-halfWindowSize:halfWindowSize) - myweigths<-myweigths/sum(myweigths) - .filter(y, hws=halfWindowSize, - coef=matrix(myweigths, nrow=windowSize, ncol=windowSize, byrow = TRUE)) -} - ## .movingAverage ## runs a simple 2-side moving average. ## ## params: ## y: double, intensity values -## halfWindowSize integer, half window size. -## +## halfWindowSize integer, half window size +## weighted boolean, if TRUE then applies weighted average, otherwise unweighted average. ## returns: ## double ## -.movingAverage <- function(y, halfWindowSize=2L) { +.movingAverage <- function(y, halfWindowSize=2L, weighted=FALSE) { .stopIfNotIsValidHalfWindowSize(halfWindowSize, n=length(y)) windowSize <- 2L * halfWindowSize + 1L + if (weighted) { + weights <- 1 / 2^abs(-halfWindowSize:halfWindowSize) + } else { + weights <- rep.int(1L, windowSize) + } .filter(y, hws=halfWindowSize, - coef=matrix(1L / windowSize, nrow=windowSize, ncol=windowSize)) + coef=matrix(weights/sum(weights), nrow=windowSize, ncol=windowSize, byrow=TRUE)) } + ## .savitzkyGolay ## runs a savitzky golay filter ## diff --git a/man/smoothIntensity-methods.Rd b/man/smoothIntensity-methods.Rd index a00e6c3..171988a 100644 --- a/man/smoothIntensity-methods.Rd +++ b/man/smoothIntensity-methods.Rd @@ -10,7 +10,7 @@ This method smoothes the intensity values of a } \usage{ \S4method{smoothIntensity}{MassSpectrum}(object, - method=c("SavitzkyGolay", "MovingAverage","MovingWeightedAverage"), + method=c("SavitzkyGolay", "MovingAverage"), halfWindowSize, \dots) } @@ -18,7 +18,7 @@ This method smoothes the intensity values of a \item{object}{\code{\linkS4class{AbstractMassObject}} object or a \code{list} of \code{\linkS4class{AbstractMassObject}} objects.} \item{method}{used smoothing method, one of \code{"SavitzkyGolay"} or - \code{"MovingAverage"} or \code{"MovingWeightedAverage"}.} + \code{"MovingAverage"}.} \item{halfWindowSize}{half window size. The resulting window reaches from \code{mass[currentIndex-halfWindowSize]} to \code{mass[currentIndex+halfWindowSize]} (window size is @@ -27,21 +27,20 @@ This method smoothes the intensity values of a selected \code{method}.} \item{\dots}{arguments to be passed to \code{method}. \code{SavitzkyGolay} has an additional \code{polynomialOrder} argument (default: \code{3}) to control - the order of the filter. Unused for \code{MovingAverage} and - \code{MovingWeightedAverage}}. + the order of the filter. Unused for \code{MovingAverage}. + \code{MovingAverage} has an additional \code{weighted} argument (default: + \code{FALSE}) to indicate if the average should be equal weight (default) or + if it should have weights depending on the distance from the center as + calculated as \code{1/2^abs(-halfWindowSize:halfWindowSize)} with the sum + of all weigths normalized to 1. } \details{ \code{halfWindowSize}: Depends on the selected \code{method}. For the \code{SavitzkyGolay} the \code{halfWindowSize} should be smaller than \emph{FWHM} of the peaks (full width at half maximum; please find details in Bromba and Ziegler 1981). - In general the \code{halfWindowSize} for the \code{MovingAverage} and the - \code{MovingWeightedAverage} has to be much smaller than for - \code{SavitzkyGolay} to conserve the peak shape. - \code{MovingWeightedAverage} uses a simple weight function \code{1/2**i} - where i is the distance from the center of the window, calculated as - \code{1/2**abs(-halfWindowSize:halfWindowSize)} in R and normalized to the - total sum of the weights. + In general the \code{halfWindowSize} for the \code{MovingAverage} has to be + much smaller than for \code{SavitzkyGolay} to conserve the peak shape. } \author{ Sebastian Gibb \email{mail@sebastiangibb.de} @@ -72,8 +71,8 @@ data("fiedler2009subset", package="MALDIquant") s <- smoothIntensity(fiedler2009subset, method="MovingAverage", halfWindowSize=2) ## or -s <- smoothIntensity(fiedler2009subset, method="MovingWeightedAverage", - halfWindowSize=2) +s <- smoothIntensity(fiedler2009subset, method="MovingAverage", + halfWindowSize=2, weights=TRUE) ## or s <- smoothIntensity(fiedler2009subset, method="SavitzkyGolay", halfWindowSize=10) diff --git a/tests/testthat/test_smoothIntensity-methods.R b/tests/testthat/test_smoothIntensity-methods.R index b29a777..a3741c8 100644 --- a/tests/testthat/test_smoothIntensity-methods.R +++ b/tests/testthat/test_smoothIntensity-methods.R @@ -20,14 +20,18 @@ test_that("smoothIntensity", { expect_equal(intensity(smoothIntensity(s, method="MovingAverage", halfWindowSize=2)), MALDIquant:::.movingAverage(i, halfWindowSize=2)) - expect_equal(length(smoothIntensity(s, method="MovingWeightedAverage", - halfWindowSize=2)), 40) - expect_equal(intensity(smoothIntensity(s, method="MovingWeightedAverage", - halfWindowSize=2)), - MALDIquant:::.movingWeightedAverage(i, halfWindowSize=2)) - expect_equal(intensity(smoothIntensity(s, method="MovingWeightedAverage", - halfWindowSize=2)), - intensity(smoothIntensity(s, method="MovingWeightedAverage"))) + expect_equal(length(smoothIntensity(s, method="MovingAverage", + halfWindowSize=2, weighted=TRUE)), 40) + expect_equal(intensity(smoothIntensity(s, method="MovingAverage", + halfWindowSize=2, weighted=TRUE)), + MALDIquant:::.movingAverage(i, halfWindowSize=2, weighted=TRUE)) + expect_equal(intensity(smoothIntensity(s, method="MovingAverage", + halfWindowSize=2, weighted=TRUE)), + intensity(smoothIntensity(s, method="MovingAverage", + weighted=TRUE))) + expect_equal(intensity(smoothIntensity(s, method="MovingAverage", + halfWindowSize=2)), + intensity(smoothIntensity(s, method="MovingAverage"))) }) test_that("smoothIntensity works with list of MassSpectrum objects", { diff --git a/tests/testthat/test_smoothingFilters-functions.R b/tests/testthat/test_smoothingFilters-functions.R index df98dbd..260f661 100644 --- a/tests/testthat/test_smoothingFilters-functions.R +++ b/tests/testthat/test_smoothingFilters-functions.R @@ -3,18 +3,14 @@ context("smoothingFilters") test_that(".movingAverage", { values <- c(rep(3, 3), 4:7, rep(8, 3)) expect_equal(MALDIquant:::.movingAverage(1:10, halfWindowSize=2), values) + expect_equal(MALDIquant:::.movingAverage(1:10, halfWindowSize=2, + weighted=TRUE), values) values <- c(rep(4, 4), 5:6, rep(7, 4)) expect_equal(MALDIquant:::.movingAverage(1:10, 3), values) + expect_equal(MALDIquant:::.movingAverage(1:10, 3, weighted=TRUE), values) }) -test_that(".movingWeightedAverage", { - values <- c(rep(3,3),4:7,rep(8,3)) - expect_equal(MALDIquant:::.movingWeightedAverage(1:10, halfWindowSize=2), values) - - values <- c(rep(4, 4), 5:6, rep(7, 4)) - expect_equal(MALDIquant:::.movingWeightedAverage(1:10, 3), values) -}) test_that(".movingAverage throws errors", { expect_error(MALDIquant:::.movingAverage(1:10, halfWindowSize=0), From 32b7c52661dbf25054ba49bea13a3a2a4f8fb220 Mon Sep 17 00:00:00 2001 From: siggismara Date: Wed, 8 Nov 2017 12:29:41 +0100 Subject: [PATCH 10/13] Fixing a typo --- man/smoothIntensity-methods.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/smoothIntensity-methods.Rd b/man/smoothIntensity-methods.Rd index 171988a..ad6dd60 100644 --- a/man/smoothIntensity-methods.Rd +++ b/man/smoothIntensity-methods.Rd @@ -32,7 +32,7 @@ This method smoothes the intensity values of a \code{FALSE}) to indicate if the average should be equal weight (default) or if it should have weights depending on the distance from the center as calculated as \code{1/2^abs(-halfWindowSize:halfWindowSize)} with the sum - of all weigths normalized to 1. + of all weigths normalized to 1.} } \details{ \code{halfWindowSize}: Depends on the selected \code{method}. From 42fbd58641a0fa8caf8efa3eaad46467d28d3003 Mon Sep 17 00:00:00 2001 From: siggismara Date: Wed, 8 Nov 2017 12:34:43 +0100 Subject: [PATCH 11/13] Another typo --- man/smoothIntensity-methods.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/smoothIntensity-methods.Rd b/man/smoothIntensity-methods.Rd index ad6dd60..df8d958 100644 --- a/man/smoothIntensity-methods.Rd +++ b/man/smoothIntensity-methods.Rd @@ -72,7 +72,7 @@ s <- smoothIntensity(fiedler2009subset, method="MovingAverage", halfWindowSize=2) ## or s <- smoothIntensity(fiedler2009subset, method="MovingAverage", - halfWindowSize=2, weights=TRUE) + halfWindowSize=2, weighted=TRUE) ## or s <- smoothIntensity(fiedler2009subset, method="SavitzkyGolay", halfWindowSize=10) From 8cf324dfd604c8e2266796a893ed2388ee5481d2 Mon Sep 17 00:00:00 2001 From: siggismara Date: Thu, 9 Nov 2017 09:51:19 +0100 Subject: [PATCH 12/13] added tests where weighted smoothing and unweigted smoothing differ code tyding in the function itself and in the man page --- R/smoothingFilters-functions.R | 2 +- man/smoothIntensity-methods.Rd | 2 +- tests/testthat/test_smoothingFilters-functions.R | 10 +++++++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/R/smoothingFilters-functions.R b/R/smoothingFilters-functions.R index 6f27895..2e929a8 100644 --- a/R/smoothingFilters-functions.R +++ b/R/smoothingFilters-functions.R @@ -17,7 +17,7 @@ weights <- rep.int(1L, windowSize) } .filter(y, hws=halfWindowSize, - coef=matrix(weights/sum(weights), nrow=windowSize, ncol=windowSize, byrow=TRUE)) + coef=matrix(weights / sum(weights), nrow=windowSize, ncol=windowSize, byrow=TRUE)) } diff --git a/man/smoothIntensity-methods.Rd b/man/smoothIntensity-methods.Rd index df8d958..2951652 100644 --- a/man/smoothIntensity-methods.Rd +++ b/man/smoothIntensity-methods.Rd @@ -27,7 +27,7 @@ This method smoothes the intensity values of a selected \code{method}.} \item{\dots}{arguments to be passed to \code{method}. \code{SavitzkyGolay} has an additional \code{polynomialOrder} argument (default: \code{3}) to control - the order of the filter. Unused for \code{MovingAverage}. + the order of the filter. \code{MovingAverage} has an additional \code{weighted} argument (default: \code{FALSE}) to indicate if the average should be equal weight (default) or if it should have weights depending on the distance from the center as diff --git a/tests/testthat/test_smoothingFilters-functions.R b/tests/testthat/test_smoothingFilters-functions.R index 260f661..4d31989 100644 --- a/tests/testthat/test_smoothingFilters-functions.R +++ b/tests/testthat/test_smoothingFilters-functions.R @@ -4,11 +4,19 @@ test_that(".movingAverage", { values <- c(rep(3, 3), 4:7, rep(8, 3)) expect_equal(MALDIquant:::.movingAverage(1:10, halfWindowSize=2), values) expect_equal(MALDIquant:::.movingAverage(1:10, halfWindowSize=2, - weighted=TRUE), values) + weighted=TRUE), values) values <- c(rep(4, 4), 5:6, rep(7, 4)) expect_equal(MALDIquant:::.movingAverage(1:10, 3), values) expect_equal(MALDIquant:::.movingAverage(1:10, 3, weighted=TRUE), values) + + signal <- c(rep(1, 4), 6, 20, 6, rep(1, 4)) + resAve <- c(2.0, 2.0, 2.0, 5.8, 6.8, 6.8, 6.8, 5.8, 2.0, 2.0, 2.0) + reswAve <- c(1.5, 1.5, 1.5, 3.9, 7.3,10.6, 7.3, 3.9, 1.5, 1.5, 1.5) + expect_equal(MALDIquant:::.movingAverage(signal, halfWindowSize=2, + weighted=FALSE),resAve) + expect_equal(MALDIquant:::.movingAverage(signal, halfWindowSize=2, + weighted=TRUE),reswAve) }) From 063c32eabf15aec6256b292f4a65b9d21153d484 Mon Sep 17 00:00:00 2001 From: siggismara Date: Thu, 9 Nov 2017 09:55:54 +0100 Subject: [PATCH 13/13] updated the news with the weighted moving average --- NEWS | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/NEWS b/NEWS index 478fece..26c0d06 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,11 @@ RELEASE HISTORY OF THE "MALDIquant" PACKAGE =========================================== +CHANGES IN MALDIquant VERSION 1.17: +-------------------------------------------------- +IMPROVEMENTS + +* Added weighted moving average as an option in moving average smoothing of spectra + CHANGES IN MALDIquant VERSION 1.16.4 [2017-08-27]: --------------------------------------------------