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

compatibility: mlr3 0.22.0 #124

Merged
merged 6 commits into from
Nov 6, 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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ Suggests:
mlr3learners,
mlr3pipelines,
rpart,
testthat (>= 3.0.0)
testthat (>= 3.0.0),
vctrs
Config/testthat/edition: 3
Config/testthat/parallel: true
Encoding: UTF-8
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# mlr3fselect (development version)

* compatibility: mlr3 0.22.0
* feat: Add internal tuning callback `mlr3fselect.internal_tuning`.

# mlr3fselect 1.2.0

* feat: Add internal tuning callback `mlr3fselect.internal_tuning`.
* fix: Register mlr3fselect in the `mlr_reflections$loaded_packages` field.

# mlr3fselect 1.1.1
Expand Down
8 changes: 6 additions & 2 deletions R/AutoFSelector.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#' @template param_check_values
#' @template param_callbacks
#' @template param_ties_method
#' @template param_id
#'
#' @export
#' @examples
Expand Down Expand Up @@ -123,7 +124,8 @@ AutoFSelector = R6Class("AutoFSelector",
store_models = FALSE,
check_values = FALSE,
callbacks = NULL,
ties_method = "least_features"
ties_method = "least_features",
id = NULL
) {
ia = list()
self$fselector = assert_r6(fselector, "FSelector")$clone()
Expand All @@ -141,8 +143,10 @@ AutoFSelector = R6Class("AutoFSelector",
ia$ties_method = assert_choice(ties_method, c("least_features", "random"))
self$instance_args = ia

id = assert_string(id, null.ok = TRUE) %??% paste0(learner$id, ".fselector")

super$initialize(
id = paste0(learner$id, ".fselector"),
id = id,
task_type = learner$task_type,
packages = c("mlr3fselect", learner$packages),
feature_types = learner$feature_types,
Expand Down
7 changes: 5 additions & 2 deletions R/auto_fselector.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#' @template param_check_values
#' @template param_callbacks
#' @template param_ties_method
#' @template param_id
#'
#' @export
#' @inherit AutoFSelector examples
Expand All @@ -36,7 +37,8 @@ auto_fselector = function(
store_models = FALSE,
check_values = FALSE,
callbacks = NULL,
ties_method = "least_features"
ties_method = "least_features",
id = NULL
) {
terminator = terminator %??% terminator_selection(term_evals, term_time)

Expand All @@ -51,5 +53,6 @@ auto_fselector = function(
store_models = store_models,
check_values = check_values,
callbacks = callbacks,
ties_method = ties_method)
ties_method = ties_method,
id = id)
}
4 changes: 2 additions & 2 deletions R/mlr_callbacks.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ load_callback_one_se_rule = function() {
label = "One Standard Error Rule Callback",
man = "mlr3fselect::mlr3fselect.one_se_rule",

on_result = function(callback, context) {
on_optimization_end = function(callback, context) {
archive = context$instance$archive
data = as.data.table(archive)
data[, "n_features" := map(get("features"), length)]
Expand Down Expand Up @@ -203,7 +203,7 @@ load_callback_internal_tuning = function() {
callback$state$internal_search_space$aggr_internal_tuned_values(internal_tuned_values)
})

data.table::set(context$aggregated_performance, j = "internal_tuned_values", value = list(internal_tuned_values))
data.table::set(context$aggregated_performance, j = "internal_tuned_values", value = list(internal_tuned_values))
},

on_optimization_end = function(callback, context) {
Expand Down
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

.onLoad = function(libname, pkgname) {
# nocov start
utils::globalVariables(c("super", "self", "n_features"))
utils::globalVariables(c("super", "self", "n_features"))

# reflections
x = utils::getFromNamespace("bbotk_reflections", ns = "bbotk")
Expand Down
6 changes: 5 additions & 1 deletion man/AutoFSelector.Rd

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

6 changes: 5 additions & 1 deletion man/auto_fselector.Rd

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

2 changes: 2 additions & 0 deletions tests/testthat/test_AutoFSelector.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ test_that("AutoFSelector get_base_learner method works", {

test_that("AutoFSelector hash works #647 in mlr3", {
afs_1 = AutoFSelector$new(
id = "afs_1",
learner = lrn("classif.rpart"),
resampling = rsmp("holdout"),
measure = msr("classif.ce"),
Expand All @@ -197,6 +198,7 @@ test_that("AutoFSelector hash works #647 in mlr3", {
store_benchmark_result = FALSE)

afs_2 = AutoFSelector$new(
id = "afs_2",
learner = lrn("classif.rpart"),
resampling = rsmp("holdout"),
measure = msr("classif.ce"),
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_FSelectorShadowVariableSearch.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test_that("second selected feature is a shadow variable works", {
expect_equal(instance$search_space, domain)
expect_equal(instance$objective$domain, domain)
# check that task is restored
expect_equal(instance$objective$task, task)
suppressWarnings(expect_equal(instance$objective$task, task))
})

test_that("search is terminated by terminator works", {
Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/test_extract_inner_fselect_archives.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ test_that("extract_inner_fselect_archives function works with resample and repea
})

test_that("extract_inner_fselect_archives function works with benchmark and cv", {
at_1 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_2 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_1 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_1")
at_2 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_2")
resampling_outer = rsmp("cv", folds = 2)
grid = benchmark_grid(tsk("iris"), list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = TRUE)
Expand All @@ -30,8 +30,8 @@ test_that("extract_inner_fselect_archives function works with benchmark and cv",
})

test_that("extract_inner_fselect_archives function works with benchmark and repeated cv", {
at_1 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_2 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_1 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_1")
at_2 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_2")
resampling_outer = rsmp("repeated_cv", folds = 2, repeats = 3)
grid = benchmark_grid(tsk("iris"), list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = TRUE)
Expand All @@ -43,8 +43,8 @@ test_that("extract_inner_fselect_archives function works with benchmark and repe
})

test_that("extract_inner_fselect_archives function works with multiple tasks", {
at_1 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_2 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_1 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_1")
at_2 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_2")
resampling_outer = rsmp("cv", folds = 2)
grid = benchmark_grid(list(tsk("iris"), tsk("pima")), list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = TRUE)
Expand Down Expand Up @@ -73,8 +73,8 @@ test_that("extract_inner_fselect_archives function works with no instance", {
})

test_that("extract_inner_fselect_archives function works with benchmark and no models", {
at_1 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_2 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_1 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_1")
at_2 = auto_fselector(fs("random_search", batch_size = 1), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_2")
resampling_outer = rsmp("cv", folds = 2)
grid = benchmark_grid(tsk("iris"), list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = FALSE)
Expand All @@ -84,9 +84,9 @@ test_that("extract_inner_fselect_archives function works with benchmark and no m

test_that("extract_inner_fselect_archives function works with mixed store instance", {
at_1 = AutoFSelector$new(lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), trm("evals", n_evals = 4),
fselector = fs("random_search", batch_size = 1), store_fselect_instance = FALSE, store_benchmark_result = FALSE)
fselector = fs("random_search", batch_size = 1), store_fselect_instance = FALSE, store_benchmark_result = FALSE, id = "at_1")
at_2 = AutoFSelector$new(lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), trm("evals", n_evals = 4),
fselector = fs("random_search", batch_size = 1))
fselector = fs("random_search", batch_size = 1), id = "at_2")
resampling_outer = rsmp("cv", folds = 2)
grid = benchmark_grid(tsk("iris"), list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = TRUE)
Expand Down
24 changes: 12 additions & 12 deletions tests/testthat/test_extract_inner_fselect_result.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ test_that("extract_inner_fselect_results function works with resample and repeat
})

test_that("extract_inner_fselect_results function works with benchmark and cv", {
at_1 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_2 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_1 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_1")
at_2 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_2")
resampling_outer = rsmp("cv", folds = 2)
grid = benchmark_grid(tsk("iris"), list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = TRUE)
Expand All @@ -30,8 +30,8 @@ test_that("extract_inner_fselect_results function works with benchmark and cv",
})

test_that("extract_inner_fselect_results function works with benchmark and repeated cv", {
at_1 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_2 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_1 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_1")
at_2 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_2")
resampling_outer = rsmp("repeated_cv", folds = 2, repeats = 3)
grid = benchmark_grid(tsk("iris"), list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = TRUE)
Expand All @@ -43,8 +43,8 @@ test_that("extract_inner_fselect_results function works with benchmark and repea
})

test_that("extract_inner_fselect_results function works with multiple tasks", {
at_1 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_2 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_1 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_1")
at_2 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_2")
resampling_outer = rsmp("cv", folds = 2)
grid = benchmark_grid(list(tsk("iris"), tsk("pima")), list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = TRUE)
Expand Down Expand Up @@ -73,8 +73,8 @@ test_that("extract_inner_fselect_results function works no instance", {
})

test_that("extract_inner_fselect_results function works with benchmark and no models", {
at_1 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_2 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_1 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_1")
at_2 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_2")
resampling_outer = rsmp("cv", folds = 2)
grid = benchmark_grid(tsk("iris"), list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = FALSE)
Expand All @@ -84,9 +84,9 @@ test_that("extract_inner_fselect_results function works with benchmark and no mo

test_that("extract_inner_fselect_results function works with mixed store instance", {
at_1 = AutoFSelector$new(lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), trm("evals", n_evals = 4),
fselector = fs("random_search"), store_fselect_instance = FALSE, store_benchmark_result = FALSE)
fselector = fs("random_search"), store_fselect_instance = FALSE, store_benchmark_result = FALSE, id = "at_1")
at_2 = AutoFSelector$new(lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), trm("evals", n_evals = 4),
fselector = fs("random_search"))
fselector = fs("random_search"), id = "at_2")
resampling_outer = rsmp("cv", folds = 2)
grid = benchmark_grid(tsk("iris"), list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = TRUE)
Expand Down Expand Up @@ -118,8 +118,8 @@ test_that("extract_inner_fselect_results function works with resample and return
})

test_that("extract_inner_fselect_results function works with benchmark and return of instance", {
at_1 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_2 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4)
at_1 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_1")
at_2 = auto_fselector(fs("random_search"), lrn("classif.rpart"), rsmp("holdout"), msr("classif.ce"), term_evals = 4, id = "at_2")
resampling_outer = rsmp("cv", folds = 2)
grid = benchmark_grid(tsk("iris"), list(at_1, at_2), resampling_outer)
bmr = benchmark(grid, store_models = TRUE)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test_fsi.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test_that("fsi and FSelectInstanceBatchSingleCrit are equal", {
instance_1 = FSelectInstanceBatchSingleCrit$new(task, learner, resampling, measures, terminator, store_benchmark_result, store_models, check_values, callbacks)
instance_2 = fsi(task, learner, resampling, measures, terminator, store_benchmark_result, store_models, check_values, callbacks)

expect_equal(instance_1, instance_2)
suppressWarnings(expect_equal(instance_1, instance_2))
})

test_that("fsi and FSelectInstanceBatchMultiCrit are equal", {
Expand All @@ -61,5 +61,5 @@ test_that("fsi and FSelectInstanceBatchMultiCrit are equal", {
instance_1 = FSelectInstanceBatchMultiCrit$new(task, learner, resampling, measures, terminator, store_benchmark_result, store_models, check_values, callbacks)
instance_2 = fsi(task, learner, resampling, measures, terminator, store_benchmark_result, store_models, check_values, callbacks)

expect_equal(instance_1, instance_2)
suppressWarnings(expect_equal(instance_1, instance_2))
})
Loading