Skip to content

Commit

Permalink
create conversion to and from hyperspec
Browse files Browse the repository at this point in the history
  • Loading branch information
wincowgerDEV committed Jul 14, 2023
1 parent 908f388 commit cc1399d
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 2 deletions.
7 changes: 7 additions & 0 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(flatten_range,default)
S3method(head,OpenSpecy)
Expand All @@ -27,7 +28,9 @@ export(adj_intens)
export(adj_neg)
export(adj_res)
export(as_OpenSpecy)
export(characterize_particles)
export(check_lib)
export(collapse_particles)
export(combine_OpenSpecy)
export(conform_res)
export(conform_spec)
Expand Down Expand Up @@ -59,7 +62,9 @@ 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)
Expand All @@ -77,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
7 changes: 7 additions & 0 deletions R/as_OpenSpecy.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,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
5 changes: 3 additions & 2 deletions R/io_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ read_spec <- function(file, share = NULL, method = NULL, ...) {

#' @rdname io_spec
#'
#' @importFrom hyperSpec new
#' @import hyperSpec
#' @importFrom data.table transpose
#' @examples
#' library("hyperSpec")
#' data(raman_hdpe)
#' hyperOpenSpecy <- to_hyperspec(raman_hdpe)
#'
#' @export
to_hyperspec <- function(object) {
new("hyperSpec", spc = as.matrix(transpose(object$spectra)), wavelength = object$wavenumber, data, labels)
new("hyperSpec", spc = as.matrix(transpose(object$spectra)), wavelength = object$wavenumber)
}
3 changes: 3 additions & 0 deletions man/as_OpenSpecy.Rd

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

47 changes: 47 additions & 0 deletions man/collapse_particles.Rd

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

7 changes: 7 additions & 0 deletions man/io_spec.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat/test-as_OpenSpecy.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ test_that("OpenSpecy objects are read correctly", {
expect_equal(os$metadata$y, 1)
expect_equal(os$metadata$license, "CC BY-NC")
})


test_that("OpenSpecy objects are transcribed to and from hyperSpec objects", {
os <- as_OpenSpecy(df)
hyperOpenSpecy <- to_hyperspec(os)
expect_true(is(hyperOpenSpecy, "hyperSpec"))
OpenHyper <- as_OpenSpecy(hyperOpenSpecy)
expect_true(is_OpenSpecy(OpenHyper))
expect_equal(OpenHyper$wavenumber,hyperOpenSpecy@wavelength)
expect_equal(unlist(OpenHyper$spectra$V1),unname(t(hyperOpenSpecy$spc)[,1]))

})

0 comments on commit cc1399d

Please sign in to comment.