File: test_base_helpLearner.R

package info (click to toggle)
r-cran-mlr 2.19.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,392 kB
  • sloc: ansic: 65; sh: 13; makefile: 5
file content (115 lines) | stat: -rw-r--r-- 3,783 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115


test_that("helpLearner of learner with single help page", {
  testfn = helpLearner
  environment(testfn) = new.env(parent = environment(testfn))

  environment(testfn)$readline = function(x) stop("Was not expecting readline.")

  expect_true(length(quiet(testfn("classif.logreg"))) == 1)
})

test_that("helpLearner of learner with multiple help pages", {
  testfn = helpLearner
  environment(testfn) = new.env(parent = environment(testfn))

  environment(testfn)$readline = function(x) {
    cat(x, "\n")
    0
  }

  expect_output(testfn("classif.qda"), "Choose help page:(\\n[0-9]+ : [0-9a-zA-Z._]+)+\\n\\.\\.\\.: *$")

  expect_null(quiet(testfn("classif.qda")))

  environment(testfn)$readline = function(x) {
    cat(x, "\n")
    1
  }

  hlp1 = quiet(testfn("classif.qda"))

  hlp2 = quiet(testfn("classif.qda"))

  # for regr.randomForest, there is mlr-specific help which should be the first option.
  rfhelp = utils::help("regr.randomForest", package = "mlr")
  # unfortunately, rtest breaks help("regr.randomForest"), so we skip this test if help() is broken.
  if (length(rfhelp) > 0) {
    expect_equal(ignore_attr = TRUE, rfhelp, quiet(testfn("regr.randomForest")))
  }

  environment(testfn)$readline = function(x) {
    cat(x, "\n")
    2
  }

  hlp3 = quiet(testfn("classif.qda"))

  expect_identical(hlp1, hlp2)

  expect_false(identical(hlp1, hlp3))

  # regr.randomForest with option '2' should give the randomForest help page.
  expect_true(length(quiet(testfn("regr.randomForest"))) == 1)
})

test_that("helpLearner of wrapped learner", {
  testfn = helpLearner
  environment(testfn) = new.env(parent = environment(testfn))

  environment(testfn)$readline = function(x) stop("Was not expecting readline.")

  # check that it doesn't give an error
  expect_output(testfn(makeBaggingWrapper(makeLearner("classif.qda"), 2)), "No information about learner")
})

test_that("helpLearnerParam", {
  # mention parameters
  expect_output(helpLearnerParam("classif.qda"), "method")
  expect_output(helpLearnerParam("classif.qda"), "nu")

  expect_output(helpLearnerParam("classif.qda", "nu"), "nu")

  # mention package
  expect_output(helpLearnerParam("classif.qda"), "MASS::qda")

  expect_output(helpLearnerParam("classif.qda", "nu"), "MASS::qda")

  # mention requirement

  nureq = capture.output(print(getParamSet("classif.qda")$pars$nu$requires))
  expect_output(helpLearnerParam("classif.qda", "nu"), paste("Requires:", nureq), fixed = TRUE)

  # error when giving unknown parameter
  expect_error(helpLearnerParam("classif.qda", "this_parameter_does_not_exist"))

  # message when querying parameter without documentation
  expect_output(helpLearnerParam("classif.__mlrmocklearners__2", "alpha"), "No documentation found")

  # check this doesn't give an error
  quiet(helpLearnerParam("classif.__mlrmocklearners__2"))

  # check that values are printed
  expect_output(helpLearnerParam(
    makeLearner("classif.qda", nu = 3), "nu"),
  "Value: +3")

  # values for vectorial params work
  expect_output(helpLearnerParam(
    makeLearner("classif.randomForest", cutoff = c(.1, .2, .3)), "cutoff"),
  "Value:.+0\\.1.+0\\.2.+0\\.3")
})

test_that("helpLearnerParam of wrapped learner", {
  w1 = makeBaggingWrapper(makeLearner("classif.qda", nu = 4), 2)
  w2 = makeOversampleWrapper(w1)

  # correct info is given
  suppressMessages(expect_output(helpLearnerParam(w1, "nu"), "Value: +4"))
  suppressMessages(expect_output(helpLearnerParam(w2, "nu"), "Value: +4"))

  expect_message(quiet(helpLearnerParam(w1)),
    "is a wrapped learner. Showing documentation of 'classif.qda' instead", fixed = TRUE, all = TRUE)
  expect_message(quiet(helpLearnerParam(w2)),
    "is a wrapped learner. Showing documentation of 'classif.qda' instead", fixed = TRUE, all = TRUE)
})