Skip to content

Commit

Permalink
fix: cleanup after unloading + doc fixes (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebffischer authored Jan 5, 2023
1 parent f938c6b commit 195a4bb
Show file tree
Hide file tree
Showing 28 changed files with 136 additions and 31 deletions.
30 changes: 29 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,32 @@ Config/testthat/edition: 3
Encoding: UTF-8
NeedsCompilation: no
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
Collate:
'aaa.R'
'LearnerClassifCVGlmnet.R'
'LearnerClassifGlmnet.R'
'LearnerClassifKKNN.R'
'LearnerClassifLDA.R'
'LearnerClassifLogReg.R'
'LearnerClassifMultinom.R'
'LearnerClassifNaiveBayes.R'
'LearnerClassifNnet.R'
'LearnerClassifQDA.R'
'LearnerClassifRanger.R'
'LearnerClassifSVM.R'
'LearnerClassifXgboost.R'
'LearnerRegrCVGlmnet.R'
'LearnerRegrGlmnet.R'
'LearnerRegrKKNN.R'
'LearnerRegrKM.R'
'LearnerRegrLM.R'
'LearnerRegrNnet.R'
'LearnerRegrRanger.R'
'LearnerRegrSVM.R'
'LearnerRegrXgboost.R'
'bibentries.R'
'helpers.R'
'helpers_glmnet.R'
'helpers_ranger.R'
'zzz.R'
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# mlr3learners 0.5.4-9000
* fix: unloading `mlr3learners` removes learners from dictionary.

# mlr3learners 0.5.4

- Added `regr.nnet` learner.
Expand Down
3 changes: 3 additions & 0 deletions R/LearnerClassifCVGlmnet.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,6 @@ LearnerClassifCVGlmnet = R6Class("LearnerClassifCVGlmnet",
}
)
)

#' @include aaa.R
learners[["classif.cv_glmnet"]] = LearnerClassifCVGlmnet
3 changes: 3 additions & 0 deletions R/LearnerClassifGlmnet.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,6 @@ LearnerClassifGlmnet = R6Class("LearnerClassifGlmnet",
}
)
)

#' @include aaa.R
learners[["classif.glmnet"]] = LearnerClassifGlmnet
3 changes: 3 additions & 0 deletions R/LearnerClassifKKNN.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,6 @@ LearnerClassifKKNN = R6Class("LearnerClassifKKNN",
}
)
)

#' @include aaa.R
learners[["classif.kknn"]] = LearnerClassifKKNN
3 changes: 3 additions & 0 deletions R/LearnerClassifLDA.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ LearnerClassifLDA = R6Class("LearnerClassifLDA",
}
)
)

#' @include aaa.R
learners[["classif.lda"]] = LearnerClassifLDA
3 changes: 3 additions & 0 deletions R/LearnerClassifLogReg.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,6 @@ LearnerClassifLogReg = R6Class("LearnerClassifLogReg",
}
)
)

#' @include aaa.R
learners[["classif.log_reg"]] = LearnerClassifLogReg
3 changes: 3 additions & 0 deletions R/LearnerClassifMultinom.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,6 @@ LearnerClassifMultinom = R6Class("LearnerClassifMultinom",
}
)
)

#' @include aaa.R
learners[["classif.multinom"]] = LearnerClassifMultinom
3 changes: 3 additions & 0 deletions R/LearnerClassifNaiveBayes.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ LearnerClassifNaiveBayes = R6Class("LearnerClassifNaiveBayes",
}
)
)

#' @include aaa.R
learners[["classif.naive_bayes"]] = LearnerClassifNaiveBayes
3 changes: 3 additions & 0 deletions R/LearnerClassifNnet.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,6 @@ LearnerClassifNnet = R6Class("LearnerClassifNnet",
}
)
)

#' @include aaa.R
learners[["classif.nnet"]] = LearnerClassifNnet
3 changes: 3 additions & 0 deletions R/LearnerClassifQDA.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,6 @@ LearnerClassifQDA = R6Class("LearnerClassifQDA",
}
)
)

