Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: default_fallback function #1151

Merged
merged 5 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Collate:
'benchmark.R'
'benchmark_grid.R'
'bibentries.R'
'default_fallback.R'
'default_measures.R'
'fix_factor_levels.R'
'helper.R'
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ S3method(col_info,DataBackend)
S3method(col_info,data.table)
S3method(create_empty_prediction_data,TaskClassif)
S3method(create_empty_prediction_data,TaskRegr)
S3method(default_fallback,Learner)
S3method(default_fallback,LearnerClassif)
S3method(default_fallback,LearnerRegr)
S3method(default_values,Learner)
S3method(default_values,LearnerClassifRpart)
S3method(default_values,LearnerRegrRpart)
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: Create default fallback learner with `default_fallback()`.
* feat: Check column roles when using `$set_col_roles()` and `$col_roles`.

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

# if there is no fallback, we get a default one
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)
fallback = default_fallback(self)
if (!is.null(fallback)) {
self$fallback = fallback
}
}
},
Expand Down
64 changes: 64 additions & 0 deletions R/default_fallback.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#' @title Create a Fallback Learner
#'
#' @description
#' Create 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 created.
#' @param ... `any`\cr
#' ignored.
#'
#' @return [Learner]
default_fallback = function(learner, ...) {
UseMethod("default_fallback")
}

#' @rdname default_fallback
#' @export
default_fallback.Learner = function(learner, ...) {
# FIXME: remove when new encapsulate/fallback system is in place
return(NULL)
}

#' @rdname default_fallback
#' @export
default_fallback.LearnerClassif = function(learner, ...) {
fallback = lrn("classif.featureless")

# 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

return(fallback)
}

#' @rdname default_fallback
#' @export
default_fallback.LearnerRegr = function(learner, ...) {
fallback = lrn("regr.featureless")

# 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
}

return(fallback)
}
5 changes: 0 additions & 5 deletions R/mlr_reflections.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ local({
regr = list(response = "response", se = c("response", "se"), quantiles = c("response", "quantiles"), distr = c("response", "se", "distr"))
)

mlr_reflections$learner_fallback = list(
classif = "classif.featureless",
regr = "regr.featureless"
)

# Allowed tags for parameters
mlr_reflections$learner_param_tags = c("train", "predict", "hotstart", "importance", "threads", "required", "internal_tuning")

Expand Down
32 changes: 32 additions & 0 deletions man/default_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
- default_fallback
- title: Measures
contents:
- starts_with("mlr_measures")
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test_set_fallback.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
test_that("fallback = default_fallback() works", {
learner = lrn("classif.rpart")
fallback = default_fallback(learner)

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

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

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

learner = lrn("regr.rpart")
fallback = default_fallback(learner)

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

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

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