Unverified Commit e0d93b5d authored by James Lamb's avatar James Lamb Committed by GitHub
Browse files

[R-package] preserve uses of '...' in Dataset slice() method (#4581)



* [R-package] preserve uses of '...' in Dataset slice() method

* Update R-package/R/lgb.Dataset.R
Co-authored-by: default avatarNikita Titov <nekit94-08@mail.ru>
Co-authored-by: default avatarNikita Titov <nekit94-08@mail.ru>
parent a0d227a1
......@@ -546,21 +546,37 @@ Dataset <- R6::R6Class(
# Slice dataset
slice = function(idxset, ...) {
additional_params <- list(...)
if (length(additional_params) > 0L) {
additional_keyword_args <- list(...)
if (length(additional_keyword_args) > 0L) {
warning(paste0(
"Dataset$slice(): Found the following passed through '...': "
, paste(names(additional_params), collapse = ", ")
, paste(names(additional_keyword_args), collapse = ", ")
, ". These are ignored and should be removed. "
, "To change the parameters of a Dataset produced by Dataset$slice(), use Dataset$set_params(). "
, "To modify attributes like 'init_score', use Dataset$setinfo(). "
, "In future releases of lightgbm, this warning will become an error."
))
}
# extract Dataset attributes passed through '...'
#
# NOTE: takes advantage of the fact that list[["non-existent-key"]] returns NULL
group <- additional_keyword_args[["group"]]
init_score <- additional_keyword_args[["init_score"]]
label <- additional_keyword_args[["label"]]
weight <- additional_keyword_args[["weight"]]
# remove attributes from '...', so only params are left
for (info_key in .INFO_KEYS()) {
additional_keyword_args[[info_key]] <- NULL
}
# Perform slicing
return(
Dataset$new(
data = NULL
, params = private$params
, params = utils::modifyList(self$get_params(), additional_keyword_args)
, reference = self
, colnames = private$colnames
, categorical_feature = private$categorical_feature
......@@ -568,7 +584,10 @@ Dataset <- R6::R6Class(
, free_raw_data = private$free_raw_data
, used_indices = sort(idxset, decreasing = FALSE)
, info = NULL
, ...
, group = group
, init_score = init_score
, label = label
, weight = weight
)
)
......
......@@ -46,6 +46,34 @@ test_that("lgb.Dataset: slice, dim", {
expect_equal(ncol(dsub1), ncol(test_data))
})
test_that("Dataset$slice() supports passing additional parameters through '...'", {
dtest <- lgb.Dataset(test_data, label = test_label)
dtest$construct()
dsub1 <- slice(
dataset = dtest
, idxset = seq_len(42L)
, feature_pre_filter = FALSE
)
dsub1$construct()
expect_identical(dtest$get_params(), list())
expect_identical(dsub1$get_params(), list(feature_pre_filter = FALSE))
})
test_that("Dataset$slice() supports passing Dataset attributes through '...'", {
dtest <- lgb.Dataset(test_data, label = test_label)
dtest$construct()
num_subset_rows <- 51L
init_score <- rnorm(n = num_subset_rows)
dsub1 <- slice(
dataset = dtest
, idxset = seq_len(num_subset_rows)
, init_score = init_score
)
dsub1$construct()
expect_null(dtest$getinfo("init_score"), NULL)
expect_identical(dsub1$getinfo("init_score"), init_score)
})
test_that("lgb.Dataset: colnames", {
dtest <- lgb.Dataset(test_data, label = test_label)
expect_equal(colnames(dtest), colnames(test_data))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment