Unverified Commit 694e41e4 authored by James Lamb's avatar James Lamb Committed by GitHub
Browse files

[R-package] standardize naming of internal functions (#6179)

parent deb70773
...@@ -25,7 +25,7 @@ test_that("learning-to-rank with lgb.train() works as expected", { ...@@ -25,7 +25,7 @@ test_that("learning-to-rank with lgb.train() works as expected", {
, data = dtrain , data = dtrain
, nrounds = 10L , nrounds = 10L
) )
expect_true(lgb.is.Booster(model)) expect_true(.is_Booster(model))
dumped_model <- jsonlite::fromJSON( dumped_model <- jsonlite::fromJSON(
model$dump_model() model$dump_model()
......
...@@ -11,16 +11,16 @@ test_that("Booster$finalize() should not fail", { ...@@ -11,16 +11,16 @@ test_that("Booster$finalize() should not fail", {
, verbose = .LGB_VERBOSITY , verbose = .LGB_VERBOSITY
, nrounds = 3L , nrounds = 3L
) )
expect_true(lgb.is.Booster(bst)) expect_true(.is_Booster(bst))
expect_false(lgb.is.null.handle(bst$.__enclos_env__$private$handle)) expect_false(.is_null_handle(bst$.__enclos_env__$private$handle))
bst$finalize() bst$finalize()
expect_true(lgb.is.null.handle(bst$.__enclos_env__$private$handle)) expect_true(.is_null_handle(bst$.__enclos_env__$private$handle))
# calling finalize() a second time shouldn't cause any issues # calling finalize() a second time shouldn't cause any issues
bst$finalize() bst$finalize()
expect_true(lgb.is.null.handle(bst$.__enclos_env__$private$handle)) expect_true(.is_null_handle(bst$.__enclos_env__$private$handle))
}) })
test_that("lgb.get.eval.result() should throw an informative error if booster is not an lgb.Booster", { test_that("lgb.get.eval.result() should throw an informative error if booster is not an lgb.Booster", {
...@@ -188,7 +188,7 @@ test_that("Loading a Booster from a text file works", { ...@@ -188,7 +188,7 @@ test_that("Loading a Booster from a text file works", {
, params = params , params = params
, nrounds = 2L , nrounds = 2L
) )
expect_true(lgb.is.Booster(bst)) expect_true(.is_Booster(bst))
pred <- predict(bst, test$data) pred <- predict(bst, test$data)
model_file <- tempfile(fileext = ".model") model_file <- tempfile(fileext = ".model")
...@@ -232,7 +232,7 @@ test_that("boosters with linear models at leaves can be written to text file and ...@@ -232,7 +232,7 @@ test_that("boosters with linear models at leaves can be written to text file and
, params = params , params = params
, verbose = .LGB_VERBOSITY , verbose = .LGB_VERBOSITY
) )
expect_true(lgb.is.Booster(bst)) expect_true(.is_Booster(bst))
# save predictions, then write the model to a file and destroy it in R # save predictions, then write the model to a file and destroy it in R
preds <- predict(bst, X) preds <- predict(bst, X)
...@@ -269,7 +269,7 @@ test_that("Loading a Booster from a string works", { ...@@ -269,7 +269,7 @@ test_that("Loading a Booster from a string works", {
) )
, nrounds = 2L , nrounds = 2L
) )
expect_true(lgb.is.Booster(bst)) expect_true(.is_Booster(bst))
pred <- predict(bst, test$data) pred <- predict(bst, test$data)
model_string <- bst$save_model_to_string() model_string <- bst$save_model_to_string()
...@@ -376,7 +376,7 @@ test_that("If a string and a file are both passed to lgb.load() the file is used ...@@ -376,7 +376,7 @@ test_that("If a string and a file are both passed to lgb.load() the file is used
) )
, nrounds = 2L , nrounds = 2L
) )
expect_true(lgb.is.Booster(bst)) expect_true(.is_Booster(bst))
pred <- predict(bst, test$data) pred <- predict(bst, test$data)
model_file <- tempfile(fileext = ".model") model_file <- tempfile(fileext = ".model")
...@@ -411,7 +411,7 @@ test_that("Creating a Booster from a Dataset should work", { ...@@ -411,7 +411,7 @@ test_that("Creating a Booster from a Dataset should work", {
), ),
train_set = dtrain train_set = dtrain
) )
expect_true(lgb.is.Booster(bst)) expect_true(.is_Booster(bst))
expect_equal(bst$current_iter(), 0L) expect_equal(bst$current_iter(), 0L)
expect_true(is.na(bst$best_score)) expect_true(is.na(bst$best_score))
expect_true(all(bst$predict(agaricus.train$data) == 0.5)) expect_true(all(bst$predict(agaricus.train$data) == 0.5))
...@@ -446,10 +446,10 @@ test_that("Creating a Booster from a Dataset with an existing predictor should w ...@@ -446,10 +446,10 @@ test_that("Creating a Booster from a Dataset with an existing predictor should w
, num_threads = .LGB_MAX_THREADS , num_threads = .LGB_MAX_THREADS
) )
) )
expect_true(lgb.is.Booster(bst)) expect_true(.is_Booster(bst))
expect_equal(bst$current_iter(), nrounds) expect_equal(bst$current_iter(), nrounds)
expect_equal(bst$eval_train()[[1L]][["value"]], 0.1115352) expect_equal(bst$eval_train()[[1L]][["value"]], 0.1115352)
expect_true(lgb.is.Booster(bst_from_ds)) expect_true(.is_Booster(bst_from_ds))
expect_equal(bst_from_ds$current_iter(), nrounds) expect_equal(bst_from_ds$current_iter(), nrounds)
expect_equal(bst_from_ds$eval_train()[[1L]][["value"]], 5.65704892) expect_equal(bst_from_ds$eval_train()[[1L]][["value"]], 5.65704892)
dumped_model <- jsonlite::fromJSON(bst$dump_model()) dumped_model <- jsonlite::fromJSON(bst$dump_model())
...@@ -531,7 +531,7 @@ test_that("Booster$rollback_one_iter() should work as expected", { ...@@ -531,7 +531,7 @@ test_that("Booster$rollback_one_iter() should work as expected", {
, nrounds = nrounds , nrounds = nrounds
) )
expect_equal(bst$current_iter(), nrounds) expect_equal(bst$current_iter(), nrounds)
expect_true(lgb.is.Booster(bst)) expect_true(.is_Booster(bst))
logloss <- bst$eval_train()[[1L]][["value"]] logloss <- bst$eval_train()[[1L]][["value"]]
expect_equal(logloss, 0.01904786) expect_equal(logloss, 0.01904786)
...@@ -539,7 +539,7 @@ test_that("Booster$rollback_one_iter() should work as expected", { ...@@ -539,7 +539,7 @@ test_that("Booster$rollback_one_iter() should work as expected", {
# rollback_one_iter() should return a booster and modify the original # rollback_one_iter() should return a booster and modify the original
# booster in place # booster in place
expect_true(lgb.is.Booster(x)) expect_true(.is_Booster(x))
expect_equal(bst$current_iter(), nrounds - 1L) expect_equal(bst$current_iter(), nrounds - 1L)
# score should now come from the model as of 4 iterations # score should now come from the model as of 4 iterations
...@@ -565,7 +565,7 @@ test_that("Booster$update() passing a train_set works as expected", { ...@@ -565,7 +565,7 @@ test_that("Booster$update() passing a train_set works as expected", {
) )
, nrounds = nrounds , nrounds = nrounds
) )
expect_true(lgb.is.Booster(bst)) expect_true(.is_Booster(bst))
expect_equal(bst$current_iter(), nrounds) expect_equal(bst$current_iter(), nrounds)
bst$update( bst$update(
train_set = Dataset$new( train_set = Dataset$new(
...@@ -574,7 +574,7 @@ test_that("Booster$update() passing a train_set works as expected", { ...@@ -574,7 +574,7 @@ test_that("Booster$update() passing a train_set works as expected", {
, params = list(verbose = .LGB_VERBOSITY) , params = list(verbose = .LGB_VERBOSITY)
) )
) )
expect_true(lgb.is.Booster(bst)) expect_true(.is_Booster(bst))
expect_equal(bst$current_iter(), nrounds + 1L) expect_equal(bst$current_iter(), nrounds + 1L)
# train with 3 rounds directly # train with 3 rounds directly
...@@ -590,7 +590,7 @@ test_that("Booster$update() passing a train_set works as expected", { ...@@ -590,7 +590,7 @@ test_that("Booster$update() passing a train_set works as expected", {
) )
, nrounds = nrounds + 1L , nrounds = nrounds + 1L
) )
expect_true(lgb.is.Booster(bst2)) expect_true(.is_Booster(bst2))
expect_equal(bst2$current_iter(), nrounds + 1L) expect_equal(bst2$current_iter(), nrounds + 1L)
# model with 2 rounds + 1 update should be identical to 3 rounds # model with 2 rounds + 1 update should be identical to 3 rounds
...@@ -716,7 +716,7 @@ test_that("Saving a model with different feature importance types works", { ...@@ -716,7 +716,7 @@ test_that("Saving a model with different feature importance types works", {
) )
, nrounds = 2L , nrounds = 2L
) )
expect_true(lgb.is.Booster(bst)) expect_true(.is_Booster(bst))
.feat_importance_from_string <- function(model_string) { .feat_importance_from_string <- function(model_string) {
file_lines <- strsplit(model_string, "\n", fixed = TRUE)[[1L]] file_lines <- strsplit(model_string, "\n", fixed = TRUE)[[1L]]
...@@ -772,7 +772,7 @@ test_that("Saving a model with unknown importance type fails", { ...@@ -772,7 +772,7 @@ test_that("Saving a model with unknown importance type fails", {
) )
, nrounds = 2L , nrounds = 2L
) )
expect_true(lgb.is.Booster(bst)) expect_true(.is_Booster(bst))
UNSUPPORTED_IMPORTANCE <- 2L UNSUPPORTED_IMPORTANCE <- 2L
expect_error({ expect_error({
...@@ -1372,7 +1372,7 @@ test_that("boosters with linear models at leaves work with saveRDS.lgb.Booster a ...@@ -1372,7 +1372,7 @@ test_that("boosters with linear models at leaves work with saveRDS.lgb.Booster a
, nrounds = 10L , nrounds = 10L
, params = params , params = params
) )
expect_true(lgb.is.Booster(bst)) expect_true(.is_Booster(bst))
# save predictions, then write the model to a file and destroy it in R # save predictions, then write the model to a file and destroy it in R
preds <- predict(bst, X) preds <- predict(bst, X)
...@@ -1412,7 +1412,7 @@ test_that("boosters with linear models at leaves can be written to RDS and re-lo ...@@ -1412,7 +1412,7 @@ test_that("boosters with linear models at leaves can be written to RDS and re-lo
, nrounds = 10L , nrounds = 10L
, params = params , params = params
) )
expect_true(lgb.is.Booster(bst)) expect_true(.is_Booster(bst))
# save predictions, then write the model to a file and destroy it in R # save predictions, then write the model to a file and destroy it in R
preds <- predict(bst, X) preds <- predict(bst, X)
......
test_that("lgb.params2str() works as expected for empty lists", { test_that(".params2str() works as expected for empty lists", {
out_str <- lgb.params2str( out_str <- .params2str(
params = list() params = list()
) )
expect_identical(class(out_str), "character") expect_identical(class(out_str), "character")
expect_equal(out_str, "") expect_equal(out_str, "")
}) })
test_that("lgb.params2str() works as expected for a key in params with multiple different-length elements", { test_that(".params2str() works as expected for a key in params with multiple different-length elements", {
metrics <- c("a", "ab", "abc", "abcdefg") metrics <- c("a", "ab", "abc", "abcdefg")
params <- list( params <- list(
objective = "magic" objective = "magic"
...@@ -14,7 +14,7 @@ test_that("lgb.params2str() works as expected for a key in params with multiple ...@@ -14,7 +14,7 @@ test_that("lgb.params2str() works as expected for a key in params with multiple
, nrounds = 10L , nrounds = 10L
, learning_rate = 0.0000001 , learning_rate = 0.0000001
) )
out_str <- lgb.params2str( out_str <- .params2str(
params = params params = params
) )
expect_identical(class(out_str), "character") expect_identical(class(out_str), "character")
...@@ -24,8 +24,8 @@ test_that("lgb.params2str() works as expected for a key in params with multiple ...@@ -24,8 +24,8 @@ test_that("lgb.params2str() works as expected for a key in params with multiple
) )
}) })
test_that("lgb.params2str() passes through duplicated params", { test_that(".params2str() passes through duplicated params", {
out_str <- lgb.params2str( out_str <- .params2str(
params = list( params = list(
objective = "regression" objective = "regression"
, bagging_fraction = 0.8 , bagging_fraction = 0.8
...@@ -35,8 +35,8 @@ test_that("lgb.params2str() passes through duplicated params", { ...@@ -35,8 +35,8 @@ test_that("lgb.params2str() passes through duplicated params", {
expect_equal(out_str, "objective=regression bagging_fraction=0.8 bagging_fraction=0.5") expect_equal(out_str, "objective=regression bagging_fraction=0.8 bagging_fraction=0.5")
}) })
test_that("lgb.check.eval works as expected with no metric", { test_that(".check_eval works as expected with no metric", {
params <- lgb.check.eval( params <- .check_eval(
params = list(device = "cpu") params = list(device = "cpu")
, eval = "binary_error" , eval = "binary_error"
) )
...@@ -44,8 +44,8 @@ test_that("lgb.check.eval works as expected with no metric", { ...@@ -44,8 +44,8 @@ test_that("lgb.check.eval works as expected with no metric", {
expect_identical(params[["metric"]], list("binary_error")) expect_identical(params[["metric"]], list("binary_error"))
}) })
test_that("lgb.check.eval adds eval to metric in params", { test_that(".check_eval adds eval to metric in params", {
params <- lgb.check.eval( params <- .check_eval(
params = list(metric = "auc") params = list(metric = "auc")
, eval = "binary_error" , eval = "binary_error"
) )
...@@ -53,8 +53,8 @@ test_that("lgb.check.eval adds eval to metric in params", { ...@@ -53,8 +53,8 @@ test_that("lgb.check.eval adds eval to metric in params", {
expect_identical(params[["metric"]], list("auc", "binary_error")) expect_identical(params[["metric"]], list("auc", "binary_error"))
}) })
test_that("lgb.check.eval adds eval to metric in params if two evaluation names are provided", { test_that(".check_eval adds eval to metric in params if two evaluation names are provided", {
params <- lgb.check.eval( params <- .check_eval(
params = list(metric = "auc") params = list(metric = "auc")
, eval = c("binary_error", "binary_logloss") , eval = c("binary_error", "binary_logloss")
) )
...@@ -62,8 +62,8 @@ test_that("lgb.check.eval adds eval to metric in params if two evaluation names ...@@ -62,8 +62,8 @@ test_that("lgb.check.eval adds eval to metric in params if two evaluation names
expect_identical(params[["metric"]], list("auc", "binary_error", "binary_logloss")) expect_identical(params[["metric"]], list("auc", "binary_error", "binary_logloss"))
}) })
test_that("lgb.check.eval adds eval to metric in params if a list is provided", { test_that(".check_eval adds eval to metric in params if a list is provided", {
params <- lgb.check.eval( params <- .check_eval(
params = list(metric = "auc") params = list(metric = "auc")
, eval = list("binary_error", "binary_logloss") , eval = list("binary_error", "binary_logloss")
) )
...@@ -71,8 +71,8 @@ test_that("lgb.check.eval adds eval to metric in params if a list is provided", ...@@ -71,8 +71,8 @@ test_that("lgb.check.eval adds eval to metric in params if a list is provided",
expect_identical(params[["metric"]], list("auc", "binary_error", "binary_logloss")) expect_identical(params[["metric"]], list("auc", "binary_error", "binary_logloss"))
}) })
test_that("lgb.check.eval drops duplicate metrics and preserves order", { test_that(".check_eval drops duplicate metrics and preserves order", {
params <- lgb.check.eval( params <- .check_eval(
params = list(metric = "l1") params = list(metric = "l1")
, eval = list("l2", "rmse", "l1", "rmse") , eval = list("l2", "rmse", "l1", "rmse")
) )
...@@ -80,9 +80,9 @@ test_that("lgb.check.eval drops duplicate metrics and preserves order", { ...@@ -80,9 +80,9 @@ test_that("lgb.check.eval drops duplicate metrics and preserves order", {
expect_identical(params[["metric"]], list("l1", "l2", "rmse")) expect_identical(params[["metric"]], list("l1", "l2", "rmse"))
}) })
test_that("lgb.check.wrapper_param() uses passed-in keyword arg if no alias found in params", { test_that(".check_wrapper_param() uses passed-in keyword arg if no alias found in params", {
kwarg_val <- sample(seq_len(100L), size = 1L) kwarg_val <- sample(seq_len(100L), size = 1L)
params <- lgb.check.wrapper_param( params <- .check_wrapper_param(
main_param_name = "num_iterations" main_param_name = "num_iterations"
, params = list() , params = list()
, alternative_kwarg_value = kwarg_val , alternative_kwarg_value = kwarg_val
...@@ -90,10 +90,10 @@ test_that("lgb.check.wrapper_param() uses passed-in keyword arg if no alias foun ...@@ -90,10 +90,10 @@ test_that("lgb.check.wrapper_param() uses passed-in keyword arg if no alias foun
expect_equal(params[["num_iterations"]], kwarg_val) expect_equal(params[["num_iterations"]], kwarg_val)
}) })
test_that("lgb.check.wrapper_param() prefers main parameter to alias and keyword arg", { test_that(".check_wrapper_param() prefers main parameter to alias and keyword arg", {
num_iterations <- sample(seq_len(100L), size = 1L) num_iterations <- sample(seq_len(100L), size = 1L)
kwarg_val <- sample(seq_len(100L), size = 1L) kwarg_val <- sample(seq_len(100L), size = 1L)
params <- lgb.check.wrapper_param( params <- .check_wrapper_param(
main_param_name = "num_iterations" main_param_name = "num_iterations"
, params = list( , params = list(
num_iterations = num_iterations num_iterations = num_iterations
...@@ -108,11 +108,11 @@ test_that("lgb.check.wrapper_param() prefers main parameter to alias and keyword ...@@ -108,11 +108,11 @@ test_that("lgb.check.wrapper_param() prefers main parameter to alias and keyword
expect_identical(params, list(num_iterations = num_iterations)) expect_identical(params, list(num_iterations = num_iterations))
}) })
test_that("lgb.check.wrapper_param() prefers alias to keyword arg", { test_that(".check_wrapper_param() prefers alias to keyword arg", {
n_estimators <- sample(seq_len(100L), size = 1L) n_estimators <- sample(seq_len(100L), size = 1L)
num_tree <- sample(seq_len(100L), size = 1L) num_tree <- sample(seq_len(100L), size = 1L)
kwarg_val <- sample(seq_len(100L), size = 1L) kwarg_val <- sample(seq_len(100L), size = 1L)
params <- lgb.check.wrapper_param( params <- .check_wrapper_param(
main_param_name = "num_iterations" main_param_name = "num_iterations"
, params = list( , params = list(
num_tree = num_tree num_tree = num_tree
...@@ -124,7 +124,7 @@ test_that("lgb.check.wrapper_param() prefers alias to keyword arg", { ...@@ -124,7 +124,7 @@ test_that("lgb.check.wrapper_param() prefers alias to keyword arg", {
expect_identical(params, list(num_iterations = num_tree)) expect_identical(params, list(num_iterations = num_tree))
# switching the order shouldn't switch which one is chosen # switching the order shouldn't switch which one is chosen
params2 <- lgb.check.wrapper_param( params2 <- .check_wrapper_param(
main_param_name = "num_iterations" main_param_name = "num_iterations"
, params = list( , params = list(
n_estimators = n_estimators n_estimators = n_estimators
...@@ -136,14 +136,14 @@ test_that("lgb.check.wrapper_param() prefers alias to keyword arg", { ...@@ -136,14 +136,14 @@ test_that("lgb.check.wrapper_param() prefers alias to keyword arg", {
expect_identical(params2, list(num_iterations = num_tree)) expect_identical(params2, list(num_iterations = num_tree))
}) })
test_that("lgb.equal.or.both.null produces expected results", { test_that(".equal_or_both_null produces expected results", {
expect_true(lgb.equal.or.both.null(NULL, NULL)) expect_true(.equal_or_both_null(NULL, NULL))
expect_false(lgb.equal.or.both.null(1.0, NULL)) expect_false(.equal_or_both_null(1.0, NULL))
expect_false(lgb.equal.or.both.null(NULL, 1.0)) expect_false(.equal_or_both_null(NULL, 1.0))
expect_true(lgb.equal.or.both.null(1.0, 1.0)) expect_true(.equal_or_both_null(1.0, 1.0))
expect_true(lgb.equal.or.both.null(1.0, 1L)) expect_true(.equal_or_both_null(1.0, 1L))
expect_false(lgb.equal.or.both.null(NA, NULL)) expect_false(.equal_or_both_null(NA, NULL))
expect_false(lgb.equal.or.both.null(NULL, NA)) expect_false(.equal_or_both_null(NULL, NA))
expect_false(lgb.equal.or.both.null(10.0, 1L)) expect_false(.equal_or_both_null(10.0, 1L))
expect_true(lgb.equal.or.both.null(0L, 0L)) expect_true(.equal_or_both_null(0L, 0L))
}) })
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