Skip to content

Commit 0bbb02f

Browse files
walkerjameschrisjameslambStrikerRUS
authored
[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: James Lamb <[email protected]> Co-authored-by: Nikita Titov <[email protected]>
1 parent ac24188 commit 0bbb02f

File tree

2 files changed

+88
-66
lines changed

2 files changed

+88
-66
lines changed

R-package/R/lgb.Booster.R

+3
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,9 @@ print.lgb.Booster <- function(x, ...) {
12351235

12361236
if (!handle_is_null) {
12371237
obj <- x$params$objective
1238+
if (is.null(obj)) {
1239+
obj <- "(default)"
1240+
}
12381241
if (obj == "none") {
12391242
obj <- "custom"
12401243
}

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

+85-66
Original file line numberDiff line numberDiff line change
@@ -1518,74 +1518,74 @@ test_that("boosters with linear models at leaves can be written to RDS and re-lo
15181518
expect_identical(preds, preds2)
15191519
})
15201520

1521-
test_that("Booster's print, show, and summary work correctly", {
1522-
.have_same_handle <- function(model, other_model) {
1523-
expect_equal(
1524-
model$.__enclos_env__$private$handle
1525-
, other_model$.__enclos_env__$private$handle
1526-
)
1527-
}
1521+
.have_same_handle <- function(model, other_model) {
1522+
expect_equal(
1523+
model$.__enclos_env__$private$handle
1524+
, other_model$.__enclos_env__$private$handle
1525+
)
1526+
}
15281527

1529-
.has_expected_content_for_fitted_model <- function(printed_txt) {
1530-
expect_true(any(startsWith(printed_txt, "LightGBM Model")))
1531-
expect_true(any(startsWith(printed_txt, "Fitted to dataset")))
1532-
}
1528+
.has_expected_content_for_fitted_model <- function(printed_txt) {
1529+
expect_true(any(startsWith(printed_txt, "LightGBM Model")))
1530+
expect_true(any(startsWith(printed_txt, "Fitted to dataset")))
1531+
}
15331532

1534-
.has_expected_content_for_finalized_model <- function(printed_txt) {
1535-
expect_true(any(printed_txt == "LightGBM Model"))
1536-
expect_true(any(grepl("Booster handle is invalid", printed_txt, fixed = TRUE)))
1537-
}
1533+
.has_expected_content_for_finalized_model <- function(printed_txt) {
1534+
expect_true(any(printed_txt == "LightGBM Model"))
1535+
expect_true(any(grepl("Booster handle is invalid", printed_txt, fixed = TRUE)))
1536+
}
15381537

1539-
.check_methods_work <- function(model) {
1540-
1541-
#--- should work for fitted models --- #
1542-
1543-
# print()
1544-
log_txt <- capture.output({
1545-
ret <- print(model)
1546-
})
1547-
.have_same_handle(ret, model)
1548-
.has_expected_content_for_fitted_model(log_txt)
1549-
1550-
# show()
1551-
log_txt <- capture.output({
1552-
ret <- show(model)
1553-
})
1554-
expect_null(ret)
1555-
.has_expected_content_for_fitted_model(log_txt)
1556-
1557-
# summary()
1558-
log_txt <- capture.output({
1559-
ret <- summary(model)
1560-
})
1561-
.have_same_handle(ret, model)
1562-
.has_expected_content_for_fitted_model(log_txt)
1563-
1564-
#--- should not fail for finalized models ---#
1565-
model$.__enclos_env__$private$finalize()
1566-
1567-
# print()
1568-
log_txt <- capture.output({
1569-
ret <- print(model)
1570-
})
1571-
.has_expected_content_for_finalized_model(log_txt)
1572-
1573-
# show()
1574-
.have_same_handle(ret, model)
1575-
log_txt <- capture.output({
1576-
ret <- show(model)
1577-
})
1578-
expect_null(ret)
1579-
.has_expected_content_for_finalized_model(log_txt)
1580-
1581-
# summary()
1582-
log_txt <- capture.output({
1583-
ret <- summary(model)
1584-
})
1585-
.have_same_handle(ret, model)
1586-
.has_expected_content_for_finalized_model(log_txt)
1587-
}
1538+
.check_methods_work <- function(model) {
1539+
1540+
#--- should work for fitted models --- #
1541+
1542+
# print()
1543+
log_txt <- capture.output({
1544+
ret <- print(model)
1545+
})
1546+
.have_same_handle(ret, model)
1547+
.has_expected_content_for_fitted_model(log_txt)
1548+
1549+
# show()
1550+
log_txt <- capture.output({
1551+
ret <- show(model)
1552+
})
1553+
expect_null(ret)
1554+
.has_expected_content_for_fitted_model(log_txt)
1555+
1556+
# summary()
1557+
log_txt <- capture.output({
1558+
ret <- summary(model)
1559+
})
1560+
.have_same_handle(ret, model)
1561+
.has_expected_content_for_fitted_model(log_txt)
1562+
1563+
#--- should not fail for finalized models ---#
1564+
model$.__enclos_env__$private$finalize()
1565+
1566+
# print()
1567+
log_txt <- capture.output({
1568+
ret <- print(model)
1569+
})
1570+
.has_expected_content_for_finalized_model(log_txt)
1571+
1572+
# show()
1573+
.have_same_handle(ret, model)
1574+
log_txt <- capture.output({
1575+
ret <- show(model)
1576+
})
1577+
expect_null(ret)
1578+
.has_expected_content_for_finalized_model(log_txt)
1579+
1580+
# summary()
1581+
log_txt <- capture.output({
1582+
ret <- summary(model)
1583+
})
1584+
.have_same_handle(ret, model)
1585+
.has_expected_content_for_finalized_model(log_txt)
1586+
}
15881587

1588+
test_that("Booster's print, show, and summary work correctly for built-in objectives", {
15891589
data("mtcars")
15901590
model <- lgb.train(
15911591
params = list(
@@ -1616,9 +1616,9 @@ test_that("Booster's print, show, and summary work correctly", {
16161616
, nrounds = 5L
16171617
)
16181618
.check_methods_work(model)
1619+
})
16191620

1620-
1621-
# with custom objective
1621+
test_that("Booster's print, show, and summary work correctly for custom objective", {
16221622
.logregobj <- function(preds, dtrain) {
16231623
labels <- get_field(dtrain, "label")
16241624
preds <- 1.0 / (1.0 + exp(-preds))
@@ -1638,6 +1638,7 @@ test_that("Booster's print, show, and summary work correctly", {
16381638
))
16391639
}
16401640

1641+
data("iris")
16411642
model <- lgb.train(
16421643
data = lgb.Dataset(
16431644
as.matrix(iris[, -5L])
@@ -1653,6 +1654,24 @@ test_that("Booster's print, show, and summary work correctly", {
16531654
.check_methods_work(model)
16541655
})
16551656

1657+
test_that("Booster's print, show, and summary work correctly when objective is not provided", {
1658+
data("iris")
1659+
model <- lgb.train(
1660+
data = lgb.Dataset(
1661+
as.matrix(iris[, seq_len(3L)])
1662+
, label = iris[, 4L]
1663+
)
1664+
, verbose = .LGB_VERBOSITY
1665+
, nrounds = 5L
1666+
, params = list(num_threads = .LGB_MAX_THREADS)
1667+
)
1668+
1669+
log_txt <- capture.output(print(model))
1670+
expect_true(any(log_txt == "Objective: (default)"))
1671+
1672+
.check_methods_work(model)
1673+
})
1674+
16561675
test_that("LGBM_BoosterGetNumFeature_R returns correct outputs", {
16571676
data("mtcars")
16581677
model <- lgb.train(

0 commit comments

Comments
 (0)