Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cli package for class printer #1206

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ Imports:
R6 (>= 2.4.1),
backports,
checkmate (>= 2.0.0),
cli,
data.table (>= 1.15.0),
evaluate,
future,
future.apply (>= 1.5.0),
lgr (>= 0.3.4),
mlbench,
mlr3measures (>= 1.0.0),
mlr3misc (>= 0.15.0),
mlr3misc,
parallelly,
palmerpenguins,
paradox (>= 1.0.1),
Expand All @@ -70,6 +71,7 @@ Suggests:
RhpcBLASctl,
rpart,
testthat (>= 3.2.0)
Remotes: mlr-org/mlr3misc
Encoding: UTF-8
Config/testthat/edition: 3
Config/testthat/parallel: false
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export(tsks)
export(unmarshal_model)
export(warn_deprecated)
import(checkmate)
import(cli)
import(data.table)
import(mlr3misc)
import(palmerpenguins)
Expand Down
3 changes: 1 addition & 2 deletions R/BenchmarkResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ BenchmarkResult = R6Class("BenchmarkResult",
print = function() {
tab = self$aggregate(measures = list(), conditions = TRUE)
setattr(tab, "class", c("data.table", "data.frame"))
catf("%s of %i rows with %i resampling runs",
format(self), private$.data$iterations(), nrow(tab))
cat_cli(cli_h1("{.cls {class(self)[1L]}} of {.val {private$.data$iterations()}} rows with {.val {nrow(tab)}} resampling run"))
if (nrow(tab)) {
tab = remove_named(tab, c("uhash", "resample_result"))
print(tab, class = FALSE, row.names = FALSE, print.keys = FALSE, digits = 3)
Expand Down
2 changes: 1 addition & 1 deletion R/DataBackend.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ DataBackend = R6Class("DataBackend", cloneable = FALSE,
#' Printer.
print = function() {
nr = self$nrow
catf("%s (%ix%i)", format(self), nr, self$ncol)
cat_cli(cli_h1("{.cls {class(self)[1L]}} ({.val {nr}}x{.val {self$ncol}})"))
print(self$head(6L), row.names = FALSE, print.keys = FALSE)
if (nr > 6L) {
catf("[...] (%i rows omitted)", nr - 6L)
Expand Down
2 changes: 1 addition & 1 deletion R/HotstartStack.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ HotstartStack = R6Class("HotstartStack",
#'
#' @param ... (ignored).
print = function(...) {
catf(format(self))
cat_cli(cli_h1("{.cls {class(self)[1L]}}"))
print(self$stack, digits = 2)
}
),
Expand Down
32 changes: 22 additions & 10 deletions R/Learner.R
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,33 @@ Learner = R6Class("Learner",
#' Printer.
#' @param ... (ignored).
print = function(...) {
catn(format(self), if (is.null(self$label) || is.na(self$label)) "" else paste0(": ", self$label))
catn(str_indent("* Model:", if (is.null(self$model)) "-" else if (is_marshaled_model(self$model)) "<marshaled>" else paste0(class(self$model)[1L])))
catn(str_indent("* Parameters:", as_short_string(self$param_set$values, 1000L)))
if (exists("validate", self)) catn(str_indent("* Validate:", format(self$validate)))
catn(str_indent("* Packages:", self$packages))
catn(str_indent("* Predict Types: ", replace(self$predict_types, self$predict_types == self$predict_type, paste0("[", self$predict_type, "]"))))
catn(str_indent("* Feature Types:", self$feature_types))
catn(str_indent("* Properties:", self$properties))
msg_h = if (is.null(self$label) || is.na(self$label)) "" else paste0(": ", self$label)
model = if (is.null(self$model)) "-" else if (is_marshaled_model(self$model)) "<marshaled>" else paste0(class(self$model)[1L])

cat_cli({
cli_h1("{.cls {class(self)[1L]}} ({self$id}){msg_h}")
cli_li("Model: {model}")
cli_li("Parameters: {as_short_string(self$param_set$values, 1000L)}")
})

if (exists("validate", self)) cat_cli(cli_li("Validate: {.cls {class(self$validate[1])}} {self$validate$id}"))
cat_cli(cli_li("Packages: {.pkg {self$packages}}"))

pred_typs = replace(self$predict_types, self$predict_types == self$predict_type, paste0("[", self$predict_type, "]"))

cat_cli({
cli_li("Predict Types: {pred_typs}")
cli_li("Feature Types: {self$feature_types}")
cli_li("Properties: {self$properties}")
})

w = self$warnings
e = self$errors
if (length(w)) {
catn(str_indent("* Warnings:", w))
cat_cli(cli_alert_warning("Warnings: {w}"))
}
if (length(e)) {
catn(str_indent("* Errors:", e))
cat_cli(cli_alert_danger("Errors: {e}"))
}
},

Expand Down
21 changes: 13 additions & 8 deletions R/Measure.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,19 @@ Measure = R6Class("Measure",
#' Printer.
#' @param ... (ignored).
print = function(...) {
catn(format(self), if (is.null(self$label) || is.na(self$label)) "" else paste0(": ", self$label))
catn(str_indent("* Packages:", self$packages))
catn(str_indent("* Range:", sprintf("[%g, %g]", self$range[1L], self$range[2L])))
catn(str_indent("* Minimize:", self$minimize))
catn(str_indent("* Average:", self$average))
catn(str_indent("* Parameters:", as_short_string(self$param_set$values, 1000L)))
catn(str_indent("* Properties:", self$properties))
catn(str_indent("* Predict type:", self$predict_type))
msg_h = if (is.null(self$label) || is.na(self$label)) "" else paste0(": ", self$label)
msg_properties = if (length(self$properties)) self$properties else "-"
cat_cli({
cli_h1("{.cls {class(self)[1L]}} ({self$id}){msg_h}")
cli_li("Packages: {.pkg {self$packages}}")
cli_li("Range: [{self$range[1L]}, {self$range[2L]}]")
cli_li("Minimize: {.val {self$minimize}}")
cli_li("Average: {self$average}")
cli_li("Parameters: {as_short_string(self$param_set$values, 1000L)}")
cli_li("Properties: {msg_properties}")
cli_li("Predict type: {self$predict_type}")
cli_li("Predict sets: {self$predict_sets}")
})
},

#' @description
Expand Down
4 changes: 2 additions & 2 deletions R/Prediction.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ Prediction = R6Class("Prediction",
print = function(...) {
n = length(self$data$row_ids)
if (n == 0L) {
catf("%s for 0 observations", format(self))
cat_cli(cli_h1("{.cls {class(self)[1L]}} for {.val 0} observations"))
} else {
data = as.data.table(self)
catf("%s for %i observations:", format(self), n)
cat_cli(cli_h1("{.cls {class(self)[1L]}} for {.val {n}} observations:"))
print(data, nrows = 10L, topn = 3L, class = FALSE, row.names = FALSE, print.keys = FALSE)
}
},
Expand Down
2 changes: 1 addition & 1 deletion R/ResampleResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ ResampleResult = R6Class("ResampleResult",
setattr(tab, "class", c("data.table", "data.frame"))
tab[, "warnings" := map(get("warnings"), length)]
tab[, "errors" := map(get("errors"), length)]
catf("%s with %i resampling iterations", format(self), self$iters)
cat_cli(cli_h1("{.cls {class(self)[1L]}} with {.val {self$iters}} resampling iterations"))
if (nrow(tab)) {
tab = remove_named(tab, c("task", "learner", "resampling", "prediction"))
print(tab, class = FALSE, row.names = FALSE, print.keys = FALSE, digits = 3)
Expand Down
11 changes: 7 additions & 4 deletions R/Resampling.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,13 @@ Resampling = R6Class("Resampling",
#' Printer.
#' @param ... (ignored).
print = function(...) {
catn(format(self), if (is.null(self$label) || is.na(self$label)) "" else paste0(": ", self$label))
catn(str_indent("* Iterations:", self$iters))
catn(str_indent("* Instantiated:", self$is_instantiated))
catn(str_indent("* Parameters:", as_short_string(self$param_set$values, 1000L)))
msg_h = if (is.null(self$label) || is.na(self$label)) "" else paste0(": ", self$label)
cat_cli({
cli_h1("{.cls {class(self)[1L]}} {msg_h}")
cli_li("Iterations: {.val {self$iters}}")
cli_li("Instantiated: {.val {self$is_instantiated}}")
cli_li("Parameters: {as_short_string(self$param_set$values, 1000L)}")
})
},

#' @description
Expand Down
59 changes: 42 additions & 17 deletions R/Task.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,44 +214,69 @@ Task = R6Class("Task",
#' Printer.
#' @param ... (ignored).
print = function(...) {
catf("%s (%i x %i)%s", format(self), self$nrow, self$ncol,
if (is.null(self$label) || is.na(self$label)) "" else paste0(": ", self$label))
msg_h = if (is.null(self$label) || is.na(self$label)) "" else paste0(": ", self$label)
cat_cli(cli_h1("{.cls {class(self)[1L]}} ({self$nrow}x{self$ncol}){msg_h}"))

roles = private$.col_roles
roles = roles[lengths(roles) > 0L]

# print additional columns as specified in reflections
before = mlr_reflections$task_print_col_roles$before
iwalk(before[before %chin% names(roles)], function(role, str) {
catn(str_indent(sprintf("* %s:", str), roles[[role]]))
cat_cli(cli_li("{str}: {roles[[role]]}"))
})

catf(str_indent("* Target:", self$target_names))
catf(str_indent("* Properties:", self$properties))
cat_cli(cli_li("Target: {self$target_names}"))

if (class(self)[1L] == "TaskClassif") {
if (!is.null(self$backend)) {
class_freqs = table(self$truth()) / self$nrow * 100
class_freqs = class_freqs[order(-class_freqs, names(class_freqs))] # Order by class frequency, then names
classes = if ("twoclass" %in% self$properties) {
sprintf("%s (positive class, %.0f%%), %s (%.0f%%)",
self$positive, class_freqs[[self$positive]], self$negative, class_freqs[[self$negative]])
} else {
paste(sprintf("%s (%.0f%%)", names(class_freqs), class_freqs), collapse = ", ")
}
} else {
classes = paste(self$class_names, collapse = ", ")
}
cat_cli(cli_li("Target classes: {classes}"))
}

properties = if (length(self$properties)) paste(self$properties, collapse = ", ") else "-"
cat_cli(cli_li("Properties: {properties}"))

types = self$feature_types
if (nrow(types)) {
id = type = NULL
catf("* Features (%i):", nrow(types))
types = types[, list(N = .N, feats = str_collapse(id, n = 100L)), by = "type"][, "type" := translate_types(type)]
setorderv(types, "N", order = -1L)
pmap(types, function(type, N, feats) {
catn(str_indent(sprintf(" - %s (%i):", type, N), feats, exdent = 4L))
})
}

cat_cli({
if (nrow(types)) {
id = type = NULL
cli_li("Features ({nrow(types)}):")
types = types[, list(N = .N, feats = str_collapse(id, n = 100L)), by = "type"][, "type" := translate_types(type)]
setorderv(types, "N", order = -1L)

ulid <- cli_ul()
pmap(types, function(type, N, feats) {
cli_li("{type} ({N}): {feats}")
})
cli_end(ulid)
}
})


# print additional columns are specified in reflections
after = mlr_reflections$task_print_col_roles$after
iwalk(after[after %chin% names(roles)], function(role, str) {
catn(str_indent(sprintf("* %s:", str), roles[[role]]))
cat_cli(cli_li("{str}: {roles[[role]]}"))
})

if (!is.null(private$.internal_valid_task)) {
catf(str_indent("* Validation Task:", sprintf("(%ix%i)", private$.internal_valid_task$nrow, private$.internal_valid_task$ncol)))
cat_cli(cli_li("Validation Task: ({private$.internal_valid_task$nrow}x{private$.internal_valid_task$ncol})"))
}

if (!is.null(self$characteristics)) {
catf(str_indent("* Characteristics: ", as_short_string(self$characteristics)))
cat_cli(cli_li("Characteristics: {as_short_string(self$characteristics)}"))
}
},

Expand Down
13 changes: 8 additions & 5 deletions R/TaskGenerator.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ TaskGenerator = R6Class("TaskGenerator",
#' Printer.
#' @param ... (ignored).
print = function(...) {
catn(format(self), if (is.null(self$label) || is.na(self$label)) "" else paste0(": ", self$label))
catn(str_indent("* Task type:", self$task_type))
catn(str_indent("* Packages:", self$packages))
catn(str_indent("* Parameters:", as_short_string(self$param_set$values, 1000L)))
catn(str_indent("* Manual:", sprintf("?%s", self$man)))
cat_cli({
msg_h = if (is.null(self$label) || is.na(self$label)) "" else paste0(": ", self$label)
cli_h1("{.cls {class(self)[1L]}} ({self$id}){msg_h}")
cli_li("Task type: {self$task_type}")
cli_li("Packages: {.pkg {self$packages}}")
cli_li("Parameters: {as_short_string(self$param_set$values, 1000L)}")
cli_li("Manual: {.help {self$man}}")
})
},

#' @description
Expand Down
1 change: 1 addition & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' @import data.table
#' @import checkmate
#' @import cli
#' @import paradox
#' @import mlr3misc
#' @import palmerpenguins
Expand Down
4 changes: 2 additions & 2 deletions inst/testthat/helper_expectations.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ expect_backend = function(b) {
checkmate::expect_r6(b, cloneable = FALSE,
public = c("nrow", "ncol", "colnames", "rownames", "head", "data", "hash"),
private = c(".data", ".hash", ".calculate_hash"))
testthat::expect_output(print(b), "^<DataBackend")
testthat::expect_output(print(b), "DataBackend")

n = checkmate::expect_count(b$nrow)
p = checkmate::expect_count(b$ncol)
Expand Down Expand Up @@ -536,7 +536,7 @@ expect_measure = function(m) {

expect_prediction = function(p) {
checkmate::expect_r6(p, "Prediction", public = c("row_ids", "truth", "predict_types"))
testthat::expect_output(print(p), "^<Prediction")
testthat::expect_output(print(p), "Prediction")
checkmate::expect_data_table(data.table::as.data.table(p), nrows = length(p$row_ids))
checkmate::expect_integerish(p$missing)
}
Expand Down
21 changes: 11 additions & 10 deletions tests/testthat/_snaps/Task.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
Code
task
Output
<TaskClassif:spam> (4601 x 58): HP Spam Detection

-- <TaskClassif> (4601x58): HP Spam Detection ----------------------------------
* Target: type
* Target classes: spam (positive class, 39%), nonspam (61%)
* Properties: twoclass
* Features (57):
- dbl (57): address, addresses, all, business, capitalAve,
capitalLong, capitalTotal, charDollar, charExclamation, charHash,
charRoundbracket, charSemicolon, charSquarebracket, conference,
credit, cs, data, direct, edu, email, font, free, george, hp, hpl,
internet, lab, labs, mail, make, meeting, money, num000, num1999,
num3d, num415, num650, num85, num857, order, original, our, over,
parts, people, pm, project, re, receive, remove, report, table,
technology, telnet, will, you, your
* Characteristics: foo=1, bar=a
* dbl (57): address, addresses, all, business, capitalAve, capitalLong,
capitalTotal, charDollar, charExclamation, charHash, charRoundbracket,
charSemicolon, charSquarebracket, conference, credit, cs, data, direct, edu,
email, font, free, george, hp, hpl, internet, lab, labs, mail, make, meeting,
money, num000, num1999, num3d, num415, num650, num85, num857, order,
original, our, over, parts, people, pm, project, re, receive, remove, report,
table, technology, telnet, will, you, your
* Characteristics: foo=1, bar=a

3 changes: 1 addition & 2 deletions tests/testthat/test_Task.R
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,7 @@ test_that("can NULL validation task", {
test_that("internal_valid_task is printed", {
task = tsk("iris")
task$internal_valid_task = c(1:10, 51:60, 101:110)
out = capture_output(print(task))
expect_match(out, "* Validation Task: (30x5)", fixed = TRUE)
expect_output(print(task), "Validation Task: \\(30x5\\)")
})

test_that("task hashes during resample", {
Expand Down
Loading