From e2ba30ca738cc6ab7aeeb092b1cd2df5c7ed80e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98topepo=E2=80=99?= Date: Mon, 14 Jul 2025 15:35:10 -0400 Subject: [PATCH 1/3] tests for tidymodels/tune#1043 --- .../test-tune-last-fit-reproducibility.R | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/testthat/test-tune-last-fit-reproducibility.R diff --git a/tests/testthat/test-tune-last-fit-reproducibility.R b/tests/testthat/test-tune-last-fit-reproducibility.R new file mode 100644 index 0000000..369811f --- /dev/null +++ b/tests/testthat/test-tune-last-fit-reproducibility.R @@ -0,0 +1,40 @@ +test_that("last_fit() is reproducible", { + skip_if_not_installed("tune", minimum_version = "1.3.0.9001") + skip_if_not_installed("ranger") + + # ------------------------------------------------------------------------------ + + set.seed(1) + dat <- sim_regression(200) + split <- initial_split(dat) + tr_dat <- training(split) + te_dat <- testing(split) + rs <- vfold_cv(tr_dat) + + rf_spec <- rand_forest(mode = "regression", trees = 20) + rf_wflow <- workflow(outcome ~ ., rf_spec) + + # ------------------------------------------------------------------------------ + + # manual: + + set.seed(2) + manual_fit <- fit(rf_wflow, tr_dat) + manual_pred <- + augment(manual_fit, new_data = te_dat) |> + select(outcome, manual = .pred) + + # auto: + + set.seed(2) + auto_res <- last_fit(rf_wflow, split) + auto_pred <- + auto_res |> + collect_predictions() |> + select(outcome, auto = .pred) + + # tune version 1.3.0: + both_pred <- full_join(manual_pred, auto_pred, by = "outcome") + expect_equal(both_pred$auto, both_pred$manual) + +}) From f018f0d749a536c04f964633677b9e57bd351081 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Wed, 13 Aug 2025 16:57:09 -0700 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Hannah Frick --- tests/testthat/test-tune-last-fit-reproducibility.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/testthat/test-tune-last-fit-reproducibility.R b/tests/testthat/test-tune-last-fit-reproducibility.R index 369811f..f3b6543 100644 --- a/tests/testthat/test-tune-last-fit-reproducibility.R +++ b/tests/testthat/test-tune-last-fit-reproducibility.R @@ -21,8 +21,7 @@ test_that("last_fit() is reproducible", { set.seed(2) manual_fit <- fit(rf_wflow, tr_dat) manual_pred <- - augment(manual_fit, new_data = te_dat) |> - select(outcome, manual = .pred) + predict(manual_fit, new_data = te_dat) # auto: From 9b0b53cabd9395e5e5e14b6f84d7b71c53acbb2b Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Wed, 13 Aug 2025 16:58:04 -0700 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Hannah Frick --- tests/testthat/test-tune-last-fit-reproducibility.R | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/tests/testthat/test-tune-last-fit-reproducibility.R b/tests/testthat/test-tune-last-fit-reproducibility.R index f3b6543..79ce522 100644 --- a/tests/testthat/test-tune-last-fit-reproducibility.R +++ b/tests/testthat/test-tune-last-fit-reproducibility.R @@ -1,5 +1,4 @@ test_that("last_fit() is reproducible", { - skip_if_not_installed("tune", minimum_version = "1.3.0.9001") skip_if_not_installed("ranger") # ------------------------------------------------------------------------------ @@ -9,7 +8,6 @@ test_that("last_fit() is reproducible", { split <- initial_split(dat) tr_dat <- training(split) te_dat <- testing(split) - rs <- vfold_cv(tr_dat) rf_spec <- rand_forest(mode = "regression", trees = 20) rf_wflow <- workflow(outcome ~ ., rf_spec) @@ -29,11 +27,8 @@ test_that("last_fit() is reproducible", { auto_res <- last_fit(rf_wflow, split) auto_pred <- auto_res |> - collect_predictions() |> - select(outcome, auto = .pred) + collect_predictions() - # tune version 1.3.0: - both_pred <- full_join(manual_pred, auto_pred, by = "outcome") - expect_equal(both_pred$auto, both_pred$manual) + expect_identical(manual_pred$.pred, auto_pred$.pred) })