Unverified Commit 960f5b91 authored by zenggyu's avatar zenggyu Committed by GitHub
Browse files

[R package] update lgb.Dataset.R to use keyword arguments (#3607)



* update lgb.Dataset.R to use keyword arguments

* minor adjustment

* Update R-package/R/lgb.Dataset.R
Co-authored-by: default avatarJames Lamb <jaylamb20@gmail.com>

* fix linting error

* Update R-package/R/lgb.Dataset.R
Co-authored-by: default avatarJames Lamb <jaylamb20@gmail.com>
Co-authored-by: default avatarNick Zeng <361304605@qq.com>
Co-authored-by: default avatarJames Lamb <jaylamb20@gmail.com>
parent 945f7bee
...@@ -13,7 +13,7 @@ Dataset <- R6::R6Class( ...@@ -13,7 +13,7 @@ Dataset <- R6::R6Class(
if (!lgb.is.null.handle(private$handle)) { if (!lgb.is.null.handle(private$handle)) {
# Freeing up 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 private$handle <- NULL
} }
...@@ -33,10 +33,10 @@ Dataset <- R6::R6Class( ...@@ -33,10 +33,10 @@ Dataset <- R6::R6Class(
...) { ...) {
# validate inputs early to avoid unnecessary computation # 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")) 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")) stop("lgb.Dataset: If provided, predictor must be a ", sQuote("lgb.Predictor"))
} }
...@@ -178,7 +178,7 @@ Dataset <- R6::R6Class( ...@@ -178,7 +178,7 @@ Dataset <- R6::R6Class(
} }
# Generate parameter str # Generate parameter str
params_str <- lgb.params2str(private$params) params_str <- lgb.params2str(params = private$params)
# Get handle of reference dataset # Get handle of reference dataset
ref_handle <- NULL ref_handle <- NULL
...@@ -194,7 +194,7 @@ Dataset <- R6::R6Class( ...@@ -194,7 +194,7 @@ Dataset <- R6::R6Class(
if (is.character(private$raw_data)) { if (is.character(private$raw_data)) {
handle <- lgb.call( handle <- lgb.call(
"LGBM_DatasetCreateFromFile_R" fun_name = "LGBM_DatasetCreateFromFile_R"
, ret = handle , ret = handle
, lgb.c_str(private$raw_data) , lgb.c_str(private$raw_data)
, params_str , params_str
...@@ -205,7 +205,7 @@ Dataset <- R6::R6Class( ...@@ -205,7 +205,7 @@ Dataset <- R6::R6Class(
# Are we using a matrix? # Are we using a matrix?
handle <- lgb.call( handle <- lgb.call(
"LGBM_DatasetCreateFromMat_R" fun_name = "LGBM_DatasetCreateFromMat_R"
, ret = handle , ret = handle
, private$raw_data , private$raw_data
, nrow(private$raw_data) , nrow(private$raw_data)
...@@ -220,7 +220,7 @@ Dataset <- R6::R6Class( ...@@ -220,7 +220,7 @@ Dataset <- R6::R6Class(
} }
# Are we using a dgCMatrix (sparsed matrix column compressed) # Are we using a dgCMatrix (sparsed matrix column compressed)
handle <- lgb.call( handle <- lgb.call(
"LGBM_DatasetCreateFromCSC_R" fun_name = "LGBM_DatasetCreateFromCSC_R"
, ret = handle , ret = handle
, private$raw_data@p , private$raw_data@p
, private$raw_data@i , private$raw_data@i
...@@ -251,7 +251,7 @@ Dataset <- R6::R6Class( ...@@ -251,7 +251,7 @@ Dataset <- R6::R6Class(
# Construct subset # Construct subset
handle <- lgb.call( handle <- lgb.call(
"LGBM_DatasetGetSubset_R" fun_name = "LGBM_DatasetGetSubset_R"
, ret = handle , ret = handle
, ref_handle , ref_handle
, c(private$used_indices) # Adding c() fixes issue in R v3.5 , c(private$used_indices) # Adding c() fixes issue in R v3.5
...@@ -277,7 +277,7 @@ Dataset <- R6::R6Class( ...@@ -277,7 +277,7 @@ Dataset <- R6::R6Class(
# Setup initial scores # Setup initial scores
init_score <- private$predictor$predict( init_score <- private$predictor$predict(
private$raw_data data = private$raw_data
, rawscore = TRUE , rawscore = TRUE
, reshape = TRUE , reshape = TRUE
) )
...@@ -300,7 +300,7 @@ Dataset <- R6::R6Class( ...@@ -300,7 +300,7 @@ Dataset <- R6::R6Class(
for (i in seq_along(private$info)) { for (i in seq_along(private$info)) {
p <- private$info[i] p <- private$info[i]
self$setinfo(names(p), p[[1L]]) self$setinfo(name = names(p), info = p[[1L]])
} }
...@@ -325,8 +325,18 @@ Dataset <- R6::R6Class( ...@@ -325,8 +325,18 @@ Dataset <- R6::R6Class(
num_col <- 0L num_col <- 0L
# Get numeric data and numeric features # Get numeric data and numeric features
c(lgb.call("LGBM_DatasetGetNumData_R", ret = num_row, private$handle), c(
lgb.call("LGBM_DatasetGetNumFeature_R", ret = num_col, private$handle)) 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")) { } else if (is.matrix(private$raw_data) || methods::is(private$raw_data, "dgCMatrix")) {
...@@ -353,7 +363,10 @@ Dataset <- R6::R6Class( ...@@ -353,7 +363,10 @@ Dataset <- R6::R6Class(
if (!lgb.is.null.handle(private$handle)) { if (!lgb.is.null.handle(private$handle)) {
# Get feature names and write them # 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 <- as.character(base::strsplit(cnames, "\t")[[1L]])
private$colnames private$colnames
...@@ -395,7 +408,7 @@ Dataset <- R6::R6Class( ...@@ -395,7 +408,7 @@ Dataset <- R6::R6Class(
# Merge names with tab separation # Merge names with tab separation
merged_name <- paste0(as.list(private$colnames), collapse = "\t") merged_name <- paste0(as.list(private$colnames), collapse = "\t")
lgb.call( lgb.call(
"LGBM_DatasetSetFeatureNames_R" fun_name = "LGBM_DatasetSetFeatureNames_R"
, ret = NULL , ret = NULL
, private$handle , private$handle
, lgb.c_str(merged_name) , lgb.c_str(merged_name)
...@@ -428,7 +441,7 @@ Dataset <- R6::R6Class( ...@@ -428,7 +441,7 @@ Dataset <- R6::R6Class(
# Get field size of info # Get field size of info
info_len <- 0L info_len <- 0L
info_len <- lgb.call( info_len <- lgb.call(
"LGBM_DatasetGetFieldSize_R" fun_name = "LGBM_DatasetGetFieldSize_R"
, ret = info_len , ret = info_len
, private$handle , private$handle
, lgb.c_str(name) , lgb.c_str(name)
...@@ -446,7 +459,7 @@ Dataset <- R6::R6Class( ...@@ -446,7 +459,7 @@ Dataset <- R6::R6Class(
} }
ret <- lgb.call( ret <- lgb.call(
"LGBM_DatasetGetField_R" fun_name = "LGBM_DatasetGetField_R"
, ret = ret , ret = ret
, private$handle , private$handle
, lgb.c_str(name) , lgb.c_str(name)
...@@ -487,7 +500,7 @@ Dataset <- R6::R6Class( ...@@ -487,7 +500,7 @@ Dataset <- R6::R6Class(
if (length(info) > 0L) { if (length(info) > 0L) {
lgb.call( lgb.call(
"LGBM_DatasetSetField_R" fun_name = "LGBM_DatasetSetField_R"
, ret = NULL , ret = NULL
, private$handle , private$handle
, lgb.c_str(name) , lgb.c_str(name)
...@@ -535,8 +548,8 @@ Dataset <- R6::R6Class( ...@@ -535,8 +548,8 @@ Dataset <- R6::R6Class(
call_state <- 0L call_state <- 0L
call_state <- .Call( call_state <- .Call(
"LGBM_DatasetUpdateParamChecking_R" "LGBM_DatasetUpdateParamChecking_R"
, lgb.params2str(private$params) , lgb.params2str(params = private$params)
, lgb.params2str(params) , lgb.params2str(params = params)
, call_state , call_state
, PACKAGE = "lib_lightgbm" , PACKAGE = "lib_lightgbm"
) )
...@@ -616,7 +629,7 @@ Dataset <- R6::R6Class( ...@@ -616,7 +629,7 @@ Dataset <- R6::R6Class(
if (!is.null(reference)) { if (!is.null(reference)) {
# Reference is unknown # 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") stop("set_reference: Can only use lgb.Dataset as a reference")
} }
...@@ -637,7 +650,7 @@ Dataset <- R6::R6Class( ...@@ -637,7 +650,7 @@ Dataset <- R6::R6Class(
# Store binary data # Store binary data
self$construct() self$construct()
lgb.call( lgb.call(
"LGBM_DatasetSaveBinary_R" fun_name = "LGBM_DatasetSaveBinary_R"
, ret = NULL , ret = NULL
, private$handle , private$handle
, lgb.c_str(fname) , lgb.c_str(fname)
...@@ -687,7 +700,7 @@ Dataset <- R6::R6Class( ...@@ -687,7 +700,7 @@ Dataset <- R6::R6Class(
if (!is.null(predictor)) { if (!is.null(predictor)) {
# Predictor is unknown # 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") stop("set_predictor: Can only use lgb.Predictor as predictor")
} }
...@@ -783,7 +796,7 @@ lgb.Dataset.create.valid <- function(dataset, data, info = list(), ...) { ...@@ -783,7 +796,7 @@ lgb.Dataset.create.valid <- function(dataset, data, info = list(), ...) {
} }
# Create validation dataset # Create validation dataset
invisible(dataset$create_valid(data, info, ...)) invisible(dataset$create_valid(data = data, info = info, ...))
} }
...@@ -957,7 +970,7 @@ slice.lgb.Dataset <- function(dataset, idxset, ...) { ...@@ -957,7 +970,7 @@ slice.lgb.Dataset <- function(dataset, idxset, ...) {
} }
# Return sliced set # Return sliced set
invisible(dataset$slice(idxset, ...)) invisible(dataset$slice(idxset = idxset, ...))
} }
...@@ -1061,7 +1074,7 @@ setinfo.lgb.Dataset <- function(dataset, name, info, ...) { ...@@ -1061,7 +1074,7 @@ setinfo.lgb.Dataset <- function(dataset, name, info, ...) {
} }
# Set information # Set information
invisible(dataset$setinfo(name, info)) invisible(dataset$setinfo(name = name, info = info))
} }
#' @name lgb.Dataset.set.categorical #' @name lgb.Dataset.set.categorical
......
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