Skip to content

Commit

Permalink
Merge branch 'v1.0-prep' into manage_spec-v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wincowgerDEV committed Jul 14, 2023
2 parents d265d4b + 255c5c7 commit 97221ca
Show file tree
Hide file tree
Showing 132 changed files with 20,657 additions and 1,363 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ google-analytics.js
droptoken.rds
^cran-comments\.md$
^CRAN-RELEASE$
^_pkgdown\.yml$
^docs$
^pkgdown$
46 changes: 46 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
release:
types: [published]
workflow_dispatch:

name: pkgdown

jobs:
pkgdown:
runs-on: ubuntu-latest
# Only restrict concurrency for non-PR jobs
concurrency:
group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::pkgdown, local::.
needs: website

- name: Build site
run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
shell: Rscript {0}

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/[email protected]
with:
clean: false
branch: gh-pages
folder: docs
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ inst/doc
.Renviron

# pkgdown site
docs/
#docs/

# System files
*.ini
Expand All @@ -55,3 +55,5 @@ docs/

# ignore old datasets but don't remove
inst/shiny/
#docs
docs
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ Description: Raman and (FT)IR spectral analysis tool for plastic particles and
Analyzed spectra can be shared with the Open Specy community. A Shiny app is
available via run_app() or online at
<https://openanalysis.org/openspecy/>.
URL: https://github.com/wincowgerDEV/OpenSpecy-package/
URL: https://github.com/wincowgerDEV/OpenSpecy-package/,
http://wincowger.com/OpenSpecy-package/
BugReports: https://github.com/wincowgerDEV/OpenSpecy-package/issues/
License: CC BY 4.0
Encoding: UTF-8
Expand Down
10 changes: 8 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ S3method(adj_intens,default)
S3method(as_OpenSpecy,OpenSpecy)
S3method(as_OpenSpecy,data.frame)
S3method(as_OpenSpecy,default)
S3method(as_OpenSpecy,hyperSpec)
S3method(as_OpenSpecy,list)
S3method(conform_spec,OpenSpecy)
S3method(conform_spec,default)
Expand All @@ -30,8 +31,10 @@ export(adj_intens)
export(adj_neg)
export(adj_res)
export(as_OpenSpecy)
export(c_spec)
export(characterize_particles)
export(check_lib)
export(collapse_particles)
export(combine_OpenSpecy)
export(conform_res)
export(conform_spec)
export(find_spec)
Expand Down Expand Up @@ -59,12 +62,13 @@ export(share_spec)
export(smooth_intens)
export(spec_res)
export(subtr_bg)
export(to_hyperspec)
export(write_spec)
import(hyperSpec)
importFrom(data.table,.SD)
importFrom(data.table,as.data.table)
importFrom(data.table,data.table)
importFrom(data.table,fread)
importFrom(data.table,rbindlist)
importFrom(data.table,transpose)
importFrom(digest,digest)
importFrom(dplyr,arrange)
Expand All @@ -78,6 +82,8 @@ importFrom(dplyr,top_n)
importFrom(dplyr,ungroup)
importFrom(hyperSpec,read.jdx)
importFrom(hyperSpec,read.spc)
importFrom(imager,as.cimg)
importFrom(imager,label)
importFrom(jsonlite,read_json)
importFrom(jsonlite,write_json)
importFrom(magrittr,"%>%")
Expand Down
151 changes: 127 additions & 24 deletions R/as_OpenSpecy.R
Original file line number Diff line number Diff line change
@@ -1,43 +1,135 @@
#' @rdname as_OpenSpecy
#'
#' @title Coerce to Open Specy
#' @title OpenSpecy object operations
#'
#' @description
#' Functions to check if an object is an Open Specy spectrum, or coerce it if
#' Functions to check if an object is an OpenSpecy, or coerce it if
#' possible.
#'
#' @details
#' \code{as_OpenSpecy()} converts spectral datasets to a threepart list,
#' one with a vector of the wavenumbers of the spectra,
#' \code{as_OpenSpecy()} converts spectral datasets to a three part list,
#' the first with a vector of the wavenumbers of the spectra,
#' the second with a \code{data.table} of all spectral intensities ordered as
#' columns,
#' the third item is another \code{data.table} with any metadata the user
#' provides or is harvested from the files themselves. Currently metadata
#' harvesting from jdx and opus files are supported as well as the two
#' Open Specy write formats yaml and json. There are many unique iterations of
#' spectral file formats so there may be bugs in the file conversion.
#' Please contact us if you identify any.
#' OpenSpecy write formats yaml and json.
#'
#' @param x x.
#' @param spectra spectra.
#' @param metadata file = NULL.
#' @param coords coords = "gen_grid".
#' @param colnames cols.
#' @param \ldots args.
#' The \code{metadata} argument may contain a named list with the following
#' details (\code{*} = minimum recommended):
#'
#' @return
#' return
#' \tabular{ll}{
#' \code{file_name*}: \tab The file name, defaults to
#' \code{\link[base]{basename}()} if not specified\cr
#' \code{user_name*}: \tab User name, e.g. "Win Cowger"\cr
#' \code{contact_info}: \tab Contact information, e.g. "1-513-673-8956,
#' wincowger@@gmail.com"\cr
#' \code{organization}: \tab Affiliation, e.g. "University of California,
#' Riverside"\cr
#' \code{citation}: \tab Data citation, e.g. "Primpke, S., Wirth, M., Lorenz,
#' C., & Gerdts, G. (2018). Reference database design for the automated analysis
#' of microplastic samples based on Fourier transform infrared (FTIR)
#' spectroscopy. \emph{Analytical and Bioanalytical Chemistry}.
#' \doi{10.1007/s00216-018-1156-x}"\cr
#' \code{spectrum_type*}: \tab Raman or FTIR\cr
#' \code{spectrum_identity*}: \tab Material/polymer analyzed, e.g.
#' "Polystyrene"\cr
#' \code{material_form}: \tab Form of the material analyzed, e.g. textile fiber,
#' rubber band, sphere, granule \cr
#' \code{material_phase}: \tab Phase of the material analyzed (liquid, gas,
#' solid) \cr
#' \code{material_producer}: \tab Producer of the material analyzed,
#' e.g. Dow \cr
#' \code{material_purity}: \tab Purity of the material analyzed, e.g. 99.98\%
#' \cr
#' \code{material_quality}: \tab Quality of the material analyzed, e.g.
#' consumer product, manufacturer material, analytical standard,
#' environmental sample \cr
#' \code{material_color}: \tab Color of the material analyzed,
#' e.g. blue, #0000ff, (0, 0, 255) \cr
#' \code{material_other}: \tab Other material description, e.g. 5 µm diameter
#' fibers, 1 mm spherical particles \cr
#' \code{cas_number}: \tab CAS number, e.g. 9003-53-6 \cr
#' \code{instrument_used}: \tab Instrument used, e.g. Horiba LabRam \cr
#' \code{instrument_accessories}: \tab Instrument accessories, e.g.
#' 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{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
#' \code{number_of_accumulations}: \tab Number of accumulations, e.g 5 \cr
#' \code{total_acquisition_time_s}: \tab Total acquisition time (s), e.g. 10 s
#' \cr
#' \code{data_processing_procedure}: \tab Data processing procedure,
#' e.g. spikefilter, baseline correction, none \cr
#' \code{level_of_confidence_in_identification}: \tab Level of confidence in
#' identification, e.g. 99\% \cr
#' \code{other_info}: \tab Other information \cr
#' \code{license}: \tab The license of the shared spectrum; defaults to
#' \code{"CC BY-NC"} (see
#' \url{https://creativecommons.org/licenses/by-nc/4.0/} for details). Any other
#' creative commons license is allowed, for example, CC0 or CC BY \cr
#' \code{session_id}: \tab A unique user and session identifier; populated
#' automatically with \code{paste(digest(Sys.info()), digest(sessionInfo()),
#' sep = "/")}\cr
#' \code{file_id}: \tab A unique file identifier; populated automatically
#' with \code{digest(object[c("wavenumber", "spectra")])}\cr
#' }
#'
#' @param x depending on the method, a list with all OpenSpecy parameters, a vector with the wavenumbers for all spectra, or a data.frame with a full spectrum in the classic Open Specy format.
#' @param spectra spectral intensities formatted as a data.table with one column per spectrum.
#' @param metadata metadata for each spectrum with one row per spectrum, see details.
#' @param coords spatial coordinates for the spectra.
#' @param session_id TRUE or FALSE whether to add a session ID to the metadata. The session ID is based on current session info so metadata of the same spectra will not return equal if session info changes. Sometimes that is desirable.
#' @param colnames names of the wavenumber column and spectra column, makes assumptions based on column names or placement if NULL.
#' @param n number of spectra to generate the spatial coordinate grid with.
#' @param \ldots additional arguments.
#'
#' @return
#' \code{as_OpenSpecy()} and \code{OpenSpecy()} returns three part lists described in details. \cr
#' \code{is_OpenSpecy()} returns TRUE if the object is an OpenSpecy and FALSE if not.\cr
#' \code{gen_grid()} returns a \code{data.table} with x y coordinates to use for generating a spatial grid for the spectra if one is not specified in the data.\cr
#'
#' @examples
#' c()
#' data("raman_hdpe") #Read in an example spectrum for 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.
#'
#' #Demonstrate compatibility in creating an OpenSpecy from its components.
#' as_OpenSpecy(x = raman_hdpe$wavenumber, spectra = raman_hdpe$spectra, metadata = raman_hdpe$metadata[,-c("x", "y")], coords = raman_hdpe$metadata[,c("x", "y")])$metadata == raman_hdpe$metadata
#' as_OpenSpecy(x = raman_hdpe$wavenumber, spectra = raman_hdpe$spectra, metadata = raman_hdpe$metadata[,-c("x", "y")], coords = raman_hdpe$metadata[,c("x", "y")])$spectra == raman_hdpe$spectra
#' as_OpenSpecy(x = raman_hdpe$wavenumber, spectra = raman_hdpe$spectra, metadata = raman_hdpe$metadata[,-c("x", "y")], coords = raman_hdpe$metadata[,c("x", "y")])$wavenumber == raman_hdpe$wavenumber
#'
#' #Demonstrate creating a list and transforming to OpenSpecy
#' as_OpenSpecy(list(wavenumber = raman_hdpe$wavenumber, spectra = raman_hdpe$spectra, metadata = raman_hdpe$metadata[,-c("x", "y")]))
#'
#' #If you try to produce an OpenSpecy using an OpenSpecy it will just return the same object.
#' as_OpenSpecy(raman_hdpe)
#'
#' #Method for creating an OpenSpecy from a data.frame
#' as_OpenSpecy(x = data.frame(wavenumber = raman_hdpe$wavenumber, 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
#'
#' #Create an artificial spatial grid
#' gen_grid(n = 5)
#'
#' @author
#' Zacharias Steinmetz, Win Cowger
#'
#' @seealso
#' seealso.
#'
#' @importFrom magrittr %>%
#' @importFrom data.table as.data.table
#' @export
as_OpenSpecy <- function(x, ...) {
Expand All @@ -58,6 +150,13 @@ as_OpenSpecy.list <- function(x, ...) {
do.call("as_OpenSpecy", unname(x))
}

#' @rdname as_OpenSpecy
#'
#' @export
as_OpenSpecy.hyperSpec <- function(x, ...) {
do.call("as_OpenSpecy", list(x = x@wavelength, spectra = as.data.table(t(x$spc)), ...))
}

#' @rdname as_OpenSpecy
#'
#' @export
Expand Down Expand Up @@ -128,6 +227,7 @@ as_OpenSpecy.default <- function(x, spectra,
instrument_used = NULL,
instrument_accessories = NULL,
instrument_mode = NULL,
intensity_units = NULL,
spectral_resolution = NULL,
laser_light_used = NULL,
number_of_accumulations = NULL,
Expand All @@ -137,6 +237,7 @@ as_OpenSpecy.default <- function(x, spectra,
other_info = NULL,
license = "CC BY-NC"),
coords = "gen_grid",
session_id = F,
...) {
if (!is.numeric(x) && !is.complex(x) && !is.logical(x))
stop("'x' must be numeric or logical", call. = F)
Expand Down Expand Up @@ -168,9 +269,11 @@ as_OpenSpecy.default <- function(x, spectra,
if (!is.null(metadata)) {
if (inherits(metadata, c("data.frame", "list"))) {
obj$metadata <- cbind(obj$metadata, as.data.table(metadata))
obj$metadata$session_id <- paste(digest(Sys.info()),
digest(sessionInfo()),
sep = "/")
if(session_id){
obj$metadata$session_id <- paste(digest(Sys.info()),
digest(sessionInfo()),
sep = "/")
}
if(!c("file_id") %in% names(obj$metadata)) {
obj$metadata$file_id = digest(obj[c("wavenumber", "spectra")])
}
Expand Down Expand Up @@ -203,8 +306,8 @@ OpenSpecy <- function(x, ...) {
#' @rdname as_OpenSpecy
#'
#' @export
gen_grid <- function(x) {
base <- sqrt(x)
expand.grid(x = 1:ceiling(base), y = 1:ceiling(base))[1:x,] %>%
as.data.table
gen_grid <- function(n) {
base <- sqrt(n)
expand.grid(x = 1:ceiling(base), y = 1:ceiling(base))[1:n,] |>
as.data.table()
}
Loading

0 comments on commit 97221ca

Please sign in to comment.