Skip to content

Commit

Permalink
[R package] update lgb.Dataset.R to use keyword arguments (#3607)
Browse files Browse the repository at this point in the history
* update lgb.Dataset.R to use keyword arguments

* minor adjustment

* Update R-package/R/lgb.Dataset.R

Co-authored-by: James Lamb <jaylamb20@gmail.com>

* fix linting error

* Update R-package/R/lgb.Dataset.R

Co-authored-by: James Lamb <jaylamb20@gmail.com>

Co-authored-by: Nick Zeng <361304605@qq.com>
Co-authored-by: James Lamb <jaylamb20@gmail.com>
  • Loading branch information
3 people authored Nov 29, 2020
1 parent 945f7be commit 960f5b9
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions R-package/R/lgb.Dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Dataset <- R6::R6Class(
if (!lgb.is.null.handle(private$handle)) {

# Freeing up handle
lgb.call("LGBM_DatasetFree_R", ret = NULL, private$handle)
lgb.call(fun_name = "LGBM_DatasetFree_R", ret = NULL, private$handle)
private$handle <- NULL

}
Expand All @@ -33,10 +33,10 @@ Dataset <- R6::R6Class(
...) {

# validate inputs early to avoid unnecessary computation
if (!(is.null(reference) || lgb.check.r6.class(reference, "lgb.Dataset"))) {
if (!(is.null(reference) || lgb.check.r6.class(object = reference, name = "lgb.Dataset"))) {
stop("lgb.Dataset: If provided, reference must be a ", sQuote("lgb.Dataset"))
}
if (!(is.null(predictor) || lgb.check.r6.class(predictor, "lgb.Predictor"))) {
if (!(is.null(predictor) || lgb.check.r6.class(object = predictor, name = "lgb.Predictor"))) {
stop("lgb.Dataset: If provided, predictor must be a ", sQuote("lgb.Predictor"))
}

Expand Down Expand Up @@ -178,7 +178,7 @@ Dataset <- R6::R6Class(
}

# Generate parameter str
params_str <- lgb.params2str(private$params)
params_str <- lgb.params2str(params = private$params)

# Get handle of reference dataset
ref_handle <- NULL
Expand All @@ -194,7 +194,7 @@ Dataset <- R6::R6Class(
if (is.character(private$raw_data)) {

handle <- lgb.call(
"LGBM_DatasetCreateFromFile_R"
fun_name = "LGBM_DatasetCreateFromFile_R"
, ret = handle
, lgb.c_str(private$raw_data)
, params_str
Expand All @@ -205,7 +205,7 @@ Dataset <- R6::R6Class(

# Are we using a matrix?
handle <- lgb.call(
"LGBM_DatasetCreateFromMat_R"
fun_name = "LGBM_DatasetCreateFromMat_R"
, ret = handle
, private$raw_data
, nrow(private$raw_data)
Expand All @@ -220,7 +220,7 @@ Dataset <- R6::R6Class(
}
# Are we using a dgCMatrix (sparsed matrix column compressed)
handle <- lgb.call(
"LGBM_DatasetCreateFromCSC_R"
fun_name = "LGBM_DatasetCreateFromCSC_R"
, ret = handle
, private$raw_data@p
, private$raw_data@i
Expand Down Expand Up @@ -251,7 +251,7 @@ Dataset <- R6::R6Class(

# Construct subset
handle <- lgb.call(
"LGBM_DatasetGetSubset_R"
fun_name = "LGBM_DatasetGetSubset_R"
, ret = handle
, ref_handle
, c(private$used_indices) # Adding c() fixes issue in R v3.5
Expand All @@ -277,7 +277,7 @@ Dataset <- R6::R6Class(

# Setup initial scores
init_score <- private$predictor$predict(
private$raw_data
data = private$raw_data
, rawscore = TRUE
, reshape = TRUE
)
Expand All @@ -300,7 +300,7 @@ Dataset <- R6::R6Class(
for (i in seq_along(private$info)) {

p <- private$info[i]
self$setinfo(names(p), p[[1L]])
self$setinfo(name = names(p), info = p[[1L]])

}

Expand All @@ -325,8 +325,18 @@ Dataset <- R6::R6Class(
num_col <- 0L

# Get numeric data and numeric features
c(lgb.call("LGBM_DatasetGetNumData_R", ret = num_row, private$handle),
lgb.call("LGBM_DatasetGetNumFeature_R", ret = num_col, private$handle))
c(
lgb.call(
fun_name = "LGBM_DatasetGetNumData_R"
, ret = num_row
, private$handle
),
lgb.call(
fun_name = "LGBM_DatasetGetNumFeature_R"
, ret = num_col
, private$handle
)
)

} else if (is.matrix(private$raw_data) || methods::is(private$raw_data, "dgCMatrix")) {

Expand All @@ -353,7 +363,10 @@ Dataset <- R6::R6Class(
if (!lgb.is.null.handle(private$handle)) {

# Get feature names and write them
cnames <- lgb.call.return.str("LGBM_DatasetGetFeatureNames_R", private$handle)
cnames <- lgb.call.return.str(
fun_name = "LGBM_DatasetGetFeatureNames_R"
, private$handle
)
private$colnames <- as.character(base::strsplit(cnames, "\t")[[1L]])
private$colnames

Expand Down Expand Up @@ -395,7 +408,7 @@ Dataset <- R6::R6Class(
# Merge names with tab separation
merged_name <- paste0(as.list(private$colnames), collapse = "\t")
lgb.call(
"LGBM_DatasetSetFeatureNames_R"
fun_name = "LGBM_DatasetSetFeatureNames_R"
, ret = NULL
, private$handle
, lgb.c_str(merged_name)
Expand Down Expand Up @@ -428,7 +441,7 @@ Dataset <- R6::R6Class(
# Get field size of info
info_len <- 0L
info_len <- lgb.call(
"LGBM_DatasetGetFieldSize_R"
fun_name = "LGBM_DatasetGetFieldSize_R"
, ret = info_len
, private$handle
, lgb.c_str(name)
Expand All @@ -446,7 +459,7 @@ Dataset <- R6::R6Class(
}

ret <- lgb.call(
"LGBM_DatasetGetField_R"
fun_name = "LGBM_DatasetGetField_R"
, ret = ret
, private$handle
, lgb.c_str(name)
Expand Down Expand Up @@ -487,7 +500,7 @@ Dataset <- R6::R6Class(
if (length(info) > 0L) {

lgb.call(
"LGBM_DatasetSetField_R"
fun_name = "LGBM_DatasetSetField_R"
, ret = NULL
, private$handle
, lgb.c_str(name)
Expand Down Expand Up @@ -535,8 +548,8 @@ Dataset <- R6::R6Class(
call_state <- 0L
call_state <- .Call(
"LGBM_DatasetUpdateParamChecking_R"
, lgb.params2str(private$params)
, lgb.params2str(params)
, lgb.params2str(params = private$params)
, lgb.params2str(params = params)
, call_state
, PACKAGE = "lib_lightgbm"
)
Expand Down Expand Up @@ -616,7 +629,7 @@ Dataset <- R6::R6Class(
if (!is.null(reference)) {

# Reference is unknown
if (!lgb.check.r6.class(reference, "lgb.Dataset")) {
if (!lgb.check.r6.class(object = reference, name = "lgb.Dataset")) {
stop("set_reference: Can only use lgb.Dataset as a reference")
}

Expand All @@ -637,7 +650,7 @@ Dataset <- R6::R6Class(
# Store binary data
self$construct()
lgb.call(
"LGBM_DatasetSaveBinary_R"
fun_name = "LGBM_DatasetSaveBinary_R"
, ret = NULL
, private$handle
, lgb.c_str(fname)
Expand Down Expand Up @@ -687,7 +700,7 @@ Dataset <- R6::R6Class(
if (!is.null(predictor)) {

# Predictor is unknown
if (!lgb.check.r6.class(predictor, "lgb.Predictor")) {
if (!lgb.check.r6.class(object = predictor, name = "lgb.Predictor")) {
stop("set_predictor: Can only use lgb.Predictor as predictor")
}

Expand Down Expand Up @@ -783,7 +796,7 @@ lgb.Dataset.create.valid <- function(dataset, data, info = list(), ...) {
}

# Create validation dataset
invisible(dataset$create_valid(data, info, ...))
invisible(dataset$create_valid(data = data, info = info, ...))

}

Expand Down Expand Up @@ -957,7 +970,7 @@ slice.lgb.Dataset <- function(dataset, idxset, ...) {
}

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

}

Expand Down Expand Up @@ -1061,7 +1074,7 @@ setinfo.lgb.Dataset <- function(dataset, name, info, ...) {
}

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

#' @name lgb.Dataset.set.categorical
Expand Down

0 comments on commit 960f5b9

Please sign in to comment.