Skip to content

Commit

Permalink
Better tests and interactive plots (#140)
Browse files Browse the repository at this point in the history
* better tests
* better plotting, split and rel
* add a couple other errors to `check_OpenSpecy()`
* add contributors
* submit v1.0.3

---------

Co-authored-by: Zacharias Steinmetz <[email protected]>
  • Loading branch information
wincowgerDEV and zsteinmetz committed Sep 13, 2023
1 parent f4c2107 commit 8a3197b
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 3,254 deletions.
6 changes: 3 additions & 3 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 1.0.2
Date: 2023-09-05 13:16:55 UTC
SHA: 92ce663a7883e3c9886fb7c2ada24fd7deb4c49b
Version: 1.0.3
Date: 2023-09-13 15:19:30 UTC
SHA: 2ff9c1a9be7e2e3c99747f8f65302223b7951830
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: OpenSpecy
Type: Package
Title: Analyze, Process, Identify, and Share Raman and (FT)IR Spectra
Version: 1.0.2
Date: 2023-09-05
Version: 1.0.3
Date: 2023-09-13
Authors@R: c(person("Win", "Cowger", role = c("cre", "aut", "dtc"),
email = "[email protected]",
comment = c(ORCID = "0000-0001-9226-3104")),
Expand Down Expand Up @@ -38,6 +38,9 @@ Authors@R: c(person("Win", "Cowger", role = c("cre", "aut", "dtc"),
person("Laura A. T.", "Markley", role = c("ctb"),
comment = c(ORCID = "0000-0003-0620-8366")),
person("Shreyas", "Patankar", role = c("ctb", "dtc")),
person("Rachel", "Kozloski", role = c("ctb", "dtc"),
comment = c(ORCID = "0000-0003-1211-9351")),
person("Samiksha", "Singh", role = c("ctb")),
person("Katherine", "Lasdin", role = c("ctb")),
person("National Renewable Energy Laboratory", role = c("fnd")),
person("Possibility Lab", role = c("fnd")))
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# OpenSpecy 1.0.3

## Minor Improvements

- Simplify `check_OpenSpecy()`
- Improve unit tests
- Improve interactive plots


# OpenSpecy 1.0.2

## Bug Fixes
Expand Down
44 changes: 25 additions & 19 deletions R/as_OpenSpecy.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
#' Focal Plane Array, CCD\cr
#' \code{instrument_mode}: \tab Instrument modes/settings, e.g.
#' transmission, reflectance \cr
#' \code{intensity_units*}: \tab Units of the intensity values for the spectrum, options
#' transmittance, reflectance, absorbance \cr
#' \code{intensity_units*}: \tab Units of the intensity values for the spectrum,
#' options transmittance, reflectance, absorbance \cr
#' \code{spectral_resolution}: \tab Spectral resolution, e.g. 4/cm \cr
#' \code{laser_light_used}: \tab Wavelength of the laser/light used, e.g.
#' 785 nm \cr
Expand Down Expand Up @@ -108,10 +108,10 @@
#' data("raman_hdpe")
#'
#' # Inspect the spectra
#' raman_hdpe # See how OpenSpecy objects print.
#' raman_hdpe$wavenumber # Look at just the wavenumbers of the spectra.
#' raman_hdpe$spectra # Look at just the spectral intensities data.table.
#' raman_hdpe$metadata # Look at just the metadata of the spectra.
#' raman_hdpe # see how OpenSpecy objects print.
#' raman_hdpe$wavenumber # look at just the wavenumbers of the spectra.
#' raman_hdpe$spectra # look at just the spectral intensities data.table.
#' raman_hdpe$metadata # look at just the metadata of the spectra.
#'
#' # Creating a list and transforming to OpenSpecy
#' as_OpenSpecy(list(wavenumber = raman_hdpe$wavenumber,
Expand All @@ -127,8 +127,8 @@
#' spectra = raman_hdpe$spectra$intensity))
#'
#' # Test that the spectrum is formatted as an OpenSpecy object.
#' is_OpenSpecy(raman_hdpe) #should be TRUE
#' is_OpenSpecy(raman_hdpe$spectra) #should be FALSE
#' is_OpenSpecy(raman_hdpe)
#' is_OpenSpecy(raman_hdpe$spectra)
#'
#' @author
#' Zacharias Steinmetz, Win Cowger
Expand Down Expand Up @@ -314,33 +314,39 @@ is_OpenSpecy <- function(x) {
#' @importFrom data.table is.data.table
#' @export
check_OpenSpecy <- function(x) {
if(!(is_OpenSpecy(x)))
stop("object 'x' is not of class 'OpenSpecy'", call. = F)
if(!identical(names(x), c("wavenumber", "spectra", "metadata")))
stop("names of the object components are incorrect", call. = F)
if(!(cos <- is_OpenSpecy(x)))
warning("Object 'x' is not of class 'OpenSpecy'", call. = F)
if(!(cln <- identical(names(x), c("wavenumber", "spectra", "metadata"))))
warning("Names of the object components are incorrect", call. = F)
if(!(cw <- is.vector(x$wavenumber)))
warning("Wavenumber is not a vector", call. = F)
if(!(cwn <- !any(is.na(x$wavenumber))))
warning("Wavenumber values have NA", call. = F)
if(!(cs <- is.data.table(x$spectra)))
message("Spectra are not of class 'data.table'")
warning("Spectra are not of class 'data.table'", call. = F)
if(!(csn <- !any(vapply(x$spectra, function(x){all(is.na(x))},
FUN.VALUE = logical(1)))))
warning("Some of the spectra have all NA values", call. = F)
if(!(cm <- is.data.table(x$metadata)))
message("Metadata are not a 'data.table'")
warning("Metadata are not a 'data.table'", call. = F)
if(!(cr <- ncol(x$spectra) == nrow(x$metadata)))
warning("Number of columns in spectra is not equal to number of rows ",
"in metadata", call. = F)
if(!(csz <- nrow(x$spectra) != 0))
warning("There are no spectral intensities in your spectra", call. = F)
if(!(cl <- length(x$wavenumber) == nrow(x$spectra)))
warning("Length of wavenumber is not equal to number of rows in spectra",
call. = F)
if(!(cu <- length(unique(names(x$spectra))) == ncol(x$spectra)))
warning("Column names in spectra are not unique", call. = F)
if(!(cv <- length(unique(names(x$metadata))) == ncol(x$metadata)))
message("Column names in metadata are not unique")
warning("Column names in metadata are not unique", call. = F)
if(!(co <- identical(order(x$wavenumber), 1:length(x$wavenumber)) |
identical(order(x$wavenumber), length(x$wavenumber):1)))
message("This is technically an 'OpenSpecy' object but wavenumbers ",
"should be a continuous sequence for all OpenSpecy functions to ",
"run smoothly")
warning("Wavenumbers should be a continuous sequence for all OpenSpecy ",
"functions to run smoothly", call. = F)

chk <- all(cw, cs, cm, cr, cl, cu, cv, co)
chk <- all(cw, cs, cm, cr, cl, cu, cv, co, csz, csn, cwn, cln, cos)

return(chk)
}
Expand Down
4 changes: 2 additions & 2 deletions R/io_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ read_spec <- function(file, share = NULL, method = NULL, ...) {
#'
#' @export
as_hyperSpec <- function(x) {
new("hyperSpec", spc = as.matrix(transpose(x$spectra)),
wavelength = x$wavenumber)
new("hyperSpec", spc = as.matrix(transpose(x$spectra)),
wavelength = x$wavenumber)
}
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ library(OpenSpecy)
run_app()
```

## See [package vignette](http://wincowger.com/OpenSpecy-package/articles/sop.html) for a detailed standard operating procedure.

## Simple workflow for single spectral identification

See [package vignette](http://wincowger.com/OpenSpecy-package/articles/sop.html)
for a detailed standard operating procedure.

```r
# Fetch current spectral library from https://osf.io/x7dpz/
get_lib("derivative")
Expand All @@ -85,11 +86,11 @@ top_matches <- match_spec(raman_proc, library = spec_lib, na.rm = T, top_n = 5,
add_library_metadata = "sample_name",
add_object_metadata = "col_id")

#Print the top 5 results with relevant metadata
# Print the top 5 results with relevant metadata
top_matches[, c("object_id", "library_id", "match_val", "SpectrumType",
"SpectrumIdentity")]

#Get all metadata for the matches
# Get all metadata for the matches
get_metadata(spec_lib, logic = top_matches$library_id)
```

Expand All @@ -101,6 +102,6 @@ Needs an Open Source Community: Open Specy to the Rescue!”
*Analytical Chemistry*, **93**(21), 7543–7548. doi:
[10.1021/acs.analchem.1c00123](https://doi.org/10.1021/acs.analchem.1c00123).

Cowger W, Steinmetz Z (2023). “OpenSpecy: Analyze, Process,
Identify, and Share Raman and (FT)IR Spectra.” *R package*, **1.0.2**.
Cowger W, Steinmetz Z, Leong N, Faltynkova A (2023). “OpenSpecy: Analyze,
Process, Identify, and Share Raman and (FT)IR Spectra.” *R package*, **1.0.3**.
[https://github.com/wincowgerDEV/OpenSpecy-package](https://github.com/wincowgerDEV/OpenSpecy-package).
2 changes: 0 additions & 2 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
url: http://wincowger.com/OpenSpecy-package/
template:
bootstrap: 5
development:
mode: auto
articles:
- title: Articles
navbar: ~
Expand Down
7 changes: 1 addition & 6 deletions cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@

## R CMD check results

0 errors | 0 warnings | 1 note
0 errors | 0 warnings | 0 notes


## Comments

Possibly misspelled words in DESCRIPTION:
wavenumber (50:62)

The word is spelled correctly, see https://en.wikipedia.org/wiki/Wavenumber

For further details, see NEWS.md
2 changes: 2 additions & 0 deletions man/OpenSpecy-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions man/as_OpenSpecy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions tests/testthat/test-as_OpenSpecy.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,21 @@ test_that("as_OpenSpecy() generates OpenSpecy objects", {
test_that("check_OpenSpecy() work as expected", {
os <- as_OpenSpecy(df)
check_OpenSpecy(os) |> expect_true()
check_OpenSpecy(df) |> expect_error()
check_OpenSpecy(df) |> expect_error() |> expect_warning() |> expect_warning() |> expect_warning() |> expect_warning()

osv <- osn <- oss <- ost <- osl <- os

osv$wavenumber <- list(osv$wavenumber)
check_OpenSpecy(osv) |> expect_false() |> expect_warning()

names(osn) <- 1:3
check_OpenSpecy(osn) |> expect_error()
check_OpenSpecy(osn) |> expect_error() |> expect_warning() |> expect_warning()|> expect_warning() |> expect_warning()

oss$wavenumber <- sample(oss$wavenumber)
check_OpenSpecy(oss) |> expect_false() |> expect_message()
check_OpenSpecy(oss) |> expect_false() |> expect_warning()

class(ost$metadata) <- class(ost$spectra) <- "data.frame"
check_OpenSpecy(ost) |> expect_false() |> expect_message() |> expect_message()
check_OpenSpecy(ost) |> expect_false() |> expect_warning() |> expect_warning()

osl$metadata <- rbind(osl$metadata, osl$metadata)
osl$spectra <- osl$spectra[-1]
Expand Down Expand Up @@ -110,3 +110,4 @@ test_that("'OpenSpecy' objects are transcribed to and from 'hyperSpec' objects",
expect_equal(openhyper$wavenumber, hyper@wavelength)
expect_equal(unlist(openhyper$spectra$V1),unname(t(hyper$spc)[,1]))
})

17 changes: 8 additions & 9 deletions tests/testthat/test-interactive_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ test_that("plotly_spec() handles input errors correctly", {
})

test_that("plotly_spec() generates 'plotly' object", {
plotly_spec(raman_hdpe) |>
plotly_spec(x = raman_hdpe, x2 = raman_hdpe) |>
expect_silent() |>
expect_s3_class("plotly")
})

test_that("interactive_plot() generates 'plotly' object with single or multiple spectra from map", {
interactive_plot(map, x2 = raman_hdpe, select = 2:3) |>
expect_s3_class("plotly")
})

test_that("heatmap_spec() handles input errors correctly", {
heatmap_spec(1:1000) |> expect_error()
})
Expand All @@ -21,13 +26,7 @@ test_that("heatmap_spec() generates 'plotly' object", {
expect_s3_class("plotly")
})

test_that("heatmap_spec() can handle all NA", {
heatmap_spec(map, z = map$metadata$y, sn = map$metadata$y, min_sn = 100) |>
expect_silent()
})

test_that("interactive_plot() generates 'plotly' object", {
interactive_plot(map, x2 = raman_hdpe, select = 2:3) |>
expect_silent() |>
expect_s3_class("plotly")
interactive_plot(map, x2 = raman_hdpe, select = 2) |>
expect_s3_class("plotly")
})
4 changes: 2 additions & 2 deletions tests/testthat/test-match_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test_that("match_spec() returns correct structure with AI", {
get_lib("model", path = tmp)
lib <- load_lib(type = "model", path = tmp)

expect_error(check_OpenSpecy(lib))
check_OpenSpecy(lib) |> expect_error() |> expect_warning() |> expect_warning() |> expect_warning() |> expect_warning() |> expect_warning()

set.seed(47)
rn <- runif(n = length(unique(lib$variables_in)))
Expand Down Expand Up @@ -119,7 +119,7 @@ test_that("filter_spec() handles input errors correctly", {
test_that("filter_spec() returns erroneous OpenSpecy object when removing all spectra", {
os_filtered <- filter_spec(test_lib, logic = rep(F, ncol(test_lib$spectra))) |>
expect_silent()
expect_warning(check_OpenSpecy(os_filtered))
check_OpenSpecy(os_filtered) |> expect_warning() |> expect_warning()
expect_equal(ncol(os_filtered$spectra), 0)
expect_equal(nrow(os_filtered$metadata), 0)
})
Expand Down
1 change: 1 addition & 0 deletions vignettes/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.R
*.html
Loading

0 comments on commit 8a3197b

Please sign in to comment.