Commit 6e3e1ec2 authored by Laurae's avatar Laurae Committed by Guolin Ke
Browse files

Unhandled errors must error properly (#797)

parent 08bdc86e
...@@ -32,51 +32,55 @@ Booster <- R6Class( ...@@ -32,51 +32,55 @@ Booster <- R6Class(
params <- append(params, list(...)) params <- append(params, list(...))
params_str <- lgb.params2str(params) params_str <- lgb.params2str(params)
handle <- 0.0 handle <- 0.0
# Check if training dataset is not null
if (!is.null(train_set)) { # Attempts to create a handle for the dataset
try({
# Check if training dataset is lgb.Dataset or not
if (!lgb.check.r6.class(train_set, "lgb.Dataset")) {
stop("lgb.Booster: Can only use lgb.Dataset as training data")
}
# Store booster handle
handle <- lgb.call("LGBM_BoosterCreate_R", ret = handle, train_set$.__enclos_env__$private$get_handle(), params_str)
# Create private booster information
private$train_set <- train_set
private$num_dataset <- 1
private$init_predictor <- train_set$.__enclos_env__$private$predictor
# Check if predictor is existing # Check if training dataset is not null
if (!is.null(private$init_predictor)) { if (!is.null(train_set)) {
# Merge booster # Check if training dataset is lgb.Dataset or not
lgb.call("LGBM_BoosterMerge_R", if (!lgb.check.r6.class(train_set, "lgb.Dataset")) {
ret = NULL, stop("lgb.Booster: Can only use lgb.Dataset as training data")
handle, }
private$init_predictor$.__enclos_env__$private$handle)
} # Store booster handle
handle <- lgb.call("LGBM_BoosterCreate_R", ret = handle, train_set$.__enclos_env__$private$get_handle(), params_str)
# Check current iteration
private$is_predicted_cur_iter <- c(private$is_predicted_cur_iter, FALSE) # Create private booster information
private$train_set <- train_set
} else if (!is.null(modelfile)) { private$num_dataset <- 1
private$init_predictor <- train_set$.__enclos_env__$private$predictor
# Do we have a model file as character?
if (!is.character(modelfile)) { # Check if predictor is existing
stop("lgb.Booster: Can only use a string as model file path") if (!is.null(private$init_predictor)) {
}
# Merge booster
# Create booster from model lgb.call("LGBM_BoosterMerge_R",
handle <- lgb.call("LGBM_BoosterCreateFromModelfile_R", ret = NULL,
ret = handle, handle,
lgb.c_str(modelfile)) private$init_predictor$.__enclos_env__$private$handle)
} else if (!is.null(model_str)) { }
# Do we have a model_str as character? # Check current iteration
private$is_predicted_cur_iter <- c(private$is_predicted_cur_iter, FALSE)
} else if (!is.null(modelfile)) {
# Do we have a model file as character?
if (!is.character(modelfile)) {
stop("lgb.Booster: Can only use a string as model file path")
}
# Create booster from model
handle <- lgb.call("LGBM_BoosterCreateFromModelfile_R",
ret = handle,
lgb.c_str(modelfile))
} else if (!is.null(model_str)) {
# Do we have a model_str as character?
if (!is.character(model_str)) { if (!is.character(model_str)) {
stop("lgb.Booster: Can only use a string as model_str") stop("lgb.Booster: Can only use a string as model_str")
} }
...@@ -85,16 +89,23 @@ Booster <- R6Class( ...@@ -85,16 +89,23 @@ Booster <- R6Class(
handle <- lgb.call("LGBM_BoosterLoadModelFromString_R", handle <- lgb.call("LGBM_BoosterLoadModelFromString_R",
ret = handle, ret = handle,
lgb.c_str(model_str)) lgb.c_str(model_str))
} else {
# Booster non existent
stop("lgb.Booster: Need at least either training dataset, model file, or model_str to create booster instance")
}
} else { })
# Booster non existent # Check whether the handle was created properly if it was not stopped earlier by a stop call
stop("lgb.Booster: Need at least either training dataset, model file, or model_str to create booster instance")
}
if (lgb.is.null.handle(handle)) { if (lgb.is.null.handle(handle)) {
stop("lgb.Booster: cannot create Booster handle") stop("lgb.Booster: cannot create Booster handle")
} else { } else {
# Create class # Create class
class(handle) <- "lgb.Booster.handle" class(handle) <- "lgb.Booster.handle"
private$handle <- handle private$handle <- handle
...@@ -102,6 +113,7 @@ Booster <- R6Class( ...@@ -102,6 +113,7 @@ Booster <- R6Class(
private$num_class <- lgb.call("LGBM_BoosterGetNumClasses_R", private$num_class <- lgb.call("LGBM_BoosterGetNumClasses_R",
ret = private$num_class, ret = private$num_class,
private$handle) private$handle)
} }
}, },
......
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