Skip to content

Added synth data infrastructure per Issue #114 - #125

Merged
ximeg merged 18 commits into
developfrom
issues/114-syndata
May 27, 2020
Merged

ximeg merged 18 commits into
developfrom
issues/114-syndata

Conversation

@bryanhanson

@bryanhanson bryanhanson commented May 15, 2020 •

Copy link
Copy Markdown
Collaborator

This request contains 3 new files:

  1. hyperSpec/data/FauxCell.RData containing the data
  2. hyperSpec/R/FauxCell.R which has the documentation for the data
  3. hyperSpec/doc/ext/Create_Faux_Cell.R which is a heavily documented script I used to create the data set

This needs to be tested: I don't have a working build system and can't test, so if @cbeleites or @ximeg can build and check I would appreciate it. Then try ?FauxCell and run the examples. Thank you.

@bryanhanson

Copy link
Copy Markdown
Collaborator Author

I probably need to shrink the size of this data set eventually, but there are clear ways to do that.

Comment thread hyperSpec/R/FauxCell.R Resolved
@bryanhanson

bryanhanson commented May 15, 2020 via email

Copy link
Copy Markdown
Collaborator Author

@ximeg

ximeg commented May 15, 2020 via email

Copy link
Copy Markdown
Collaborator

@bryanhanson

bryanhanson commented May 15, 2020 via email

Copy link
Copy Markdown
Collaborator Author

@cbeleites

Copy link
Copy Markdown
Owner

Result from video call on 2020-05-18: we can probably remove the additional dependencies of FauxCell.R and then generate a chondro-like data set on the fly.

@bryanhanson bryanhanson reopened this May 19, 2020
@bryanhanson
bryanhanson marked this pull request as draft May 19, 2020 22:11
Comment thread hyperSpec/DESCRIPTION Outdated
person("Alois", "Bonifacio", role = c ("ctb", "dtc")),
person("Marcel", "Dahms", role = "ctb"),
person("Björn", "Egert", role = "ctb"),
person("Bj<U+00F6>rn", "Egert", role = "ctb"),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is an encoding issue introduced here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I have tracked this down to R version 4.0.0 RC (2020-04-21 r78267) which I am using. It only has locale "C" for some reason. It is corrected in 4.1 but other things have changed there (changes in apply arguments) which affect hyperSpec so once #126 is fixed we can move to 4.1 and this locale issue should go away.

@bryanhanson

bryanhanson commented May 19, 2020 via email

Copy link
Copy Markdown
Collaborator Author

@bryanhanson

Copy link
Copy Markdown
Collaborator Author

Alright... Thanks @ximeg for assisting me with understanding the commit process here. Based on comments on the first commit, the desire was to move from a .RData approach to a build it on the fly approach. Hence the first commit is irrelevant, and the 2nd commit removes all the files in the first commit and replaces them with different files. I'm sure folks will want some additional changes, but this should be pretty close. Here are 3 plots produced by the FauxCell example. First is the synthetic spectra, 2nd is PCA on the data set, and 3rd is the map (fuzzy after conversion to png, much better as pdf).

Also, I eliminated the SpecHelpers dependency but kept the splancs dependency -- it would be a fair amount of work to eliminate it. Perhaps later.

This builds, checks and installs using R 4.0.

Plot1
Plot2
Plot3

@bryanhanson
bryanhanson marked this pull request as ready for review May 19, 2020 23:05
@bryanhanson
bryanhanson requested a review from cbeleites May 20, 2020 14:34
@cbeleites

Copy link
Copy Markdown
Owner

@bryanhanson may I (substantially) mess around in your file?

@bryanhanson

bryanhanson commented May 21, 2020 via email

Copy link
Copy Markdown
Collaborator Author

Comment thread hyperSpec/R/fauxCell.R Outdated
#' pca <- prcomp(fauxCell)
#' plot(pca)
#'
#' loadings <- decomposition(fauxCell, t(pca$rotation), scors = FALSE)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Typo: scores.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

fixed now

@bryanhanson

Copy link
Copy Markdown
Collaborator Author

So with the delayedAssign this will not be available until it's requested somewhere. That seems fine. However, documenting it as a data set, but not putting it in the data folder, is a little non-standard. Users would not be able to do data(fauxCell). I wonder if this will be an issue for our friends at CRAN.

@cbeleites

Copy link
Copy Markdown
Owner

chondro uses delayedAssign() since about ever - that has never been a problem.

However, I just read at ? data that we should be able to put the generating function in fauxCell.R into directory data/. We can try that.

@bryanhanson

bryanhanson commented May 23, 2020 •

Copy link
Copy Markdown
Collaborator Author

I need to ask about this line

spc[[]] <- rpois(n = length(spc[[]]), lambda = spc[[]])

from the perspective of the inner workings of hyperSpec, which I admittedly am only getting to know. [[ extracts the object@data$spc matrix of intensities. Calling rpois this way will return length(spc[[]]) random values, which is nrow(spc[[]]) * ncol(spc[[]]) (this is the standard definition of the length of a matrix in R). When specifying lambda = spc[[]] as a vector of means, these are real positive values of intensities, and rpois takes them as a vector of means (similar to the first argument, passing a matrix to lambda causes c() to be applied first, giving a vector of the same length as n). So the 2 arguments to rpois become become vectors, and the result is a vector. Here are my questions:

  1. Is there something about x[[i, j, l, wl.index = FALSE, ...]] <- value, the replacement function, that coerces the vector from rpois into the proper matrix dimensions? A yes/no answer would be sufficient. This must be defined in replace.R but I need to study more, I don't work with S4 much. I think the answer has to be yes, since it works.

  2. It looks to me like the line of code above completely replaces spc[[]] with the random values produced by rpois. Did you mean to do:

spc[[]] <- rpois(n = length(spc[[]]), lambda = spc[[]]) + spc[[]]

? This would add the noise to the original object and not replace the original object. I may have a follow up question about the choice of lambda.

Update: After reading about this a bit more, I see why your original line works (Question 2). If one were implementing Gaussian noise, you would use a mean of zero and chose a sd and add it to the original signal, causing the peaks to shift a bit up or down. Gaussian is a continuous function. Poisson however is discrete. The PMF for Poisson has lambda = expectation value/mean = the variance, which means that the greater intensity of the peak, the wider the distribution. So by replacing a peak with a draw from the Poisson distribution, you add more noise to the higher peaks. In contrast, adding Gaussian noise is essentially adding a fixed amount of noise to every peak, whether small or large. I think what this comes around to is adding Gaussian noise is homoskedastic process, but adding Poisson noise is a heteroskedastic process. Interestingly, about 3 months ago I asked about the source of noise here and got some not quite satisfying answers. Oddly enough the Wikipedia entry for shot noise answers quite clearly.

If you are still reading I welcome feedback! Now it's time to understand the math behind the vanderMonde matrix and the sources of wandering baselines.

And that's how I spent my Saturday night!

Comment thread hyperSpec/R/vandermonde.R
##' @param ... hyperSpec method: further arguments to \code{\link{decomposition}}
##' @return hyperSpec method: hyperSpec object containing van der Monde matrix as spectra and an additional column ".vdm.order" giving the order of each spectrum (term).
##' other function). `hyperSpec::normalize01()` maps the wavelength range to the interval
##' [0, 1]. Use `base::I()` to turn off.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I suggest using [base::I()] (square brackets instead of backticks) as this syntax will automatically create a link to the documentation of function I().
The same applies to [hyperSpec::decomposition()] and the other functions.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Especially as the previous Rd code was \code{\link[base]{I}} (i.e., defined a link)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@GegznaV ...automatically creates a link to the documentation... Where? In pkgdown? Not sure what kind of text processing you are thinking about.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

In R documentation as well as in pkgdown website.

Here is more on links in Markdown-favored R documentation:
https://roxygen2.r-lib.org/articles/markdown.html#links

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This markdown syntax and roxygen comments (#', or ##' in hyperSpec's case) indicate that roxygen2 is used to create Rd files automatically. And syntax [pkg::func()] in the roxygen comments is converted into \code{\link[pkg:func]{pkg::func()}} in appropriate Rd file. Finally, \code{\link[pkg:func]{pkg::func()}} is converted into a link when documentation is built from Rd file.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fantastic I did not know this trick. Thank you! Not only saves typing but when you do it all manually it's easy to miss a closing brace or square bracket. Fabulous!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If you use RStudio, you may notice, that text inside square brackets inside roxygen comments is highlighted differently than the surrounding text.
Of course, these corrections in the documentation are not crucial. I just draw your attention that current changes in syntax (using just backticks) will not result in the links, which were present in the documentation when LaTeX based Rd syntax was used.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There is a package, that converts from Rd to markdown syntax in roxygen comments: roxygen2md.

@ximeg

ximeg commented May 27, 2020

Copy link
Copy Markdown
Collaborator

I reviewed the new synthetic data set and I really like how you've done it! Everything seems to work with R3.6.3, it builds and passes the checks, so I am merging this pull request into develop. We'll fine polish it later, if necessary. Great job everyone! 😃 👍

@ximeg
ximeg merged commit 94bf154 into develop May 27, 2020
@ximeg
ximeg deleted the issues/114-syndata branch May 27, 2020 04:28
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.

4 participants