Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weighted moving average #54

Merged
merged 13 commits into from
Nov 12, 2017
Merged

Weighted moving average #54

merged 13 commits into from
Nov 12, 2017

Conversation

SiggiSmara
Copy link
Contributor

Preemptively creating the pull request for the weighted moving average. More explanation in #53

@codecov
Copy link

codecov bot commented Nov 7, 2017

Codecov Report

Merging #54 into master will increase coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #54      +/-   ##
==========================================
+ Coverage   88.65%   88.68%   +0.03%     
==========================================
  Files          80       80              
  Lines        1613     1618       +5     
==========================================
+ Hits         1430     1435       +5     
  Misses        183      183
Impacted Files Coverage Δ
R/smoothingFilters-functions.R 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cce5c67...063c32e. Read the comment docs.

Copy link
Owner

@sgibb sgibb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this great PR. I really appreciate it (and I am very happy that you already included unit tests).

In fact .movingAverage and .movingWeightedAverage just differ in the calculation of their weights:
1/length(windowSize) vs myweigths<-1/2**abs(-halfWindowSize:halfWindowSize); myweights/sum(myweights).
What do you think about changing .movingAverage into?:

.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(weights/sum(weights), nrow=windowSize, ncol=windowSize, byrow=TRUE))
}

Additional I just have some comments about the coding style (everyone has his own, sorry).

##
.movingWeightedAverage<-function(y, halfWindowSize=2L) {
windowSize <- 2L * halfWindowSize + 1L
myweigths<-1/2**abs(-halfWindowSize:halfWindowSize)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please use ^ instead of ** (sorry but I never used ** in R before). Please put spaces around binary operators (/) and <- but not around = (three lines below). In general I try to follow the Bioconductor coding style (except I never used 4 spaces for indentation): https://bioconductor.org/developers/how-to/coding-style/

myweigths<-1/2**abs(-halfWindowSize:halfWindowSize)
myweigths<-myweigths/sum(myweigths)
.filter(y, hws=halfWindowSize,
coef=matrix(myweigths, nrow=windowSize, ncol=windowSize, byrow = TRUE))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use byrow=TRUE instead of byrow = TRUE.

@SiggiSmara
Copy link
Contributor Author

No problem with the coding style. I shall try to be more careful in the future. I think your suggestion of incorporating the weights into the MovingAverage is a very good one. This also lessens the changes to be added to packages that rely on this function. I've made the changes, also to the unit tests and man pages.

Copy link
Owner

@sgibb sgibb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thanks for applying these changes. The PR is in a pretty good shape. I just have a few minor comments left.

Please feel free to add an entry to the NEWS file (with version 1.17, I will bump it in the DESCRIPTION myself after merging this PR) and crediting yourself.

@@ -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. Unused for \code{MovingAverage}.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\dots isn't "unused" in MovingAverage anymore. This sentence could be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry.. missed that one. Done.

.filter(y, hws=halfWindowSize,
coef=matrix(1L / windowSize, nrow=windowSize, ncol=windowSize))
coef=matrix(weights/sum(weights), nrow=windowSize, ncol=windowSize, byrow=TRUE))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add spaces around / (I know I haven't done it myself in my last comment, sry).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. Done.


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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add an unit test where the default and the weighted version differ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Done.

@sgibb sgibb merged commit ce2ae33 into sgibb:master Nov 12, 2017
@sgibb
Copy link
Owner

sgibb commented Nov 12, 2017

Thanks a lot! I will run a few checks and tests and submit to CRAN soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants