mlr-org / mlr3misc

Miscellaneous helper functions for mlr3
https://mlr3misc.mlr-org.com
GNU Lesser General Public License v3.0
11 stars 2 forks source link

Leanification fails with installing package with `--with-keep.source` #88

Closed sebffischer closed 11 months ago

sebffischer commented 11 months ago

The object size below depends on how many packages are loaded before

library(mlr3)
library(mlr3verse)
library(data.table)

task = tsk("iris")
learner = lrn("classif.rpart")

learner$fallback = lrn("classif.featureless")

learner$train(task)

pth = tempfile(fileext = ".rds")

saveRDS(learner$state, pth)

x = readRDS(pth)

pryr::object_size(x)
#> 19.78 MB

Created on 2023-09-13 with reprex v2.0.2

sebffischer commented 11 months ago
library(mlr3)

l = lrn("classif.rpart")
unlockBinding("train", l)

attributes(l$train)
#> $srcref
#> function(task, row_ids = NULL) {
#>       task = assert_task(as_task(task))
#>       assert_learnable(task, self)
#>       row_ids = assert_row_ids(row_ids, null.ok = TRUE)
#> 
#>       if (!is.null(self$hotstart_stack)) {
#>         # search for hotstart learner
#>         start_learner = get_private(self$hotstart_stack)$.start_learner(self, task$hash)
#>       }
#>       if (is.null(self$hotstart_stack) || is.null(start_learner)) {
#>          # no hotstart learners stored or no adaptable model found
#>         learner = self
#>         mode = "train"
#>       } else {
#>         self$state = start_learner$clone()$state
#>         learner = self
#>         mode = "hotstart"
#>       }
#> 
#>       train_row_ids = if (!is.null(row_ids)) row_ids else task$row_roles$use
#>       test_row_ids = task$row_roles$test
#> 
#>       learner_train(learner, task, train_row_ids = train_row_ids, test_row_ids = test_row_ids, mode = mode)
#> 
#>       # store the task w/o the data
#>       self$state$train_task = task_rm_backend(task$clone(deep = TRUE))
#> 
#>       invisible(self)
#>     }
l$train = removeSource(lrn("classif.rpart")$train)
l$train
#> function (task, row_ids = NULL) 
#> .__Learner__train(self = self, private = private, super = super, 
#>     task = task, row_ids = row_ids)
#> <environment: 0x55ea67e47dd8>

Created on 2023-09-14 with reprex v2.0.2

mllg commented 11 months ago

Fixed via #89