Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R-package] consolidate testing constants in helpers file #5992

Merged
merged 3 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions R-package/tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ref for this file:
#
# * https://r-pkgs.org/testing-design.html#testthat-helper-files
# * https://r-pkgs.org/testing-design.html#testthat-setup-files

# LightGBM-internal fix to comply with CRAN policy of only using up to 2 threads in tests and example.
Expand All @@ -10,3 +11,21 @@
# the check farm is a shared resource and will typically be running many checks simultaneously.
#
.LGB_MAX_THREADS <- 2L

# by default, how much should results in tests be allowed to differ from hard-coded expected numbers?
.LGB_NUMERIC_TOLERANCE <- 1e-6

# are the tests running on Windows?
.LGB_ON_WINDOWS <- .Platform$OS.type == "windows"
.LGB_ON_32_BIT_WINDOWS <- .LGB_ON_WINDOWS && .Machine$sizeof.pointer != 8L

# are the tests running in a UTF-8 locale?
.LGB_UTF8_LOCALE <- all(endsWith(
Sys.getlocale(category = "LC_CTYPE")
, "UTF-8"
))

# control how many loud LightGBM's logger is in tests
.LGB_VERBOSITY <- as.integer(
Sys.getenv("LIGHTGBM_TEST_VERBOSITY", "-1")
)
44 changes: 19 additions & 25 deletions R-package/tests/testthat/test_Predictor.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
library(Matrix)

VERBOSITY <- as.integer(
Sys.getenv("LIGHTGBM_TEST_VERBOSITY", "-1")
)

TOLERANCE <- 1e-6

