Skip to content

Commit

Permalink
Merge pull request #54 from SiggiSmara/master
Browse files Browse the repository at this point in the history
Weighted moving average
  • Loading branch information
sgibb authored Nov 12, 2017
2 parents cce5c67 + 063c32e commit ce2ae33
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -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]:
--------------------------------------------------
Expand Down
15 changes: 10 additions & 5 deletions R/smoothingFilters-functions.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@

## .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
##
Expand Down
14 changes: 12 additions & 2 deletions man/smoothIntensity-methods.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ This method smoothes the intensity values of a
}
\usage{
\S4method{smoothIntensity}{MassSpectrum}(object,
method=c("SavitzkyGolay", "MovingAverage"), halfWindowSize,
method=c("SavitzkyGolay", "MovingAverage"),
halfWindowSize,
\dots)
}
\arguments{
Expand All @@ -26,7 +27,12 @@ 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
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}.
Expand All @@ -38,6 +44,7 @@ This method smoothes the intensity values of a
}
\author{
Sebastian Gibb \email{mail@sebastiangibb.de}
Sigurdur Smarason (WeightedAverage smoothing)
}
\references{
A. Savitzky and M. J. Golay. 1964.
Expand All @@ -64,6 +71,9 @@ data("fiedler2009subset", package="MALDIquant")
s <- smoothIntensity(fiedler2009subset, method="MovingAverage",
halfWindowSize=2)
## or
s <- smoothIntensity(fiedler2009subset, method="MovingAverage",
halfWindowSize=2, weighted=TRUE)
## or
s <- smoothIntensity(fiedler2009subset, method="SavitzkyGolay",
halfWindowSize=10)
}
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test_smoothIntensity-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +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="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", {
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test_smoothingFilters-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@ 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)

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)
})


test_that(".movingAverage throws errors", {
expect_error(MALDIquant:::.movingAverage(1:10, halfWindowSize=0),
"too small")
Expand Down

0 comments on commit ce2ae33

Please sign in to comment.