#' @include aaa.R
learners[["classif.qda"]] = LearnerClassifQDA
3 changes: 3 additions & 0 deletions R/LearnerClassifRanger.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,6 @@ default_values.LearnerClassifRanger = function(x, search_space, task, ...) { # n
defaults = insert_named(default_values(x$param_set), special_defaults)
defaults[search_space$ids()]
}

#' @include aaa.R
learners[["classif.ranger"]] = LearnerClassifRanger
3 changes: 3 additions & 0 deletions R/LearnerClassifSVM.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,6 @@ default_values.LearnerClassifSVM = function(x, search_space, task, ...) { # noli
defaults = insert_named(default_values(x$param_set), special_defaults)
defaults[search_space$ids()]
}

#' @include aaa.R
learners[["classif.svm"]] = LearnerClassifSVM
8 changes: 8 additions & 0 deletions R/LearnerClassifXgboost.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#' for binary classification problems and set to `"mlogloss"` for multiclass problems.
#' This was necessary to silence a deprecation warning.
#'
#' Note that using the `watchlist` parameter directly will lead to problems when wrapping this [`Learner`] in a
#' `mlr3pipelines` `GraphLearner` as the preprocessing steps will not be applied to the data in the watchlist.
#'
#' @template note_xgboost
#' @section Initial parameter values:
#' - `nrounds`:
Expand Down Expand Up @@ -232,6 +235,8 @@ LearnerClassifXgboost = R6Class("LearnerClassifXgboost",
pv$watchlist = c(pv$watchlist, list(train = data))
}

# the last element in the watchlist is used as the early stopping set

if (pv$early_stopping_set == "test" && !is.null(task$row_roles$test)) {
test_data = task$data(rows = task$row_roles$test, cols = task$feature_names)
test_label = nlvls - as.integer(task$truth(rows = task$row_roles$test))
Expand Down Expand Up @@ -307,3 +312,6 @@ LearnerClassifXgboost = R6Class("LearnerClassifXgboost",
}
)
)

#' @include aaa.R
learners[["classif.xgboost"]] = LearnerClassifXgboost
3 changes: 3 additions & 0 deletions R/LearnerRegrCVGlmnet.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,6 @@ LearnerRegrCVGlmnet = R6Class("LearnerRegrCVGlmnet",
}
)
)

#' @include aaa.R
learners[["regr.cv_glmnet"]] = LearnerRegrCVGlmnet
3 changes: 3 additions & 0 deletions R/LearnerRegrGlmnet.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,6 @@ LearnerRegrGlmnet = R6Class("LearnerRegrGlmnet",
}
)
)

#' @include aaa.R
learners[["regr.glmnet"]] = LearnerRegrGlmnet
3 changes: 3 additions & 0 deletions R/LearnerRegrKKNN.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,6 @@ LearnerRegrKKNN = R6Class("LearnerRegrKKNN",
}
)
)

#' @include aaa.R
learners[["regr.kknn"]] = LearnerRegrKKNN
3 changes: 3 additions & 0 deletions R/LearnerRegrKM.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,6 @@ LearnerRegrKM = R6Class("LearnerRegrKM",
}
)
)

#' @include aaa.R
learners[["regr.km"]] = LearnerRegrKM
3 changes: 3 additions & 0 deletions R/LearnerRegrLM.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@ LearnerRegrLM = R6Class("LearnerRegrLM",
}
)
)

#' @include aaa.R
learners[["regr.lm"]] = LearnerRegrLM
3 changes: 3 additions & 0 deletions R/LearnerRegrNnet.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,6 @@ LearnerRegrNnet = R6Class("LearnerRegrNnet",
}
)
)

#' @include aaa.R
learners[["regr.nnet"]] = LearnerRegrNnet
3 changes: 3 additions & 0 deletions R/LearnerRegrRanger.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,6 @@ default_values.LearnerRegrRanger = function(x, search_space, task, ...) { # noli
defaults = insert_named(default_values(x$param_set), special_defaults)
defaults[search_space$ids()]
}

#' @include aaa.R
learners[["regr.ranger"]] = LearnerRegrRanger
3 changes: 3 additions & 0 deletions R/LearnerRegrSVM.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@ default_values.LearnerRegrSVM = function(x, search_space, task, ...) { # nolint
defaults = insert_named(default_values(x$param_set), special_defaults)
defaults[search_space$ids()]
}

