Skip to content

Commit

Permalink
fix(task): don't call $data() unnecessarily (#1262)
Browse files Browse the repository at this point in the history
* fix(task): don't call $data() unnecessarily

For tasks in mlr3torch, `$data()` can cause downloads
of large files, but it should be possible to construct
tasks without downloading the data (e.g. for
as.data.table(mlr_tasks)).

* add test
  • Loading branch information
sebffischer authored Feb 13, 2025
1 parent 0213604 commit b534950
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/Task.R
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ task_check_col_roles.Task = function(task, new_roles, ...) {
stopf("Offset column(s) %s must be a numeric or integer column", paste0("'", new_roles[["offset"]], "'", collapse = ","))
}

if (any(task$missings(cols = new_roles[["offset"]]) > 0)) {
if (length(new_roles[["offset"]]) && any(task$missings(cols = new_roles[["offset"]]) > 0)) {
missings = task$missings(cols = new_roles[["offset"]])
missings = names(missings[missings > 0])
stopf("Offset column(s) %s contain missing values", paste0("'", missings, "'", collapse = ","))
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test_Task.R
Original file line number Diff line number Diff line change
Expand Up @@ -731,3 +731,17 @@ test_that("warn when internal valid task has 0 obs", {
expect_warning({task$internal_valid_task = 151}, "has 0 observations")
})


test_that("$data() is not called during task construction", {
tbl = data.table(x = 1:10, y = 1:10, ..row_id = 1:10)
DataBackendTest = R6Class("DataBackendTest",
inherit = DataBackendDataTable,
cloneable = FALSE,
public = list(
data = function(rows, cols, data_format) {
stop("Bug")
}
)
)$new(tbl, "..row_id")
expect_task(as_task_regr(tbl, target = "y"))
})

0 comments on commit b534950

Please sign in to comment.