Skip to content

Commit

Permalink
Merge branch 'master' into feat/cran-install
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslamb committed Jul 24, 2020
2 parents 04906c5 + af51428 commit de114e1
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 13 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,10 @@ lightgbm.model
# VSCode
.vscode

# IntelliJ IDEA
.idea/
# IntelliJ/CLion
.idea
*.iml
/cmake-build-debug/

# Files from local Python install
python-package/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion R-package/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ Imports:
utils
SystemRequirements:
C++11
RoxygenNote: 7.1.0
RoxygenNote: 7.1.1
4 changes: 2 additions & 2 deletions R-package/R/lgb.unloader.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' @name lgb.unloader
#' @title LightGBM unloading error fix
#' @title Remove lightgbm and its objects from an environment
#' @description Attempts to unload LightGBM packages so you can remove objects cleanly without
#' having to restart R. This is useful for instance if an object becomes stuck for no
#' apparent reason and you do not want to restart R to fix the lost object.
Expand Down Expand Up @@ -69,6 +69,6 @@ lgb.unloader <- function(restore = TRUE, wipe = FALSE, envir = .GlobalEnv) {
library(lightgbm)
}

invisible()
return(invisible(NULL))

}
21 changes: 14 additions & 7 deletions R-package/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ lgb.is.null.handle <- function(x) {
}

lgb.encode.char <- function(arr, len) {

if (!is.raw(arr)) {
stop("lgb.encode.char: Can only encode from raw type") # Not an object of type raw
stop("lgb.encode.char: Can only encode from raw type")
}
rawToChar(arr[seq_len(len)]) # Return the conversion of raw type to character type

return(rawToChar(arr[seq_len(len)]))
}

# [description] Raise an error. Before raising that error, check for any error message
Expand Down Expand Up @@ -311,17 +309,26 @@ lgb.check.obj <- function(params, obj) {

}

# [description]
# make sure that "metric" is populated on params,
# and add any eval values to itt
# [return]
# params, where "metric" is a list
lgb.check.eval <- function(params, eval) {

# Check if metric is null, if yes put a list instead
if (is.null(params$metric)) {
params$metric <- list()
} else if (is.character(params$metric)) {
params$metric <- as.list(params$metric)
}

# If 'eval' is a list or character vector, store it in 'metric'
if (is.character(eval) || identical(class(eval), "list")) {
if (is.character(eval)) {
params$metric <- append(params$metric, eval)
}

if (identical(class(eval), "list")) {
params$metric <- append(params$metric, unlist(eval))
}

return(params)
}
2 changes: 1 addition & 1 deletion R-package/man/lgb.unloader.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions R-package/tests/testthat/test_lgb.unloader.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
context("lgb.unloader")

test_that("lgb.unloader works as expected", {
data(agaricus.train, package = "lightgbm")
train <- agaricus.train
dtrain <- lgb.Dataset(train$data, label = train$label)
bst <- lgb.train(
params = list(
objective = "regression"
, metric = "l2"
)
, data = dtrain
, nrounds = 1L
, min_data = 1L
, learning_rate = 1.0
)
expect_true(exists("bst"))
result <- lgb.unloader(restore = TRUE, wipe = TRUE, envir = environment())
expect_false(exists("bst"))
expect_null(result)
})

test_that("lgb.unloader finds all boosters and removes them", {
data(agaricus.train, package = "lightgbm")
train <- agaricus.train
dtrain <- lgb.Dataset(train$data, label = train$label)
bst1 <- lgb.train(
params = list(
objective = "regression"
, metric = "l2"
)
, data = dtrain
, nrounds = 1L
, min_data = 1L
, learning_rate = 1.0
)
bst2 <- lgb.train(
params = list(
objective = "regression"
, metric = "l2"
)
, data = dtrain
, nrounds = 1L
, min_data = 1L
, learning_rate = 1.0
)
expect_true(exists("bst1"))
expect_true(exists("bst2"))
lgb.unloader(restore = TRUE, wipe = TRUE, envir = environment())
expect_false(exists("bst1"))
expect_false(exists("bst2"))
})
56 changes: 56 additions & 0 deletions R-package/tests/testthat/test_utils.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
context("lgb.encode.char")

test_that("lgb.encode.char throws an informative error if it is passed a non-raw input", {
x <- "some-string"
expect_error({
lgb.encode.char(x)
}, regexp = "Can only encode from raw type")
})

context("lgb.check.r6.class")

test_that("lgb.check.r6.class() should return FALSE for NULL input", {
Expand Down Expand Up @@ -68,3 +77,50 @@ test_that("lgb.last_error() correctly returns errors from the C++ side", {
dvalid1$construct()
}, regexp = "[LightGBM] [Fatal] Length of label is not same with #data", fixed = TRUE)
})

context("lgb.check.eval")

test_that("lgb.check.eval works as expected with no metric", {
params <- lgb.check.eval(
params = list(device = "cpu")
, eval = "binary_error"
)
expect_named(params, c("device", "metric"))
expect_identical(params[["metric"]], list("binary_error"))
})

test_that("lgb.check.eval adds eval to metric in params", {
params <- lgb.check.eval(
params = list(metric = "auc")
, eval = "binary_error"
)
expect_named(params, "metric")
expect_identical(params[["metric"]], list("auc", "binary_error"))
})

test_that("lgb.check.eval adds eval to metric in params", {
params <- lgb.check.eval(
params = list(metric = "auc")
, eval = "binary_error"
)
expect_named(params, "metric")
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", {
params <- lgb.check.eval(
params = list(metric = "auc")
, eval = c("binary_error", "binary_logloss")
)
expect_named(params, "metric")
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", {
params <- lgb.check.eval(
params = list(metric = "auc")
, eval = list("binary_error", "binary_logloss")
)
expect_named(params, "metric")
expect_identical(params[["metric"]], list("auc", "binary_error", "binary_logloss"))
})

0 comments on commit de114e1

Please sign in to comment.