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

[R-package] remove support for '...' in slice() #4872

Merged
merged 3 commits into from
Dec 10, 2021
Merged
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
41 changes: 5 additions & 36 deletions R-package/R/lgb.Dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -532,49 +532,19 @@ Dataset <- R6::R6Class(
},

# Slice dataset
slice = function(idxset, ...) {

additional_keyword_args <- list(...)

if (length(additional_keyword_args) > 0L) {
warning(paste0(
"Dataset$slice(): Found the following passed through '...': "
, 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$set_field(). "
, "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
}
slice = function(idxset) {

# Perform slicing
return(
Dataset$new(
data = NULL
, params = utils::modifyList(self$get_params(), additional_keyword_args)
, params = self$get_params()
, reference = self
, colnames = private$colnames
, categorical_feature = private$categorical_feature
, predictor = private$predictor
, free_raw_data = private$free_raw_data
, used_indices = sort(idxset, decreasing = FALSE)
, group = group
, init_score = init_score
, label = label
, weight = weight
)
)

Expand Down Expand Up @@ -1068,7 +1038,6 @@ dimnames.lgb.Dataset <- function(x) {
#' original \code{lgb.Dataset} object
#' @param dataset Object of class \code{lgb.Dataset}
#' @param idxset an integer vector of indices of rows needed
#' @param ... other parameters (currently not used)
#' @return constructed sub dataset
#'
#' @examples
Expand All @@ -1082,19 +1051,19 @@ dimnames.lgb.Dataset <- function(x) {
#' labels <- lightgbm::get_field(dsub, "label")
#' }
#' @export
slice <- function(dataset, ...) {
slice <- function(dataset, idxset) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for reviewers wondering how it is ok to remove this ... when it wasn't for predict.lgb.Booster() (#4857 (comment))... this situation is different because slice() is not a generic exported from base R.

UseMethod("slice")
}

#' @rdname slice
#' @export
slice.lgb.Dataset <- function(dataset, idxset, ...) {
slice.lgb.Dataset <- function(dataset, idxset) {

if (!lgb.is.Dataset(x = dataset)) {
stop("slice.lgb.Dataset: input dataset should be an lgb.Dataset object")
}

return(invisible(dataset$slice(idxset = idxset, ...)))
return(invisible(dataset$slice(idxset = idxset)))

}

Expand Down
6 changes: 2 additions & 4 deletions R-package/man/slice.Rd

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

28 changes: 0 additions & 28 deletions R-package/tests/testthat/test_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,6 @@ 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$get_field("init_score"), NULL)
expect_identical(dsub1$get_field("init_score"), init_score)
})

test_that("Dataset$set_reference() on a constructed Dataset fails if raw data has been freed", {
dtrain <- lgb.Dataset(train_data, label = train_label)
dtrain$construct()
Expand Down