test_dataset.R 2.62 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
require(lightgbm)
require(Matrix)

context("testing lgb.Dataset functionality")

6
7
8
data(agaricus.test, package = "lightgbm")
test_data <- agaricus.test$data[1L:100L, ]
test_label <- agaricus.test$label[1L:100L]
Guolin Ke's avatar
Guolin Ke committed
9
10
11

test_that("lgb.Dataset: basic construction, saving, loading", {
  # from sparse matrix
12
  dtest1 <- lgb.Dataset(test_data, label = test_label)
13
  # from dense matrix
14
  dtest2 <- lgb.Dataset(as.matrix(test_data), label = test_label)
15
  expect_equal(getinfo(dtest1, "label"), getinfo(dtest2, "label"))
16

Guolin Ke's avatar
Guolin Ke committed
17
  # save to a local file
18
  tmp_file <- tempfile("lgb.Dataset_")
Guolin Ke's avatar
Guolin Ke committed
19
20
21
22
23
  lgb.Dataset.save(dtest1, tmp_file)
  # read from a local file
  dtest3 <- lgb.Dataset(tmp_file)
  lgb.Dataset.construct(dtest3)
  unlink(tmp_file)
24
  expect_equal(getinfo(dtest1, "label"), getinfo(dtest3, "label"))
Guolin Ke's avatar
Guolin Ke committed
25
26
27
28
})

test_that("lgb.Dataset: getinfo & setinfo", {
  dtest <- lgb.Dataset(test_data)
29
  dtest$construct()
30

31
32
33
  setinfo(dtest, "label", test_label)
  labels <- getinfo(dtest, "label")
  expect_equal(test_label, getinfo(dtest, "label"))
34

35
36
  expect_true(length(getinfo(dtest, "weight")) == 0L)
  expect_true(length(getinfo(dtest, "init_score")) == 0L)
37

Guolin Ke's avatar
Guolin Ke committed
38
  # any other label should error
39
  expect_error(setinfo(dtest, "asdf", test_label))
Guolin Ke's avatar
Guolin Ke committed
40
41
42
})

test_that("lgb.Dataset: slice, dim", {
43
  dtest <- lgb.Dataset(test_data, label = test_label)
Guolin Ke's avatar
Guolin Ke committed
44
45
  lgb.Dataset.construct(dtest)
  expect_equal(dim(dtest), dim(test_data))
46
  dsub1 <- slice(dtest, seq_len(42L))
Guolin Ke's avatar
Guolin Ke committed
47
  lgb.Dataset.construct(dsub1)
48
  expect_equal(nrow(dsub1), 42L)
Guolin Ke's avatar
Guolin Ke committed
49
50
51
52
  expect_equal(ncol(dsub1), ncol(test_data))
})

test_that("lgb.Dataset: colnames", {
53
  dtest <- lgb.Dataset(test_data, label = test_label)
Guolin Ke's avatar
Guolin Ke committed
54
55
56
  expect_equal(colnames(dtest), colnames(test_data))
  lgb.Dataset.construct(dtest)
  expect_equal(colnames(dtest), colnames(test_data))
57
58
59
60
  expect_error({
    colnames(dtest) <- "asdf"
  })
  new_names <- make.names(seq_len(ncol(test_data)))
Guolin Ke's avatar
Guolin Ke committed
61
62
63
64
65
  expect_silent(colnames(dtest) <- new_names)
  expect_equal(colnames(dtest), new_names)
})

test_that("lgb.Dataset: nrow is correct for a very sparse matrix", {
66
67
  nr <- 1000L
  x <- Matrix::rsparsematrix(nr, 100L, density = 0.0005)
Guolin Ke's avatar
Guolin Ke committed
68
69
70
71
72
  # we want it very sparse, so that last rows are empty
  expect_lt(max(x@i), nr)
  dtest <- lgb.Dataset(x)
  expect_equal(dim(dtest), dim(x))
})
73
74

test_that("lgb.Dataset: Dataset should be able to construct from matrix and return non-null handle", {
75
  rawData <- matrix(runif(1000L), ncol = 10L)
76
77
  handle <- NA_real_
  ref_handle <- NULL
78
79
80
81
82
83
84
85
86
  handle <- lightgbm:::lgb.call(
    "LGBM_DatasetCreateFromMat_R"
    , ret = handle
    , rawData
    , nrow(rawData)
    , ncol(rawData)
    , lightgbm:::lgb.params2str(params = list())
    , ref_handle
  )
87
88
  expect_false(is.na(handle))
})