Skip to content

Commit

Permalink
Updated unit tests to take into account new possible parameters accep…
Browse files Browse the repository at this point in the history
…ted by get_search_space_limits() function
  • Loading branch information
ruthkr committed Jan 8, 2024
1 parent b4f76fe commit 7f0bd77
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 54 deletions.
40 changes: 0 additions & 40 deletions tests/testthat/test-optimisation.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,6 @@ query <- "Col0"
gene_data <- brapa_sample_data[gene_id == "BRAA03G051930.3C"]
all_data <- preprocess_data(gene_data, reference, query, scaling_method = "z-score")

test_that("get_search_space_limits works", {
space_lims <- get_search_space_limits(all_data)

# Expected outputs
expect_equal(names(space_lims), c("stretch_init", "stretch_lower", "stretch_upper", "shift_init", "shift_lower", "shift_upper"))
expect_equal(space_lims$stretch_init, 2.667, tolerance = 1e-2)
expect_equal(space_lims$stretch_lower, 1.333, tolerance = 1e-2)
expect_equal(space_lims$stretch_upper, 4, tolerance = 1e-2)
expect_equal(space_lims$shift_init, 0, tolerance = 1e-2)
expect_equal(space_lims$shift_lower, -20, tolerance = 1e-2)
expect_equal(space_lims$shift_upper, 16, tolerance = 1e-2)
expect_no_error(get_search_space_limits(all_data))
expect_error(get_search_space_limits(gene_data))
})

test_that("get_search_space_limits_from_params works", {
stretches <- c(1, 2, 3)
shifts <- c(-4, 4)
space_lims <- get_search_space_limits_from_params(stretches = stretches, shifts = shifts)

# Expected outputs
expect_equal(names(space_lims), c("stretch_init", "stretch_lower", "stretch_upper", "shift_init", "shift_lower", "shift_upper"))
expect_equal(space_lims$stretch_init, mean(stretches), tolerance = 1e-2)
expect_equal(space_lims$stretch_lower, min(stretches), tolerance = 1e-2)
expect_equal(space_lims$stretch_upper, max(stretches), tolerance = 1e-2)
expect_equal(space_lims$shift_init, 0, tolerance = 1e-2)
expect_equal(space_lims$shift_lower, min(shifts), tolerance = 1e-2)
expect_equal(space_lims$shift_upper, max(shifts), tolerance = 1e-2)
})

test_that("calc_overlapping_percent works", {
all_data_reg <- apply_registration(all_data, 3.10, 2.13)
overlapping_raw <- calc_overlapping_percent(all_data)
overlapping_reg <- calc_overlapping_percent(all_data_reg)

# Expected outputs
expect_gte(overlapping_reg, overlapping_raw)
expect_equal(overlapping_reg, 1, tolerance = 1e-1)
})

