Skip to content

Commit

Permalink
Extend task printer to customize col role printing (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
mllg authored Oct 25, 2022
1 parent 249b2e9 commit 1d79659
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
28 changes: 15 additions & 13 deletions R/Task.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ Task = R6Class("Task",
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))

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

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

catf(str_indent("* Target:", self$target_names))
catf(str_indent("* Properties:", self$properties))

Expand All @@ -182,19 +192,11 @@ Task = R6Class("Task",
})
}

roles = self$col_roles
if (length(roles$order)) {
catn(str_indent("* Order by:", roles$order))
}
if ("strata" %in% self$properties) {
catn(str_indent("* Strata:", roles$stratum))
}
if ("groups" %in% self$properties) {
catn(str_indent("* Groups:", roles$group))
}
if ("weights" %in% self$properties) {
catn(str_indent("* Weights:", roles$weight))
}
# print additional columns are specified in reflections
after = mlr_reflections$task_print_col_roles$after
iwalk(after[after %in% names(roles)], function(role, str) {
catn(str_indent(sprintf("* %s:", str), role))
})
},

#' @description
Expand Down
9 changes: 9 additions & 0 deletions R/mlr_reflections.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
#' I.e., if the task property is not found in the set of the learner properties, an exception
#' is raised.
#'
#' * `task_print_col_roles` (list of named `character()`)\cr
#' Vector of column roles to print via `print(task)` if the role is not empty, either
#' before or after the task's target, properties and features.
#' The names of the column roles are the values, the names correspond to the labels to use in the printer.
#'
#' * `learner_properties` (list of `character()`)\cr
#' List of vectors of supported [Learner] properties, named by their task type.
#'
Expand Down Expand Up @@ -114,6 +119,10 @@ local({
classif = c("twoclass", "multiclass")
)

mlr_reflections$task_print_col_roles = list(
before = character(),
after = c("Order by" = "order", "Strata" = "stratum", "Groups" = "group", "Weights" = "weight")
)

### Learner
tmp = c("featureless", "missings", "weights", "importance", "selected_features", "oob_error", "loglik", "hotstart_forward", "hotstart_backward")
Expand Down
4 changes: 4 additions & 0 deletions man/mlr_reflections.Rd

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

0 comments on commit 1d79659

Please sign in to comment.