Skip to content

Commit

Permalink
Merge branch 'set_fallback' into featureless_quantiles
Browse files Browse the repository at this point in the history
  • Loading branch information
be-marc committed Aug 30, 2024
2 parents b79f862 + beeb1b3 commit 2deea95
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 13 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ Collate:
'predict.R'
'reexports.R'
'resample.R'
'set_fallback.R'
'set_threads.R'
'set_validate.R'
'task_converters.R'
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* feat: Add option to calculate the mean of the true values on the train set in `msr("regr.rsq")`.
* feat: Default fallback learner is set when encapsulation is activated.
* feat: Learners classif.debug and regr.debug have new methods `$importance()` and `$selected_features()` for testing, also in downstream packages
* feat: Set default fallback with `set_fallback()`.

# mlr3 0.20.2

Expand Down
14 changes: 2 additions & 12 deletions R/Learner.R
Original file line number Diff line number Diff line change
Expand Up @@ -567,18 +567,8 @@ Learner = R6Class("Learner",
assert_names(names(rhs), subset.of = c("train", "predict"))
private$.encapsulate = insert_named(default, rhs)

if (is.null(private$.fallback)) {
# if there is no fallback, we get a default one from the reflections table
fallback_id = mlr_reflections$learner_fallback[[self$task_type]]
if (!is.null(fallback_id)) {
self$fallback = lrn(fallback_id, predict_type = self$predict_type)

if (self$predict_type == "quantiles") {
self$fallback$quantiles = self$quantiles
self$fallback$quantile_response = self$quantile_response
}
}
}
# if there is no fallback, we get a default one from the reflections table
if (is.null(private$.fallback)) set_fallback(self)
},

#' @field fallback ([Learner])\cr
Expand Down
14 changes: 14 additions & 0 deletions R/Task.R
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,20 @@ task_check_col_roles = function(self, new_roles) {
}
}

# check weights
if (length(new_roles[["weight"]])) {
weights = self$backend$data(self$backend$rownames, cols = new_roles[["weight"]])
assert_numeric(weights[[1L]], lower = 0, any.missing = FALSE, .var.name = names(weights))
}

# check name
if (length(new_roles[["name"]])) {
row_names = self$backend$data(self$backend$rownames, cols = new_roles[["name"]])
if (!is.character(row_names[[1L]]) && !is.factor(row_names[[1L]])) {
stopf("Assertion on '%s' failed: Must be of type 'character' or 'factor', not %s", names(row_names), class(row_names[[1]]))
}
}