test_that("Predictor$finalize() should not fail", {
X <- as.matrix(as.integer(iris[, "Species"]), ncol = 1L)
y <- iris[["Sepal.Length"]]
Expand All @@ -16,7 +10,7 @@ test_that("Predictor$finalize() should not fail", {
objective = "regression"
, num_threads = .LGB_MAX_THREADS
)
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
, nrounds = 3L
)
model_file <- tempfile(fileext = ".model")
Expand Down Expand Up @@ -45,7 +39,7 @@ test_that("predictions do not fail for integer input", {
objective = "regression"
, num_threads = .LGB_MAX_THREADS
)
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
, nrounds = 3L
)
X_double <- X[c(1L, 51L, 101L), , drop = FALSE]
Expand Down Expand Up @@ -78,7 +72,7 @@ test_that("start_iteration works correctly", {
num_leaves = 4L
, learning_rate = 0.6
, objective = "binary"
, verbosity = VERBOSITY
, verbosity = .LGB_VERBOSITY
, num_threads = .LGB_MAX_THREADS
)
, nrounds = 50L
Expand Down Expand Up @@ -128,7 +122,7 @@ test_that("Feature contributions from sparse inputs produce sparse outputs", {
data = dtrain
, obj = "regression"
, nrounds = 5L
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
, params = list(min_data_in_leaf = 5L, num_threads = .LGB_MAX_THREADS)
)

Expand Down Expand Up @@ -159,7 +153,7 @@ test_that("Sparse feature contribution predictions do not take inputs with wrong
data = dtrain
, obj = "regression"
, nrounds = 5L
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
, params = list(min_data_in_leaf = 5L, num_threads = .LGB_MAX_THREADS)
)

Expand Down Expand Up @@ -189,7 +183,7 @@ test_that("Feature contribution predictions do not take non-general CSR or CSC i
data = dtrain
, obj = "regression"
, nrounds = 5L
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
, params = list(min_data_in_leaf = 5L, num_threads = .LGB_MAX_THREADS)
)

Expand Down Expand Up @@ -217,14 +211,14 @@ test_that("predict() params should override keyword argument for raw-score predi
, num_threads = .LGB_MAX_THREADS
)
, nrounds = 10L
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
)

# check that the predictions from predict.lgb.Booster() really look like raw score predictions
preds_prob <- predict(bst, X)
preds_raw_s3_keyword <- predict(bst, X, type = "raw")
preds_prob_from_raw <- 1.0 / (1.0 + exp(-preds_raw_s3_keyword))
expect_equal(preds_prob, preds_prob_from_raw, tolerance = TOLERANCE)
expect_equal(preds_prob, preds_prob_from_raw, tolerance = .LGB_NUMERIC_TOLERANCE)
accuracy <- sum(as.integer(preds_prob_from_raw > 0.5) == y) / length(y)
expect_equal(accuracy, 1.0)

Expand Down Expand Up @@ -269,7 +263,7 @@ test_that("predict() params should override keyword argument for leaf-index pred
, num_threads = .LGB_MAX_THREADS
)
, nrounds = 10L
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
)

# check that predictions really look like leaf index predictions
Expand Down Expand Up @@ -323,7 +317,7 @@ test_that("predict() params should override keyword argument for feature contrib
, num_threads = .LGB_MAX_THREADS
)
, nrounds = 10L
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
)

# check that predictions really look like feature contributions
Expand Down Expand Up @@ -431,7 +425,7 @@ test_that("predict() keeps row names from data (regression)", {
data = dtrain
, obj = "regression"
, nrounds = 5L
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
, params = list(min_data_in_leaf = 1L, num_threads = .LGB_MAX_THREADS)
)
.check_all_row_name_expectations(bst, X)
Expand All @@ -447,7 +441,7 @@ test_that("predict() keeps row names from data (binary classification)", {
data = dtrain
, obj = "binary"
, nrounds = 5L
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
, params = list(num_threads = .LGB_MAX_THREADS)
)
.check_all_row_name_expectations(bst, X)
Expand All @@ -464,7 +458,7 @@ test_that("predict() keeps row names from data (multi-class classification)", {
, obj = "multiclass"
, params = list(num_class = 3L, num_threads = .LGB_MAX_THREADS)
, nrounds = 5L
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
)
.check_all_row_name_expectations(bst, X)
})
Expand All @@ -485,7 +479,7 @@ test_that("predictions for regression and binary classification are returned as
data = dtrain
, obj = "regression"
, nrounds = 5L
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
, params = list(min_data_in_leaf = 1L, num_threads = .LGB_MAX_THREADS)
)
pred <- predict(model, X)
Expand All @@ -503,7 +497,7 @@ test_that("predictions for regression and binary classification are returned as
data = dtrain
, obj = "binary"
, nrounds = 5L
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
, params = list(num_threads = .LGB_MAX_THREADS)
)
pred <- predict(model, X)
Expand All @@ -523,7 +517,7 @@ test_that("predictions for multiclass classification are returned as matrix", {
data = dtrain
, obj = "multiclass"
, nrounds = 5L
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
, params = list(num_class = 3L, num_threads = .LGB_MAX_THREADS)
)
pred <- predict(model, X)
Expand Down Expand Up @@ -668,7 +662,7 @@ test_that("predict type='class' returns predicted class for classification objec
data = dtrain
, obj = "binary"
, nrounds = 5L
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
, params = list(num_threads = .LGB_MAX_THREADS)
)
pred <- predict(bst, X, type = "class")
Expand All @@ -682,7 +676,7 @@ test_that("predict type='class' returns predicted class for classification objec
data = dtrain
, obj = "multiclass"
, nrounds = 5L
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
, params = list(num_class = 3L, num_threads = .LGB_MAX_THREADS)
)
pred <- predict(model, X, type = "class")
Expand All @@ -698,7 +692,7 @@ test_that("predict type='class' returns values in the target's range for regress
data = dtrain
, obj = "regression"
, nrounds = 5L
, verbose = VERBOSITY
, verbose = .LGB_VERBOSITY
, params = list(num_threads = .LGB_MAX_THREADS)
)
pred <- predict(bst, X, type = "class")
Expand Down