Skip to content

Commit

Permalink
Add share argument back to reading functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zsteinmetz committed Aug 21, 2023
1 parent d7e66ad commit 55e12f7
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 41 deletions.
17 changes: 11 additions & 6 deletions R/as_OpenSpecy.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,14 @@ as_OpenSpecy <- function(x, ...) {
#' @rdname as_OpenSpecy
#'
#' @export
as_OpenSpecy.OpenSpecy <- function(x, ...) {
as_OpenSpecy.OpenSpecy <- function(x, session_id = FALSE, ...) {
if(session_id)
x$metadata$session_id <- paste(digest(Sys.info()),
digest(sessionInfo()),
sep = "/")
if(!c("file_id") %in% names(x$metadata))
x$metadata$file_id = digest(x[c("wavenumber", "spectra")])

return(x)
}

Expand Down Expand Up @@ -246,7 +253,7 @@ as_OpenSpecy.default <- function(x, spectra,
other_info = NULL,
license = "CC BY-NC"),
coords = "gen_grid",
session_id = F,
session_id = FALSE,
...) {
if (!is.numeric(x) && !is.complex(x) && !is.vector(x))
stop("'x' must be numeric vector", call. = F)
Expand Down Expand Up @@ -281,14 +288,12 @@ 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))
if(session_id){
if(session_id)
obj$metadata$session_id <- paste(digest(Sys.info()),
digest(sessionInfo()),
sep = "/")
}
if(!c("file_id") %in% names(obj$metadata)) {
if(!c("file_id") %in% names(obj$metadata))
obj$metadata$file_id = digest(obj[c("wavenumber", "spectra")])
}
} else {
stop("inconsistent input for 'metadata'", call. = F)
}
Expand Down
3 changes: 2 additions & 1 deletion R/read_envi.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ read_envi <- function(file, header = NULL, share = NULL,
os <- as_OpenSpecy(x = hdr$wavelength,
spectra = dcast(dt, z ~ x + y)[, -1],
metadata = c(metadata, md),
coords = dt[, 1:2] |> unique())
coords = dt[, 1:2] |> unique(),
session_id = T)

if (!is.null(share)) share_spec(os, file = file, share = share)

Expand Down
23 changes: 14 additions & 9 deletions R/read_ext.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ read_text <- function(file, colnames = NULL, method = "fread",
"alternative read method",
call. = F)

os <- as_OpenSpecy(dt, colnames = colnames, metadata = metadata)
os <- as_OpenSpecy(dt, colnames = colnames, metadata = metadata,
session_id = T)

if (!is.null(share)) share_spec(os, file = file, share = share)

