Skip to content

Commit

Permalink
[R package] move error handling to C++ and minor changes in documenta…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
fabsig committed Apr 22, 2021
1 parent a8e6180 commit bfa019a
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 37 deletions.
35 changes: 18 additions & 17 deletions R-package/R/gpb.Dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -558,26 +558,27 @@ Dataset <- R6::R6Class(
if (gpb.is.null.handle(x = private$handle)) {
private$params <- modifyList(private$params, params)
} else {
call_state <- 0L
call_state <- .Call(
"LGBM_DatasetUpdateParamChecking_R"
, gpb.params2str(params = private$params)
, gpb.params2str(params = params)
, call_state
, PACKAGE = "lib_gpboost"
)
call_state <- as.integer(call_state)
if (call_state != 0L) {

# raise error if raw data is freed
tryCatch({
call_state <- 0L
.Call(
"LGBM_DatasetUpdateParamChecking_R"
, gpb.params2str(params = private$params)
, gpb.params2str(params = params)
, call_state
, PACKAGE = "lib_gpboost"
)
}, 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
if (is.null(private$raw_data)) {
gpb.last_error()
stop(e)
}

# Overwrite paramms

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

Expand Down Expand Up @@ -737,7 +738,7 @@ Dataset <- R6::R6Class(
#' @param data a \code{matrix} object, a \code{dgCMatrix} object or a character representing a filename
#' @param params a list of parameters. See
#' \href{https://github.com/fabsig/GPBoost/blob/master/docs/Parameters.rst#dataset-parameters}{
#' The "Dataset Parameters" section} for a list of parameters
#' the "Dataset Parameters" section of the parameter documentation} for a list of parameters
#' and valid values.
#' @param reference reference dataset. When GPBoost creates a Dataset, it does some preprocessing like binning
#' continuous features into histograms. If you want to apply the same bin boundaries from an existing
Expand Down
2 changes: 1 addition & 1 deletion R-package/R/gpb.train.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @param reset_data Boolean, setting it to TRUE (not the default value) will transform the
#' booster model into a predictor model which frees up memory and the
#' original datasets
#' @param ... other parameters, see \href{https://github.com/fabsig/GPBoost/blob/master/docs/Parameters.rst} for more information.
#' @param ... other parameters, see \href{https://github.com/fabsig/GPBoost/blob/master/docs/Parameters.rst}{the parameter documentation} for more information.
#' @inheritSection gpb_shared_params Early Stopping
#' @return a trained booster model \code{gpb.Booster}.
#'
Expand Down
4 changes: 2 additions & 2 deletions R-package/R/gpboost.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#' If you provide a character vector to this argument, it should contain strings with valid
#' evaluation metrics.
#' See \href{https://github.com/fabsig/GPBoost/blob/master/docs/Parameters.rst#metric-parameters}{
#' The "metric" section}
#' the "metric" section of the parameter documentation}
#' for a list of valid metrics.
#' }
#' \item{\bold{b. function}:
Expand Down Expand Up @@ -61,7 +61,7 @@
#' \code{regression}, \code{regression_l1}, \code{huber},
#' \code{binary}, \code{lambdarank}, \code{multiclass}, \code{multiclass}
#' @param params list of ("tuning") parameters.
#' See \href{https://github.com/fabsig/GPBoost/blob/master/docs/Parameters.rst} for more information.
#' See \href{https://github.com/fabsig/GPBoost/blob/master/docs/Parameters.rst}{the parameter documentation} for more information.
#' A few key parameters:
#' \itemize{
#' \item{\code{learning_rate}}{ The learning rate, also called shrinkage or damping parameter
Expand Down
5 changes: 0 additions & 5 deletions R-package/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ gpb.call <- function(fun_name, ret, ...) {
, PACKAGE = "lib_gpboost"
)
}
call_state <- as.integer(call_state)
# Check for call state value post call
if (call_state != 0L) {
gpb.last_error()
}

return(ret)

Expand Down
2 changes: 1 addition & 1 deletion R-package/man/gpb.Dataset.Rd

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

4 changes: 2 additions & 2 deletions R-package/man/gpb.cv.Rd

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

2 changes: 1 addition & 1 deletion R-package/man/gpb.grid.search.tune.parameters.Rd

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

6 changes: 3 additions & 3 deletions R-package/man/gpb.train.Rd

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

4 changes: 2 additions & 2 deletions R-package/man/gpb_shared_params.Rd

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

4 changes: 2 additions & 2 deletions R-package/man/gpboost.Rd

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

6 changes: 5 additions & 1 deletion R-package/src/gpboost_R.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

#include <R_ext/Rdynload.h>

#define R_NO_REMAP
#define R_USE_C99_IN_CXX
#include <R_ext/Error.h>

#include <string>
#include <cstdio>
#include <cstring>
Expand All @@ -32,7 +36,7 @@

#define CHECK_CALL(x) \
if ((x) != 0) { \
R_INT_PTR(call_state)[0] = -1;\
Rf_error(LGBM_GetLastError()); \
return call_state;\
}

Expand Down
13 changes: 13 additions & 0 deletions R-package/tests/testthat/test_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ test_that("gpb.Dataset: Dataset should be able to construct from matrix and retu
handle <- NULL
})

test_that("cpp errors should be raised as proper R errors", {
data(agaricus.train, package = "gpboost")
train <- agaricus.train
dtrain <- gpb.Dataset(
train$data
, label = train$label
, init_score = seq_len(10L)
)
expect_error({
dtrain$construct()
}, regexp = "Initial score size doesn't match data size")
})

test_that("gpb.Dataset$setinfo() should convert 'group' to integer", {
ds <- gpb.Dataset(
data = matrix(rnorm(100L), nrow = 50L, ncol = 2L)
Expand Down

0 comments on commit bfa019a

Please sign in to comment.