test_lgb.plot.importance.R 1.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
context("lgb.plot.importance()")

test_that("lgb.plot.importance() should run without error for well-formed inputs", {
    data(agaricus.train, package = "lightgbm")
    train <- agaricus.train
    dtrain <- lgb.Dataset(train$data, label = train$label)
    params <- list(
        objective = "binary"
        , learning_rate = 0.01
        , num_leaves = 63L
        , max_depth = -1L
        , min_data_in_leaf = 1L
        , min_sum_hessian_in_leaf = 1.0
    )
    model <- lgb.train(params, dtrain, 10L)
    tree_imp <- lgb.importance(model, percentage = TRUE)

    # Check that there are no plots present before plotting
    expect_null(dev.list())
20
21
22

    args_no_cex <- list(
        "tree_imp" = tree_imp
23
24
25
        , top_n = 10L
        , measure = "Gain"
    )
26
27
    args_cex <- args_no_cex
    args_cex[["cex"]] <- 0.75
28

29
    for (arg_list in list(args_no_cex, args_cex)) {
30

31
32
33
34
        resDT <- do.call(
            what = lgb.plot.importance
            , args = arg_list
        )
35

36
37
38
39
40
41
42
43
44
45
46
        # Check that lgb.plot.importance() returns the data.table of the plotted data
        expect_true(data.table::is.data.table(resDT))
        expect_named(resDT, c("Feature", "Gain", "Cover", "Frequency"))

        # Check that a plot was produced
        expect_false(is.null(dev.list()))

        # remove all plots
        dev.off()
        expect_null(dev.list())
    }
47
})