Expand Down Expand Up @@ -204,7 +205,8 @@ read_asp <- function(file, share = NULL,
y <- lns[-c(1:6)]
x <- seq(lns[2], lns[3], length.out = lns[1])

os <- as_OpenSpecy(x, data.table(intensity = y), metadata = metadata)
os <- as_OpenSpecy(x, data.table(intensity = y), metadata = metadata,
session_id = T)

if (!is.null(share)) share_spec(os, file = file, share = share)

Expand Down Expand Up @@ -276,7 +278,8 @@ read_spa <- function(file, share = NULL,
x <- seq(spr[1], spr[2], length = length(floatData))
y <- floatData

os <- as_OpenSpecy(x, data.table(intensity = y), metadata = metadata)
os <- as_OpenSpecy(x, data.table(intensity = y), metadata = metadata,
session_id = T)

if (!is.null(share)) share_spec(os, file = file, share = share)

Expand Down Expand Up @@ -321,16 +324,17 @@ read_jdx <- function(file, share = NULL,
x <- jdx@wavelength
y <- as.numeric(unname(jdx@data$spc[1,]))

file <- readLines(file)
test <- file[grepl("##",file)|grepl("[:alpha:]",file)]
values <- ifelse(grepl("##",test), gsub("##.{1,}=", "", test),
lns <- readLines(file)
test <- lns[grepl("##", lns)|grepl("[:alpha:]", lns)]
vals <- ifelse(grepl("##",test), gsub("##.{1,}=", "", test),
gsub(".{1,}:", "", test))
names <- ifelse(grepl("##",test), gsub("##", "", gsub("=.{1,}", "", test)),
gsub(":.{1,}", "", test))
df_metadata <- as.data.table(t(values))
df_metadata <- as.data.table(t(vals))
colnames(df_metadata) <- names

os <- as_OpenSpecy(x, data.table(intensity = y), metadata = df_metadata)
os <- as_OpenSpecy(x, data.table(intensity = y), metadata = df_metadata,
session_id = T)

if (!is.null(share)) share_spec(os, file = file, share = share)

Expand Down Expand Up @@ -375,7 +379,8 @@ read_spc <- function(file, share = NULL,
x <- spc@wavelength
y <- as.numeric(unname(spc@data$spc[1,]))

os <- as_OpenSpecy(x, data.table(intensity = y), metadata = metadata)
os <- as_OpenSpecy(x, data.table(intensity = y), metadata = metadata,
session_id = T)

if (!is.null(share)) share_spec(os, file = file, share = share)

Expand Down
3 changes: 2 additions & 1 deletion R/read_opus.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ read_opus <- function(file, share = NULL,

os <- as_OpenSpecy(x = res$wavenumbers,
spectra = res$spec,
metadata = cbind(as.data.table(metadata), res$metadata))
metadata = cbind(as.data.table(metadata), res$metadata),
session_id = T)

if (!is.null(share)) share_spec(os, file = file, share = share)

Expand Down
20 changes: 12 additions & 8 deletions R/share_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,20 @@ share_spec.default <- function(x, ...) {
share_spec.OpenSpecy <- function(x, file = NULL, share = "system",
credentials = NULL, ...) {
md <- x$metadata
if (any(!c("user_name", "spectrum_type", "spectrum_identity") %in%
names(md)) |
is.null(md$user_name) | is.null(md$spectrum_type) |
is.null(md$spectrum_identity))
if(any(!c("user_name", "spectrum_type", "spectrum_identity") %in%
names(md)) |
is.null(md$user_name) | is.null(md$spectrum_type) |
is.null(md$spectrum_identity))
warning("Fields 'user_name', 'spectrum_type', and 'spectrum_identity' ",
"should not be empty if you like to share your metadata", call. = F)

if(is.null(md$session_id))
stop("'session_id' must not be empty if you like to share your metadata;",
"run 'as_OpenSpecy(x, session_id = T)' to add one", call. = F)

if (share == "system") {
fp <- file.path(system.file("extdata", package = "OpenSpecy"),
"user_spectra", md$session_id)
"user_spectra", md$session_id) |> unique()
} else if (share == "cloud") {
pkg <- "aws.s3"
mpkg <- pkg[!(pkg %in% installed.packages()[ , "Package"])]
Expand All @@ -87,13 +91,13 @@ share_spec.OpenSpecy <- function(x, file = NULL, share = "system",
stop("'credentials' needs to be a named list containing the following ",
"items: 's3_key', 's3_secret', 's3_region', 's3_bucket'", call. = F)

fp <- file.path(tempdir(), md$session_id)
fp <- file.path(tempdir(), md$session_id) |> unique()
} else {
fp <- file.path(share, md$session_id)
fp <- file.path(share, md$session_id) |> unique()
}
dir.create(fp, recursive = T, showWarnings = F)

fd <- file.path(fp, paste0(md$file_id, ".yml"))
fd <- file.path(fp, paste0(md$file_id, ".yml")) |> unique()

write_spec(x, fd)

Expand Down
14 changes: 7 additions & 7 deletions man/as_OpenSpecy.Rd

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

10 changes: 7 additions & 3 deletions tests/testthat/test-io_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ test_that("write_spec() works without errors", {
})

test_that("read_spec() gives expected output", {
yml <- read_spec(read_extdata("raman_hdpe.yml")) |> expect_silent()
jsn <- read_spec(read_extdata("raman_hdpe.json")) |> expect_silent()
rds <- read_spec(read_extdata("raman_hdpe.rds")) |> expect_silent()
yml <- read_extdata("raman_hdpe.yml") |> read_spec() |> expect_silent()
jsn <- read_extdata("raman_hdpe.json") |> read_spec() |> expect_silent()
rds <- read_extdata("raman_hdpe.rds") |> read_spec() |> expect_silent()

read_spec(read_extdata("raman_hdpe.csv")) |> expect_error()

read_extdata("raman_hdpe.yml") |> read_spec(share = tmp) |> expect_message()
read_extdata("raman_hdpe.json") |> read_spec(share = tmp) |> expect_message()
read_extdata("raman_hdpe.rds") |> read_spec(share = tmp) |> expect_message()

expect_s3_class(yml, "OpenSpecy")
expect_s3_class(jsn, "OpenSpecy")
expect_s3_class(rds, "OpenSpecy")
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-read_envi.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Create temp dir for testthat
tmp <- file.path(tempdir(), "OpenSpecy-testthat")
dir.create(tmp, showWarnings = F)

test_that("ENVI files are read", {
tiny_map <- read_extdata("CA_tiny_map.zip") |> read_zip() |>
expect_silent()
read_extdata("CA_tiny_map.zip") |> read_zip(share = tmp) |>
expect_message() |> expect_warning()

expect_s3_class(tiny_map, "OpenSpecy")

expect_equal(ncol(tiny_map$spectra), 208)
Expand All @@ -17,3 +24,6 @@ test_that("ENVI files are read", {
expect_contains(c("x", "y", "file_name", "file_id", "description",
"pixel size"))
})

# Tidy up
unlink(tmp, recursive = T)
13 changes: 10 additions & 3 deletions tests/testthat/test-read_ext.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ test_that("extdata files are present", {
test_that("read_text() gives expected output", {
csv <- read_extdata("raman_hdpe.csv") |> read_text() |>
expect_silent()
# expect_warning(
# expect_message(read_text(read_extdata("raman_hdpe.csv"), share = tmp))
# )
read_extdata("raman_hdpe.csv") |> read_text(share = tmp) |>
expect_message() |> expect_warning()

dtf <- read_extdata("raman_hdpe.csv") |> read_text(method = "fread") |>
expect_silent()
read_extdata("ftir_pva_without_header.csv") |> read_text() |>
Expand All @@ -43,6 +43,8 @@ test_that("read_asp() gives expected output", {
expect_silent()
read_extdata("raman_hdpe.csv") |> read_asp() |>
expect_error()
read_extdata("ftir_ldpe_soil.asp") |> read_asp(share = tmp) |>
expect_message() |> expect_warning()

expect_s3_class(asp, "OpenSpecy")
expect_equal(names(asp), c("wavenumber", "spectra", "metadata"))
Expand All @@ -58,6 +60,9 @@ test_that("read_spa() gives expected output", {
read_spa() |> expect_silent()
read_extdata("raman_hdpe.csv") |> read_spa() |>
expect_error()
read_extdata("ftir_polyethylene_reflectance_adjustment_not_working.spa") |>
read_spa(share = tmp) |>
expect_message() |> expect_warning()

expect_s3_class(spa, "OpenSpecy")
expect_equal(names(spa), c("wavenumber", "spectra", "metadata"))
Expand Down Expand Up @@ -92,6 +97,8 @@ test_that("read_spc() gives expected output", {
expect_silent()
read_extdata("raman_hdpe.csv") |> read_spc() |>
expect_error()
read_extdata("raman_atacamit.spc") |> read_spc(share = tmp) |>
expect_message() |> expect_warning()

expect_s3_class(spc, "OpenSpecy")
expect_equal(names(spc), c("wavenumber", "spectra", "metadata"))
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-read_multi.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
# Create temp dir for testthat
tmp <- file.path(tempdir(), "OpenSpecy-testthat")
dir.create(tmp, showWarnings = F)

data("raman_hdpe")

test_that("reading in multi files doesn't throw error", {
expect_silent(multi <- read_extdata("testdata_zipped.zip") |> read_any())
expect_s3_class(multi, "OpenSpecy")

read_extdata("testdata_zipped.zip") |> read_any(share = tmp) |>
expect_message() |> expect_message() |> expect_message() |>
expect_warning() |> expect_warning() |> expect_warning()

expect_equal(multi$wavenumber, raman_hdpe$wavenumber)
expect_equal(multi$spectra$intensity, raman_hdpe$spectra$intensity)
expect_equal(multi$spectra$intensity.1, raman_hdpe$spectra$intensity)
expect_equal(multi$spectra$intensity.2, raman_hdpe$spectra$intensity)
})

# Tidy up
unlink(tmp, recursive = T)
9 changes: 9 additions & 0 deletions tests/testthat/test-read_opus.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# Create temp dir for testthat
tmp <- file.path(tempdir(), "OpenSpecy-testthat")
dir.create(tmp, showWarnings = F)

test_that("opus files are read correctly", {
single <- read_extdata("ftir_ps.0") |> read_opus() |>
expect_silent()
multi <- c(read_extdata("ftir_ps.0"), read_extdata("ftir_ps.0")) |>
read_opus() |> expect_silent()
read_extdata("raman_hdpe.csv") |> read_opus() |>
expect_error()
read_extdata("ftir_ps.0") |> read_opus(share = tmp) |> expect_message() |>
expect_warning()

expect_s3_class(single, "OpenSpecy")
expect_s3_class(multi, "OpenSpecy")
Expand All @@ -29,3 +35,6 @@ test_that("opus files are read correctly", {
"hum_abs_sm", "hum_abs_rf", "file_id"))
expect_identical(names(multi$metadata), names(single$metadata))
})

# Tidy up
unlink(tmp, recursive = T)
10 changes: 7 additions & 3 deletions tests/testthat/test-share_spec.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
data("raman_hdpe")

# Create temp dir for testthat
tmp <- file.path(tempdir(), "OpenSpecy-testthat")
dir.create(tmp, showWarnings = F)

data("raman_hdpe")

wo_meta <- raman_hdpe
wo_meta$metadata$spectrum_identity <- NULL

test_that("share_text() gives expected output", {
share_spec(raman_hdpe, share = tmp) |>
share_spec(raman_hdpe, file = read_extdata("raman_hdpe.csv"), share = tmp) |>
expect_message()
read_extdata("testdata_zipped.zip") |> read_zip() |>
share_spec(share = tmp) |>
expect_message() |> expect_warning()

file.path(tmp, raman_hdpe$metadata$session_id,
paste0(raman_hdpe$metadata$file_id, ".yml")) |>
file.exists() |>
Expand Down

0 comments on commit 55e12f7

Please sign in to comment.