From 2487993de190970dfc03cf93a94e8ce8113e7ed7 Mon Sep 17 00:00:00 2001 From: Lona Date: Thu, 14 Nov 2024 11:19:13 +0100 Subject: [PATCH 1/6] Use cli package to class printers --- DESCRIPTION | 1 + NAMESPACE | 1 + R/Graph.R | 2 +- R/PipeOp.R | 18 ++-- R/multiplicity.R | 2 +- R/zzz.R | 1 + inst/testthat/helper_functions.R | 2 +- tests/testthat/_snaps/PipeOp.md | 122 +++++++++++++++++++++++++++ tests/testthat/test_Graph.R | 8 +- tests/testthat/test_PipeOp.R | 41 +++++---- tests/testthat/test_multiplicities.R | 2 +- 11 files changed, 166 insertions(+), 34 deletions(-) create mode 100644 tests/testthat/_snaps/PipeOp.md diff --git a/DESCRIPTION b/DESCRIPTION index c89039e39..478a87212 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -62,6 +62,7 @@ Depends: Imports: backports, checkmate, + cli, data.table, digest, lgr, diff --git a/NAMESPACE b/NAMESPACE index 05672abde..a153d85e2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -221,6 +221,7 @@ if (getRversion() >= "4.3.0") S3method(chooseOpsMethod,CnfAtom) if (getRversion() >= "4.3.0") S3method(chooseOpsMethod,CnfClause) if (getRversion() >= "4.3.0") S3method(chooseOpsMethod,CnfFormula) import(checkmate) +import(cli) import(data.table) import(mlr3) import(mlr3misc) diff --git a/R/Graph.R b/R/Graph.R index 366f2332b..eb42d65da 100644 --- a/R/Graph.R +++ b/R/Graph.R @@ -414,7 +414,7 @@ Graph = R6Class("Graph", scc = self$edges[, list(sccssors = paste(unique(dst_id), collapse = ",")), by = list(ID = src_id)] lines = scc[prd[lines, on = "ID"], on = "ID"][, c("ID", "State", "sccssors", "prdcssors")] lines[is.na(lines)] = "" - catf("Graph with %s PipeOps:", nrow(lines)) + cli_h1(sprintf("Graph with %s PipeOps:", nrow(lines))) ## limit column width ## outwidth = getOption("width") %??% 80 # output width we want (default 80) diff --git a/R/PipeOp.R b/R/PipeOp.R index b3487b0d6..8e1b037e8 100644 --- a/R/PipeOp.R +++ b/R/PipeOp.R @@ -271,18 +271,18 @@ PipeOp = R6Class("PipeOp", print = function(...) { type_table_printout = function(table) { - strings = do.call(sprintf, cbind(fmt = "%s`[%s,%s]", table[, c("name", "train", "predict")])) - strings = strwrap(paste(strings, collapse = ", "), indent = 2, exdent = 2) - if (length(strings) > 6) { - strings = c(strings[1:5], sprintf(" [... (%s lines omitted)]", length(strings) - 5)) + print(head(table, 5L), row.names = FALSE, print.keys = FALSE) + if (nrow(table) > 5L) { + catf("[...] (%i rows omitted)", nrow(table) - 5L) } - gsub("`", " ", paste(strings, collapse = "\n")) } - catf("PipeOp: <%s> (%strained)", self$id, if (self$is_trained) "" else "not ") - catf("values: <%s>", as_short_string(self$param_set$values)) - catf("Input channels :\n%s", type_table_printout(self$input)) - catf("Output channels :\n%s", type_table_printout(self$output)) + cli_h1(sprintf("PipeOp %s: %strained", self$id, if (self$is_trained) "" else "not ")) + cli_text(sprintf("values: %s", as_short_string(self$param_set$values))) + cli_h3("Input channels:") + type_table_printout(self$input) + cli_h3("Output channels:") + type_table_printout(self$output) }, train = function(input) { diff --git a/R/multiplicity.R b/R/multiplicity.R index a8643d597..d777dac4a 100644 --- a/R/multiplicity.R +++ b/R/multiplicity.R @@ -56,7 +56,7 @@ print.Multiplicity = function(x, ...) { if (!length(x)) { cat("Empty Multiplicity.\n") } else { - cat("Multiplicity:\n") + cli_h2("Multiplicity:") print(unclass(x), ...) } } diff --git a/R/zzz.R b/R/zzz.R index a4333c8a8..34b3aa4d5 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,5 +1,6 @@ #' @import data.table #' @import checkmate +#' @import cli #' @import mlr3 #' @import paradox #' @import mlr3misc diff --git a/inst/testthat/helper_functions.R b/inst/testthat/helper_functions.R index f05d640fe..4eecabeee 100644 --- a/inst/testthat/helper_functions.R +++ b/inst/testthat/helper_functions.R @@ -97,7 +97,7 @@ expect_pipeop = function(po, check_ps_default_values = TRUE) { expect_class(po$param_set, "ParamSet", label = label) expect_list(po$param_set$values, names = "unique", label = label) expect_flag(po$is_trained, label = label) - expect_output(print(po), "PipeOp:", label = label) + expect_message(print(po), "PipeOp", label = label) expect_character(po$packages, any.missing = FALSE, unique = TRUE, label = label) expect_function(po$train, nargs = 1) expect_function(po$predict, nargs = 1) diff --git a/tests/testthat/_snaps/PipeOp.md b/tests/testthat/_snaps/PipeOp.md new file mode 100644 index 000000000..eb46edd82 --- /dev/null +++ b/tests/testthat/_snaps/PipeOp.md @@ -0,0 +1,122 @@ +# PipeOp printer + + Code + print(PipeOpNOP$new()) + Message + + -- PipeOp nop: not trained ----------------------------------------------------- + values: list() + + -- Input channels: + Output + name train predict + + input * * + Message + + -- Output channels: + Output + name train predict + + output * * + +--- + + Code + print(PipeOpDebugMulti$new(3, 4)) + Message + + -- PipeOp debug.multi: not trained --------------------------------------------- + values: list() + + -- Input channels: + Output + name train predict + + input_1 * * + input_2 * * + input_3 * * + Message + + -- Output channels: + Output + name train predict + + output_1 * * + output_2 * * + output_3 * * + output_4 * * + +--- + + Code + print(PipeOpDebugMulti$new(100, 0)) + Message + + -- PipeOp debug.multi: not trained --------------------------------------------- + values: list() + + -- Input channels: + Output + name train predict + + input_1 * * + input_2 * * + input_3 * * + input_4 * * + input_5 * * + [...] (95 rows omitted) + Message + + -- Output channels: + Output + name train predict + + output_ * * + +--- + + Code + print(PipeOpBranch$new(c("odin", "dva", "tri"))) + Message + + -- PipeOp branch: not trained -------------------------------------------------- + values: selection=odin + + -- Input channels: + Output + name train predict + + input * * + Message + + -- Output channels: + Output + name train predict + + odin * * + dva * * + tri * * + +--- + + Code + print(PipeOpLearner$new(mlr_learners$get("classif.debug"))) + Message + + -- PipeOp classif.debug: not trained ------------------------------------------- + values: list() + + -- Input channels: + Output + name train predict + + input TaskClassif TaskClassif + Message + + -- Output channels: + Output + name train predict + + output NULL PredictionClassif + diff --git a/tests/testthat/test_Graph.R b/tests/testthat/test_Graph.R index 0e8501643..41836220c 100644 --- a/tests/testthat/test_Graph.R +++ b/tests/testthat/test_Graph.R @@ -20,15 +20,15 @@ test_that("linear graph", { expect_graph(g) - expect_output(print(g), "Graph with 2 PipeOps.*subsample.*UNTRAINED.*pca.*UNTRAINED") - - + expect_message(print(g), "Graph with 2 PipeOps:") + expect_output(print(g), ".*subsample.*UNTRAINED.*pca.*UNTRAINED") inputs = mlr_tasks$get("iris") x = g$train(inputs) expect_task(x[[1]]) - expect_output(print(g), "Graph with 2 PipeOps.*subsample.*list.*pca.*prcomp") + expect_message(print(g), "Graph with 2 PipeOps") + expect_output(print(g), ".*subsample.*list.*pca.*prcomp") out = g$predict(inputs) expect_task(x[[1]]) diff --git a/tests/testthat/test_PipeOp.R b/tests/testthat/test_PipeOp.R index 6a030929b..bffc2dbe5 100644 --- a/tests/testthat/test_PipeOp.R +++ b/tests/testthat/test_PipeOp.R @@ -9,7 +9,7 @@ test_that("PipeOp - General functions", { expect_false(po_1$is_trained) expect_class(po_1$param_set, "ParamSet") expect_list(po_1$param_set$values, names = "unique") - expect_output(print(po_1), "PipeOp:") + expect_message(print(po_1), "PipeOp") expect_equal(po_1$packages, "mlr3pipelines") expect_null(po_1$state) assert_subset(po_1$tags, mlr_reflections$pipeops$valid_tags) @@ -29,22 +29,29 @@ test_that("PipeOp - simple tests with PipeOpScale", { }) test_that("PipeOp printer", { - expect_output(print(PipeOpNOP$new()), - "PipeOp.*.*not trained.*values.*list().*Input channels.*input \\[\\*,\\*\\]\n.*Output channels.*output \\[\\*,\\*\\]$") - - - expect_output(print(PipeOpDebugMulti$new(3, 4)), - "PipeOp.*.*not trained.*values.*list().*Input channels.*input_1 \\[\\*,\\*\\], input_2 \\[\\*,\\*\\], input_3 \\[\\*,\\*\\]\n.*Output channels.*output_1 \\[\\*,\\*\\], output_2 \\[\\*,\\*\\], output_3 \\[\\*,\\*\\], output_4 \\[\\*,\\*\\]$") - - - expect_output(print(PipeOpDebugMulti$new(100, 0)), - "\\[\\.\\.\\. \\([0-9]+ lines omitted\\)\\]") - - expect_output(print(PipeOpBranch$new(c("odin", "dva", "tri"))), - "Output channels.*odin \\[\\*,\\*\\], dva \\[\\*,\\*\\], tri \\[\\*,\\*\\]$") - - expect_output(print(PipeOpLearner$new(mlr_learners$get("classif.debug"))), - "PipeOp.*.*Input channels.*input \\[TaskClassif,TaskClassif\\]\nOutput channels.*output \\[NULL,PredictionClassif\\]$") + expect_snapshot(print(PipeOpNOP$new())) + expect_snapshot(print(PipeOpDebugMulti$new(3, 4))) + expect_snapshot(print(PipeOpDebugMulti$new(100, 0))) + expect_snapshot(print(PipeOpBranch$new(c("odin", "dva", "tri")))) + expect_snapshot(print(PipeOpLearner$new(mlr_learners$get("classif.debug")))) + + + # expect_output(print(PipeOpNOP$new()), + # "PipeOp.*.*not trained.*values.*list().*Input channels.*input \\[\\*,\\*\\]\n.*Output channels.*output \\[\\*,\\*\\]$") + # + # + # expect_output(print(PipeOpDebugMulti$new(3, 4)), + # "PipeOp.*.*not trained.*values.*list().*Input channels.*input_1 \\[\\*,\\*\\], input_2 \\[\\*,\\*\\], input_3 \\[\\*,\\*\\]\n.*Output channels.*output_1 \\[\\*,\\*\\], output_2 \\[\\*,\\*\\], output_3 \\[\\*,\\*\\], output_4 \\[\\*,\\*\\]$") + # + # + # expect_output(print(PipeOpDebugMulti$new(100, 0)), + # "\\[\\.\\.\\. \\([0-9]+ lines omitted\\)\\]") + # + # expect_output(print(PipeOpBranch$new(c("odin", "dva", "tri"))), + # "Output channels.*odin \\[\\*,\\*\\], dva \\[\\*,\\*\\], tri \\[\\*,\\*\\]$") + # + # expect_output(print(PipeOpLearner$new(mlr_learners$get("classif.debug"))), + # "PipeOp.*.*Input channels.*input \\[TaskClassif,TaskClassif\\]\nOutput channels.*output \\[NULL,PredictionClassif\\]$") }) test_that("Prevent creation of PipeOps with no channels", { diff --git a/tests/testthat/test_multiplicities.R b/tests/testthat/test_multiplicities.R index cbb362fd7..4e0c05530 100644 --- a/tests/testthat/test_multiplicities.R +++ b/tests/testthat/test_multiplicities.R @@ -10,7 +10,7 @@ test_that("Multiplicity class and methods", { expect_multiplicity(assert_multiplicity(nmp)) expect_multiplicity(assert_multiplicity(nmp, check_nesting = TRUE)) expect_error(assert_multiplicity(as.Multiplicity(list(0, Multiplicity(0))), .var.name = "y", check_nesting = TRUE), regexp = "Inconsistent multiplicity nesting level") - expect_output(print(mp), regexp = "Multiplicity:") + expect_message(print(mp), regexp = "Multiplicity:") expect_output(print(Multiplicity()), regexp = "Empty Multiplicity.") }) From 9024161d0a5ca3c260a202e07d3a18103c1dbe61 Mon Sep 17 00:00:00 2001 From: Lona Date: Mon, 25 Nov 2024 11:43:46 +0100 Subject: [PATCH 2/6] Add pipeline information to printer --- R/Graph.R | 8 ++++++++ R/GraphLearner.R | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/R/Graph.R b/R/Graph.R index eb42d65da..aa4670de3 100644 --- a/R/Graph.R +++ b/R/Graph.R @@ -423,6 +423,14 @@ Graph = R6Class("Graph", with_options(list(datatable.prettyprint.char = collimit), { print(lines, row.names = FALSE) }) + + if(all(!duplicated(self$edges[["src_id"]]))) { + ppunit = paste0(self$ids(), collapse = " -> ") + } else { + ppunit = "" + } + pp = paste0(c("", ppunit, ""), collapse = " -> ") + cli_h3(sprintf("Pipeline: %s", pp)) } else { cat("Empty Graph.\n") } diff --git a/R/GraphLearner.R b/R/GraphLearner.R index f519e5007..c94609755 100644 --- a/R/GraphLearner.R +++ b/R/GraphLearner.R @@ -312,6 +312,17 @@ GraphLearner = R6Class("GraphLearner", inherit = Learner, }, plot = function(html = FALSE, horizontal = FALSE, ...) { private$.graph$plot(html = html, horizontal = horizontal, ...) + }, + print = function() { + super$print(self) + + if(all(!duplicated(self$edges[["src_id"]]))) { + ppunit = paste0(self$ids(), collapse = " -> ") + } else { + ppunit = "" + } + pp = paste0(c("", ppunit, ""), collapse = " -> ") + cli_h3(sprintf("Pipeline: %s", pp)) } ), active = list( From 268eab70fbf4382fd0fae1d8c0ec8717a27ab703 Mon Sep 17 00:00:00 2001 From: Lona Date: Fri, 20 Dec 2024 17:39:50 +0100 Subject: [PATCH 3/6] Remove sprintf and add cli inline markup. --- R/Graph.R | 4 ++-- R/GraphLearner.R | 2 +- R/PipeOp.R | 9 +++++---- tests/testthat/_snaps/PipeOp.md | 10 +++++----- tests/testthat/test_PipeOp.R | 18 ------------------ 5 files changed, 13 insertions(+), 30 deletions(-) diff --git a/R/Graph.R b/R/Graph.R index aa4670de3..80b0b183b 100644 --- a/R/Graph.R +++ b/R/Graph.R @@ -414,7 +414,7 @@ Graph = R6Class("Graph", scc = self$edges[, list(sccssors = paste(unique(dst_id), collapse = ",")), by = list(ID = src_id)] lines = scc[prd[lines, on = "ID"], on = "ID"][, c("ID", "State", "sccssors", "prdcssors")] lines[is.na(lines)] = "" - cli_h1(sprintf("Graph with %s PipeOps:", nrow(lines))) + cli_h1("Graph with {nrow(lines)} PipeOps:") ## limit column width ## outwidth = getOption("width") %??% 80 # output width we want (default 80) @@ -430,7 +430,7 @@ Graph = R6Class("Graph", ppunit = "" } pp = paste0(c("", ppunit, ""), collapse = " -> ") - cli_h3(sprintf("Pipeline: %s", pp)) + cli_h3("Pipeline: {.strong {pp}}") } else { cat("Empty Graph.\n") } diff --git a/R/GraphLearner.R b/R/GraphLearner.R index c94609755..1266ea226 100644 --- a/R/GraphLearner.R +++ b/R/GraphLearner.R @@ -322,7 +322,7 @@ GraphLearner = R6Class("GraphLearner", inherit = Learner, ppunit = "" } pp = paste0(c("", ppunit, ""), collapse = " -> ") - cli_h3(sprintf("Pipeline: %s", pp)) + cli_h3("Pipeline: {.strong {pp}}") } ), active = list( diff --git a/R/PipeOp.R b/R/PipeOp.R index 8e1b037e8..8e61d9a6d 100644 --- a/R/PipeOp.R +++ b/R/PipeOp.R @@ -277,11 +277,12 @@ PipeOp = R6Class("PipeOp", } } - cli_h1(sprintf("PipeOp %s: %strained", self$id, if (self$is_trained) "" else "not ")) - cli_text(sprintf("values: %s", as_short_string(self$param_set$values))) - cli_h3("Input channels:") + msg_h = if (self$is_trained) "" else "not " + cli_h1("PipeOp {self$id}: {msg_h}trained") + cli_text("values: {.arg {as_short_string(self$param_set$values)}}") + cli_h3("{.strong Input channels:}") type_table_printout(self$input) - cli_h3("Output channels:") + cli_h3("{.strong Output channels:}") type_table_printout(self$output) }, diff --git a/tests/testthat/_snaps/PipeOp.md b/tests/testthat/_snaps/PipeOp.md index eb46edd82..f58944a69 100644 --- a/tests/testthat/_snaps/PipeOp.md +++ b/tests/testthat/_snaps/PipeOp.md @@ -5,7 +5,7 @@ Message -- PipeOp nop: not trained ----------------------------------------------------- - values: list() + values: `list()` -- Input channels: Output @@ -27,7 +27,7 @@ Message -- PipeOp debug.multi: not trained --------------------------------------------- - values: list() + values: `list()` -- Input channels: Output @@ -54,7 +54,7 @@ Message -- PipeOp debug.multi: not trained --------------------------------------------- - values: list() + values: `list()` -- Input channels: Output @@ -81,7 +81,7 @@ Message -- PipeOp branch: not trained -------------------------------------------------- - values: selection=odin + values: `selection=odin` -- Input channels: Output @@ -105,7 +105,7 @@ Message -- PipeOp classif.debug: not trained ------------------------------------------- - values: list() + values: `list()` -- Input channels: Output diff --git a/tests/testthat/test_PipeOp.R b/tests/testthat/test_PipeOp.R index bffc2dbe5..6a0f140fa 100644 --- a/tests/testthat/test_PipeOp.R +++ b/tests/testthat/test_PipeOp.R @@ -34,24 +34,6 @@ test_that("PipeOp printer", { expect_snapshot(print(PipeOpDebugMulti$new(100, 0))) expect_snapshot(print(PipeOpBranch$new(c("odin", "dva", "tri")))) expect_snapshot(print(PipeOpLearner$new(mlr_learners$get("classif.debug")))) - - - # expect_output(print(PipeOpNOP$new()), - # "PipeOp.*.*not trained.*values.*list().*Input channels.*input \\[\\*,\\*\\]\n.*Output channels.*output \\[\\*,\\*\\]$") - # - # - # expect_output(print(PipeOpDebugMulti$new(3, 4)), - # "PipeOp.*.*not trained.*values.*list().*Input channels.*input_1 \\[\\*,\\*\\], input_2 \\[\\*,\\*\\], input_3 \\[\\*,\\*\\]\n.*Output channels.*output_1 \\[\\*,\\*\\], output_2 \\[\\*,\\*\\], output_3 \\[\\*,\\*\\], output_4 \\[\\*,\\*\\]$") - # - # - # expect_output(print(PipeOpDebugMulti$new(100, 0)), - # "\\[\\.\\.\\. \\([0-9]+ lines omitted\\)\\]") - # - # expect_output(print(PipeOpBranch$new(c("odin", "dva", "tri"))), - # "Output channels.*odin \\[\\*,\\*\\], dva \\[\\*,\\*\\], tri \\[\\*,\\*\\]$") - # - # expect_output(print(PipeOpLearner$new(mlr_learners$get("classif.debug"))), - # "PipeOp.*.*Input channels.*input \\[TaskClassif,TaskClassif\\]\nOutput channels.*output \\[NULL,PredictionClassif\\]$") }) test_that("Prevent creation of PipeOps with no channels", { From 8da9f9742808fc491389c8b21bb1dce3bad8b20d Mon Sep 17 00:00:00 2001 From: Lona Date: Sun, 12 Jan 2025 23:39:23 +0100 Subject: [PATCH 4/6] Transform printout from message to output --- R/Graph.R | 4 +-- R/GraphLearner.R | 2 +- R/PipeOp.R | 12 ++++++--- inst/testthat/helper_functions.R | 2 +- tests/testthat/_snaps/PipeOp.md | 45 +++++++++++--------------------- tests/testthat/test_Graph.R | 4 +-- tests/testthat/test_PipeOp.R | 2 +- 7 files changed, 30 insertions(+), 41 deletions(-) diff --git a/R/Graph.R b/R/Graph.R index 80b0b183b..46bc38987 100644 --- a/R/Graph.R +++ b/R/Graph.R @@ -414,7 +414,7 @@ Graph = R6Class("Graph", scc = self$edges[, list(sccssors = paste(unique(dst_id), collapse = ",")), by = list(ID = src_id)] lines = scc[prd[lines, on = "ID"], on = "ID"][, c("ID", "State", "sccssors", "prdcssors")] lines[is.na(lines)] = "" - cli_h1("Graph with {nrow(lines)} PipeOps:") + cat_cli(cli_h1("Graph with {nrow(lines)} PipeOps:")) ## limit column width ## outwidth = getOption("width") %??% 80 # output width we want (default 80) @@ -430,7 +430,7 @@ Graph = R6Class("Graph", ppunit = "" } pp = paste0(c("", ppunit, ""), collapse = " -> ") - cli_h3("Pipeline: {.strong {pp}}") + cat_cli(cli_h3("Pipeline: {.strong {pp}}")) } else { cat("Empty Graph.\n") } diff --git a/R/GraphLearner.R b/R/GraphLearner.R index 1266ea226..a36ecfdfc 100644 --- a/R/GraphLearner.R +++ b/R/GraphLearner.R @@ -322,7 +322,7 @@ GraphLearner = R6Class("GraphLearner", inherit = Learner, ppunit = "" } pp = paste0(c("", ppunit, ""), collapse = " -> ") - cli_h3("Pipeline: {.strong {pp}}") + cat_cli(cli_h3("Pipeline: {.strong {pp}}")) } ), active = list( diff --git a/R/PipeOp.R b/R/PipeOp.R index 8e61d9a6d..a3e4d2a01 100644 --- a/R/PipeOp.R +++ b/R/PipeOp.R @@ -278,11 +278,15 @@ PipeOp = R6Class("PipeOp", } msg_h = if (self$is_trained) "" else "not " - cli_h1("PipeOp {self$id}: {msg_h}trained") - cli_text("values: {.arg {as_short_string(self$param_set$values)}}") - cli_h3("{.strong Input channels:}") + cat_cli({ + cli_h1("PipeOp {.cls {self$id}}: {msg_h}trained") + cli_text("Values: {as_short_string(self$param_set$values)}") + cli_h3("{.strong Input channels:}") + }) type_table_printout(self$input) - cli_h3("{.strong Output channels:}") + cat_cli({ + cli_h3("{.strong Output channels:}") + }) type_table_printout(self$output) }, diff --git a/inst/testthat/helper_functions.R b/inst/testthat/helper_functions.R index 4eecabeee..c8056552a 100644 --- a/inst/testthat/helper_functions.R +++ b/inst/testthat/helper_functions.R @@ -97,7 +97,7 @@ expect_pipeop = function(po, check_ps_default_values = TRUE) { expect_class(po$param_set, "ParamSet", label = label) expect_list(po$param_set$values, names = "unique", label = label) expect_flag(po$is_trained, label = label) - expect_message(print(po), "PipeOp", label = label) + expect_output(print(po), "PipeOp", label = label) expect_character(po$packages, any.missing = FALSE, unique = TRUE, label = label) expect_function(po$train, nargs = 1) expect_function(po$predict, nargs = 1) diff --git a/tests/testthat/_snaps/PipeOp.md b/tests/testthat/_snaps/PipeOp.md index f58944a69..880a6aeed 100644 --- a/tests/testthat/_snaps/PipeOp.md +++ b/tests/testthat/_snaps/PipeOp.md @@ -2,20 +2,17 @@ Code print(PipeOpNOP$new()) - Message + Output - -- PipeOp nop: not trained ----------------------------------------------------- - values: `list()` + -- PipeOp : not trained --------------------------------------------------- + Values: list() -- Input channels: - Output name train predict input * * - Message -- Output channels: - Output name train predict output * * @@ -24,22 +21,19 @@ Code print(PipeOpDebugMulti$new(3, 4)) - Message + Output - -- PipeOp debug.multi: not trained --------------------------------------------- - values: `list()` + -- PipeOp : not trained ------------------------------------------- + Values: list() -- Input channels: - Output name train predict input_1 * * input_2 * * input_3 * * - Message -- Output channels: - Output name train predict output_1 * * @@ -51,13 +45,12 @@ Code print(PipeOpDebugMulti$new(100, 0)) - Message + Output - -- PipeOp debug.multi: not trained --------------------------------------------- - values: `list()` + -- PipeOp : not trained ------------------------------------------- + Values: list() -- Input channels: - Output name train predict input_1 * * @@ -66,10 +59,8 @@ input_4 * * input_5 * * [...] (95 rows omitted) - Message -- Output channels: - Output name train predict output_ * * @@ -78,20 +69,17 @@ Code print(PipeOpBranch$new(c("odin", "dva", "tri"))) - Message + Output - -- PipeOp branch: not trained -------------------------------------------------- - values: `selection=odin` + -- PipeOp : not trained ------------------------------------------------ + Values: selection=odin -- Input channels: - Output name train predict input * * - Message -- Output channels: - Output name train predict odin * * @@ -102,20 +90,17 @@ Code print(PipeOpLearner$new(mlr_learners$get("classif.debug"))) - Message + Output - -- PipeOp classif.debug: not trained ------------------------------------------- - values: `list()` + -- PipeOp : not trained ----------------------------------------- + Values: list() -- Input channels: - Output name train predict input TaskClassif TaskClassif - Message -- Output channels: - Output name train predict output NULL PredictionClassif diff --git a/tests/testthat/test_Graph.R b/tests/testthat/test_Graph.R index 41836220c..b3ac9ee93 100644 --- a/tests/testthat/test_Graph.R +++ b/tests/testthat/test_Graph.R @@ -20,14 +20,14 @@ test_that("linear graph", { expect_graph(g) - expect_message(print(g), "Graph with 2 PipeOps:") + expect_output(print(g), "Graph with 2 PipeOps:") expect_output(print(g), ".*subsample.*UNTRAINED.*pca.*UNTRAINED") inputs = mlr_tasks$get("iris") x = g$train(inputs) expect_task(x[[1]]) - expect_message(print(g), "Graph with 2 PipeOps") + expect_output(print(g), "Graph with 2 PipeOps") expect_output(print(g), ".*subsample.*list.*pca.*prcomp") out = g$predict(inputs) diff --git a/tests/testthat/test_PipeOp.R b/tests/testthat/test_PipeOp.R index 6a0f140fa..daca39329 100644 --- a/tests/testthat/test_PipeOp.R +++ b/tests/testthat/test_PipeOp.R @@ -9,7 +9,7 @@ test_that("PipeOp - General functions", { expect_false(po_1$is_trained) expect_class(po_1$param_set, "ParamSet") expect_list(po_1$param_set$values, names = "unique") - expect_message(print(po_1), "PipeOp") + expect_output(print(po_1), "PipeOp") expect_equal(po_1$packages, "mlr3pipelines") expect_null(po_1$state) assert_subset(po_1$tags, mlr_reflections$pipeops$valid_tags) From 7f54f742340bb192cc106dc650b2173d82237900 Mon Sep 17 00:00:00 2001 From: Lona Date: Sun, 12 Jan 2025 23:44:24 +0100 Subject: [PATCH 5/6] Change pipeline information in print --- R/Graph.R | 7 ++++--- R/GraphLearner.R | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/R/Graph.R b/R/Graph.R index 46bc38987..d5fdc09d8 100644 --- a/R/Graph.R +++ b/R/Graph.R @@ -424,12 +424,13 @@ Graph = R6Class("Graph", print(lines, row.names = FALSE) }) - if(all(!duplicated(self$edges[["src_id"]]))) { + is_sequential = all(table(self$edges$src_id) <= 1) && all(table(self$edges$dst_id) <= 1) + if(is_sequential) { ppunit = paste0(self$ids(), collapse = " -> ") + pp = paste0(c("", ppunit, ""), collapse = " -> ") } else { - ppunit = "" + pp = "non-sequential" } - pp = paste0(c("", ppunit, ""), collapse = " -> ") cat_cli(cli_h3("Pipeline: {.strong {pp}}")) } else { cat("Empty Graph.\n") diff --git a/R/GraphLearner.R b/R/GraphLearner.R index a36ecfdfc..ff85fd669 100644 --- a/R/GraphLearner.R +++ b/R/GraphLearner.R @@ -316,12 +316,13 @@ GraphLearner = R6Class("GraphLearner", inherit = Learner, print = function() { super$print(self) - if(all(!duplicated(self$edges[["src_id"]]))) { + is_sequential = all(table(self$edges$src_id) <= 1) && all(table(self$edges$dst_id) <= 1) + if(is_sequential) { ppunit = paste0(self$ids(), collapse = " -> ") + pp = paste0(c("", ppunit, ""), collapse = " -> ") } else { - ppunit = "" + pp = "non-sequential" } - pp = paste0(c("", ppunit, ""), collapse = " -> ") cat_cli(cli_h3("Pipeline: {.strong {pp}}")) } ), From b0df928a77b3ad02290d9f4bf4a63b8b1a3efc8b Mon Sep 17 00:00:00 2001 From: Lona Date: Mon, 13 Jan 2025 13:09:47 +0100 Subject: [PATCH 6/6] Change mlr3misc import to Remotes --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 478a87212..6780b1180 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -104,6 +104,7 @@ Suggests: htmlwidgets, ranger, themis +Remotes: mlr-org/mlr3misc ByteCompile: true Encoding: UTF-8 Config/testthat/edition: 3