if (inherits(self, "TaskSupervised")) {
if (length(new_roles$target) == 0L) {
stopf("Supervised tasks need at least one target column")
Expand Down
45 changes: 45 additions & 0 deletions R/set_fallback.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' @title Set a Fallback Learner
#'
#' @description
#' Set a fallback learner for a given learner.
#' The function searches for a suitable fallback learner based on the task type.
#' Additional checks are performed to ensure that the fallback learner supports the predict type.
#'
#' @param learner [Learner]\cr
#' The learner for which a fallback learner should be set.
#'
#' @return
#' Returns the learner itself, but modified **by reference**.
set_fallback = function(learner) {
assert_learner(learner)

# search for suitable fallback learner
fallback_id = mlr_reflections$learner_fallback[[learner$task_type]]

if (is.null(fallback_id)) {
stopf("No fallback learner available for task type '%s'.", learner$task_type)
}

fallback = lrn(fallback_id)

# set predict type
if (learner$predict_type %nin% fallback$predict_types) {
stopf("Fallback learner '%s' does not support predict type '%s'.", fallback_id, learner$predict_type)
}

fallback$predict_type = learner$predict_type

# set quantiles
if (learner$predict_type == "quantiles") {

if (is.null(learner$quantiles) || is.null(learner$quantile_response)) {
stopf("Cannot set quantiles for fallback learner. Set `$quantiles` and `$quantile_response` in %s.", learner$id)
}

fallback$quantiles = learner$quantiles
fallback$quantile_response = learner$quantile_response
}

learner$fallback = fallback
return(learner)
}
10 changes: 10 additions & 0 deletions R/worker.R
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ workhorse = function(iteration, task, learner, resampling, param_values = NULL,
old_blas_threads = RhpcBLASctl::blas_get_num_procs()
on.exit(RhpcBLASctl::blas_set_num_threads(old_blas_threads), add = TRUE)
RhpcBLASctl::blas_set_num_threads(1)
} else { # try the bare minimum to disable threading of the most popular blas implementations
old_blas = Sys.getenv("OPENBLAS_NUM_THREADS")
old_mkl = Sys.getenv("MKL_NUM_THREADS")
Sys.setenv(OPENBLAS_NUM_THREADS = 1)
Sys.setenv(MKL_NUM_THREADS = 1)

on.exit({
Sys.setenv(OPENBLAS_NUM_THREADS = old_blas)
Sys.setenv(MKL_NUM_THREADS = old_mkl)
}, add = TRUE)
}
}
# restore logger thresholds
Expand Down
2 changes: 2 additions & 0 deletions man-roxygen/section_parallelization.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
#' One job is one resampling iteration, and all jobs are send to an apply function
#' from \CRANpkg{future.apply} in a single batch.
#' To select a parallel backend, use [future::plan()].
#' More on parallelization can be found in the book:
#' \url{https://mlr3book.mlr-org.com/chapters/chapter10/advanced_technical_aspects_of_mlr3.html}
2 changes: 1 addition & 1 deletion man/Learner.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/benchmark.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/resample.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/set_fallback.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ reference:
- starts_with("mlr_learners")
- as_learner
- HotstartStack
- set_fallback
- title: Measures
contents:
- starts_with("mlr_measures")
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test_Task.R
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,25 @@ test_that("groups/weights work", {
}, "up to one")
})

test_that("col roles are valid", {
b = as_data_backend(data.table(
y = runif(20),
logical = sample(c(TRUE, FALSE), 20, replace = TRUE),
numeric = runif(20),
integer = sample(1:3, 20, replace = TRUE),
factor = factor(sample(letters[1:3], 20, replace = TRUE))))
task = TaskRegr$new("test", b, target = "y")

# weight
expect_error(task$set_col_roles("logical", roles = "weight"), "type")
expect_error(task$set_col_roles("factor", roles = "weight"), "type")

# name
expect_error(task$set_col_roles("logical", roles = "name"), "type")
expect_error(task$set_col_roles("integer", roles = "name"), "type")
expect_error(task$set_col_roles("numeric", roles = "name"), "type")
})

test_that("ordered factors (#95)", {
df = data.frame(
x = c(1, 2, 3),
Expand Down
29 changes: 29 additions & 0 deletions tests/testthat/test_set_fallback.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
test_that("set_fallback() works", {
learner = lrn("classif.rpart")
set_fallback(learner)

expect_class(learner, "LearnerClassifRpart")
expect_class(learner$fallback, "LearnerClassifFeatureless")
expect_equal(learner$fallback$predict_type, "response")

learner = lrn("classif.rpart", predict_type = "prob")
set_fallback(learner)

expect_class(learner, "LearnerClassifRpart")
expect_class(learner$fallback, "LearnerClassifFeatureless")
expect_equal(learner$fallback$predict_type, "prob")

learner = lrn("regr.rpart")
set_fallback(learner)

expect_class(learner, "LearnerRegrRpart")
expect_class(learner$fallback, "LearnerRegrFeatureless")
expect_equal(learner$fallback$predict_type, "response")

learner = lrn("regr.debug", predict_type = "se")
set_fallback(learner)

expect_class(learner, "LearnerRegrDebug")
expect_class(learner$fallback, "LearnerRegrFeatureless")
expect_equal(learner$fallback$predict_type, "se")
})

0 comments on commit 2deea95

Please sign in to comment.