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] respect 'verbose' argument in lgb.cv() (fixes #4667) #4903

Merged
merged 11 commits into from
Jan 6, 2022
17 changes: 12 additions & 5 deletions R-package/R/lgb.Dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Dataset <- R6::R6Class(
params = list()) {

# the Dataset's existing parameters should be overwritten by any passed in to this call
params <- modifyList(self$get_params(), params)
params <- modifyList(private$params, params)

# Create new dataset
ret <- Dataset$new(
Expand Down Expand Up @@ -535,7 +535,7 @@ Dataset <- R6::R6Class(
return(
Dataset$new(
data = NULL
, params = self$get_params()
, params = private$params
, reference = self
, colnames = private$colnames
, categorical_feature = private$categorical_feature
Expand All @@ -554,15 +554,17 @@ Dataset <- R6::R6Class(
if (length(params) == 0L) {
return(invisible(self))
}
new_params <- utils::modifyList(private$params, params)
if (lgb.is.null.handle(x = private$handle)) {
private$params <- utils::modifyList(private$params, params)
private$params <- new_params
} else {
tryCatch({
.Call(
LGBM_DatasetUpdateParamChecking_R
, lgb.params2str(params = private$params)
, lgb.params2str(params = params)
, lgb.params2str(params = new_params)
)
private$params <- new_params
}, error = function(e) {
# If updating failed but raw data is not available, raise an error because
# achieving what the user asked for is not possible
Expand All @@ -572,14 +574,19 @@ Dataset <- R6::R6Class(

# If updating failed but raw data is available, modify the params
# on the R side and re-set ("deconstruct") the Dataset
private$params <- utils::modifyList(private$params, params)
private$params <- new_params
self$finalize()
})
}
return(invisible(self))

},

# [description] Get only Dataset-specific parameters. This is primarily used by
# Booster to update its parameters based on the characteristics of
# a Dataset. It should not be used by other methods in this class,
# since "verbose" is not a Dataset parameter and needs to be passed
# through to avoid globally re-setting verbosity.
get_params = function() {
dataset_params <- unname(unlist(.DATASET_PARAMETERS()))
ret <- list()
Expand Down
50 changes: 50 additions & 0 deletions R-package/tests/testthat/test_basic.R
Original file line number Diff line number Diff line change
Expand Up @@ -2062,6 +2062,56 @@ test_that("early stopping works with lgb.cv()", {
)
})

test_that("lgb.cv() respects changes to logging verbosity", {
dtrain <- lgb.Dataset(
data = train$data
, label = train$label
)
# (verbose = 1) should be INFO and WARNING level logs
lgb_cv_logs <- capture.output({
cv_bst <- lgb.cv(
params = list()
, nfold = 2L
, nrounds = 5L
, data = dtrain
, obj = "binary"
, verbose = 1L
)
})
expect_true(any(grepl("\\[LightGBM\\] \\[Info\\]", lgb_cv_logs)))
expect_true(any(grepl("\\[LightGBM\\] \\[Warning\\]", lgb_cv_logs)))

# (verbose = 0) should be WARNING level logs only
lgb_cv_logs <- capture.output({
cv_bst <- lgb.cv(
params = list()
, nfold = 2L
, nrounds = 5L
, data = dtrain
, obj = "binary"
, verbose = 0L
)
})
expect_false(any(grepl("\\[LightGBM\\] \\[Info\\]", lgb_cv_logs)))
expect_true(any(grepl("\\[LightGBM\\] \\[Warning\\]", lgb_cv_logs)))

# (verbose = -1) no logs
lgb_cv_logs <- capture.output({
cv_bst <- lgb.cv(
params = list()
, nfold = 2L
, nrounds = 5L
, data = dtrain
, obj = "binary"
, verbose = -1L
)
})
# NOTE: this is not length(lgb_cv_logs) == 0 because lightgbm's
# dependencies might print other messages
expect_false(any(grepl("\\[LightGBM\\] \\[Info\\]", lgb_cv_logs)))
expect_false(any(grepl("\\[LightGBM\\] \\[Warning\\]", lgb_cv_logs)))
})

test_that("lgb.cv() updates params based on keyword arguments", {
dtrain <- lgb.Dataset(
data = matrix(rnorm(400L), ncol = 4L)
Expand Down