Skip to content

Commit

Permalink
Align tests with latest code base
Browse files Browse the repository at this point in the history
  • Loading branch information
zsteinmetz committed Aug 9, 2023
1 parent 845ffd9 commit cd50169
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 86 deletions.
Binary file modified data/raman_hdpe.RData
Binary file not shown.
Binary file modified inst/extdata/raman_hdpe.rds
Binary file not shown.
1 change: 1 addition & 0 deletions tests/testthat/test-adj_intens.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ test_that("adj_intens() runs without error", {
expect_no_error(adj_intens(raman_hdpe, type = "transmittance"))
expect_no_error(adj_intens(raman_hdpe))
})

13 changes: 7 additions & 6 deletions tests/testthat/test-adj_range.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_that("restric range provides correct range", {
test_that("restrict_range() provides correct range", {
test_noise = as_OpenSpecy(x = seq(400,4000, by = 10), spectra = data.table(intensity = rnorm(361)))
expect_silent(single_range <- restrict_range(test_noise, min_range = 1000, max_range = 2000))
expect_silent(double_range <- restrict_range(test_noise, min_range = c(1000, 2000) , max_range = c(1500, 2500)))
Expand All @@ -8,17 +8,18 @@ test_that("restric range provides correct range", {
expect_identical(double_range$wavenumber, c(seq(1000,1500, by = 10), seq(2000,2500, by = 10)))
})

test_that("flatten_range.OpenSpecy() function test", {
test_that("flatten_range() function test", {
#spectrum <- read_any(read_extdata(file = "ftir_ps.0"))
test <- as_OpenSpecy(x = 1:10, spectra = data.table(V1 = 1:10))
flattened_data <- flatten_range(object = test, min_range = c(4, 7), max_range = c(5, 10), make_rel = F)
flattened_data <- flatten_range(test, min_range = c(4, 7), max_range = c(5, 10), make_rel = F)
expect_equal(flattened_data$spectra$V1[4:5], c(4.5, 4.5))
expect_equal(flattened_data$spectra$V1[7:10], c(8.5, 8.5, 8.5, 8.5))
})


test_that("flatten_range() error handling", {
expect_error(flatten_range(test_data), "You need to specify a min and max range to flatten.")
expect_error(flatten_range(test_data, min_range = c(1000), max_range = c(2000, 3000)), "min_range and max_range need to be the same length.")
expect_error(flatten_range(test_data, min_range = c(2000), max_range = c(1000)), "all min_range values must be lower than corresponding max_range")
test <- as_OpenSpecy(x = 1:10, spectra = data.table(V1 = 1:10))
expect_error(flatten_range(test), "You need to specify a min and max range to flatten.")
expect_error(flatten_range(test, min_range = c(1000), max_range = c(2000, 3000)), "min_range and max_range need to be the same length.")
expect_error(flatten_range(test, min_range = c(2000), max_range = c(1000)), "all min_range values must be lower than corresponding max_range")
})
3 changes: 1 addition & 2 deletions tests/testthat/test-as_OpenSpecy.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,5 @@ test_that("OpenSpecy objects are transcribed to and from hyperSpec objects", {
expect_true(is_OpenSpecy(OpenHyper))
expect_equal(OpenHyper$wavenumber,hyperOpenSpecy@wavelength)
expect_equal(unlist(OpenHyper$spectra$V1),unname(t(hyperOpenSpecy$spc)[,1]))

})

})
10 changes: 4 additions & 6 deletions tests/testthat/test-conform_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test_that("conform_spec.default throws an error for non-OpenSpecy objects", {
# Create a non-OpenSpecy object
non_open_specy_object <- data.frame(wavenumber = c(1000, 1100, 1200),
intensity = c(0.1, 0.2, 0.3))

# Test if the function throws an error for non-OpenSpecy object
expect_error(conform_spec(non_open_specy_object, c(1000, 2000)))
})
Expand All @@ -22,17 +22,15 @@ test_that("conform_spec.OpenSpecy conforms wavenumbers correctly", {
wavenumber <- seq(1000, 2000, 5)
intensity <- rnorm(length(wavenumber))
open_specy_object <- as_OpenSpecy(wavenumber, data.table(intensity = intensity))

# Conform the wavenumbers to a new range
new_wavenumbers <- c(1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900)
conform_result <- conform_spec(open_specy_object, new_wavenumbers)

# Test if the wavenumber and intensity vectors have the same length after conforming
expect_equal(length(conform_result$wavenumber), length(conform_result$spectra[[1]]))

# Test if the wavenumber range matches the new_wavenumbers vector
expect_equal(min(conform_result$wavenumber), min(new_wavenumbers))
expect_equal(max(conform_result$wavenumber), max(new_wavenumbers))
})


Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ test_that("check that particles are identified when given character", {

test_that("check that an error is thrown for invalid 'particles' input", {
map <- read_extdata("CA_tiny_map.zip") |> read_any()
expect_error(def_features(map, map$metadata),
"Particles needs to be a character or logical vector.")
expect_error(def_features(map, map$metadata))
})

test_that("check that particles are identified with all TRUE or FALSE logical vectors", {
map <- read_extdata("CA_tiny_map.zip") |> read_any()

# All TRUE case
map$metadata$particles <- rep(TRUE, nrow(map$metadata))
expect_error(identified_map <- def_features(map, map$metadata$particles))

# All FALSE case
map$metadata$particles <- rep("test_FALSE", nrow(map$metadata))
expect_error(identified_map <- def_features(map, map$metadata$particles))
Expand All @@ -44,25 +43,26 @@ test_that("check that the original OpenSpecy object remains unmodified", {

particles <- ifelse(map$metadata$x == 1, "particle", "not_particle")
identified_map <- def_features(map, particles)

expect_equal(map, map2)
})

test_that("check that collapse particles returns expected values", {
map <- read_extdata("CA_tiny_map.zip") |> read_any()
particles <- ifelse(map$metadata$x == 1, "particleA", "particleB")
identified_map <- def_features(map, particles)
test_collapsed <- collapse_spec(identified_map)
expect_true(is_OpenSpecy(test_collapsed))

map <- read_extdata("CA_tiny_map.zip") |> read_any()
particles <- map$metadata$x == 1
identified_map <- def_features(map, particles)
test_collapsed <- collapse_spec(identified_map)
expect_true(is_OpenSpecy(test_collapsed))
map <- read_extdata("CA_tiny_map.zip") |> read_any()
particles <- ifelse(map$metadata$x == 1, "particleA", "particleB")
identified_map <- def_features(map, particles)
test_collapsed <- collapse_spec(identified_map)
expect_true(is_OpenSpecy(test_collapsed))

expect_equal(c("particle_id", "area", "feret_max", "centroid_y", "centroid_x"), names(test_collapsed$metadata))
map <- read_extdata("CA_tiny_map.zip") |> read_any()
particles <- map$metadata$x == 1
identified_map <- def_features(map, particles)
test_collapsed <- collapse_spec(identified_map)
expect_true(is_OpenSpecy(test_collapsed))

expect_contains(names(test_collapsed$metadata),
c("particle_id", "area", "feret_max",
"centroid_y", "centroid_x"))
})


19 changes: 9 additions & 10 deletions tests/testthat/test-io_spec.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Loading test data
data(raman_hdpe)

Expand All @@ -7,21 +6,21 @@ test_that("Test read_spec and write_spec functions", {
temp_file_yml <- tempfile(fileext = ".yml")
temp_file_json <- tempfile(fileext = ".json")
temp_file_rds <- tempfile(fileext = ".rds")

write_spec(raman_hdpe, temp_file_yml)
write_spec(raman_hdpe, temp_file_json)
write_spec(raman_hdpe, temp_file_rds)

# Now read the data back in and test equality
spec_yml <- read_spec(temp_file_yml)
spec_json <- read_spec(temp_file_json)
spec_rds <- read_spec(temp_file_rds)

# Verify the equality of the original and the read data
expect_equal(raman_hdpe, spec_yml)
expect_equal(raman_hdpe, spec_json)
expect_equal(raman_hdpe, spec_rds)
expect_equal(spec_yml, raman_hdpe)
expect_equal(spec_json, raman_hdpe)
expect_equal(spec_rds, raman_hdpe)

# Clean up temporary files
file.remove(temp_file_yml)
file.remove(temp_file_json)
Expand All @@ -30,10 +29,10 @@ test_that("Test read_spec and write_spec functions", {

test_that("Test to_hyperspec function", {
hyperspec_object <- to_hyperSpec(raman_hdpe)

# Verify the class of the output
expect_s4_class(hyperspec_object, "hyperSpec")

# Verify the equality of the content
expect_equal(hyperspec_object@wavelength, raman_hdpe$wavenumber)
expect_equal(c(hyperspec_object$spc), raman_hdpe$spectra$intensity)
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-match_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ test_lib_extract <- filter_spec(test_lib, logic = test_lib$metadata$polymer_clas

# Write the tests for cor_spec function
test_that("cor_spec returns a data.table with correct columns", {
matches <- cor_spec(object = unknown,library = test_lib)
matches <- cor_spec(unknown,library = test_lib)
expect_true(inherits(matches, "matrix"))
expect_identical(dim(matches), c(ncol(test_lib$spectra), ncol(unknown$spectra)))
expect_identical(dim(matches), c(ncol(test_lib$spectra), ncol(unknown$spectra)))
top_matches <- max_cor_named(cor_matrix = matches, na.rm = T)
expect_true(length(top_matches) == 1)
expect_true(ncol(filter_spec(test_lib, logic = names(top_matches))$spectra) == 1)
test_lib$metadata$test <- NA
test_metadata <- get_metadata(object = test_lib, logic = names(top_matches), remove_empty = T)
test_metadata <- get_metadata(test_lib, logic = names(top_matches), remove_empty = T)
expect_true(nrow(test_metadata) == 1)
expect_true(!"test" %in% names(test_metadata))
full_test <- ident_spec(cor_matrix = matches, object = unknown, library = test_lib, top_n = 5, add_library_metadata = "sample_name")
full_test <- ident_spec(matches, unknown, library = test_lib, top_n = 5, add_library_metadata = "sample_name")
expect_true(nrow(full_test) == 5)
})

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-process_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ test_that("process returns expected values",{
test_that("sample_spec returns an OpenSpecy object with a subset of the spectra", {
tiny_map <- read_any(read_extdata("CA_tiny_map.zip"))
sampled <- sample_spec(tiny_map, size = 5)
expect_s3_class(result, "OpenSpecy")
expect_equal(ncol(result$spectra), 5)
expect_s3_class(sampled, "OpenSpecy")
expect_equal(ncol(sampled$spectra), 5)
})
34 changes: 5 additions & 29 deletions tests/testthat/test-read_opus.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
test_that("check that opus files are read", {
expect_silent(data <- read_opus(read_extdata("ftir_ps.0")))
expect_true(is_OpenSpecy(data))
expect_true(is_OpenSpecy(data2))
expect_true(ncol(data$spectra) == 1)
expect_true(ncol(data2$spectra) == 1)
expect_true(length(data$wavenumber) == 2126)
expect_true(length(data2$wavenumber) == 2126)
expect_identical(names(data$metadata), c("x",
"y",
"y",
"unique_id",
"sample_id",
"date_time_sm",
Expand All @@ -19,33 +16,12 @@ test_that("check that opus files are read", {
"beamspl",
"laser_wn",
"spc_in_file",
"zero_filling",
"zero_filling",
"temp_scanner_sm",
"temp_scanner_rf",
"temp_scanner_rf",
"hum_rel_sm",
"hum_rel_rf",
"hum_rel_rf",
"hum_abs_sm",
"hum_abs_rf",
"file_id"))
expect_identical(names(data2$metadata), c("x",
"y",
"unique_id",
"sample_id",
"date_time_sm",
"date_time_rf",
"sample_name",
"instr_name_range",
"resolution_wn",
"result_spc",
"beamspl",
"laser_wn",
"spc_in_file",
"zero_filling",
"temp_scanner_sm",
"temp_scanner_rf",
"hum_rel_sm",
"hum_rel_rf",
"hum_abs_sm",
"hum_abs_rf",
"hum_abs_rf",
"file_id"))
})
13 changes: 7 additions & 6 deletions tests/testthat/test-share_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ test_that("share_text() gives expected output", {
)
})

test_that("check that data will upload", {
skip_on_cran()
library(config)
conf <- config::get()
expect_no_error(share_spec(object = raman_hdpe, file = read_extdata("raman_hdpe.csv"), share = "cloud", s3_key_id = conf$s3_key_id, s3_secret_key = conf$s3_secret_key, s3_region = conf$s3_region, s3_bucket = conf$s3_bucket))
})
# TODO: Need to think about config file
# test_that("check that data will upload", {
# skip_on_cran()
# library(config)
# conf <- config::get()
# expect_no_error(share_spec(object = raman_hdpe, file = read_extdata("raman_hdpe.csv"), share = "cloud", s3_key_id = conf$s3_key_id, s3_secret_key = conf$s3_secret_key, s3_region = conf$s3_region, s3_bucket = conf$s3_bucket))
# })


# Tidy up
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-workflows.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ test_that("Zip folder Raman batch analysis", {
expect_silent(matches <- cor_spec(batch3, library = lib))
expect_silent(test_max_cor <- max_cor_named(matches))
expect_silent(test_sn <- signal_noise(batch3, return = "run_signal_over_noise"))
heatmap_spec(object = batch3,
sn = test_sn,
cor = test_max_cor,

heatmap_spec(batch3,
sn = test_sn,
cor = test_max_cor,
min_sn = 4,
min_cor = 0.7,
selected_spectrum = 2,
Expand Down

0 comments on commit cd50169

Please sign in to comment.