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] clarified error messages and documentation for lgb.get.eval.result() #2686

Merged
merged 4 commits into from
Jan 24, 2020
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
44 changes: 36 additions & 8 deletions R-package/R/lgb.Booster.R
Original file line number Diff line number Diff line change
Expand Up @@ -884,16 +884,19 @@ lgb.dump <- function(booster, num_iteration = NULL) {

#' @name lgb.get.eval.result
#' @title Get record evaluation result from booster
#' @description Get record evaluation result from booster
#' @description Given a \code{lgb.Booster}, return evaluation results for a
#' particular metric on a particular dataset.
#' @param booster Object of class \code{lgb.Booster}
#' @param data_name name of dataset
#' @param eval_name name of evaluation
#' @param iters iterations, NULL will return all
#' @param data_name Name of the dataset to return evaluation results for.
#' @param eval_name Name of the evaluation metric to return results for.
#' @param iters An integer vector of iterations you want to get evaluation results for. If NULL
#' (the default), evaluation results for all iterations will be returned.
#' @param is_err TRUE will return evaluation error instead
#'
#' @return vector of evaluation result
#'
#' @examples
#' # train a regression model
#' library(lightgbm)
#' data(agaricus.train, package = "lightgbm")
#' train <- agaricus.train
Expand All @@ -912,6 +915,14 @@ lgb.dump <- function(booster, num_iteration = NULL) {
#' , learning_rate = 1.0
#' , early_stopping_rounds = 5L
#' )
#'
#' # Examine valid data_name values
#' print(setdiff(names(model$record_evals), "start_iter"))
#'
#' # Examine valid eval_name values for dataset "test"
#' print(names(model$record_evals[["test"]]))
#'
#' # Get L2 values for "test" dataset
#' lgb.get.eval.result(model, "test", "l2")
#' @export
lgb.get.eval.result <- function(booster, data_name, eval_name, iters = NULL, is_err = FALSE) {
Expand All @@ -926,13 +937,30 @@ lgb.get.eval.result <- function(booster, data_name, eval_name, iters = NULL, is_
stop("lgb.get.eval.result: data_name and eval_name should be characters")
}

# Check if recorded evaluation is existing
if (is.null(booster$record_evals[[data_name]])) {
stop("lgb.get.eval.result: wrong data name")
# NOTE: "start_iter" exists in booster$record_evals but is not a valid data_name
data_names <- setdiff(names(booster$record_evals), "start_iter")
if (!(data_name %in% data_names)) {
stop(paste0(
"lgb.get.eval.result: data_name "
, shQuote(data_name)
, " not found. Only the following datasets exist in record evals: ["
, paste(data_names, collapse = ", ")
, "]"
))
}

# Check if evaluation result is existing
if (is.null(booster$record_evals[[data_name]][[eval_name]])) {
eval_names <- names(booster$record_evals[[data_name]])
if (!(eval_name %in% eval_names)) {
stop(paste0(
"lgb.get.eval.result: eval_name "
, shQuote(eval_name)
, " not found. Only the following eval_names exist for dataset "
, shQuote(data_name)
, ": ["
, paste(eval_names, collapse = ", ")
, "]"
))
stop("lgb.get.eval.result: wrong eval name")
}

Expand Down
19 changes: 15 additions & 4 deletions R-package/man/lgb.get.eval.result.Rd

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

90 changes: 90 additions & 0 deletions R-package/tests/testthat/test_lgb.Booster.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
context("lgb.get.eval.result")

test_that("lgb.get.eval.result() should throw an informative error if booster is not an lgb.Booster", {
bad_inputs <- list(
matrix(1.0:10.0, 2L, 5L)
, TRUE
, c("a", "b")
, NA
, 10L
, lgb.Dataset(
data = matrix(1.0:10.0, 2L, 5L)
, params = list()
)
)
for (bad_input in bad_inputs) {
expect_error({
lgb.get.eval.result(
booster = bad_input
, data_name = "test"
, eval_name = "l2"
)
}, regexp = "Can only use", fixed = TRUE)
}
})

test_that("lgb.get.eval.result() should throw an informative error for incorrect data_name", {
data(agaricus.train, package = "lightgbm")
data(agaricus.test, package = "lightgbm")
dtrain <- lgb.Dataset(
agaricus.train$data
, label = agaricus.train$label
)
model <- lgb.train(
params = list(
objective = "regression"
, metric = "l2"
)
, data = dtrain
, nrounds = 5L
, valids = list(
"test" = lgb.Dataset.create.valid(
dtrain
, agaricus.test$data
, label = agaricus.test$label
)
)
, min_data = 1L
, learning_rate = 1.0
)
expect_error({
eval_results <- lgb.get.eval.result(
booster = model
, data_name = "testing"
, eval_name = "l2"
)
}, regexp = "Only the following datasets exist in record evals: [test]", fixed = TRUE)
})

test_that("lgb.get.eval.result() should throw an informative error for incorrect eval_name", {
data(agaricus.train, package = "lightgbm")
data(agaricus.test, package = "lightgbm")
dtrain <- lgb.Dataset(
agaricus.train$data
, label = agaricus.train$label
)
model <- lgb.train(
params = list(
objective = "regression"
, metric = "l2"
)
, data = dtrain
, nrounds = 5L
, valids = list(
"test" = lgb.Dataset.create.valid(
dtrain
, agaricus.test$data
, label = agaricus.test$label
)
)
, min_data = 1L
, learning_rate = 1.0
)
expect_error({
eval_results <- lgb.get.eval.result(
booster = model
, data_name = "test"
, eval_name = "l1"
)
}, regexp = "Only the following eval_names exist for dataset 'test': [l2]", fixed = TRUE)
})