"git@developer.sourcefind.cn:tianlh/lightgbm-dcu.git" did not exist on "ce3e31219c568323d66fa6e918fffcb96b3df92a"
Unverified Commit 0bbb02fd authored by Chris's avatar Chris Committed by GitHub
Browse files

[R-package] Ensure print.lgb.Booster() works when objective is not explicitly set (#6850)



* Handling NULL objective when booster is printed, moving test functions out of method tests, creating unique tests for different method situations

* Correcting whitespace

* Adding types for integer slices

* Moving test functions nearer to relevant tests, setting print to be default

* Update R-package/tests/testthat/test_lgb.Booster.R

---------
Co-authored-by: default avatarJames Lamb <jaylamb20@gmail.com>
Co-authored-by: default avatarNikita Titov <nekit94-08@mail.ru>
parent ac241888
...@@ -1235,6 +1235,9 @@ print.lgb.Booster <- function(x, ...) { ...@@ -1235,6 +1235,9 @@ print.lgb.Booster <- function(x, ...) {
if (!handle_is_null) { if (!handle_is_null) {
obj <- x$params$objective obj <- x$params$objective
if (is.null(obj)) {
obj <- "(default)"
}
if (obj == "none") { if (obj == "none") {
obj <- "custom" obj <- "custom"
} }
......
...@@ -1518,74 +1518,74 @@ test_that("boosters with linear models at leaves can be written to RDS and re-lo ...@@ -1518,74 +1518,74 @@ test_that("boosters with linear models at leaves can be written to RDS and re-lo
expect_identical(preds, preds2) expect_identical(preds, preds2)
}) })
test_that("Booster's print, show, and summary work correctly", { .have_same_handle <- function(model, other_model) {
.have_same_handle <- function(model, other_model) { expect_equal(
expect_equal( model$.__enclos_env__$private$handle
model$.__enclos_env__$private$handle , other_model$.__enclos_env__$private$handle
, other_model$.__enclos_env__$private$handle )
) }
}
.has_expected_content_for_fitted_model <- function(printed_txt) { .has_expected_content_for_fitted_model <- function(printed_txt) {
expect_true(any(startsWith(printed_txt, "LightGBM Model"))) expect_true(any(startsWith(printed_txt, "LightGBM Model")))
expect_true(any(startsWith(printed_txt, "Fitted to dataset"))) expect_true(any(startsWith(printed_txt, "Fitted to dataset")))
} }
.has_expected_content_for_finalized_model <- function(printed_txt) { .has_expected_content_for_finalized_model <- function(printed_txt) {
expect_true(any(printed_txt == "LightGBM Model")) expect_true(any(printed_txt == "LightGBM Model"))
expect_true(any(grepl("Booster handle is invalid", printed_txt, fixed = TRUE))) expect_true(any(grepl("Booster handle is invalid", printed_txt, fixed = TRUE)))
} }
.check_methods_work <- function(model) { .check_methods_work <- function(model) {
#--- should work for fitted models --- # #--- should work for fitted models --- #
# print() # print()
log_txt <- capture.output({ log_txt <- capture.output({
ret <- print(model) ret <- print(model)
}) })
.have_same_handle(ret, model) .have_same_handle(ret, model)
.has_expected_content_for_fitted_model(log_txt) .has_expected_content_for_fitted_model(log_txt)
# show() # show()
log_txt <- capture.output({ log_txt <- capture.output({
ret <- show(model) ret <- show(model)
}) })
expect_null(ret) expect_null(ret)
.has_expected_content_for_fitted_model(log_txt) .has_expected_content_for_fitted_model(log_txt)
# summary() # summary()
log_txt <- capture.output({ log_txt <- capture.output({
ret <- summary(model) ret <- summary(model)
}) })
.have_same_handle(ret, model) .have_same_handle(ret, model)
.has_expected_content_for_fitted_model(log_txt) .has_expected_content_for_fitted_model(log_txt)
#--- should not fail for finalized models ---# #--- should not fail for finalized models ---#
model$.__enclos_env__$private$finalize() model$.__enclos_env__$private$finalize()
# print() # print()
log_txt <- capture.output({ log_txt <- capture.output({
ret <- print(model) ret <- print(model)
}) })
.has_expected_content_for_finalized_model(log_txt) .has_expected_content_for_finalized_model(log_txt)
# show() # show()
.have_same_handle(ret, model) .have_same_handle(ret, model)
log_txt <- capture.output({ log_txt <- capture.output({
ret <- show(model) ret <- show(model)
}) })
expect_null(ret) expect_null(ret)
.has_expected_content_for_finalized_model(log_txt) .has_expected_content_for_finalized_model(log_txt)
# summary() # summary()
log_txt <- capture.output({ log_txt <- capture.output({
ret <- summary(model) ret <- summary(model)
}) })
.have_same_handle(ret, model) .have_same_handle(ret, model)
.has_expected_content_for_finalized_model(log_txt) .has_expected_content_for_finalized_model(log_txt)
} }
test_that("Booster's print, show, and summary work correctly for built-in objectives", {
data("mtcars") data("mtcars")
model <- lgb.train( model <- lgb.train(
params = list( params = list(
...@@ -1616,9 +1616,9 @@ test_that("Booster's print, show, and summary work correctly", { ...@@ -1616,9 +1616,9 @@ test_that("Booster's print, show, and summary work correctly", {
, nrounds = 5L , nrounds = 5L
) )
.check_methods_work(model) .check_methods_work(model)
})
test_that("Booster's print, show, and summary work correctly for custom objective", {
# with custom objective
.logregobj <- function(preds, dtrain) { .logregobj <- function(preds, dtrain) {
labels <- get_field(dtrain, "label") labels <- get_field(dtrain, "label")
preds <- 1.0 / (1.0 + exp(-preds)) preds <- 1.0 / (1.0 + exp(-preds))
...@@ -1638,6 +1638,7 @@ test_that("Booster's print, show, and summary work correctly", { ...@@ -1638,6 +1638,7 @@ test_that("Booster's print, show, and summary work correctly", {
)) ))
} }
data("iris")
model <- lgb.train( model <- lgb.train(
data = lgb.Dataset( data = lgb.Dataset(
as.matrix(iris[, -5L]) as.matrix(iris[, -5L])
...@@ -1653,6 +1654,24 @@ test_that("Booster's print, show, and summary work correctly", { ...@@ -1653,6 +1654,24 @@ test_that("Booster's print, show, and summary work correctly", {
.check_methods_work(model) .check_methods_work(model)
}) })
test_that("Booster's print, show, and summary work correctly when objective is not provided", {
data("iris")
model <- lgb.train(
data = lgb.Dataset(
as.matrix(iris[, seq_len(3L)])
, label = iris[, 4L]
)
, verbose = .LGB_VERBOSITY
, nrounds = 5L
, params = list(num_threads = .LGB_MAX_THREADS)
)
log_txt <- capture.output(print(model))
expect_true(any(log_txt == "Objective: (default)"))
.check_methods_work(model)
})
test_that("LGBM_BoosterGetNumFeature_R returns correct outputs", { test_that("LGBM_BoosterGetNumFeature_R returns correct outputs", {
data("mtcars") data("mtcars")
model <- lgb.train( model <- lgb.train(
......
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