R/mlr_reflections.R

#' @title Reflections for mlr3
#'
#' @format [environment].
#' @description
#' Environment which stores various information to allow objects to examine and introspect their structure and properties
#' (c.f. [Reflections](https://www.wikiwand.com/en/Reflection_(computer_programming))).
#'
#' This environment be modified by third-party packages, e.g. by adding information about new task types
#' or by extending the set of allowed feature types.
#'
#' The following objects are set by \CRANpkg{mlr3}:
#'
#' * `data_formats` :: `character()`\cr
#'   Vectors of supported data formats, e.g. `"data.table"` or `"Matrix"`.
#'
#' * `task_types` :: `data.table()`\cr
#'   Table with task type (`"type"`), the implementing package (`"pkg"`), and the names of the generators
#'   of the corresponding [Task] (`"task"`), [Learner] (`"learner"`),
#'   [Prediction] (`"prediction"`) and [Measure] (`"measure"`).
#'
#' * `task_feature_types` :: named `character()`\cr
#'   Vector of base R types supported as [Task] features, named with a 3 letter abbreviation.
#'
#' * `task_row_roles` :: `character()`\cr
#'   Vector of supported row roles for a [Task].
#'
#' * `task_col_roles` :: list of `character()`\cr
#'   List of vectors of supported column roles for a [Task], named by their task type.
#'
#' * `task_properties` :: list of `character()`\cr
#'   List of vectors of supported [Task] properties, named by their task type.
#'
#' * `learner_properties` :: list of `character()`\cr
#'   List of vectors of supported [Learner] properties, named by their task type.
#'
#' * `learner_predict_types` :: list of list of `character()`\cr
#'   List of lists of supported [Learner] predict_types, named by their task type.
#'   The inner list translates the `"predict_type"` to all predict types returned, e.g.
#'   predict type `"prob"` for a [LearnerClassif] provides the probabilities as well as the predicted labels, therefore `"prob"` maps to `c("response", "prob")`.
#'
#' * `learner_predict_types` :: list of list of `character()`\cr
#'   List of lists of supported [Learner] predict_types, named by their task type.
#'
#' * `predict_sets` :: `character()`\cr
#'   Vector of possible predict sets. Currently supported are `"train"` and `"test"`.
#'
#' * `measure_properties` :: list of `character()`\cr
#'   List of vectors of supported [Measure] properties, named by their task type.
#'
#' * `default_measures` :: list of `character()`\cr
#'   List of keys for the default [Measure]s, named by their task type.
#'
#' * `rr_names` :: `character()`\cr
#'   Names of the objects stored in a [ResampleResult].
#'
#' @keywords internal
#' @export
#' @examples
#' ls.str(mlr_reflections)
mlr_reflections = new.env(parent = emptyenv())


local({
  ### DataBackend
  mlr_reflections$data_formats = c(
    "data.table", "Matrix"
  )

  ### Task
  # task types + constructors
  mlr_reflections$task_types = rowwise_table(.key = "type",
    ~type,     ~package, ~task,         ~learner,         ~prediction,         ~measure,
    "regr",    "mlr3",   "TaskRegr",    "LearnerRegr",    "PredictionRegr",    "MeasureRegr",
    "classif", "mlr3",   "TaskClassif", "LearnerClassif", "PredictionClassif", "MeasureClassif"
  )

  mlr_reflections$task_feature_types = c(
    lgl = "logical", int = "integer", dbl = "numeric", chr = "character", fct = "factor", ord = "ordered", pxc = "POSIXct"
  )

  mlr_reflections$task_row_roles = c(
    "use", "validation"
  )

  tmp = c("feature", "target", "label", "order", "groups", "weights")
  mlr_reflections$task_col_roles = list(
    regr = tmp,
    classif = tmp
  )

  tmp = c("weights", "groups")
  mlr_reflections$task_properties = list(
    classif = c(tmp, "twoclass", "multiclass"),
    regr = tmp
  )

  ### Learner
  tmp = c("missings", "weights", "importance", "selected_features", "oob_error")
  mlr_reflections$learner_properties = list(
    classif = c(tmp, "twoclass", "multiclass"),
    regr = tmp
  )

  mlr_reflections$learner_predict_types = list(
    classif = list(response = "response", prob = c("response", "prob")),
    regr = list(response = "response", se = c("response", "se"))
  )

  ### Prediction
  mlr_reflections$predict_sets = c("train", "test")


  ### Measures
  tmp = c("na_score", "requires_task", "requires_learner", "requires_train_set")
  mlr_reflections$measure_properties = list(
    classif = tmp,
    regr = tmp
  )

  mlr_reflections$default_measures = list(
    classif = "classif.ce",
    regr = "regr.mse"
  )

  ### ResampleResult
  mlr_reflections$rr_names = c("task", "learner", "resampling", "iteration", "prediction")
})
mllg/mlr3 documentation built on Sept. 27, 2019, 9:38 a.m.