#' @include aaa.R
learners[["regr.svm"]] = LearnerRegrSVM
8 changes: 8 additions & 0 deletions R/LearnerRegrXgboost.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#' To compute on GPUs, you first need to compile \CRANpkg{xgboost} yourself and link
#' against CUDA. See \url{https://xgboost.readthedocs.io/en/stable/build.html#building-with-gpu-support}.
#'
#' Note that using the `watchlist` parameter directly will lead to problems when wrapping this [`Learner`] in a
#' `mlr3pipelines` `GraphLearner` as the preprocessing steps will not be applied to the data in the watchlist.
#'
#' @template note_xgboost
#' @inheritSection mlr_learners_classif.xgboost Early stopping
#' @inheritSection mlr_learners_classif.xgboost Initial parameter values
Expand Down Expand Up @@ -180,6 +183,8 @@ LearnerRegrXgboost = R6Class("LearnerRegrXgboost",
pv$watchlist = c(pv$watchlist, list(train = data))
}

# the last element in the watchlist is used as the early stopping set

if (pv$early_stopping_set == "test" && !is.null(task$row_roles$test)) {
test_data = task$data(rows = task$row_roles$test, cols = task$feature_names)
test_target = task$data(rows = task$row_roles$test, cols = task$target_names)
Expand Down Expand Up @@ -222,3 +227,6 @@ LearnerRegrXgboost = R6Class("LearnerRegrXgboost",
}
)
)

#' @include aaa.R
learners[["regr.xgboost"]] = LearnerRegrXgboost
1 change: 1 addition & 0 deletions R/aaa.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
learners = list()
32 changes: 8 additions & 24 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,21 @@
#' Feel invited to contribute a missing learner to the \CRANpkg{mlr3} ecosystem!
"_PACKAGE"

#' @include aaa.R
register_mlr3 = function() {
x = utils::getFromNamespace("mlr_learners", ns = "mlr3")

# classification learners
x$add("classif.cv_glmnet", LearnerClassifCVGlmnet)
x$add("classif.glmnet", LearnerClassifGlmnet)
x$add("classif.kknn", LearnerClassifKKNN)
x$add("classif.lda", LearnerClassifLDA)
x$add("classif.log_reg", LearnerClassifLogReg)
x$add("classif.multinom", LearnerClassifMultinom)
x$add("classif.naive_bayes", LearnerClassifNaiveBayes)
x$add("classif.nnet", LearnerClassifNnet)
x$add("classif.qda", LearnerClassifQDA)
x$add("classif.ranger", LearnerClassifRanger)
x$add("classif.svm", LearnerClassifSVM)
x$add("classif.xgboost", LearnerClassifXgboost)

# regression learners
x$add("regr.cv_glmnet", LearnerRegrCVGlmnet)
x$add("regr.glmnet", LearnerRegrGlmnet)
x$add("regr.kknn", LearnerRegrKKNN)
x$add("regr.km", LearnerRegrKM)
x$add("regr.lm", LearnerRegrLM)
x$add("regr.nnet", LearnerRegrNnet)
x$add("regr.ranger", LearnerRegrRanger)
x$add("regr.svm", LearnerRegrSVM)
x$add("regr.xgboost", LearnerRegrXgboost)
iwalk(learners, function(obj, nm) x$add(nm, obj))
}

.onLoad = function(libname, pkgname) { # nolint
register_namespace_callback(pkgname, "mlr3", register_mlr3)
} # nocov end

.onUnload = function(libpaths) { # nolint
mlr_learners = mlr3::mlr_learners

walk(names(learners), function(id) mlr_learners$remove(id))
}

leanify_package()
10 changes: 7 additions & 3 deletions man/mlr_learners_classif.xgboost.Rd

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

10 changes: 7 additions & 3 deletions man/mlr_learners_regr.xgboost.Rd

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

8 changes: 8 additions & 0 deletions tests/testthat/test_unload.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test_that("unloading leaves no trace", {
library(mlr3learners)
n_learners = length(learners)
n_total = length(mlr_learners$keys())
unloadNamespace("mlr3learners")
n_mlr = length(mlr_learners$keys())
expect_true(n_learners == n_total - n_mlr)
})

0 comments on commit 195a4bb

Please sign in to comment.