test_lgb.plot.importance.R 1.41 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
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
16
        , verbosity = VERBOSITY
17
    )
18
    model <- lgb.train(params, dtrain, 3L)
19
20
21
22
    tree_imp <- lgb.importance(model, percentage = TRUE)

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

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

32
    for (arg_list in list(args_no_cex, args_cex)) {
33

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

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