Skip to content

Commit 2487993

Browse files
committed
Use cli package to class printers
1 parent 0b88eec commit 2487993

File tree

11 files changed

+166
-34
lines changed

11 files changed

+166
-34
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Depends:
6262
Imports:
6363
backports,
6464
checkmate,
65+
cli,
6566
data.table,
6667
digest,
6768
lgr,

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ if (getRversion() >= "4.3.0") S3method(chooseOpsMethod,CnfAtom)
221221
if (getRversion() >= "4.3.0") S3method(chooseOpsMethod,CnfClause)
222222
if (getRversion() >= "4.3.0") S3method(chooseOpsMethod,CnfFormula)
223223
import(checkmate)
224+
import(cli)
224225
import(data.table)
225226
import(mlr3)
226227
import(mlr3misc)

R/Graph.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ Graph = R6Class("Graph",
414414
scc = self$edges[, list(sccssors = paste(unique(dst_id), collapse = ",")), by = list(ID = src_id)]
415415
lines = scc[prd[lines, on = "ID"], on = "ID"][, c("ID", "State", "sccssors", "prdcssors")]
416416
lines[is.na(lines)] = ""
417-
catf("Graph with %s PipeOps:", nrow(lines))
417+
cli_h1(sprintf("Graph with %s PipeOps:", nrow(lines)))
418418
## limit column width ##
419419

420420
outwidth = getOption("width") %??% 80 # output width we want (default 80)

R/PipeOp.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,18 +271,18 @@ PipeOp = R6Class("PipeOp",
271271

272272
print = function(...) {
273273
type_table_printout = function(table) {
274-
strings = do.call(sprintf, cbind(fmt = "%s`[%s,%s]", table[, c("name", "train", "predict")]))
275-
strings = strwrap(paste(strings, collapse = ", "), indent = 2, exdent = 2)
276-
if (length(strings) > 6) {
277-
strings = c(strings[1:5], sprintf(" [... (%s lines omitted)]", length(strings) - 5))
274+
print(head(table, 5L), row.names = FALSE, print.keys = FALSE)
275+
if (nrow(table) > 5L) {
276+
catf("[...] (%i rows omitted)", nrow(table) - 5L)
278277
}
279-
gsub("`", " ", paste(strings, collapse = "\n"))
280278
}
281279

282-
catf("PipeOp: <%s> (%strained)", self$id, if (self$is_trained) "" else "not ")
283-
catf("values: <%s>", as_short_string(self$param_set$values))
284-
catf("Input channels <name [train type, predict type]>:\n%s", type_table_printout(self$input))
285-
catf("Output channels <name [train type, predict type]>:\n%s", type_table_printout(self$output))
280+
cli_h1(sprintf("PipeOp %s: %strained", self$id, if (self$is_trained) "" else "not "))
281+
cli_text(sprintf("values: %s", as_short_string(self$param_set$values)))
282+
cli_h3("Input channels:")
283+
type_table_printout(self$input)
284+
cli_h3("Output channels:")
285+
type_table_printout(self$output)
286286
},
287287

288288
train = function(input) {

R/multiplicity.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ print.Multiplicity = function(x, ...) {
5656
if (!length(x)) {
5757
cat("Empty Multiplicity.\n")
5858
} else {
59-
cat("Multiplicity:\n")
59+
cli_h2("Multiplicity:")
6060
print(unclass(x), ...)
6161
}
6262
}

R/zzz.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#' @import data.table
22
#' @import checkmate
3+
#' @import cli
34
#' @import mlr3
45
#' @import paradox
56
#' @import mlr3misc

inst/testthat/helper_functions.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ expect_pipeop = function(po, check_ps_default_values = TRUE) {
9797
expect_class(po$param_set, "ParamSet", label = label)
9898
expect_list(po$param_set$values, names = "unique", label = label)
9999
expect_flag(po$is_trained, label = label)
100-
expect_output(print(po), "PipeOp:", label = label)
100+
expect_message(print(po), "PipeOp", label = label)
101101
expect_character(po$packages, any.missing = FALSE, unique = TRUE, label = label)
102102
expect_function(po$train, nargs = 1)
103103
expect_function(po$predict, nargs = 1)

tests/testthat/_snaps/PipeOp.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# PipeOp printer
2+
3+
Code
4+
print(PipeOpNOP$new())
5+
Message
6+
7+
-- PipeOp nop: not trained -----------------------------------------------------
8+
values: list()
9+
10+
-- Input channels:
11+
Output
12+
name train predict
13+
<char> <char> <char>
14+
input * *
15+
Message
16+
17+
-- Output channels:
18+
Output
19+
name train predict
20+
<char> <char> <char>
21+
output * *
22+
23+
---
24+
25+
Code
26+
print(PipeOpDebugMulti$new(3, 4))
27+
Message
28+
29+
-- PipeOp debug.multi: not trained ---------------------------------------------
30+
values: list()
31+
32+
-- Input channels:
33+
Output
34+
name train predict
35+
<char> <char> <char>
36+
input_1 * *
37+
input_2 * *
38+
input_3 * *
39+
Message
40+
41+
-- Output channels:
42+
Output
43+
name train predict
44+
<char> <char> <char>
45+
output_1 * *
46+
output_2 * *
47+
output_3 * *
48+
output_4 * *
49+
50+
---
51+
52+
Code
53+
print(PipeOpDebugMulti$new(100, 0))
54+
Message
55+
56+
-- PipeOp debug.multi: not trained ---------------------------------------------
57+
values: list()
58+
59+
-- Input channels:
60+
Output
61+
name train predict
62+
<char> <char> <char>
63+
input_1 * *
64+
input_2 * *
65+
input_3 * *
66+
input_4 * *
67+
input_5 * *
68+
[...] (95 rows omitted)
69+
Message
70+
71+
-- Output channels:
72+
Output
73+
name train predict
74+
<char> <char> <char>
75+
output_ * *
76+
77+
---
78+
79+
Code
80+
print(PipeOpBranch$new(c("odin", "dva", "tri")))
81+
Message
82+
83+
-- PipeOp branch: not trained --------------------------------------------------
84+
values: selection=odin
85+
86+
-- Input channels:
87+
Output
88+
name train predict
89+
<char> <char> <char>
90+
input * *
91+
Message
92+
93+
-- Output channels:
94+
Output
95+
name train predict
96+
<char> <char> <char>
97+
odin * *
98+
dva * *
99+
tri * *
100+
101+
---
102+
103+
Code
104+
print(PipeOpLearner$new(mlr_learners$get("classif.debug")))
105+
Message
106+
107+
-- PipeOp classif.debug: not trained -------------------------------------------
108+
values: list()
109+
110+
-- Input channels:
111+
Output
112+
name train predict
113+
<char> <char> <char>
114+
input TaskClassif TaskClassif
115+
Message
116+
117+
-- Output channels:
118+
Output
119+
name train predict
120+
<char> <char> <char>
121+
output NULL PredictionClassif
122+

tests/testthat/test_Graph.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ test_that("linear graph", {
2020

2121
expect_graph(g)
2222

23-
expect_output(print(g), "Graph with 2 PipeOps.*subsample.*UNTRAINED.*pca.*UNTRAINED")
24-
25-
23+
expect_message(print(g), "Graph with 2 PipeOps:")
24+
expect_output(print(g), ".*subsample.*UNTRAINED.*pca.*UNTRAINED")
2625

2726
inputs = mlr_tasks$get("iris")
2827
x = g$train(inputs)
2928
expect_task(x[[1]])
3029

31-
expect_output(print(g), "Graph with 2 PipeOps.*subsample.*list.*pca.*prcomp")
30+
expect_message(print(g), "Graph with 2 PipeOps")
31+
expect_output(print(g), ".*subsample.*list.*pca.*prcomp")
3232

3333
out = g$predict(inputs)
3434
expect_task(x[[1]])

tests/testthat/test_PipeOp.R

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ test_that("PipeOp - General functions", {
99
expect_false(po_1$is_trained)
1010
expect_class(po_1$param_set, "ParamSet")
1111
expect_list(po_1$param_set$values, names = "unique")
12-
expect_output(print(po_1), "PipeOp:")
12+
expect_message(print(po_1), "PipeOp")
1313
expect_equal(po_1$packages, "mlr3pipelines")
1414
expect_null(po_1$state)
1515
assert_subset(po_1$tags, mlr_reflections$pipeops$valid_tags)
@@ -29,22 +29,29 @@ test_that("PipeOp - simple tests with PipeOpScale", {
2929
})
3030

3131
test_that("PipeOp printer", {
32-
expect_output(print(PipeOpNOP$new()),
33-
"PipeOp.*<nop>.*not trained.*values.*list().*Input channels.*input \\[\\*,\\*\\]\n.*Output channels.*output \\[\\*,\\*\\]$")
34-
35-
36-
expect_output(print(PipeOpDebugMulti$new(3, 4)),
37-
"PipeOp.*<debug.multi>.*not trained.*values.*list().*Input channels.*input_1 \\[\\*,\\*\\], input_2 \\[\\*,\\*\\], input_3 \\[\\*,\\*\\]\n.*Output channels.*output_1 \\[\\*,\\*\\], output_2 \\[\\*,\\*\\], output_3 \\[\\*,\\*\\], output_4 \\[\\*,\\*\\]$")
38-
39-
40-
expect_output(print(PipeOpDebugMulti$new(100, 0)),
41-
"\\[\\.\\.\\. \\([0-9]+ lines omitted\\)\\]")
42-
43-
expect_output(print(PipeOpBranch$new(c("odin", "dva", "tri"))),
44-
"Output channels.*odin \\[\\*,\\*\\], dva \\[\\*,\\*\\], tri \\[\\*,\\*\\]$")
45-
46-
expect_output(print(PipeOpLearner$new(mlr_learners$get("classif.debug"))),
47-
"PipeOp.*<classif.debug>.*Input channels.*input \\[TaskClassif,TaskClassif\\]\nOutput channels.*output \\[NULL,PredictionClassif\\]$")
32+
expect_snapshot(print(PipeOpNOP$new()))
33+
expect_snapshot(print(PipeOpDebugMulti$new(3, 4)))
34+
expect_snapshot(print(PipeOpDebugMulti$new(100, 0)))
35+
expect_snapshot(print(PipeOpBranch$new(c("odin", "dva", "tri"))))
36+
expect_snapshot(print(PipeOpLearner$new(mlr_learners$get("classif.debug"))))
37+
38+
39+
# expect_output(print(PipeOpNOP$new()),
40+
# "PipeOp.*<nop>.*not trained.*values.*list().*Input channels.*input \\[\\*,\\*\\]\n.*Output channels.*output \\[\\*,\\*\\]$")
41+
#
42+
#
43+
# expect_output(print(PipeOpDebugMulti$new(3, 4)),
44+
# "PipeOp.*<debug.multi>.*not trained.*values.*list().*Input channels.*input_1 \\[\\*,\\*\\], input_2 \\[\\*,\\*\\], input_3 \\[\\*,\\*\\]\n.*Output channels.*output_1 \\[\\*,\\*\\], output_2 \\[\\*,\\*\\], output_3 \\[\\*,\\*\\], output_4 \\[\\*,\\*\\]$")
45+
#
46+
#
47+
# expect_output(print(PipeOpDebugMulti$new(100, 0)),
48+
# "\\[\\.\\.\\. \\([0-9]+ lines omitted\\)\\]")
49+
#
50+
# expect_output(print(PipeOpBranch$new(c("odin", "dva", "tri"))),
51+
# "Output channels.*odin \\[\\*,\\*\\], dva \\[\\*,\\*\\], tri \\[\\*,\\*\\]$")
52+
#
53+
# expect_output(print(PipeOpLearner$new(mlr_learners$get("classif.debug"))),
54+
# "PipeOp.*<classif.debug>.*Input channels.*input \\[TaskClassif,TaskClassif\\]\nOutput channels.*output \\[NULL,PredictionClassif\\]$")
4855
})
4956

5057
test_that("Prevent creation of PipeOps with no channels", {

0 commit comments

Comments
 (0)