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

Converters for Resampling <-> data.table #1162

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ S3method(as_resample_result,ResampleResult)
S3method(as_resample_result,ResultData)
S3method(as_resample_result,list)
S3method(as_resampling,Resampling)
S3method(as_resampling,data.table)
S3method(as_resamplings,default)
S3method(as_resamplings,list)
S3method(as_task,Task)
Expand Down
11 changes: 11 additions & 0 deletions R/Resampling.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@
#' Next, the grouping information is replaced with the respective row ids to generate training and test sets.
#' The sets can be accessed via `$train_set(i)` and `$test_set(i)`, respectively.
#'
#' @section Conversion to and from Tabular Data:
#' Resamplings can be converted to a plain `data.table` with `as.data.table()`, resulting in a table with the
#' following columns:
#'
#' * `set`: factor with levels `"train"` and `"test"`
#' * `iteration`: non-negative integer
#' * `row_id`: integer row ids
#'
#' This table can serve as exchange format with other toolkits. A table with this exact structure can also be
#' converted to a [ResamplingCustom] again via [as_resampling()]. Note that while the training and test sets are
#' preserved for all iterations, the elements inside the set may be reordered.
#'
#' @template seealso_resampling
#' @export
Expand Down
26 changes: 26 additions & 0 deletions R/as_resampling.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#' @description
#' Convert object to a [Resampling] or a list of [Resampling].
#'
#' For the conversion of a `data.table`, the following columns (as returned by
#' [as.data.table.Resampling()]) are mandatory to create a [ResamplingCustom]:
#' * `set`: factor with levels `"train"` and `"test"`
#' * `iteration`: non-negative integer
#' * `row_id`: integer row ids
#'
#' @inheritParams as_task
#' @export
as_resampling = function(x, ...) { # nolint
Expand All @@ -15,6 +21,26 @@ as_resampling.Resampling = function(x, clone = FALSE, ...) { # nolint
if (isTRUE(clone)) x$clone() else x
}


#' @export
#' @rdname as_resampling
as_resampling.data.table = function(x, ...) { # nolint
assert_data_table(x, min.rows = 1L, col.names = "unique")
assert_names(names(x), permutation.of = c("set", "iteration", "row_id"))
assert_factor(x$set, any.missing = FALSE)
assert_set_equal(levels(x$set), c("train", "test"))
assert_integerish(x$iteration, lower = 1L, any.missing = FALSE)
assert_integerish(x$row_id, any.missing = FALSE)

row_id = NULL
resampling = ResamplingCustom$new()
resampling$instance = list(
train_sets = x[list("train"), list(ids = list(row_id)), by = "iteration"]$ids,
test_sets = x[list("test"), list(ids = list(row_id)), by = "iteration"]$ids
)
resampling
}

#' @export
#' @rdname as_resampling
as_resamplings = function(x, ...) { # nolint
Expand Down
15 changes: 15 additions & 0 deletions man/Resampling.Rd

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

11 changes: 11 additions & 0 deletions man/as_resampling.Rd

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

7 changes: 7 additions & 0 deletions tests/testthat/test_Resampling.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ test_that("as.data.table.Resampling", {
expect_integer(tab$iteration, any.missing = FALSE)
expect_factor(tab$set, levels = c("train", "test"), any.missing = FALSE)
expect_integer(tab$row_id, any.missing = FALSE)

r2 = as_resampling(tab)
expect_r6(r2, "ResamplingCustom")
for (i in seq_len(r$iters)) {
expect_set_equal(r$train_set(i), r2$train_set(i))
expect_set_equal(r$test_set(i), r2$test_set(i))
}
})

test_that("custom_cv", {
Expand Down
Loading