From 8aa4b1c2e8f2530947a409b057f5024de177aaf4 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Fri, 20 Oct 2023 16:54:57 +0200 Subject: [PATCH 1/4] feat: respect prototype arguments in dict -> dt conversion Previously, when adding learners with construction arguments that do not have defaults (such exist in `mlr3torch`), the `Dictionary` -> `data.table` conversion stopped working. --- DESCRIPTION | 2 ++ NEWS.md | 2 ++ R/mlr_learners.R | 2 +- tests/testthat/test_Dictionary.R | 23 +++++++++++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 716ed3891..8edb30832 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -68,6 +68,8 @@ Suggests: remotes, rpart, testthat (>= 3.1.0) +Remotes: + mlr-org/mlr3misc@feat/dict-prototype Encoding: UTF-8 Config/testthat/edition: 3 Config/testthat/parallel: false diff --git a/NEWS.md b/NEWS.md index dde56f9b4..533834a64 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # mlr3 0.16.1-9000 +* feat: dictionary conversion of `mlr_learners` respects prototype arguments +recently added in mlr3i * export generic function `col_info` to allow adding new methods for backends * Add `"mlr3.exec_chunk_bins"` option to split the resampling iterations into a number of bins. diff --git a/R/mlr_learners.R b/R/mlr_learners.R index c544ea25e..4ac19a432 100644 --- a/R/mlr_learners.R +++ b/R/mlr_learners.R @@ -45,7 +45,7 @@ as.data.table.DictionaryLearner = function(x, ..., objects = FALSE) { assert_flag(objects) setkeyv(map_dtr(x$keys(), function(key) { - l = withCallingHandlers(x$get(key), + l = withCallingHandlers(x$get(key, .prototype = TRUE), packageNotFoundWarning = function(w) invokeRestart("muffleWarning")) insert_named( list(key = key, label = l$label, task_type = l$task_type, feature_types = list(l$feature_types), packages = list(l$packages), diff --git a/tests/testthat/test_Dictionary.R b/tests/testthat/test_Dictionary.R index 98402030f..2624ea077 100644 --- a/tests/testthat/test_Dictionary.R +++ b/tests/testthat/test_Dictionary.R @@ -9,3 +9,26 @@ test_that("Dictionary: clone works", { test_that("$keys(pattern) works", { expect_subset(mlr_learners$keys("classif"), mlr_learners$keys(), empty.ok = FALSE) }) + +test_that("dictionary to data.table conversion works with prototype arguments", { + LearnerRegrRpart2 = R6Class("LearnerRegrRpart2", + inherit = LearnerRegrRpart, + public = list( + x = NULL, + initialize = function(x) { + self$x = x + super$initialize() + } + ) + ) + on.exit(mlr_learners$remove("regr.rpart2")) + mlr_learners$add("regr.rpart2", LearnerRegrRpart2, .prototype_args = list(x = 123)) + + dt = as.data.table(mlr_learners) + expect_data_table(dt) + + expect_identical( + dt[list("regr.rpart"), -1, on = "key"], + dt[list("regr.rpart2"), -1, on = "key"] + ) +}) From 329dbbb361308f420118e3db994bbc35be8a949f Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Wed, 25 Oct 2023 13:58:36 +0200 Subject: [PATCH 2/4] Update NEWS.md --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 533834a64..efe361d3b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,7 @@ # mlr3 0.16.1-9000 * feat: dictionary conversion of `mlr_learners` respects prototype arguments -recently added in mlr3i +recently added in mlr3misc * export generic function `col_info` to allow adding new methods for backends * Add `"mlr3.exec_chunk_bins"` option to split the resampling iterations into a number of bins. From cfe469f169275d2e028580863bddc7d320d4d577 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Wed, 7 Feb 2024 17:00:08 +0100 Subject: [PATCH 3/4] deps: bump misc version --- DESCRIPTION | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b5041329f..bb7badd18 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -52,7 +52,7 @@ Imports: lgr (>= 0.3.4), mlbench, mlr3measures (>= 0.4.1), - mlr3misc (>= 0.12.0), + mlr3misc (>= 0.14.0), parallelly, palmerpenguins, paradox (>= 0.10.0), @@ -69,8 +69,6 @@ Suggests: remotes, rpart, testthat (>= 3.1.0) -Remotes: - mlr-org/mlr3misc@feat/dict-prototype Encoding: UTF-8 Config/testthat/edition: 3 Config/testthat/parallel: false From 84f2fd19a531ded705e80ce2cf633f63208982b0 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Wed, 7 Feb 2024 19:08:36 +0100 Subject: [PATCH 4/4] tests: simplify test --- tests/testthat/test_Dictionary.R | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/testthat/test_Dictionary.R b/tests/testthat/test_Dictionary.R index 2624ea077..67a76ae37 100644 --- a/tests/testthat/test_Dictionary.R +++ b/tests/testthat/test_Dictionary.R @@ -24,11 +24,5 @@ test_that("dictionary to data.table conversion works with prototype arguments", on.exit(mlr_learners$remove("regr.rpart2")) mlr_learners$add("regr.rpart2", LearnerRegrRpart2, .prototype_args = list(x = 123)) - dt = as.data.table(mlr_learners) - expect_data_table(dt) - - expect_identical( - dt[list("regr.rpart"), -1, on = "key"], - dt[list("regr.rpart2"), -1, on = "key"] - ) + expect_data_table(as.data.table(mlr_learners)) })