From efc11aa275cedc5c040beee4a30b269f2ccb39c7 Mon Sep 17 00:00:00 2001 From: Ruth Kristianingsih Date: Wed, 30 Aug 2023 06:57:20 +0100 Subject: [PATCH] Use z-score scaling_method and updated expected stretch and shift values in registration unit tests --- tests/testthat/test-dynamics.R | 2 +- tests/testthat/test-optimisation.R | 6 +++--- tests/testthat/test-registration.R | 22 +++++++++++++--------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/tests/testthat/test-dynamics.R b/tests/testthat/test-dynamics.R index cde224a..0a76c4f 100644 --- a/tests/testthat/test-dynamics.R +++ b/tests/testthat/test-dynamics.R @@ -4,7 +4,7 @@ query <- "Col0" gene_data <- brapa_sample_data[gene_id == "BRAA03G051930.3C"] test_that("calc_loglik_H1 and calc_loglik_H2 work", { - all_data <- preprocess_data(gene_data, reference, query) + all_data <- preprocess_data(gene_data, reference, query, scaling_method = "z-score") # Expected outputs expect_no_error(calc_loglik_H2(all_data)) diff --git a/tests/testthat/test-optimisation.R b/tests/testthat/test-optimisation.R index b5f5dc7..6acdf5d 100644 --- a/tests/testthat/test-optimisation.R +++ b/tests/testthat/test-optimisation.R @@ -2,7 +2,7 @@ brapa_sample_data <- data.table::fread(system.file("extdata/brapa_arabidopsis_al reference <- "Ro18" query <- "Col0" gene_data <- brapa_sample_data[gene_id == "BRAA03G051930.3C"] -all_data <- preprocess_data(gene_data, reference, query) +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) @@ -35,7 +35,7 @@ test_that("get_search_space_limits_from_params works", { }) test_that("calc_overlapping_percent works", { - all_data_reg <- apply_registration(all_data, 2.75, 3.6) + 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) @@ -46,7 +46,7 @@ test_that("calc_overlapping_percent works", { test_that("objective_fun works", { # Expected outputs - expect_equal(objective_fun(all_data, 2.75, 3.6, 0.5), -11.19, tolerance = 1e-2) + expect_equal(objective_fun(all_data, 3.10, 2.13, 0.5), -11.19, tolerance = 1e-2) expect_equal(objective_fun(all_data), -999) }) diff --git a/tests/testthat/test-registration.R b/tests/testthat/test-registration.R index a1f53a8..fa5d1de 100644 --- a/tests/testthat/test-registration.R +++ b/tests/testthat/test-registration.R @@ -8,10 +8,12 @@ gene_data <- brapa_sample_data[gene_id == "BRAA03G051930.3C"] test_that("preprocess_data works", { all_data <- preprocess_data(brapa_sample_data, reference, query) all_data_norm <- preprocess_data(brapa_sample_data, reference, query, scaling_method = "z-score") + all_data_minmax <- preprocess_data(brapa_sample_data, reference, query, scaling_method = "min-max") # Expected outputs expect_equal(class(all_data)[1], "data.table") expect_equal(class(all_data_norm)[1], "data.table") + expect_equal(class(all_data_minmax)[1], "data.table") expect_gte(mean(all_data$expression_value), mean(all_data_norm$expression_value)) expect_equal(colnames(all_data), c(colnames(brapa_sample_data), "time_delta", "var")) expect_equal(levels(unique(all_data$accession)), c("ref", "query")) @@ -19,9 +21,9 @@ test_that("preprocess_data works", { }) test_that("register_manually works", { - gene_data <- preprocess_data(gene_data, reference, query) - stretch <- 2.75 - shift <- 3.6 + gene_data <- preprocess_data(gene_data, reference, query, scaling_method = "z-score") + stretch <- 3.10 + shift <- 2.13 loglik_separate <- -10.404 results <- register_manually(gene_data, stretch, shift, loglik_separate) results_simple <- register_manually(gene_data, stretch, shift, loglik_separate, return_data_reg = FALSE) @@ -40,15 +42,16 @@ test_that("register_manually works", { # Full pipeline ---- test_that("register (with no optimisation) works", { - stretch <- 2.75 - shift <- 3.6 + stretch <- 3.10 + shift <- 2.13 registration_results <- register( gene_data, reference = "Ro18", query = "Col0", stretches = stretch, shifts = shift, - optimise_registration_parameters = FALSE + optimise_registration_parameters = FALSE, + scaling_method = "z-score" ) |> suppressMessages() @@ -68,7 +71,8 @@ test_that("register (with optimisation) works", { gene_data, reference = "Ro18", query = "Col0", - optimisation_method = "nm", + optimisation_method = "lbfgsb", + scaling_method = "z-score", optimisation_config = list(num_iterations = 10, num_fun_evals = 10) ) |> suppressMessages() @@ -81,7 +85,7 @@ test_that("register (with optimisation) works", { expect_equal(colnames(data_reg), c("gene_id", "accession", "expression_value", "replicate", "timepoint", "timepoint_reg")) expect_equal(colnames(model_comparison), c("gene_id", "stretch", "shift", "BIC_diff", "registered")) expect_equal(model_comparison$registered, TRUE) - expect_equal(model_comparison$stretch, 2.809, tolerance = 1e-2) - expect_equal(model_comparison$shift, 0.143, tolerance = 1e-2) + expect_equal(model_comparison$stretch, 3.10, tolerance = 1e-2) + expect_equal(model_comparison$shift, 2.13, tolerance = 1e-2) expect_error(register(gene_data, reference = "Ro19", query = "Col0")) })