Skip to content

Commit

Permalink
tests: silence logger output and predict type warning (#1194)
Browse files Browse the repository at this point in the history
  • Loading branch information
be-marc authored Nov 1, 2024
1 parent f8c9e6f commit 74ad2e1
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 16 deletions.
2 changes: 1 addition & 1 deletion R/assertions.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ assert_measure = function(measure, task = NULL, learner = NULL, prediction = NUL
}
}

if (!is.null(prediction)) {
if (!is.null(prediction) && is.null(learner)) {
# same as above but works without learner e.g. measure$score(prediction)
if (measure$check_prerequisites != "ignore" && measure$predict_type %nin% prediction$predict_types) {
warningf("Measure '%s' is missing predict type '%s' of prediction", measure$id, measure$predict_type)
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/teardown.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
options(old_opts)
lg$set_threshold(old_threshold)
future::plan(old_plan)
file.remove("tests/testthat/Rplots.pdf")
4 changes: 2 additions & 2 deletions tests/testthat/test_Measure.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ test_that("check_prerequisites / predict_type", {
p = learner$train(task)$predict(task)
m = msr("classif.auc")

expect_identical(unname(m$score(p)), NaN)
expect_identical(unname(p$score(m)), NaN)
expect_identical(unname(suppressWarnings(m$score(p))), NaN)
expect_identical(unname(suppressWarnings(p$score(m))), NaN)
expect_warning(m$score(p, learner = learner), "predict type", fixed = TRUE)
expect_warning(p$score(m, learner = learner), "predict type", fixed = TRUE)

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_convert_task.R
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ test_that("matrix converters", {
})

test_that("Matrix converters", {
requireNamespace("Matrix")
requireNamespace("Matrix", quietly = TRUE)
X = Matrix::Matrix(1:9, nrow = 3)
force(X)
colnames(X) = letters[1:3]
Expand Down
18 changes: 6 additions & 12 deletions tests/testthat/test_encapsulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ learner = lrn("classif.debug")
learner$param_set$values = list(message_train = 1, warning_train = 1, message_predict = 1, warning_predict = 1)

test_that("evaluate / single step", {

lg$set_threshold("off")
on.exit({
lg$set_threshold("warn")
})

row_ids = 1:120
expect_message(expect_warning(disable_encapsulation(learner)$train(task, row_ids)))
log = learner$log
Expand Down Expand Up @@ -50,12 +44,6 @@ test_that("evaluate / single step", {
})

test_that("evaluate / resample", {

lg$set_threshold("off")
on.exit({
lg$set_threshold("warn")
})

resampling = rsmp("cv", folds = 3)

rr = suppressMessages(suppressWarnings(resample(task, disable_encapsulation(learner), resampling)))
Expand All @@ -68,6 +56,12 @@ test_that("evaluate / resample", {
test_that("errors and warnings are printed with logger", {
task = tsk("spam")

old_threshold = lg$threshold
lg$set_threshold("warn")
on.exit({
lg$set_threshold(old_threshold)
})

learner = enable_encapsulation(lrn("classif.debug", error_train = 1))
expect_output(learner$train(task), "ERROR")

Expand Down

0 comments on commit 74ad2e1

Please sign in to comment.