test_lgb.plot.importance.R 1.44 KB
Newer Older
1
2
3
4
VERBOSITY <- as.integer(
    Sys.getenv("LIGHTGBM_TEST_VERBOSITY", "-1")
)

5
6
7
8
9
10
11
12
13
14
15
16
17
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
18
        , verbosity = VERBOSITY
19
    )
20
    model <- lgb.train(params, dtrain, 3L)
21
22
23
24
    tree_imp <- lgb.importance(model, percentage = TRUE)

    # Check that there are no plots present before plotting
    expect_null(dev.list())
25
26
27

    args_no_cex <- list(
        "tree_imp" = tree_imp
28
29
30
        , top_n = 10L
        , measure = "Gain"
    )
31
32
    args_cex <- args_no_cex
    args_cex[["cex"]] <- 0.75
33

34
    for (arg_list in list(args_no_cex, args_cex)) {
35

36
37
38
39
        resDT <- do.call(
            what = lgb.plot.importance
            , args = arg_list
        )
40

41
42
43
44
45
46
47
48
49
50
51
        # 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())
    }
52
})