Skip to content

Commit

Permalink
Use cli package for class printer
Browse files Browse the repository at this point in the history
  • Loading branch information
lona-k committed Jan 12, 2025
1 parent ab6360a commit 2fd4981
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 27 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Depends:
Imports:
bbotk (>= 1.2.0),
checkmate (>= 2.0.0),
cli,
data.table,
lgr,
mlr3misc (>= 0.15.1),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export(trm)
export(trms)
import(bbotk)
import(checkmate)
import(cli)
import(data.table)
import(mlr3)
import(mlr3misc)
Expand Down
2 changes: 1 addition & 1 deletion R/ArchiveBatchFSelect.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ ArchiveBatchFSelect = R6Class("ArchiveBatchFSelect",
#'
#' @param ... (ignored).
print = function() {
catf(format(self))
cat_cli(cli_h1("{.cls {class(self)[1]}}"))
print(self$data[, setdiff(names(self$data), "uhash"), with = FALSE], digits=2)
},

Expand Down
22 changes: 14 additions & 8 deletions R/AutoFSelector.R
Original file line number Diff line number Diff line change
Expand Up @@ -234,19 +234,25 @@ AutoFSelector = R6Class("AutoFSelector",
#' Printer.
#' @param ... (ignored).
print = function() {
catf(format(self))
catf(str_indent("* Model:", if (is.null(self$model)) "-" else class(self$model)[1L]))
catf(str_indent("* Packages:", self$packages))
catf(str_indent("* Predict Type:", self$predict_type))
catf(str_indent("* Feature Types:", self$feature_types))
catf(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 class(self$model)[1L]

cat_cli({
cli_h1("{.cls {class(self)[1L]}} ({self$id}){msg_h}")
cli_li("Model: {model}")
cli_li("Packages: {.pkg {self$packages}}")
cli_li("Predict Type: {self$predict_type}")
cli_li("Feature Types: {self$feature_types}")
cli_li("Properties: {self$properties}")
})

w = self$warnings
e = self$errors
if (length(w)) {
catf(str_indent("* Warnings:", w))
cat_cli(cli_alert_warning("Warnings: {w}"))
}
if (length(e)) {
catf(str_indent("* Errors:", e))
cat_cli(cli_alert_danger("Errors: {e}"))
}
}
),
Expand Down
3 changes: 1 addition & 2 deletions R/EnsembleFSResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ EnsembleFSResult = R6Class("EnsembleFSResult",
#'
#' @param ... (ignored).
print = function(...) {
catf("%s with %s learners and %s initial resamplings",
format(self), self$n_learners, self$n_resamples)
cat_cli(cli_h1("{.cls {class(self)[1L]}} with {.val {self$n_learners}} learners and {.val {self$n_resamples}} initial resamplings"))
print(private$.result[, c("resampling_iteration", "learner_id", "n_features"), with = FALSE])
},

Expand Down
14 changes: 8 additions & 6 deletions R/FSelectInstanceBatchMultiCrit.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,16 @@ FSelectInstanceBatchMultiCrit = R6Class("FSelectInstanceBatchMultiCrit",
#'
#' @param ... (ignored).
print = function(...) {
catf(format(self))
catf(str_indent("* State: ", if (is.null(private$.result)) "Not optimized" else "Optimized"))
catf(str_indent("* Objective:", format(self$objective)))
catf(str_indent("* Terminator:", format(self$terminator)))
cli_h1("{.cls {class(self)[1]}}")
is_optimized = if (is.null(private$.result)) "Not optimized" else "Optimized"
cli_li("State: {is_optimized}")
cli_li("Objective: {.cls {class(self$objective)[1]}} ({self$objective$id})")
cli_li("Terminator: {.cls {class(self$terminator)[1]}}")

if (!is.null(private$.result)) {
catf("* Result:")
cli_li("Result:")
print(self$result[, c(self$archive$cols_x, self$archive$cols_y), with = FALSE])
catf("* Archive:")
cli_li("Archive")
print(as.data.table(self$archive)[, c(self$archive$cols_x, self$archive$cols_y), with = FALSE])
}
}
Expand Down
16 changes: 10 additions & 6 deletions R/FSelectInstanceBatchSingleCrit.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,18 @@ FSelectInstanceBatchSingleCrit = R6Class("FSelectInstanceBatchSingleCrit",
#'
#' @param ... (ignored).
print = function(...) {
catf(format(self))
catf(str_indent("* State: ", if (is.null(private$.result)) "Not optimized" else "Optimized"))
catf(str_indent("* Objective:", format(self$objective)))
catf(str_indent("* Terminator:", format(self$terminator)))
is_optimized = if (is.null(private$.result)) "Not optimized" else "Optimized"
cat_cli({
cli_h1("{.cls {class(self)[1L]}}")
cli_li("State: {is_optimized}")
cli_li("Objective: {.cls {class(self$objective)[1]}} ({self$objective$id})")
cli_li("Terminator: {.cls {class(self$terminator)[1]}}")
})

if (!is.null(private$.result)) {
catf("* Result:")
cat_cli(cli_li("Result:"))
print(self$result[, c(self$archive$cols_x, self$archive$cols_y), with = FALSE])
catf("* Archive:")
cat_cli(cli_li("Archive:"))
print(as.data.table(self$archive)[, c(self$archive$cols_x, self$archive$cols_y), with = FALSE])
}
}
Expand Down
11 changes: 7 additions & 4 deletions R/FSelector.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ FSelector = R6Class("FSelector",
#'
#' @return (`character()`).
print = function() {
catn(format(self), if (is.na(self$label)) "" else paste0(": ", self$label))
catf(str_indent("* Parameters:", as_short_string(self$param_set$values)))
catf(str_indent("* Properties:", self$properties))
catf(str_indent("* Packages:", self$packages))
msg_h = if (is.na(self$label)) "" else paste0(": ", self$label)
cat_cli({
cli_h1("{.cls {class(self)[1L]}}{msg_h}")
cli_li("Parameters: {as_short_string(self$param_set$values)}")
cli_li("Properties: {self$properties}")
cli_li("Packages: {.pkg {self$packages}}")
})
},

#' @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 mlr3
Expand Down
1 change: 1 addition & 0 deletions man/AutoFSelector.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2fd4981

Please sign in to comment.