test_that("objective_fun works", {
# Expected outputs
expect_equal(objective_fun(all_data, 3.10, 2.13, 0.5), -18.82, tolerance = 1e-2)
Expand Down
88 changes: 74 additions & 14 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
brapa_sample_data <- data.table::fread(system.file("extdata/brapa_arabidopsis_all_replicates.csv", package = "greatR"))

test_that("get_approximate_stretch works", {
approximate_stretch <- get_approximate_stretch(
brapa_sample_data,
reference = "Ro18",
query = "Col0"
)

# Expected outputs
expect_equal(approximate_stretch, 2.6666666, tolerance = 1e-6)
expect_gte(approximate_stretch, 0)
expect_equal(class(approximate_stretch), "numeric")
})
reference <- "Ro18"
query <- "Col0"
gene_data <- brapa_sample_data[gene_id == "BRAA03G051930.3C"]
all_data <- preprocess_data(gene_data, reference, query, scaling_method = "z-score")

test_that("match_names works", {
a <- LETTERS[1:3]
Expand All @@ -27,9 +18,10 @@ test_that("validate_params works", {
# Expected outputs
expect_no_error(suppressMessages(validate_params(stretches = 1, shifts = 0, registration_type = "optimisation")))
expect_no_error(suppressMessages(validate_params(stretches = NA, shifts = NA, registration_type = "optimisation")))
expect_error(suppressMessages(validate_params(stretches = 1, shifts = NA, registration_type = "optimisation")))
expect_no_error(suppressMessages(validate_params(stretches = 1, shifts = NA, registration_type = "optimisation")))
expect_no_error(suppressMessages(validate_params(stretches = 1, shifts = 0, registration_type = "manual")))
expect_error(suppressMessages(validate_params(stretches = NA, shifts = NA, registration_type = "manual")))
expect_error(suppressMessages(validate_params(stretches = 1, shifts = NA, registration_type = "manual")))
})

test_that("cross_join works", {
Expand All @@ -42,3 +34,71 @@ test_that("cross_join works", {
expect_equal(dim(dt_cj)[2], ncol(dt_a) + ncol(dt_b))
expect_equal(colnames(dt_cj), c(colnames(dt_a), colnames(dt_b)))
})

test_that("get_approximate_stretch works", {
approximate_stretch <- get_approximate_stretch(
brapa_sample_data,
reference = "Ro18",
query = "Col0"
)

# Expected outputs
expect_equal(approximate_stretch, 2.6666666, tolerance = 1e-6)
expect_gte(approximate_stretch, 0)
expect_equal(class(approximate_stretch), "numeric")
})

test_that("get_search_space_limits (auto) works", {
space_lims <- get_search_space_limits(all_data)

# Expected outputs
expect_equal(names(space_lims), c("stretch_init", "stretch_lower", "stretch_upper", "shift_init", "shift_lower", "shift_upper"))
expect_equal(space_lims$stretch_init, 2.667, tolerance = 1e-2)
expect_equal(space_lims$stretch_lower, 1.333, tolerance = 1e-2)
expect_equal(space_lims$stretch_upper, 4, tolerance = 1e-2)
expect_equal(space_lims$shift_init, 0, tolerance = 1e-2)
expect_equal(space_lims$shift_lower, -20, tolerance = 1e-2)
expect_equal(space_lims$shift_upper, 16, tolerance = 1e-2)
expect_no_error(get_search_space_limits(all_data))
expect_error(get_search_space_limits(gene_data))
})

test_that("get_search_space_limits (bound) works", {
stretches <- c(1, 2, 3)
shifts <- c(-4, 4)
space_lims <- get_search_space_limits(all_data, stretches = stretches, shifts = shifts)

# Expected outputs
expect_equal(names(space_lims), c("stretch_init", "stretch_lower", "stretch_upper", "shift_init", "shift_lower", "shift_upper"))
expect_equal(space_lims$stretch_init, mean(stretches), tolerance = 1e-2)
expect_equal(space_lims$stretch_lower, min(stretches), tolerance = 1e-2)
expect_equal(space_lims$stretch_upper, max(stretches), tolerance = 1e-2)
expect_equal(space_lims$shift_init, 0, tolerance = 1e-2)
expect_equal(space_lims$shift_lower, min(shifts), tolerance = 1e-2)
expect_equal(space_lims$shift_upper, max(shifts), tolerance = 1e-2)
})

test_that("get_search_space_limits (init) works", {
stretches <- 1
shifts <- 4
space_lims <- get_search_space_limits(all_data, stretches = stretches, shifts = shifts)

# Expected outputs
expect_equal(names(space_lims), c("stretch_init", "stretch_lower", "stretch_upper", "shift_init", "shift_lower", "shift_upper"))
expect_equal(space_lims$stretch_init, stretches, tolerance = 1e-2)
expect_equal(space_lims$stretch_lower, 1.333, tolerance = 1e-2)
expect_equal(space_lims$stretch_upper, 4, tolerance = 1e-2)
expect_equal(space_lims$shift_init, shifts, tolerance = 1e-2)
expect_equal(space_lims$shift_lower, -20, tolerance = 1e-2)
expect_equal(space_lims$shift_upper, 16, tolerance = 1e-2)
})

test_that("calc_overlapping_percent works", {
all_data_reg <- apply_registration(all_data, 3.10, 2.13)
overlapping_raw <- calc_overlapping_percent(all_data)
overlapping_reg <- calc_overlapping_percent(all_data_reg)

# Expected outputs
expect_gte(overlapping_reg, overlapping_raw)
expect_equal(overlapping_reg, 1, tolerance = 1e-1)
})

0 comments on commit 7f0bd77

Please sign in to comment.