[R] Don't write files to user's directory (#9966)
This commit is contained in:
@@ -330,17 +330,17 @@ test_that("training continuation works", {
|
||||
}
|
||||
expect_equal(dim(bst2$evaluation_log), c(2, 2))
|
||||
# test continuing from a model in file
|
||||
xgb.save(bst1, "xgboost.json")
|
||||
bst2 <- xgb.train(param, dtrain, nrounds = 2, watchlist, verbose = 0, xgb_model = "xgboost.json")
|
||||
fname <- file.path(tempdir(), "xgboost.json")
|
||||
xgb.save(bst1, fname)
|
||||
bst2 <- xgb.train(param, dtrain, nrounds = 2, watchlist, verbose = 0, xgb_model = fname)
|
||||
if (!windows_flag && !solaris_flag) {
|
||||
expect_equal(bst$raw, bst2$raw)
|
||||
}
|
||||
expect_equal(dim(bst2$evaluation_log), c(2, 2))
|
||||
file.remove("xgboost.json")
|
||||
})
|
||||
|
||||
test_that("model serialization works", {
|
||||
out_path <- "model_serialization"
|
||||
out_path <- file.path(tempdir(), "model_serialization")
|
||||
dtrain <- xgb.DMatrix(train$data, label = train$label, nthread = n_threads)
|
||||
watchlist <- list(train = dtrain)
|
||||
param <- list(objective = "binary:logistic", nthread = n_threads)
|
||||
|
||||
@@ -174,16 +174,17 @@ test_that("cb.reset.parameters works as expected", {
|
||||
|
||||
test_that("cb.save.model works as expected", {
|
||||
files <- c('xgboost_01.json', 'xgboost_02.json', 'xgboost.json')
|
||||
files <- unname(sapply(files, function(f) file.path(tempdir(), f)))
|
||||
for (f in files) if (file.exists(f)) file.remove(f)
|
||||
|
||||
bst <- xgb.train(param, dtrain, nrounds = 2, watchlist, eta = 1, verbose = 0,
|
||||
save_period = 1, save_name = "xgboost_%02d.json")
|
||||
expect_true(file.exists('xgboost_01.json'))
|
||||
expect_true(file.exists('xgboost_02.json'))
|
||||
b1 <- xgb.load('xgboost_01.json')
|
||||
save_period = 1, save_name = file.path(tempdir(), "xgboost_%02d.json"))
|
||||
expect_true(file.exists(files[1]))
|
||||
expect_true(file.exists(files[2]))
|
||||
b1 <- xgb.load(files[1])
|
||||
xgb.parameters(b1) <- list(nthread = 2)
|
||||
expect_equal(xgb.ntree(b1), 1)
|
||||
b2 <- xgb.load('xgboost_02.json')
|
||||
b2 <- xgb.load(files[2])
|
||||
xgb.parameters(b2) <- list(nthread = 2)
|
||||
expect_equal(xgb.ntree(b2), 2)
|
||||
|
||||
@@ -193,9 +194,9 @@ test_that("cb.save.model works as expected", {
|
||||
|
||||
# save_period = 0 saves the last iteration's model
|
||||
bst <- xgb.train(param, dtrain, nrounds = 2, watchlist, eta = 1, verbose = 0,
|
||||
save_period = 0, save_name = 'xgboost.json')
|
||||
expect_true(file.exists('xgboost.json'))
|
||||
b2 <- xgb.load('xgboost.json')
|
||||
save_period = 0, save_name = file.path(tempdir(), 'xgboost.json'))
|
||||
expect_true(file.exists(files[3]))
|
||||
b2 <- xgb.load(files[3])
|
||||
xgb.config(b2) <- xgb.config(bst)
|
||||
expect_equal(bst$raw, b2$raw)
|
||||
|
||||
@@ -225,14 +226,13 @@ test_that("early stopping xgb.train works", {
|
||||
)
|
||||
expect_equal(bst$evaluation_log, bst0$evaluation_log)
|
||||
|
||||
xgb.save(bst, "model.bin")
|
||||
loaded <- xgb.load("model.bin")
|
||||
fname <- file.path(tempdir(), "model.bin")
|
||||
xgb.save(bst, fname)
|
||||
loaded <- xgb.load(fname)
|
||||
|
||||
expect_false(is.null(loaded$best_iteration))
|
||||
expect_equal(loaded$best_iteration, bst$best_ntreelimit)
|
||||
expect_equal(loaded$best_ntreelimit, bst$best_ntreelimit)
|
||||
|
||||
file.remove("model.bin")
|
||||
})
|
||||
|
||||
test_that("early stopping using a specific metric works", {
|
||||
|
||||
@@ -67,20 +67,22 @@ test_that("xgb.DMatrix: NA", {
|
||||
x[1, "x1"] <- NA
|
||||
|
||||
m <- xgb.DMatrix(x, nthread = n_threads)
|
||||
xgb.DMatrix.save(m, "int.dmatrix")
|
||||
fname_int <- file.path(tempdir(), "int.dmatrix")
|
||||
xgb.DMatrix.save(m, fname_int)
|
||||
|
||||
x <- matrix(as.numeric(x), nrow = n_samples, ncol = 2)
|
||||
colnames(x) <- c("x1", "x2")
|
||||
m <- xgb.DMatrix(x, nthread = n_threads)
|
||||
|
||||
xgb.DMatrix.save(m, "float.dmatrix")
|
||||
fname_float <- file.path(tempdir(), "float.dmatrix")
|
||||
xgb.DMatrix.save(m, fname_float)
|
||||
|
||||
iconn <- file("int.dmatrix", "rb")
|
||||
fconn <- file("float.dmatrix", "rb")
|
||||
iconn <- file(fname_int, "rb")
|
||||
fconn <- file(fname_float, "rb")
|
||||
|
||||
expect_equal(file.size("int.dmatrix"), file.size("float.dmatrix"))
|
||||
expect_equal(file.size(fname_int), file.size(fname_float))
|
||||
|
||||
bytes <- file.size("int.dmatrix")
|
||||
bytes <- file.size(fname_int)
|
||||
idmatrix <- readBin(iconn, "raw", n = bytes)
|
||||
fdmatrix <- readBin(fconn, "raw", n = bytes)
|
||||
|
||||
@@ -90,8 +92,8 @@ test_that("xgb.DMatrix: NA", {
|
||||
close(iconn)
|
||||
close(fconn)
|
||||
|
||||
file.remove("int.dmatrix")
|
||||
file.remove("float.dmatrix")
|
||||
file.remove(fname_int)
|
||||
file.remove(fname_float)
|
||||
})
|
||||
|
||||
test_that("xgb.DMatrix: saving, loading", {
|
||||
@@ -274,17 +276,19 @@ test_that("xgb.DMatrix: Inf as missing", {
|
||||
x_nan[2, 1] <- NA_real_
|
||||
|
||||
m_inf <- xgb.DMatrix(x_inf, nthread = n_threads, missing = Inf)
|
||||
xgb.DMatrix.save(m_inf, "inf.dmatrix")
|
||||
fname_inf <- file.path(tempdir(), "inf.dmatrix")
|
||||
xgb.DMatrix.save(m_inf, fname_inf)
|
||||
|
||||
m_nan <- xgb.DMatrix(x_nan, nthread = n_threads, missing = NA_real_)
|
||||
xgb.DMatrix.save(m_nan, "nan.dmatrix")
|
||||
fname_nan <- file.path(tempdir(), "nan.dmatrix")
|
||||
xgb.DMatrix.save(m_nan, fname_nan)
|
||||
|
||||
infconn <- file("inf.dmatrix", "rb")
|
||||
nanconn <- file("nan.dmatrix", "rb")
|
||||
infconn <- file(fname_inf, "rb")
|
||||
nanconn <- file(fname_nan, "rb")
|
||||
|
||||
expect_equal(file.size("inf.dmatrix"), file.size("nan.dmatrix"))
|
||||
expect_equal(file.size(fname_inf), file.size(fname_nan))
|
||||
|
||||
bytes <- file.size("inf.dmatrix")
|
||||
bytes <- file.size(fname_inf)
|
||||
infdmatrix <- readBin(infconn, "raw", n = bytes)
|
||||
nandmatrix <- readBin(nanconn, "raw", n = bytes)
|
||||
|
||||
@@ -294,8 +298,8 @@ test_that("xgb.DMatrix: Inf as missing", {
|
||||
close(infconn)
|
||||
close(nanconn)
|
||||
|
||||
file.remove("inf.dmatrix")
|
||||
file.remove("nan.dmatrix")
|
||||
file.remove(fname_inf)
|
||||
file.remove(fname_nan)
|
||||
})
|
||||
|
||||
test_that("xgb.DMatrix: error on three-dimensional array", {
|
||||
|
||||
@@ -217,9 +217,9 @@ test_that("xgb-attribute functionality", {
|
||||
xgb.attributes(bst.Tree) <- list.val
|
||||
expect_equal(xgb.attributes(bst.Tree), list.ch)
|
||||
# serializing:
|
||||
xgb.save(bst.Tree, 'xgb.model')
|
||||
bst <- xgb.load('xgb.model')
|
||||
if (file.exists('xgb.model')) file.remove('xgb.model')
|
||||
fname <- file.path(tempdir(), "xgb.model")
|
||||
xgb.save(bst.Tree, fname)
|
||||
bst <- xgb.load(fname)
|
||||
expect_equal(xgb.attr(bst, "my_attr"), val)
|
||||
expect_equal(xgb.attributes(bst), list.ch)
|
||||
# deletion:
|
||||
@@ -256,15 +256,15 @@ if (grepl('Windows', Sys.info()[['sysname']], fixed = TRUE) ||
|
||||
|
||||
test_that("xgb.Booster serializing as R object works", {
|
||||
.skip_if_vcd_not_available()
|
||||
saveRDS(bst.Tree, 'xgb.model.rds')
|
||||
bst <- readRDS('xgb.model.rds')
|
||||
fname_rds <- file.path(tempdir(), "xgb.model.rds")
|
||||
saveRDS(bst.Tree, fname_rds)
|
||||
bst <- readRDS(fname_rds)
|
||||
dtrain <- xgb.DMatrix(sparse_matrix, label = label, nthread = 2)
|
||||
expect_equal(predict(bst.Tree, dtrain), predict(bst, dtrain), tolerance = float_tolerance)
|
||||
expect_equal(xgb.dump(bst.Tree), xgb.dump(bst))
|
||||
xgb.save(bst, 'xgb.model')
|
||||
if (file.exists('xgb.model')) file.remove('xgb.model')
|
||||
bst <- readRDS('xgb.model.rds')
|
||||
if (file.exists('xgb.model.rds')) file.remove('xgb.model.rds')
|
||||
fname_bin <- file.path(tempdir(), "xgb.model")
|
||||
xgb.save(bst, fname_bin)
|
||||
bst <- readRDS(fname_rds)
|
||||
nil_ptr <- new("externalptr")
|
||||
class(nil_ptr) <- "xgb.Booster.handle"
|
||||
expect_true(identical(bst$handle, nil_ptr))
|
||||
|
||||
Reference in New Issue
Block a user