Skip to content

Commit fc6e74d

Browse files
authored
Merge pull request #846 from mlr-org/cli
feat: add cli package for class printer
2 parents 7fcdcf0 + 74b57b4 commit fc6e74d

File tree

12 files changed

+161
-34
lines changed

12 files changed

+161
-34
lines changed

DESCRIPTION

Lines changed: 2 additions & 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,
@@ -101,6 +102,7 @@ Suggests:
101102
htmlwidgets,
102103
ranger,
103104
themis
105+
Remotes: mlr-org/mlr3misc
104106
ByteCompile: true
105107
Encoding: UTF-8
106108
Config/testthat/edition: 3

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ if (getRversion() >= "4.3.0") S3method(chooseOpsMethod,CnfAtom)
229229
if (getRversion() >= "4.3.0") S3method(chooseOpsMethod,CnfClause)
230230
if (getRversion() >= "4.3.0") S3method(chooseOpsMethod,CnfFormula)
231231
import(checkmate)
232+
import(cli)
232233
import(data.table)
233234
import(mlr3)
234235
import(mlr3misc)

R/Graph.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,15 +414,25 @@ 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+
cat_cli(cli_h1("Graph with {nrow(lines)} PipeOps:"))
418418
## limit column width ##
419419

420420
outwidth = getOption("width") %??% 80 # output width we want (default 80)
421421
colwidths = map_int(lines, function(x) max(nchar(x), na.rm = TRUE)) # original width of columns
422422
collimit = calculate_collimit(colwidths, outwidth)
423+
423424
opts = options(datatable.prettyprint.char = collimit)
424425
on.exit(options(opts), add = TRUE)
425426
print(lines, row.names = FALSE)
427+
428+
is_sequential = all(table(self$edges$src_id) <= 1) && all(table(self$edges$dst_id) <= 1)
429+
if(is_sequential) {
430+
ppunit = paste0(self$ids(), collapse = " -> ")
431+
pp = paste0(c("<INPUT>", ppunit, "<OUTPUT>"), collapse = " -> ")
432+
} else {
433+
pp = "non-sequential"
434+
}
435+
cat_cli(cli_h3("Pipeline: {.strong {pp}}"))
426436
} else {
427437
cat("Empty Graph.\n")
428438
}

R/GraphLearner.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,18 @@ GraphLearner = R6Class("GraphLearner", inherit = Learner,
312312
},
313313
plot = function(html = FALSE, horizontal = FALSE, ...) {
314314
private$.graph$plot(html = html, horizontal = horizontal, ...)
315+
},
316+
print = function() {
317+
super$print(self)
318+
319+
is_sequential = all(table(self$edges$src_id) <= 1) && all(table(self$edges$dst_id) <= 1)
320+
if(is_sequential) {
321+
ppunit = paste0(self$ids(), collapse = " -> ")
322+
pp = paste0(c("<INPUT>", ppunit, "<OUTPUT>"), collapse = " -> ")
323+
} else {
324+
pp = "non-sequential"
325+
}
326+
cat_cli(cli_h3("Pipeline: {.strong {pp}}"))
315327
}
316328
),
317329
active = list(

R/PipeOp.R

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,23 @@ PipeOp = R6Class("PipeOp",
278278

279279
print = function(...) {
280280
type_table_printout = function(table) {
281-
strings = do.call(sprintf, cbind(fmt = "%s`[%s,%s]", table[, c("name", "train", "predict")]))
282-
strings = strwrap(paste(strings, collapse = ", "), indent = 2, exdent = 2)
283-
if (length(strings) > 6) {
284-
strings = c(strings[1:5], sprintf(" [... (%s lines omitted)]", length(strings) - 5))
281+
print(head(table, 5L), row.names = FALSE, print.keys = FALSE)
282+
if (nrow(table) > 5L) {
283+
catf("[...] (%i rows omitted)", nrow(table) - 5L)
285284
}
286-
gsub("`", " ", paste(strings, collapse = "\n"))
287285
}
288286

289-
catf("PipeOp: <%s> (%strained)", self$id, if (self$is_trained) "" else "not ")
290-
catf("values: <%s>", as_short_string(self$param_set$values))
291-
catf("Input channels <name [train type, predict type]>:\n%s", type_table_printout(self$input))
292-
catf("Output channels <name [train type, predict type]>:\n%s", type_table_printout(self$output))
287+
msg_h = if (self$is_trained) "" else "not "
288+
cat_cli({
289+
cli_h1("PipeOp {.cls {self$id}}: {msg_h}trained")
290+
cli_text("Values: {as_short_string(self$param_set$values)}")
291+
cli_h3("{.strong Input channels:}")
292+
})
293+
type_table_printout(self$input)
294+
cat_cli({
295+
cli_h3("{.strong Output channels:}")
296+
})
297+
type_table_printout(self$output)
293298
},
294299

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

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_output(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_output(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]])

0 commit comments

Comments
 (0)