Skip to content

Commit

Permalink
[R-package] remove unnecessary comments (#4383)
Browse files Browse the repository at this point in the history
* [R-package] remove unnecessary comments

* Update R-package/R/lgb.Booster.R
  • Loading branch information
jameslamb authored Jun 17, 2021
1 parent 6afd9bf commit 4167649
Show file tree
Hide file tree
Showing 9 changed files with 3 additions and 72 deletions.
40 changes: 1 addition & 39 deletions R-package/R/lgb.Booster.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,17 @@ Booster <- R6::R6Class(
# Add validation data
add_valid = function(data, name) {

# Check if data is lgb.Dataset
if (!lgb.is.Dataset(data)) {
stop("lgb.Booster.add_valid: Can only use lgb.Dataset as validation data")
}

# Check if predictors are identical
if (!identical(data$.__enclos_env__$private$predictor, private$init_predictor)) {
stop(
"lgb.Booster.add_valid: Failed to add validation data; "
, "you should use the same predictor for these data"
)
}

# Check if names are character
if (!is.character(name)) {
stop("lgb.Booster.add_valid: Can only use characters as data name")
}
Expand All @@ -170,7 +167,6 @@ Booster <- R6::R6Class(
, data$.__enclos_env__$private$get_handle()
)

# Store private information
private$valid_sets <- c(private$valid_sets, data)
private$name_valid_sets <- c(private$name_valid_sets, name)
private$num_dataset <- private$num_dataset + 1L
Expand All @@ -180,7 +176,6 @@ Booster <- R6::R6Class(

},

# Reset parameters of booster
reset_parameter = function(params, ...) {

if (methods::is(self$params, "list")) {
Expand Down Expand Up @@ -210,27 +205,22 @@ Booster <- R6::R6Class(
}
}

# Check if training set is not null
if (!is.null(train_set)) {

# Check if training set is lgb.Dataset
if (!lgb.is.Dataset(train_set)) {
stop("lgb.Booster.update: Only can use lgb.Dataset as training data")
}

# Check if predictors are identical
if (!identical(train_set$predictor, private$init_predictor)) {
stop("lgb.Booster.update: Change train_set failed, you should use the same predictor for these data")
}

# Reset training data on booster
.Call(
LGBM_BoosterResetTrainingData_R
, private$handle
, train_set$.__enclos_env__$private$get_handle()
)

# Store private train set
private$train_set <- train_set
private$train_set_version <- train_set$.__enclos_env__$private$version

Expand All @@ -249,7 +239,6 @@ Booster <- R6::R6Class(

} else {

# Check if objective is function
if (!is.function(fobj)) {
stop("lgb.Booster.update: fobj should be a function")
}
Expand Down Expand Up @@ -289,7 +278,6 @@ Booster <- R6::R6Class(
# Return one iteration behind
rollback_one_iter = function() {

# Return one iteration behind
.Call(
LGBM_BoosterRollbackOneIter_R
, private$handle
Expand Down Expand Up @@ -346,7 +334,6 @@ Booster <- R6::R6Class(
# Evaluate data on metrics
eval = function(data, name, feval = NULL) {

# Check if dataset is lgb.Dataset
if (!lgb.is.Dataset(data)) {
stop("lgb.Booster.eval: Can only use lgb.Dataset to eval")
}
Expand All @@ -360,7 +347,6 @@ Booster <- R6::R6Class(
# Check for validation data
if (length(private$valid_sets) > 0L) {

# Loop through each validation set
for (i in seq_along(private$valid_sets)) {

# Check for identical validation data with training data
Expand Down Expand Up @@ -406,15 +392,12 @@ Booster <- R6::R6Class(
# Evaluation validation data
eval_valid = function(feval = NULL) {

# Create ret list
ret <- list()

# Check if validation is empty
if (length(private$valid_sets) <= 0L) {
return(ret)
}

# Loop through each validation set
for (i in seq_along(private$valid_sets)) {
ret <- append(
x = ret
Expand All @@ -429,12 +412,10 @@ Booster <- R6::R6Class(
# Save model
save_model = function(filename, num_iteration = NULL, feature_importance_type = 0L) {

# Check if number of iteration is non existent
if (is.null(num_iteration)) {
num_iteration <- self$best_iter
}

# Save booster model
.Call(
LGBM_BoosterSaveModel_R
, private$handle
Expand All @@ -446,10 +427,8 @@ Booster <- R6::R6Class(
return(invisible(self))
},

# Save model to string
save_model_to_string = function(num_iteration = NULL, feature_importance_type = 0L) {

# Check if number of iteration is non existent
if (is.null(num_iteration)) {
num_iteration <- self$best_iter
}
Expand All @@ -468,7 +447,6 @@ Booster <- R6::R6Class(
# Dump model in memory
dump_model = function(num_iteration = NULL, feature_importance_type = 0L) {

# Check if number of iteration is non existent
if (is.null(num_iteration)) {
num_iteration <- self$best_iter
}
Expand All @@ -495,11 +473,10 @@ Booster <- R6::R6Class(
reshape = FALSE,
...) {

# Check if number of iteration is non existent
if (is.null(num_iteration)) {
num_iteration <- self$best_iter
}
# Check if start iteration is non existent

if (is.null(start_iteration)) {
start_iteration <- 0L
}
Expand Down Expand Up @@ -565,7 +542,6 @@ Booster <- R6::R6Class(
# Store data name
data_name <- private$name_train_set

# Check for id bigger than 1
if (idx > 1L) {
data_name <- private$name_valid_sets[[idx - 1L]]
}
Expand Down Expand Up @@ -609,14 +585,12 @@ Booster <- R6::R6Class(
# Get evaluation information
get_eval_info = function() {

# Check for evaluation names emptiness
if (is.null(private$eval_names)) {
eval_names <- .Call(
LGBM_BoosterGetEvalNames_R
, private$handle
)

# Check names' length
if (length(eval_names) > 0L) {

# Parse and store privately names
Expand All @@ -635,21 +609,17 @@ Booster <- R6::R6Class(

},

# Perform inner evaluation
inner_eval = function(data_name, data_idx, feval = NULL) {

# Check for unknown dataset (over the maximum provided range)
if (data_idx > private$num_dataset) {
stop("data_idx should not be greater than num_dataset")
}

# Get evaluation information
private$get_eval_info()

# Prepare return
ret <- list()

# Check evaluation names existence
if (length(private$eval_names) > 0L) {

# Create evaluation values
Expand All @@ -661,7 +631,6 @@ Booster <- R6::R6Class(
, tmp_vals
)

# Loop through all evaluation names
for (i in seq_along(private$eval_names)) {

# Store evaluation and append to return
Expand All @@ -684,7 +653,6 @@ Booster <- R6::R6Class(
stop("lgb.Booster.eval: feval should be a function")
}

# Prepare data
data <- private$train_set

# Check if data to assess is existing differently
Expand All @@ -695,7 +663,6 @@ Booster <- R6::R6Class(
# Perform function evaluation
res <- feval(private$inner_predict(data_idx), data)

# Check for name correctness
if (is.null(res$name) || is.null(res$value) || is.null(res$higher_better)) {
stop("lgb.Booster.eval: custom eval function should return a
list with attribute (name, value, higher_better)");
Expand Down Expand Up @@ -780,7 +747,6 @@ predict.lgb.Booster <- function(object,
stop("predict.lgb.Booster: object should be an ", sQuote("lgb.Booster"))
}

# Return booster predictions
return(
object$predict(
data = data
Expand Down Expand Up @@ -995,12 +961,10 @@ lgb.dump <- function(booster, num_iteration = NULL) {
#' @export
lgb.get.eval.result <- function(booster, data_name, eval_name, iters = NULL, is_err = FALSE) {

# Check if booster is booster
if (!lgb.is.Booster(x = booster)) {
stop("lgb.get.eval.result: Can only use ", sQuote("lgb.Booster"), " to get eval result")
}

# Check if data and evaluation name are characters or not
if (!is.character(data_name) || !is.character(eval_name)) {
stop("lgb.get.eval.result: data_name and eval_name should be characters")
}
Expand Down Expand Up @@ -1039,7 +1003,6 @@ lgb.get.eval.result <- function(booster, data_name, eval_name, iters = NULL, is_
result <- booster$record_evals[[data_name]][[eval_name]][[.EVAL_ERR_KEY()]]
}

# Check if iteration is non existant
if (is.null(iters)) {
return(as.numeric(result))
}
Expand All @@ -1049,6 +1012,5 @@ lgb.get.eval.result <- function(booster, data_name, eval_name, iters = NULL, is_
delta <- booster$record_evals$start_iter - 1.0
iters <- iters - delta

# Return requested result
return(as.numeric(result[iters]))
}
8 changes: 0 additions & 8 deletions R-package/R/lgb.Dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,6 @@ slice.lgb.Dataset <- function(dataset, idxset, ...) {
stop("slice.lgb.Dataset: input dataset should be an lgb.Dataset object")
}

# Return sliced set
return(invisible(dataset$slice(idxset = idxset, ...)))

}
Expand Down Expand Up @@ -1076,7 +1075,6 @@ setinfo.lgb.Dataset <- function(dataset, name, info, ...) {
stop("setinfo.lgb.Dataset: input dataset should be an lgb.Dataset object")
}

# Set information
return(invisible(dataset$setinfo(name = name, info = info)))
}

Expand Down Expand Up @@ -1108,7 +1106,6 @@ lgb.Dataset.set.categorical <- function(dataset, categorical_feature) {
stop("lgb.Dataset.set.categorical: input dataset should be an lgb.Dataset object")
}

# Set categoricals
return(invisible(dataset$set_categorical_feature(categorical_feature = categorical_feature)))

}
Expand All @@ -1135,12 +1132,10 @@ lgb.Dataset.set.categorical <- function(dataset, categorical_feature) {
#' @export
lgb.Dataset.set.reference <- function(dataset, reference) {

# Check if dataset is not a dataset
if (!lgb.is.Dataset(x = dataset)) {
stop("lgb.Dataset.set.reference: input dataset should be an lgb.Dataset object")
}

# Set reference
return(invisible(dataset$set_reference(reference = reference)))
}

Expand All @@ -1163,16 +1158,13 @@ lgb.Dataset.set.reference <- function(dataset, reference) {
#' @export
lgb.Dataset.save <- function(dataset, fname) {

# Check if dataset is not a dataset
if (!lgb.is.Dataset(x = dataset)) {
stop("lgb.Dataset.set: input dataset should be an lgb.Dataset object")
}

# File-type is not matching
if (!is.character(fname)) {
stop("lgb.Dataset.set: fname should be a character or a file connection")
}

# Store binary
return(invisible(dataset$save_binary(fname = fname)))
}
1 change: 0 additions & 1 deletion R-package/R/lgb.Predictor.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Predictor <- R6::R6Class(
private$params <- lgb.params2str(params = params)
handle <- NULL

# Check if handle is a character
if (is.character(modelfile)) {

# Create handle on it
Expand Down
10 changes: 0 additions & 10 deletions R-package/R/lgb.cv.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ lgb.cv <- function(params = list()
, column_names = cnames
)

# Check for weights
if (!is.null(weight)) {
data$setinfo(name = "weight", info = weight)
}
Expand All @@ -210,20 +209,17 @@ lgb.cv <- function(params = list()
data$set_categorical_feature(categorical_feature = categorical_feature)
}

# Check for folds
if (!is.null(folds)) {

# Check for list of folds or for single value
if (!identical(class(folds), "list") || length(folds) < 2L) {
stop(sQuote("folds"), " must be a list with 2 or more elements that are vectors of indices for each CV-fold")
}

# Set number of folds
nfold <- length(folds)

} else {

# Check fold value
if (nfold <= 1L) {
stop(sQuote("nfold"), " must be > 1")
}
Expand Down Expand Up @@ -583,15 +579,12 @@ lgb.stratified.folds <- function(y, k) {

lgb.merge.cv.result <- function(msg, showsd) {

# Get CV message length
if (length(msg) == 0L) {
stop("lgb.cv: size of cv result error")
}

# Get evaluation message length
eval_len <- length(msg[[1L]])

# Is evaluation message empty?
if (eval_len == 0L) {
stop("lgb.cv: should provide at least one metric for CV")
}
Expand All @@ -606,7 +599,6 @@ lgb.merge.cv.result <- function(msg, showsd) {
# get structure (name, higher_better, data_name)
ret_eval <- msg[[1L]]

# Go through evaluation length items
for (j in seq_len(eval_len)) {
ret_eval[[j]]$value <- mean(eval_result[[j]])
}
Expand All @@ -624,12 +616,10 @@ lgb.merge.cv.result <- function(msg, showsd) {
)
}

# Convert to list
ret_eval_err <- as.list(ret_eval_err)

}

# Return errors
return(
list(
eval_list = ret_eval
Expand Down
Loading

0 comments on commit 4167649

Please sign in to comment.