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 Dataset setinfo() #4854

Merged
merged 6 commits into from
Dec 6, 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
2 changes: 0 additions & 2 deletions R-package/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ S3method(getinfo,lgb.Dataset)
S3method(predict,lgb.Booster)
S3method(print,lgb.Booster)
S3method(set_field,lgb.Dataset)
S3method(setinfo,lgb.Dataset)
S3method(slice,lgb.Dataset)
S3method(summary,lgb.Booster)
export(get_field)
Expand Down Expand Up @@ -39,7 +38,6 @@ export(lightgbm)
export(readRDS.lgb.Booster)
export(saveRDS.lgb.Booster)
export(set_field)
export(setinfo)
export(slice)
import(methods)
importFrom(Matrix,Matrix)
Expand Down
78 changes: 1 addition & 77 deletions R-package/R/lgb.Dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -529,19 +529,6 @@ Dataset <- R6::R6Class(

},

setinfo = function(name, info) {
warning(paste0(
"Dataset$setinfo() is deprecated and will be removed in a future release. "
, "Use Dataset$set_field() instead."
))
return(
self$set_field(
field_name = name
, data = info
)
)
},

set_field = function(field_name, data) {

# Check if attribute key is in the known attribute list
Expand Down Expand Up @@ -1200,7 +1187,7 @@ slice.lgb.Dataset <- function(dataset, idxset, ...) {
#' lgb.Dataset.construct(dtrain)
#'
#' labels <- lightgbm::getinfo(dtrain, "label")
#' lightgbm::setinfo(dtrain, "label", 1 - labels)
#' lightgbm::set_field(dtrain, "label", 1 - labels)
#'
#' labels2 <- lightgbm::getinfo(dtrain, "label")
#' stopifnot(all(labels2 == 1 - labels))
Expand Down Expand Up @@ -1234,69 +1221,6 @@ getinfo.lgb.Dataset <- function(dataset, name, ...) {

}

#' @name setinfo
#' @title Set information of an \code{lgb.Dataset} object
#' @description Set one attribute of a \code{lgb.Dataset}
#' @param dataset Object of class \code{lgb.Dataset}
#' @param name the name of the field to get
#' @param info the specific field of information to set
#' @param ... other parameters (ignored)
#' @return the dataset you passed in
#'
#' @details
#' The \code{name} field can be one of the following:
#'
#' \itemize{
#' \item{\code{label}: vector of labels to use as the target variable}
#' \item{\code{weight}: to do a weight rescale}
#' \item{\code{init_score}: initial score is the base prediction lightgbm will boost from}
#' \item{\code{group}: used for learning-to-rank tasks. An integer vector describing how to
#' group rows together as ordered results from the same set of candidate results to be ranked.
#' For example, if you have a 100-document dataset with \code{group = c(10, 20, 40, 10, 10, 10)},
#' that means that you have 6 groups, where the first 10 records are in the first group,
#' records 11-30 are in the second group, etc.}
#' }
#'
#' @examples
#' \donttest{
#' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train
#' dtrain <- lgb.Dataset(train$data, label = train$label)
#' lgb.Dataset.construct(dtrain)
#'
#' labels <- lightgbm::getinfo(dtrain, "label")
#' lightgbm::setinfo(dtrain, "label", 1 - labels)
#'
#' labels2 <- lightgbm::getinfo(dtrain, "label")
#' stopifnot(all.equal(labels2, 1 - labels))
#' }
#' @export
setinfo <- function(dataset, ...) {
UseMethod("setinfo")
}

#' @rdname setinfo
#' @export
setinfo.lgb.Dataset <- function(dataset, name, info, ...) {

warning("Calling setinfo() on a lgb.Dataset is deprecated. Use set_field() instead.")

additional_args <- list(...)
if (length(additional_args) > 0L) {
warning(paste0(
"setinfo.lgb.Dataset: Found the following passed through '...': "
, paste(names(additional_args), collapse = ", ")
, ". These are ignored. In future releases of lightgbm, this warning will become an error. "
, "See ?setinfo.lgb.Dataset for documentation on how to call this function."
))
}

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

return(invisible(dataset$set_field(field_name = name, data = info)))
}

#' @name get_field
#' @title Get one attribute of a \code{lgb.Dataset}
Expand Down
2 changes: 1 addition & 1 deletion R-package/man/getinfo.Rd

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

54 changes: 0 additions & 54 deletions R-package/man/setinfo.Rd

This file was deleted.

19 changes: 3 additions & 16 deletions R-package/tests/testthat/test_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ test_that("lgb.Dataset: basic construction, saving, loading", {
expect_equal(get_field(dtest1, "label"), get_field(dtest3, "label"))
})

test_that("lgb.Dataset: getinfo & setinfo", {
test_that("lgb.Dataset: getinfo", {
dtest <- lgb.Dataset(test_data)
dtest$construct()

setinfo(dtest, "label", test_label)
set_field(dtest, "label", test_label)
labels <- getinfo(dtest, "label")
expect_equal(test_label, getinfo(dtest, "label"))

expect_true(length(getinfo(dtest, "weight")) == 0L)
expect_true(length(getinfo(dtest, "init_score")) == 0L)

# any other label should error
expect_error(setinfo(dtest, "asdf", test_label))
expect_error(set_field(dtest, "asdf", test_label))
})

test_that("lgb.Dataset: get_field & set_field", {
Expand Down Expand Up @@ -259,19 +259,6 @@ test_that("cpp errors should be raised as proper R errors", {
}, regexp = "Initial score size doesn't match data size")
})

test_that("lgb.Dataset$setinfo() should convert 'group' to integer", {
ds <- lgb.Dataset(
data = matrix(rnorm(100L), nrow = 50L, ncol = 2L)
, label = sample(c(0L, 1L), size = 50L, replace = TRUE)
)
ds$construct()
current_group <- ds$getinfo("group")
expect_null(current_group)
group_as_numeric <- rep(25.0, 2L)
ds$setinfo("group", group_as_numeric)
expect_identical(ds$getinfo("group"), as.integer(group_as_numeric))
})

test_that("lgb.Dataset$set_field() should convert 'group' to integer", {
ds <- lgb.Dataset(
data = matrix(rnorm(100L), nrow = 50L, ncol = 2L)
Expand Down
7 changes: 5 additions & 2 deletions docs/FAQ.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,11 @@ This is a known issue: `Microsoft/LightGBM#698 <https://github.com/microsoft/Lig
2. I used ``setinfo()``, tried to print my ``lgb.Dataset``, and now the R console froze!
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah ok, yes I agree with you. Fixed in 0770c94.

I checked on v3.3.0 and latest master, and there are no longer issues printing a Dataset (maybe as a result of work in #3016 ).

library(lightgbm)

data(agaricus.train, package = "lightgbm")
train <- agaricus.train
dtrain <- lgb.Dataset(train$data, label = train$label)
print(dtrain)
dtrain$construct()
print(dtrain)
dtrain$setinfo("weight", rep(1.0, nrow(train$data)))
print(dtrain)

----------------------------------------------------------------------------------------

Avoid printing the ``lgb.Dataset`` after using ``setinfo``.
This is a known bug: `Microsoft/LightGBM#539 <https://github.com/microsoft/LightGBM/issues/539>`__.
As of at least LightGBM v3.3.0, this issue has been resolved and printing a ``Dataset`` object does not cause the console to freeze.

In older versions, avoid printing the ``Dataset`` after calling ``setinfo()``.

As of LightGBM v4.0.0, ``setinfo()`` has been replaced by a new method, ``set_field()``.

3. ``error in data.table::data.table()...argument 2 is NULL``
-------------------------------------------------------------
Expand Down