From 6b4ab746e5474dfeda9237f01bc2dd01f1bb62ee Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Wed, 27 Feb 2019 12:04:52 +0100 Subject: Increase test coverage --- tests/testthat/DFOP_FOCUS_C_messages.txt | 270 +++++++++++++++++++++++++++++++ tests/testthat/FOCUS_2006_C_mkinds.txt | 0 tests/testthat/test_from_max_mean.R | 17 ++ tests/testthat/test_mkinds.R | 6 + tests/testthat/test_mkinfit_errors.R | 71 ++++++++ tests/testthat/test_plots_summary_twa.R | 33 ++-- 6 files changed, 380 insertions(+), 17 deletions(-) create mode 100644 tests/testthat/DFOP_FOCUS_C_messages.txt create mode 100644 tests/testthat/FOCUS_2006_C_mkinds.txt create mode 100644 tests/testthat/test_from_max_mean.R create mode 100644 tests/testthat/test_mkinds.R create mode 100644 tests/testthat/test_mkinfit_errors.R (limited to 'tests') diff --git a/tests/testthat/DFOP_FOCUS_C_messages.txt b/tests/testthat/DFOP_FOCUS_C_messages.txt new file mode 100644 index 00000000..7abde0b6 --- /dev/null +++ b/tests/testthat/DFOP_FOCUS_C_messages.txt @@ -0,0 +1,270 @@ +parent_0 log_k1 log_k2 g_ilr +85.1 -2.302585 -4.60517 0 +Model cost at call 1 : 7391.39 +85.1 -2.302585 -4.60517 0 +85.1 -2.302585 -4.60517 0 +Model cost at call 3 : 7391.389 +85.1 -2.302585 -4.60517 0 +Model cost at call 4 : 7391.389 +85.1 -2.302585 -4.60517 1.490116e-08 +85.06371 -1.77328 -4.250366 0.7698268 +Model cost at call 6 : 2000.127 +85.06375 -1.77328 -4.250366 0.7698268 +85.06371 -1.773322 -4.250366 0.7698268 +85.06371 -1.77328 -4.250408 0.7698268 +85.06371 -1.77328 -4.250366 0.7697847 +85.03542 -0.9608523 -4.11546 1.336361 +Model cost at call 11 : 32.97798 +85.03542 -0.9608523 -4.11546 1.336361 +85.03542 -0.9608526 -4.11546 1.336361 +85.03542 -0.9608523 -4.11546 1.336361 +85.03542 -0.9608523 -4.11546 1.336361 +85.03704 -0.256064 -4.273512 0.6447755 +85.03285 -0.7822828 -4.127513 1.312494 +Model cost at call 17 : 5.348133 +85.03286 -0.7822828 -4.127513 1.312494 +Model cost at call 18 : 5.348132 +85.03285 -0.7822828 -4.127513 1.312494 +Model cost at call 19 : 5.348131 +85.03285 -0.7822828 -4.127513 1.312494 +85.03285 -0.7822828 -4.127513 1.312494 +Model cost at call 21 : 5.348131 +85.02325 -0.74968 -4.059 1.14891 +85.03127 -0.7909068 -4.114802 1.268157 +Model cost at call 23 : 4.704445 +85.03127 -0.7909068 -4.114802 1.268157 +Model cost at call 24 : 4.704444 +85.03127 -0.7909068 -4.114802 1.268157 +85.03127 -0.7909068 -4.1148 1.268157 +Model cost at call 26 : 4.704433 +85.03127 -0.7909068 -4.114802 1.268158 +85.03001 -0.7801506 -4.069435 1.262797 +Model cost at call 28 : 4.421625 +85.03001 -0.7801506 -4.069435 1.262797 +85.03001 -0.7801507 -4.069435 1.262797 +Model cost at call 30 : 4.421624 +85.03001 -0.7801506 -4.069435 1.262797 +85.03001 -0.7801506 -4.069435 1.262797 +85.02878 -0.7900844 -4.023945 1.256918 +85.02964 -0.7857352 -4.054587 1.260236 +Model cost at call 34 : 4.414346 +85.02964 -0.7857352 -4.054587 1.260236 +85.02964 -0.7857351 -4.054587 1.260236 +Model cost at call 36 : 4.414346 +85.02964 -0.7857352 -4.054588 1.260236 +85.02964 -0.7857352 -4.054587 1.260236 +85.02812 -0.7778128 -4.042219 1.25389 +Model cost at call 39 : 4.372463 +85.02812 -0.7778128 -4.042219 1.25389 +85.02812 -0.7778129 -4.042219 1.25389 +Model cost at call 41 : 4.372462 +85.02812 -0.7778128 -4.042219 1.25389 +85.02812 -0.7778128 -4.042219 1.25389 +85.02419 -0.7765144 -4.02942 1.245094 +85.0263 -0.7778419 -4.036021 1.249634 +Model cost at call 45 : 4.369313 +85.0263 -0.7778419 -4.036021 1.249634 +85.0263 -0.7778418 -4.036021 1.249634 +Model cost at call 47 : 4.369313 +85.0263 -0.7778419 -4.036022 1.249634 +85.0263 -0.7778419 -4.036021 1.249634 +Model cost at call 49 : 4.369313 +85.02267 -0.7786811 -4.02967 1.252015 +Model cost at call 50 : 4.365062 +85.02268 -0.7786811 -4.02967 1.252015 +85.02267 -0.7786812 -4.02967 1.252015 +85.02267 -0.7786811 -4.02967 1.252015 +Model cost at call 53 : 4.365062 +85.02267 -0.7786811 -4.02967 1.252015 +85.01633 -0.7763163 -4.027611 1.248897 +Model cost at call 55 : 4.364078 +85.01633 -0.7763163 -4.027611 1.248897 +Model cost at call 56 : 4.364078 +85.01633 -0.7763164 -4.027611 1.248897 +Model cost at call 57 : 4.364077 +85.01633 -0.7763163 -4.027611 1.248897 +85.01633 -0.7763163 -4.027611 1.248897 +85.00894 -0.7777917 -4.026307 1.24772 +Model cost at call 60 : 4.364052 +85.00894 -0.7777917 -4.026307 1.24772 +Model cost at call 61 : 4.364052 +85.00894 -0.7777917 -4.026307 1.24772 +Model cost at call 62 : 4.364052 +85.00894 -0.7777917 -4.026307 1.24772 +85.00894 -0.7777917 -4.026307 1.24772 +Model cost at call 64 : 4.364052 +85.00518 -0.7773082 -4.026004 1.248453 +Model cost at call 65 : 4.362751 +85.00519 -0.7773082 -4.026004 1.248453 +85.00518 -0.7773083 -4.026004 1.248453 +Model cost at call 67 : 4.362751 +85.00518 -0.7773082 -4.026005 1.248453 +85.00518 -0.7773082 -4.026004 1.248453 +85.00134 -0.7776046 -4.025878 1.248775 +Model cost at call 70 : 4.362721 +85.00135 -0.7776046 -4.025878 1.248775 +Model cost at call 71 : 4.362721 +85.00134 -0.7776046 -4.025878 1.248775 +Model cost at call 72 : 4.362721 +85.00134 -0.7776046 -4.025878 1.248775 +85.00134 -0.7776046 -4.025878 1.248775 +85.0032 -0.7774734 -4.0257 1.248643 +Model cost at call 75 : 4.362715 +85.0032 -0.7774734 -4.0257 1.248643 +85.0032 -0.7774734 -4.0257 1.248643 +Model cost at call 77 : 4.362715 +85.0032 -0.7774735 -4.0257 1.248643 +85.0032 -0.7774734 -4.0257 1.248643 +85.0032 -0.7774734 -4.0257 1.248643 +85.0032 -0.7774734 -4.0257 1.248643 +85.00249 -0.7774909 -4.025911 1.248679 +Model cost at call 82 : 4.362715 +85.0025 -0.7774909 -4.025911 1.248679 +Model cost at call 83 : 4.362715 +85.00249 -0.7774909 -4.025911 1.248679 +85.00249 -0.7774905 -4.025911 1.248679 +85.00249 -0.7774914 -4.025911 1.248679 +Model cost at call 86 : 4.362715 +85.00249 -0.7774909 -4.025911 1.248679 +85.00249 -0.7774909 -4.025911 1.248679 +85.00249 -0.7774909 -4.025911 1.248679 +85.00249 -0.7774909 -4.025911 1.248679 +85.00274 -0.7774922 -4.025821 1.248672 +Model cost at call 91 : 4.362714 +85.00274 -0.7774922 -4.025821 1.248672 +85.00274 -0.7774922 -4.025821 1.248672 +Model cost at call 93 : 4.362714 +85.00274 -0.7774921 -4.025821 1.248672 +Model cost at call 94 : 4.362714 +85.00274 -0.7774922 -4.025821 1.248672 +85.00274 -0.7774922 -4.025821 1.248672 +85.00274 -0.7774922 -4.025821 1.248672 +85.00274 -0.7774922 -4.025821 1.248672 +85.00274 -0.7774922 -4.025821 1.248672 +85.00273 -0.7774912 -4.025817 1.24867 +Model cost at call 100 : 4.362714 +85.00275 -0.7774912 -4.025817 1.24867 +85.00271 -0.7774912 -4.025817 1.24867 +85.00273 -0.7774905 -4.025817 1.24867 +85.00273 -0.7774919 -4.025817 1.24867 +85.00273 -0.7774912 -4.025814 1.24867 +85.00273 -0.7774912 -4.025821 1.24867 +85.00273 -0.7774912 -4.025817 1.248671 +85.00273 -0.7774912 -4.025817 1.248669 +85.00274 -0.7774913 -4.025819 1.248671 +Model cost at call 109 : 4.362714 +85.00276 -0.7774913 -4.025819 1.248671 +85.00272 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774904 -4.025819 1.248671 +85.00274 -0.7774922 -4.025819 1.248671 +85.00274 -0.7774913 -4.025815 1.248671 +85.00274 -0.7774913 -4.025822 1.248671 +85.00274 -0.7774913 -4.025819 1.248672 +85.00274 -0.7774913 -4.025819 1.248669 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +Model cost at call 123 : 4.362714 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +IRLS based on variance estimates according to the two component error model +Initial variance components are: +sigma_low rsd_high + 1.08984 0.00000 +85.00274 -0.7774913 -4.025819 1.248671 +Model cost at call 129 : 3.673061 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +Model cost at call 132 : 3.673061 +85.00274 -0.7774913 -4.025819 1.248671 +85.00273 -0.7775309 -4.025818 1.24866 +85.00274 -0.7774953 -4.025819 1.24867 +85.00274 -0.7774917 -4.025819 1.248671 +85.00274 -0.7774914 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +Model cost at call 138 : 3.673061 +85.00277 -0.7774913 -4.025819 1.248671 +85.0027 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774744 -4.025819 1.248671 +85.00274 -0.7775083 -4.025819 1.248671 +85.00274 -0.7774913 -4.025779 1.248671 +85.00274 -0.7774913 -4.025858 1.248671 +85.00274 -0.7774913 -4.025819 1.248702 +85.00274 -0.7774913 -4.025819 1.248639 +85.00274 -0.7774913 -4.025819 1.248671 +Model cost at call 147 : 3.673061 +85.00276 -0.7774913 -4.025819 1.248671 +85.00271 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774906 -4.025819 1.248671 +85.00274 -0.7774921 -4.025819 1.248671 +85.00274 -0.7774913 -4.025812 1.248671 +85.00274 -0.7774913 -4.025825 1.248671 +85.00274 -0.7774913 -4.025819 1.248672 +85.00274 -0.7774913 -4.025819 1.248669 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +Model cost at call 159 : 3.673061 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +Iteration 1 yields variance estimates: +sigma_low rsd_high +0.7434091 0.0000000 +Sum of squared differences to last variance (component) estimates: 0.12 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00273 -0.7775366 -4.025821 1.248652 +85.00274 -0.7774959 -4.025819 1.248669 +85.00274 -0.7774918 -4.025819 1.24867 +85.00274 -0.7774914 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00279 -0.7774913 -4.025819 1.248671 +85.00268 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774621 -4.025819 1.248671 +85.00274 -0.7775206 -4.025819 1.248671 +85.00274 -0.7774913 -4.025761 1.248671 +85.00274 -0.7774913 -4.025876 1.248671 +85.00274 -0.7774913 -4.025819 1.248714 +85.00274 -0.7774913 -4.025819 1.248627 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00273 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774908 -4.025819 1.248671 +85.00274 -0.7774919 -4.025819 1.248671 +85.00274 -0.7774913 -4.025816 1.248671 +85.00274 -0.7774913 -4.025822 1.248671 +85.00274 -0.7774913 -4.025819 1.248672 +85.00274 -0.7774913 -4.025819 1.24867 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +85.00274 -0.7774913 -4.025819 1.248671 +Iteration 2 yields variance estimates: +sigma_low rsd_high +0.7434091 0.0000000 +Sum of squared differences to last variance (component) estimates: 2.9e-16 +Optimisation by method Port successfully terminated. diff --git a/tests/testthat/FOCUS_2006_C_mkinds.txt b/tests/testthat/FOCUS_2006_C_mkinds.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/testthat/test_from_max_mean.R b/tests/testthat/test_from_max_mean.R new file mode 100644 index 00000000..8e5953b9 --- /dev/null +++ b/tests/testthat/test_from_max_mean.R @@ -0,0 +1,17 @@ +context("Test fitting the decline of metabolites from their maximum") + +test_that("Fitting from maximum mean value works", { + SFO_SFO <- mkinmod(parent = mkinsub("SFO", "m1"), + m1 = mkinsub("SFO")) + expect_error(mkinfit(SFO_SFO, FOCUS_2006_D, from_max_mean = TRUE)) + + # We can either explicitly create a model for m1, or subset the data + SFO_m1 <- mkinmod(m1 = mkinsub("SFO")) + f.1 <- mkinfit(SFO_m1, FOCUS_2006_D, from_max_mean = TRUE, quiet = TRUE) + expect_equivalent(endpoints(f.1)$distimes["m1", ], c(170.8, 567.5), + scale = 1, tolerance = 0.1) + + f.2 <- mkinfit("SFO", subset(FOCUS_2006_D, name == "m1"), from_max_mean = TRUE, quiet = TRUE) + expect_equivalent(endpoints(f.2)$distimes["m1", ], c(170.8, 567.5), + scale = 1, tolerance = 0.1) +}) diff --git a/tests/testthat/test_mkinds.R b/tests/testthat/test_mkinds.R new file mode 100644 index 00000000..e017301c --- /dev/null +++ b/tests/testthat/test_mkinds.R @@ -0,0 +1,6 @@ +context("Test dataset class mkinds used in gmkin") + +test_that("An mkinds object can be created and printed", { + testdata <- mkinds$new("FOCUS C", data = FOCUS_2006_C, time_unit = "days", unit = "%AR") + expect_known_output(testdata, "FOCUS_2006_C_mkinds.txt") +}) diff --git a/tests/testthat/test_mkinfit_errors.R b/tests/testthat/test_mkinfit_errors.R new file mode 100644 index 00000000..50032628 --- /dev/null +++ b/tests/testthat/test_mkinfit_errors.R @@ -0,0 +1,71 @@ +library(mkin) +library(testthat) + +context("Special cases of mkinfit calls") + +SFO_SFO.ff.nosink <- mkinmod( + parent = mkinsub("SFO", "m1", sink = FALSE), + m1 = mkinsub("SFO"), quiet = TRUE, use_of_ff = "max") + +SFO_SFO.ff <- mkinmod( + parent = mkinsub("SFO", "m1"), + m1 = mkinsub("SFO"), quiet = TRUE, use_of_ff = "max") + +test_that("mkinfit stops to prevent and/or explain user errors", { + expect_error(mkinfit("foo", FOCUS_2006_A)) + expect_error(mkinfit(3, FOCUS_2006_A)) + + # We get a warning if we use transform_fractions = FALSE with formation fractions + # and an error if any pathway to sink is turned off as well + expect_warning( + expect_error( + mkinfit(SFO_SFO.ff.nosink, FOCUS_2006_D, transform_fractions = FALSE, quiet = TRUE), + "turn off pathways to sink" + ), + "sum of formation fractions") + + expect_error(mkinfit(SFO_SFO.ff, FOCUS_2006_D, transform_fractions = TRUE, + parms.ini = c(f_parent_to_m1 = 0.5), fixed_parms = "f_parent_to_m1", quiet = TRUE), + "not supported") + + expect_error(mkinfit(SFO_SFO.ff, FOCUS_2006_D, + parms.ini = c(f_parent_to_m1 = 1.1), quiet = TRUE), + "sum up to more than 1") + + expect_error(mkinfit(SFO_SFO.ff, FOCUS_2006_D, solution_type = "analytical"), "not implemented") + + expect_error(mkinfit("FOMC", FOCUS_2006_A, solution_type = "eigen"), "coefficient matrix not present") + + # We suppress a message stemming from the interrupted call to system.time() + expect_error(suppressMessages(mkinfit("SFO", FOCUS_2006_A, reweight.method = + "foo", quiet = TRUE), "implemented")) + +}) + +test_that("mkinfit stops early when a low maximum number of iterations is specified", { + expect_warning(mkinfit("SFO", FOCUS_2006_A, maxit.modFit = 1, quiet = TRUE)) + expect_warning(mkinfit("SFO", FOCUS_2006_A, maxit.modFit = 1, quiet = TRUE, method.modFit = "Marq")) +}) + +test_that("mkinfit warns if the user chooses the SANN method", { + expect_warning(mkinfit("SFO", FOCUS_2006_A, method.modFit = "SANN", maxit.modFit = 10, quiet = TRUE)) + skip("The SANN algorithm takes very long with the default maximum number of iterations of 10000") + expect_warning(mkinfit("SFO", FOCUS_2006_A, method.modFit = "SANN")) +}) + +test_that("mkinfit warns if a specified initial parameter value is not in the model", { + expect_warning(mkinfit("SFO", FOCUS_2006_A, parms.ini = c(k_xy = 0.1), quiet = TRUE)) +}) + +test_that("We get reproducible output if quiet = FALSE", { + # We cannot expect parameter and sum of squares traces to be the same across platforms + skip_on_cran() + skip_on_travis() + expect_known_output(mkinfit("DFOP", FOCUS_2006_C, reweight.method = "tc", trace_parms = TRUE), + file = "DFOP_FOCUS_C_messages.txt") +}) + +test_that("We get warnings in case of overparameterisation", { + expect_warning(f <- mkinfit("FOMC", FOCUS_2006_A, quiet = TRUE), "not converge") + s2 <- expect_warning(summary(mkinfit("DFOP", FOCUS_2006_A, quiet = TRUE)), "singular system") +}) diff --git a/tests/testthat/test_plots_summary_twa.R b/tests/testthat/test_plots_summary_twa.R index e13111bb..f58ce764 100644 --- a/tests/testthat/test_plots_summary_twa.R +++ b/tests/testthat/test_plots_summary_twa.R @@ -28,23 +28,22 @@ test_that("Time weighted average concentrations are correct", { outtimes_10 <- seq(0, 10, length.out = 10000) - for (ds in c("FOCUS_C", "FOCUS_D")) { - for (model in models) { - fit <- fits[[model, ds]] - bpar <- summary(fit)$bpar[, "Estimate"] - pred_10 <- mkinpredict(fit$mkinmod, - odeparms = bpar[2:length(bpar)], - odeini = c(parent = bpar[[1]]), - outtimes = outtimes_10) - twa_num <- mean(pred_10$parent) - names(twa_num) <- 10 - twa_ana <- max_twa_parent(fit, 10) - - # Test for absolute difference (scale = 1) - # The tolerance can be reduced if the length of outtimes is increased, - # but this needs more computing time so we stay with lenght.out = 10k - expect_equal(twa_num, twa_ana, tolerance = 0.003, scale = 1) - } + ds <- "FOCUS_C" + for (model in models) { + fit <- fits[[model, ds]] + bpar <- summary(fit)$bpar[, "Estimate"] + pred_10 <- mkinpredict(fit$mkinmod, + odeparms = bpar[2:length(bpar)], + odeini = c(parent = bpar[[1]]), + outtimes = outtimes_10) + twa_num <- mean(pred_10$parent) + names(twa_num) <- 10 + twa_ana <- max_twa_parent(fit, 10) + + # Test for absolute difference (scale = 1) + # The tolerance can be reduced if the length of outtimes is increased, + # but this needs more computing time so we stay with lenght.out = 10k + expect_equal(twa_num, twa_ana, tolerance = 0.003, scale = 1) } }) -- cgit v1.2.1